| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 5 #ifndef MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ |
| 6 #define MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 6 #define MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 13 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 18 #include "base/synchronization/lock.h" |
| 14 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 15 #include "mojo/edk/embedder/platform_channel_pair.h" | 20 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 16 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
| 17 #include "mojo/shell/runner/child/child_controller.mojom.h" | 22 #include "mojo/shell/runner/child/child_controller.mojom.h" |
| 18 #include "mojo/shell/runner/host/child_process_host.h" | 23 #include "mojo/shell/runner/host/child_process_host.h" |
| 19 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h" | 24 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h" |
| 20 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | 25 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" |
| 21 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | 26 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" |
| 22 | 27 |
| 23 namespace base { | 28 namespace base { |
| 24 class TaskRunner; | 29 class TaskRunner; |
| 25 } | 30 } |
| 26 | 31 |
| 27 namespace mojo { | 32 namespace mojo { |
| 28 namespace shell { | 33 namespace shell { |
| 29 | 34 |
| 30 // This class represents a "child process host". Handles launching and | 35 // This class represents a "child process host". Handles launching and |
| 31 // connecting a platform-specific "pipe" to the child, and supports joining the | 36 // connecting a platform-specific "pipe" to the child, and supports joining the |
| 32 // child process. Currently runs a single app (loaded from the file system). | 37 // child process. Currently runs a single app (loaded from the file system). |
| 33 // | 38 // |
| 34 // This class is not thread-safe. It should be created/used/destroyed on a | 39 // This class is not thread-safe. It should be created/used/destroyed on a |
| 35 // single thread. | 40 // single thread. |
| 36 // | 41 // |
| 37 // Note: Does not currently work on Windows before Vista. | 42 // Note: Does not currently work on Windows before Vista. |
| 38 // Note: After |Start()|, |StartApp| must be called and this object must | 43 // Note: After |Start()|, |StartApp| must be called and this object must |
| 39 // remained alive until the |on_app_complete| callback is called. | 44 // remained alive until the |on_app_complete| callback is called. |
| 40 class ChildProcessHost { | 45 class ChildProcessHost { |
| 41 public: | 46 public: |
| 47 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; |
| 48 |
| 42 // |name| is just for debugging ease. We will spawn off a process so that it | 49 // |name| is just for debugging ease. We will spawn off a process so that it |
| 43 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the | 50 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the |
| 44 // mojo application we wish to start. | 51 // mojo application we wish to start. |
| 45 ChildProcessHost(base::TaskRunner* launch_process_runner, | 52 ChildProcessHost(base::TaskRunner* launch_process_runner, |
| 46 bool start_sandboxed, | 53 bool start_sandboxed, |
| 47 const base::FilePath& app_path); | 54 const base::FilePath& app_path); |
| 48 // Allows a ChildProcessHost to be instantiated for an existing channel | 55 // Allows a ChildProcessHost to be instantiated for an existing channel |
| 49 // created by someone else (e.g. an app that launched its own process). | 56 // created by someone else (e.g. an app that launched its own process). |
| 50 explicit ChildProcessHost(ScopedHandle channel); | 57 explicit ChildProcessHost(ScopedHandle channel); |
| 51 virtual ~ChildProcessHost(); | 58 virtual ~ChildProcessHost(); |
| 52 | 59 |
| 53 // |Start()|s the child process; calls |DidStart()| (on the thread on which | 60 // |Start()|s the child process; calls |DidStart()| (on the thread on which |
| 54 // |Start()| was called) when the child has been started (or failed to start). | 61 // |Start()| was called) when the child has been started (or failed to start). |
| 55 void Start( | 62 void Start(const ProcessReadyCallback& callback); |
| 56 const base::Callback<void(base::ProcessId)>& pid_available_callback); | |
| 57 | 63 |
| 58 // Waits for the child process to terminate, and returns its exit code. | 64 // Waits for the child process to terminate, and returns its exit code. |
| 59 int Join(); | 65 int Join(); |
| 60 | 66 |
| 61 // See |ChildController|: | 67 // See |ChildController|: |
| 62 void StartApp(InterfaceRequest<Application> application_request, | 68 void StartApp(InterfaceRequest<Application> application_request, |
| 63 const ChildController::StartAppCallback& on_app_complete); | 69 const ChildController::StartAppCallback& on_app_complete); |
| 64 void ExitNow(int32_t exit_code); | 70 void ExitNow(int32_t exit_code); |
| 65 | 71 |
| 66 protected: | 72 protected: |
| 67 // virtual for testing. | 73 void DidStart(); |
| 68 virtual void DidStart( | |
| 69 const base::Callback<void(base::ProcessId)>& pid_available_callback); | |
| 70 | 74 |
| 71 private: | 75 private: |
| 76 // A thread-safe holder for the bootstrap message pipe to this child process. |
| 77 // The pipe is established on an arbitrary thread and may not be connected |
| 78 // until the host's message loop has stopped running. |
| 79 class PipeHolder : public base::RefCountedThreadSafe<PipeHolder> { |
| 80 public: |
| 81 PipeHolder(); |
| 82 |
| 83 void Reject(); |
| 84 void SetPipe(ScopedMessagePipeHandle pipe); |
| 85 ScopedMessagePipeHandle PassPipe(); |
| 86 |
| 87 private: |
| 88 friend class base::RefCountedThreadSafe<PipeHolder>; |
| 89 |
| 90 ~PipeHolder(); |
| 91 |
| 92 base::Lock lock_; |
| 93 bool reject_pipe_ = false; |
| 94 ScopedMessagePipeHandle pipe_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(PipeHolder); |
| 97 }; |
| 98 |
| 72 void DoLaunch(); | 99 void DoLaunch(); |
| 73 | 100 |
| 74 void AppCompleted(int32_t result); | 101 void AppCompleted(int32_t result); |
| 75 | 102 |
| 76 // Callback for |embedder::CreateChannel()|. | 103 // Callback for |embedder::CreateChannel()|. |
| 77 void DidCreateChannel(embedder::ChannelInfo* channel_info); | 104 void DidCreateChannel(embedder::ChannelInfo* channel_info); |
| 78 | 105 |
| 106 // Called once |pipe_holder_| is bound to a pipe. |
| 107 void OnMessagePipeCreated(); |
| 108 |
| 109 // Called when the child process is launched and when the bootstrap |
| 110 // message pipe is created. Once both things have happened (which may happen |
| 111 // in either order), |process_ready_callback_| is invoked. |
| 112 void MaybeNotifyProcessReady(); |
| 113 |
| 114 // Callback used to receive the child message pipe from the ports EDK. |
| 115 // This may be called on any thread. It will always stash the pipe in |
| 116 // |holder|, and it will then attempt to call |callback| on |
| 117 // |callback_task_runner| (which may or may not still be running tasks.) |
| 118 static void OnParentMessagePipeCreated( |
| 119 scoped_refptr<PipeHolder> holder, |
| 120 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 121 const base::Closure& callback, |
| 122 ScopedMessagePipeHandle pipe); |
| 123 |
| 79 scoped_refptr<base::TaskRunner> launch_process_runner_; | 124 scoped_refptr<base::TaskRunner> launch_process_runner_; |
| 80 bool start_sandboxed_; | 125 bool start_sandboxed_; |
| 81 const base::FilePath app_path_; | 126 const base::FilePath app_path_; |
| 82 base::Process child_process_; | 127 base::Process child_process_; |
| 83 // Used for the ChildController binding. | 128 // Used for the ChildController binding. |
| 84 embedder::PlatformChannelPair platform_channel_pair_; | 129 embedder::PlatformChannelPair platform_channel_pair_; |
| 85 ChildControllerPtr controller_; | 130 ChildControllerPtr controller_; |
| 86 embedder::ChannelInfo* channel_info_; | 131 embedder::ChannelInfo* channel_info_; |
| 87 ChildController::StartAppCallback on_app_complete_; | 132 ChildController::StartAppCallback on_app_complete_; |
| 88 embedder::HandlePassingInformation handle_passing_info_; | 133 embedder::HandlePassingInformation handle_passing_info_; |
| 89 | 134 |
| 90 // Used only when --use-new-edk is specified, as a communication channel for | 135 // Used only when --use-new-edk is specified. Used to back the NodeChannel |
| 91 // Broker. | 136 // between the parent and child node. |
| 92 scoped_ptr<edk::PlatformChannelPair> serializer_platform_channel_pair_; | 137 scoped_ptr<edk::PlatformChannelPair> node_channel_; |
| 93 | 138 |
| 94 // Since Start() calls a method on another thread, we use an event to block | 139 // Since Start() calls a method on another thread, we use an event to block |
| 95 // the main thread if it tries to destruct |this| while launching the process. | 140 // the main thread if it tries to destruct |this| while launching the process. |
| 96 base::WaitableEvent start_child_process_event_; | 141 base::WaitableEvent start_child_process_event_; |
| 97 | 142 |
| 98 // A message pipe to the child process. Valid immediately after creation. | 143 // A token the child can use to connect a primordial pipe to the host. |
| 99 mojo::ScopedMessagePipeHandle child_message_pipe_; | 144 std::string primordial_pipe_token_; |
| 145 |
| 146 // Holds the message pipe to the child process until it is either closed or |
| 147 // bound to the controller interface. |
| 148 scoped_refptr<PipeHolder> pipe_holder_; |
| 149 |
| 150 // Invoked exactly once, as soon as the child process's ID is known and |
| 151 // a pipe to the child has been established. |
| 152 ProcessReadyCallback process_ready_callback_; |
| 100 | 153 |
| 101 base::WeakPtrFactory<ChildProcessHost> weak_factory_; | 154 base::WeakPtrFactory<ChildProcessHost> weak_factory_; |
| 102 | 155 |
| 103 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | 156 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); |
| 104 }; | 157 }; |
| 105 | 158 |
| 106 } // namespace shell | 159 } // namespace shell |
| 107 } // namespace mojo | 160 } // namespace mojo |
| 108 | 161 |
| 109 #endif // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 162 #endif // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ |
| OLD | NEW |