James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 5 | #ifndef SHELL_CHILD_PROCESS_HOST_H_ |
| 6 | #define SHELL_CHILD_PROCESS_HOST_H_ |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 7 | |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Viet-Trung Luu | b712a8d | 2015-05-20 12:27:16 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 12 | #include "base/macros.h" |
Viet-Trung Luu | d5e2131 | 2015-05-20 16:35:04 -0700 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
James Robinson | 5e66a79 | 2015-01-21 17:02:08 -0800 | [diff] [blame] | 14 | #include "base/process/process.h" |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 15 | #include "mojo/edk/embedder/channel_info_forward.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 16 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 17 | #include "shell/child_controller.mojom.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 18 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 19 | namespace shell { |
| 20 | |
| 21 | class Context; |
| 22 | |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 23 | // Child process host: parent-process representation of a child process, which |
| 24 | // hosts/runs a native Mojo application loaded from the file system. This class |
| 25 | // handles launching and communicating with the child process. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 26 | // |
| 27 | // This class is not thread-safe. It should be created/used/destroyed on a |
| 28 | // single thread. |
Viet-Trung Luu | 01e64cf | 2015-07-08 09:55:53 -0700 | [diff] [blame] | 29 | class ChildProcessHost { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 30 | public: |
Viet-Trung Luu | 85fe109 | 2015-03-31 12:10:31 -0700 | [diff] [blame] | 31 | explicit ChildProcessHost(Context* context); |
Viet-Trung Luu | 01e64cf | 2015-07-08 09:55:53 -0700 | [diff] [blame] | 32 | // TODO(vtl): Virtual because |DidStart()| is, even though it shouldn't be |
| 33 | // (see |DidStart()|). |
| 34 | virtual ~ChildProcessHost(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 35 | |
Viet-Trung Luu | 85fe109 | 2015-03-31 12:10:31 -0700 | [diff] [blame] | 36 | // |Start()|s the child process; calls |DidStart()| (on the thread on which |
| 37 | // |Start()| was called) when the child has been started (or failed to start). |
| 38 | // After calling |Start()|, this object must not be destroyed until |
| 39 | // |DidStart()| has been called. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 40 | // TODO(vtl): Consider using weak pointers and removing this requirement. |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 41 | // TODO(vtl): This should probably take a callback instead. |
| 42 | // TODO(vtl): Consider merging this with |StartApp()|. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | void Start(); |
| 44 | |
| 45 | // Waits for the child process to terminate, and returns its exit code. |
| 46 | // Note: If |Start()| has been called, this must not be called until the |
| 47 | // callback has been called. |
| 48 | int Join(); |
| 49 | |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 50 | // Methods relayed to the |ChildController|. These methods may be only be |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 51 | // called after |Start()|, but may be called immediately (without waiting for |
| 52 | // |DidStart()|). |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 53 | |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 54 | // Like |ChildController::StartApp()|, but with one difference: |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 55 | // |on_app_complete| will *always* get called, even on connection error (or |
| 56 | // even if the child process failed to start at all). |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 57 | void StartApp(const mojo::String& app_path, |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 58 | mojo::InterfaceRequest<mojo::Application> application_request, |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 59 | const ChildController::StartAppCallback& on_app_complete); |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 60 | void ExitNow(int32_t exit_code); |
Viet-Trung Luu | 85fe109 | 2015-03-31 12:10:31 -0700 | [diff] [blame] | 61 | |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 62 | // TODO(vtl): This is virtual, so tests can override it, but really |Start()| |
| 63 | // should take a callback (see above) and this should be private. |
Viet-Trung Luu | d5e2131 | 2015-05-20 16:35:04 -0700 | [diff] [blame] | 64 | virtual void DidStart(base::Process child_process); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
Viet-Trung Luu | d5e2131 | 2015-05-20 16:35:04 -0700 | [diff] [blame] | 67 | struct LaunchData; |
| 68 | |
Viet-Trung Luu | d2e1b3f | 2015-06-09 09:55:33 -0700 | [diff] [blame] | 69 | // Callback for |mojo::embedder::ConnectToSlave()|. |
| 70 | void DidConnectToSlave(); |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 71 | |
Viet-Trung Luu | d5e2131 | 2015-05-20 16:35:04 -0700 | [diff] [blame] | 72 | // Note: This is probably executed on a different thread (namely, using the |
| 73 | // blocking pool). |
| 74 | base::Process DoLaunch(scoped_ptr<LaunchData> launch_data); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 75 | |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 76 | void AppCompleted(int32_t result); |
Viet-Trung Luu | 01e64cf | 2015-07-08 09:55:53 -0700 | [diff] [blame] | 77 | void OnConnectionError(); |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 78 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 79 | Context* const context_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 80 | |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 81 | ChildControllerPtr controller_; |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 82 | mojo::embedder::ChannelInfo* channel_info_; |
Viet-Trung Luu | f09c0d9 | 2015-04-01 14:38:57 -0700 | [diff] [blame] | 83 | ChildController::StartAppCallback on_app_complete_; |
Viet-Trung Luu | 2cdfa55 | 2015-04-01 14:27:43 -0700 | [diff] [blame] | 84 | |
| 85 | base::Process child_process_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); |
| 88 | }; |
| 89 | |
| 90 | } // namespace shell |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 91 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 92 | #endif // SHELL_CHILD_PROCESS_HOST_H_ |