| 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 SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_RUNNER_HOST_SERVICE_PROCESS_LAUNCHER_H_ |
| 6 #define SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 6 #define SERVICES_SERVICE_MANAGER_RUNNER_HOST_SERVICE_PROCESS_LAUNCHER_H_ |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | 7 |
| 10 #include <memory> | 8 #include <memory> |
| 11 #include <string> | 9 #include <string> |
| 12 | 10 |
| 13 #include "base/callback.h" | 11 #include "base/callback.h" |
| 14 #include "base/command_line.h" | |
| 15 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 16 #include "base/macros.h" | 13 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 19 #include "base/process/process.h" | 16 #include "base/process/process.h" |
| 20 #include "base/synchronization/lock.h" | |
| 21 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 22 #include "mojo/edk/embedder/platform_channel_pair.h" | 18 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 23 #include "mojo/public/cpp/system/message_pipe.h" | |
| 24 #include "services/service_manager/public/cpp/identity.h" | |
| 25 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 19 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 26 #include "services/service_manager/runner/host/child_process_host.h" | |
| 27 | 20 |
| 28 namespace base { | 21 namespace base { |
| 22 class CommandLine; |
| 29 class TaskRunner; | 23 class TaskRunner; |
| 30 } | 24 } |
| 31 | 25 |
| 32 namespace service_manager { | 26 namespace service_manager { |
| 33 | 27 |
| 34 class Identity; | 28 class Identity; |
| 35 class NativeRunnerDelegate; | |
| 36 | 29 |
| 37 // This class represents a "child process host". Handles launching and | 30 // This class represents a "child process host". Handles launching and |
| 38 // connecting a platform-specific "pipe" to the child, and supports joining the | 31 // connecting a platform-specific "pipe" to the child, and supports joining the |
| 39 // child process. Currently runs a single service, loaded from a standalone | 32 // child process. Currently runs a single service, loaded from a standalone |
| 40 // service executable on the file system. | 33 // service executable on the file system. |
| 41 // | 34 // |
| 42 // This class is not thread-safe. It should be created/used/destroyed on a | 35 // This class is not thread-safe. It should be created/used/destroyed on a |
| 43 // single thread. | 36 // single thread. |
| 44 // | 37 // |
| 45 // Note: Does not currently work on Windows before Vista. | 38 // Note: Does not currently work on Windows before Vista. |
| 46 class ChildProcessHost { | 39 class ServiceProcessLauncher { |
| 47 public: | 40 public: |
| 48 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; | 41 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; |
| 49 | 42 |
| 43 class Delegate { |
| 44 public: |
| 45 // Called to adjust the commandline for launching the specified app. |
| 46 // WARNING: this is called on a background thread. |
| 47 virtual void AdjustCommandLineArgumentsForTarget( |
| 48 const Identity& target, |
| 49 base::CommandLine* command_line) = 0; |
| 50 |
| 51 protected: |
| 52 virtual ~Delegate() {} |
| 53 }; |
| 54 |
| 50 // |name| is just for debugging ease. We will spawn off a process so that it | 55 // |name| is just for debugging ease. We will spawn off a process so that it |
| 51 // can be sandboxed if |start_sandboxed| is true. |service_path| is a path to | 56 // can be sandboxed if |start_sandboxed| is true. |service_path| is a path to |
| 52 // the service executable we wish to start. | 57 // the service executable we wish to start. |
| 53 ChildProcessHost(base::TaskRunner* launch_process_runner, | 58 ServiceProcessLauncher(base::TaskRunner* launch_process_runner, |
| 54 NativeRunnerDelegate* delegate, | 59 Delegate* delegate, |
| 55 bool start_sandboxed, | 60 const base::FilePath& service_path); |
| 56 const Identity& target, | 61 ~ServiceProcessLauncher(); |
| 57 const base::FilePath& service_path); | |
| 58 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 mojom::ServicePtr Start(const Identity& target, | 65 mojom::ServicePtr Start(const Identity& target, |
| 63 const ProcessReadyCallback& callback, | 66 bool start_sandboxed, |
| 64 const base::Closure& quit_closure); | 67 const ProcessReadyCallback& callback); |
| 65 | 68 |
| 66 // Waits for the child process to terminate. | 69 // Waits for the child process to terminate. |
| 67 void Join(); | 70 void Join(); |
| 68 | 71 |
| 69 protected: | 72 private: |
| 70 void DidStart(const ProcessReadyCallback& callback); | 73 void DidStart(const ProcessReadyCallback& callback); |
| 71 | |
| 72 private: | |
| 73 void DoLaunch(std::unique_ptr<base::CommandLine> child_command_line); | 74 void DoLaunch(std::unique_ptr<base::CommandLine> child_command_line); |
| 74 | 75 |
| 75 scoped_refptr<base::TaskRunner> launch_process_runner_; | 76 scoped_refptr<base::TaskRunner> launch_process_runner_; |
| 76 NativeRunnerDelegate* delegate_ = nullptr; | 77 Delegate* delegate_ = nullptr; |
| 77 bool start_sandboxed_ = false; | 78 bool start_sandboxed_ = false; |
| 78 Identity target_; | 79 Identity target_; |
| 79 base::FilePath service_path_; | 80 base::FilePath service_path_; |
| 80 base::Process child_process_; | 81 base::Process child_process_; |
| 81 | 82 |
| 82 // Used to initialize the Mojo IPC channel between parent and child. | 83 // Used to initialize the Mojo IPC channel between parent and child. |
| 83 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; | 84 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; |
| 84 mojo::edk::HandlePassingInformation handle_passing_info_; | 85 mojo::edk::HandlePassingInformation handle_passing_info_; |
| 85 const std::string child_token_; | 86 const std::string child_token_; |
| 86 | 87 |
| 87 // Since Start() calls a method on another thread, we use an event to block | 88 // Since Start() calls a method on another thread, we use an event to block |
| 88 // the main thread if it tries to destruct |this| while launching the process. | 89 // the main thread if it tries to destruct |this| while launching the process. |
| 89 base::WaitableEvent start_child_process_event_; | 90 base::WaitableEvent start_child_process_event_; |
| 90 | 91 |
| 91 base::WeakPtrFactory<ChildProcessHost> weak_factory_; | 92 base::WeakPtrFactory<ServiceProcessLauncher> weak_factory_; |
| 92 | 93 |
| 93 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | 94 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncher); |
| 95 }; |
| 96 |
| 97 class ServiceProcessLauncherFactory { |
| 98 public: |
| 99 virtual ~ServiceProcessLauncherFactory() {} |
| 100 virtual std::unique_ptr<ServiceProcessLauncher> Create( |
| 101 const base::FilePath& service_path) = 0; |
| 94 }; | 102 }; |
| 95 | 103 |
| 96 } // namespace service_manager | 104 } // namespace service_manager |
| 97 | 105 |
| 98 #endif // SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | 106 #endif // SERVICES_SERVICE_MANAGER_RUNNER_HOST_SERVICE_PROCESS_LAUNCHER_H_ |
| OLD | NEW |