OLD | NEW |
| (Empty) |
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 | |
5 #ifndef SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
6 #define SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/command_line.h" | |
15 #include "base/files/file_path.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/memory/weak_ptr.h" | |
19 #include "base/process/process.h" | |
20 #include "base/synchronization/lock.h" | |
21 #include "base/synchronization/waitable_event.h" | |
22 #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" | |
26 #include "services/service_manager/runner/host/child_process_host.h" | |
27 | |
28 namespace base { | |
29 class TaskRunner; | |
30 } | |
31 | |
32 namespace service_manager { | |
33 | |
34 class Identity; | |
35 class NativeRunnerDelegate; | |
36 | |
37 // This class represents a "child process host". Handles launching and | |
38 // connecting a platform-specific "pipe" to the child, and supports joining the | |
39 // child process. Currently runs a single service, loaded from a standalone | |
40 // service executable on the file system. | |
41 // | |
42 // This class is not thread-safe. It should be created/used/destroyed on a | |
43 // single thread. | |
44 // | |
45 // Note: Does not currently work on Windows before Vista. | |
46 class ChildProcessHost { | |
47 public: | |
48 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; | |
49 | |
50 // |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 | |
52 // the service executable we wish to start. | |
53 ChildProcessHost(base::TaskRunner* launch_process_runner, | |
54 NativeRunnerDelegate* delegate, | |
55 bool start_sandboxed, | |
56 const Identity& target, | |
57 const base::FilePath& service_path); | |
58 virtual ~ChildProcessHost(); | |
59 | |
60 // |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). | |
62 mojom::ServicePtr Start(const Identity& target, | |
63 const ProcessReadyCallback& callback, | |
64 const base::Closure& quit_closure); | |
65 | |
66 // Waits for the child process to terminate. | |
67 void Join(); | |
68 | |
69 protected: | |
70 void DidStart(const ProcessReadyCallback& callback); | |
71 | |
72 private: | |
73 void DoLaunch(std::unique_ptr<base::CommandLine> child_command_line); | |
74 | |
75 scoped_refptr<base::TaskRunner> launch_process_runner_; | |
76 NativeRunnerDelegate* delegate_ = nullptr; | |
77 bool start_sandboxed_ = false; | |
78 Identity target_; | |
79 base::FilePath service_path_; | |
80 base::Process child_process_; | |
81 | |
82 // Used to initialize the Mojo IPC channel between parent and child. | |
83 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; | |
84 mojo::edk::HandlePassingInformation handle_passing_info_; | |
85 const std::string child_token_; | |
86 | |
87 // 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 base::WaitableEvent start_child_process_event_; | |
90 | |
91 base::WeakPtrFactory<ChildProcessHost> weak_factory_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | |
94 }; | |
95 | |
96 } // namespace service_manager | |
97 | |
98 #endif // SERVICES_SERVICE_MANAGER_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
OLD | NEW |