| 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> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 20 #include "mojo/edk/embedder/platform_channel_pair.h" | 20 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 21 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
| 22 #include "mojo/shell/identity.h" |
| 22 #include "mojo/shell/runner/child/child_controller.mojom.h" | 23 #include "mojo/shell/runner/child/child_controller.mojom.h" |
| 23 #include "mojo/shell/runner/host/child_process_host.h" | 24 #include "mojo/shell/runner/host/child_process_host.h" |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 class TaskRunner; | 27 class TaskRunner; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace mojo { | 30 namespace mojo { |
| 30 namespace shell { | 31 namespace shell { |
| 31 | 32 |
| 32 struct CommandLineSwitch; | 33 class Identity; |
| 34 class NativeRunnerDelegate; |
| 33 | 35 |
| 34 // This class represents a "child process host". Handles launching and | 36 // This class represents a "child process host". Handles launching and |
| 35 // connecting a platform-specific "pipe" to the child, and supports joining the | 37 // connecting a platform-specific "pipe" to the child, and supports joining the |
| 36 // child process. Currently runs a single app (loaded from the file system). | 38 // child process. Currently runs a single app (loaded from the file system). |
| 37 // | 39 // |
| 38 // This class is not thread-safe. It should be created/used/destroyed on a | 40 // This class is not thread-safe. It should be created/used/destroyed on a |
| 39 // single thread. | 41 // single thread. |
| 40 // | 42 // |
| 41 // Note: Does not currently work on Windows before Vista. | 43 // Note: Does not currently work on Windows before Vista. |
| 42 // Note: After |Start()|, |StartApp| must be called and this object must | 44 // Note: After |Start()|, |StartApp| must be called and this object must |
| 43 // remained alive until the |on_app_complete| callback is called. | 45 // remained alive until the |on_app_complete| callback is called. |
| 44 class ChildProcessHost { | 46 class ChildProcessHost { |
| 45 public: | 47 public: |
| 46 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; | 48 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; |
| 47 | 49 |
| 48 // |name| is just for debugging ease. We will spawn off a process so that it | 50 // |name| is just for debugging ease. We will spawn off a process so that it |
| 49 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the | 51 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the |
| 50 // mojo application we wish to start. | 52 // mojo application we wish to start. |
| 51 ChildProcessHost(base::TaskRunner* launch_process_runner, | 53 ChildProcessHost(base::TaskRunner* launch_process_runner, |
| 54 NativeRunnerDelegate* delegate, |
| 52 bool start_sandboxed, | 55 bool start_sandboxed, |
| 53 const base::FilePath& app_path, | 56 const Identity& target, |
| 54 const std::vector<CommandLineSwitch>& switches); | 57 const base::FilePath& app_path); |
| 55 // Allows a ChildProcessHost to be instantiated for an existing channel | 58 // Allows a ChildProcessHost to be instantiated for an existing channel |
| 56 // created by someone else (e.g. an app that launched its own process). | 59 // created by someone else (e.g. an app that launched its own process). |
| 57 explicit ChildProcessHost(ScopedHandle channel); | 60 explicit ChildProcessHost(ScopedHandle channel); |
| 58 virtual ~ChildProcessHost(); | 61 virtual ~ChildProcessHost(); |
| 59 | 62 |
| 60 // |Start()|s the child process; calls |DidStart()| (on the thread on which | 63 // |Start()|s the child process; calls |DidStart()| (on the thread on which |
| 61 // |Start()| was called) when the child has been started (or failed to start). | 64 // |Start()| was called) when the child has been started (or failed to start). |
| 62 void Start(const ProcessReadyCallback& callback); | 65 void Start(const ProcessReadyCallback& callback); |
| 63 | 66 |
| 64 // Waits for the child process to terminate, and returns its exit code. | 67 // Waits for the child process to terminate, and returns its exit code. |
| 65 int Join(); | 68 int Join(); |
| 66 | 69 |
| 67 // See |mojom::ChildController|: | 70 // See |mojom::ChildController|: |
| 68 void StartApp( | 71 void StartApp( |
| 69 InterfaceRequest<mojom::ShellClient> request, | 72 InterfaceRequest<mojom::ShellClient> request, |
| 70 const mojom::ChildController::StartAppCallback& on_app_complete); | 73 const mojom::ChildController::StartAppCallback& on_app_complete); |
| 71 void ExitNow(int32_t exit_code); | 74 void ExitNow(int32_t exit_code); |
| 72 | 75 |
| 73 protected: | 76 protected: |
| 74 void DidStart(const ProcessReadyCallback& callback); | 77 void DidStart(const ProcessReadyCallback& callback); |
| 75 | 78 |
| 76 private: | 79 private: |
| 77 void DoLaunch(); | 80 void DoLaunch(); |
| 78 | 81 |
| 79 void AppCompleted(int32_t result); | 82 void AppCompleted(int32_t result); |
| 80 | 83 |
| 81 scoped_refptr<base::TaskRunner> launch_process_runner_; | 84 scoped_refptr<base::TaskRunner> launch_process_runner_; |
| 85 NativeRunnerDelegate* delegate_; |
| 82 bool start_sandboxed_; | 86 bool start_sandboxed_; |
| 87 Identity target_; |
| 83 const base::FilePath app_path_; | 88 const base::FilePath app_path_; |
| 84 base::Process child_process_; | 89 base::Process child_process_; |
| 85 // Used for the ChildController binding. | 90 // Used for the ChildController binding. |
| 86 edk::PlatformChannelPair platform_channel_pair_; | 91 edk::PlatformChannelPair platform_channel_pair_; |
| 87 mojom::ChildControllerPtr controller_; | 92 mojom::ChildControllerPtr controller_; |
| 88 mojom::ChildController::StartAppCallback on_app_complete_; | 93 mojom::ChildController::StartAppCallback on_app_complete_; |
| 89 edk::HandlePassingInformation handle_passing_info_; | 94 edk::HandlePassingInformation handle_passing_info_; |
| 90 | 95 |
| 91 // Used to back the NodeChannel between the parent and child node. | 96 // Used to back the NodeChannel between the parent and child node. |
| 92 scoped_ptr<edk::PlatformChannelPair> node_channel_; | 97 scoped_ptr<edk::PlatformChannelPair> node_channel_; |
| 93 | 98 |
| 94 // Since Start() calls a method on another thread, we use an event to block | 99 // 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. | 100 // the main thread if it tries to destruct |this| while launching the process. |
| 96 base::WaitableEvent start_child_process_event_; | 101 base::WaitableEvent start_child_process_event_; |
| 97 | 102 |
| 98 // A token the child can use to connect a primordial pipe to the host. | 103 // A token the child can use to connect a primordial pipe to the host. |
| 99 std::string primordial_pipe_token_; | 104 std::string primordial_pipe_token_; |
| 100 | 105 |
| 101 const std::vector<CommandLineSwitch> command_line_switches_; | |
| 102 | |
| 103 base::WeakPtrFactory<ChildProcessHost> weak_factory_; | 106 base::WeakPtrFactory<ChildProcessHost> weak_factory_; |
| 104 | 107 |
| 105 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | 108 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 } // namespace shell | 111 } // namespace shell |
| 109 } // namespace mojo | 112 } // namespace mojo |
| 110 | 113 |
| 111 #endif // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 114 #endif // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ |
| OLD | NEW |