| 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 MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
| 6 #define MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/files/file_path.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.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 "mojo/shell/public/cpp/identity.h" | |
| 25 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" | |
| 26 #include "mojo/shell/runner/host/child_process_host.h" | |
| 27 | |
| 28 namespace base { | |
| 29 class TaskRunner; | |
| 30 } | |
| 31 | |
| 32 namespace mojo { | |
| 33 class Identity; | |
| 34 namespace shell { | |
| 35 | |
| 36 class NativeRunnerDelegate; | |
| 37 | |
| 38 // This class represents a "child process host". Handles launching and | |
| 39 // connecting a platform-specific "pipe" to the child, and supports joining the | |
| 40 // child process. Currently runs a single app (loaded from 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 // Note: After |Start()|, |StartApp| must be called and this object must | |
| 47 // remained alive until the |on_app_complete| callback is called. | |
| 48 class ChildProcessHost { | |
| 49 public: | |
| 50 using ProcessReadyCallback = base::Callback<void(base::ProcessId)>; | |
| 51 | |
| 52 // |name| is just for debugging ease. We will spawn off a process so that it | |
| 53 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the | |
| 54 // mojo application we wish to start. | |
| 55 ChildProcessHost(base::TaskRunner* launch_process_runner, | |
| 56 NativeRunnerDelegate* delegate, | |
| 57 bool start_sandboxed, | |
| 58 const Identity& target, | |
| 59 const base::FilePath& app_path); | |
| 60 virtual ~ChildProcessHost(); | |
| 61 | |
| 62 // |Start()|s the child process; calls |DidStart()| (on the thread on which | |
| 63 // |Start()| was called) when the child has been started (or failed to start). | |
| 64 mojom::ShellClientPtr Start(const Identity& target, | |
| 65 const ProcessReadyCallback& callback, | |
| 66 const base::Closure& quit_closure); | |
| 67 | |
| 68 // Waits for the child process to terminate. | |
| 69 void Join(); | |
| 70 | |
| 71 protected: | |
| 72 void DidStart(const ProcessReadyCallback& callback); | |
| 73 | |
| 74 private: | |
| 75 void DoLaunch(scoped_ptr<base::CommandLine> child_command_line); | |
| 76 | |
| 77 scoped_refptr<base::TaskRunner> launch_process_runner_; | |
| 78 NativeRunnerDelegate* delegate_ = nullptr; | |
| 79 bool start_sandboxed_ = false; | |
| 80 Identity target_; | |
| 81 const base::FilePath app_path_; | |
| 82 base::Process child_process_; | |
| 83 | |
| 84 // Used to initialize the Mojo IPC channel between parent and child. | |
| 85 scoped_ptr<edk::PlatformChannelPair> mojo_ipc_channel_; | |
| 86 edk::HandlePassingInformation handle_passing_info_; | |
| 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 } // namespace mojo | |
| 99 | |
| 100 #endif // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_ | |
| OLD | NEW |