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_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
6 #define SERVICES_SHELL_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/shell/public/cpp/identity.h" | |
25 #include "services/shell/public/interfaces/service_factory.mojom.h" | |
26 #include "services/shell/runner/host/child_process_host.h" | |
27 | |
28 namespace base { | |
29 class TaskRunner; | |
30 } | |
31 | |
32 namespace shell { | |
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 app (loaded from the file system). | |
40 // | |
41 // This class is not thread-safe. It should be created/used/destroyed on a | |
42 // single thread. | |
43 // | |
44 // Note: Does not currently work on Windows before Vista. | |
45 // Note: After |Start()|, |StartApp| must be called and this object must | |
46 // remained alive until the |on_app_complete| callback is called. | |
47 class ChildProcessHost { | |
48 public: | |
49 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; | |
50 | |
51 // |name| is just for debugging ease. We will spawn off a process so that it | |
52 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the | |
53 // service we wish to start. | |
54 ChildProcessHost(base::TaskRunner* launch_process_runner, | |
55 NativeRunnerDelegate* delegate, | |
56 bool start_sandboxed, | |
57 const Identity& target, | |
58 const base::FilePath& app_path); | |
59 virtual ~ChildProcessHost(); | |
60 | |
61 // |Start()|s the child process; calls |DidStart()| (on the thread on which | |
62 // |Start()| was called) when the child has been started (or failed to start). | |
63 mojom::ServicePtr Start(const Identity& target, | |
64 const ProcessReadyCallback& callback, | |
65 const base::Closure& quit_closure); | |
66 | |
67 // Waits for the child process to terminate. | |
68 void Join(); | |
69 | |
70 protected: | |
71 void DidStart(const ProcessReadyCallback& callback); | |
72 | |
73 private: | |
74 void DoLaunch(std::unique_ptr<base::CommandLine> child_command_line); | |
75 | |
76 scoped_refptr<base::TaskRunner> launch_process_runner_; | |
77 NativeRunnerDelegate* delegate_ = nullptr; | |
78 bool start_sandboxed_ = false; | |
79 Identity target_; | |
80 const base::FilePath app_path_; | |
81 base::Process child_process_; | |
82 | |
83 // Used to initialize the Mojo IPC channel between parent and child. | |
84 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; | |
85 mojo::edk::HandlePassingInformation handle_passing_info_; | |
86 const std::string child_token_; | |
87 | |
88 // Since Start() calls a method on another thread, we use an event to block | |
89 // the main thread if it tries to destruct |this| while launching the process. | |
90 base::WaitableEvent start_child_process_event_; | |
91 | |
92 base::WeakPtrFactory<ChildProcessHost> weak_factory_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | |
95 }; | |
96 | |
97 } // namespace shell | |
98 | |
99 #endif // SERVICES_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
OLD | NEW |