| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_HELPER_H_ |
| 6 #define CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_HELPER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/process/kill.h" |
| 13 #include "base/process/process.h" |
| 14 #include "build/build_config.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/common/result_codes.h" |
| 17 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 19 |
| 20 #if defined(OS_WIN) |
| 21 #include "sandbox/win/src/sandbox_types.h" |
| 22 #else |
| 23 #include "content/public/browser/file_descriptor_info.h" |
| 24 #endif |
| 25 |
| 26 #if defined(OS_LINUX) |
| 27 #include "content/public/common/zygote_handle.h" |
| 28 #endif |
| 29 |
| 30 namespace base { |
| 31 class CommandLine; |
| 32 } |
| 33 |
| 34 namespace content { |
| 35 |
| 36 class ChildProcessLauncher; |
| 37 class FileDescriptorInfo; |
| 38 class SandboxedProcessLauncherDelegate; |
| 39 |
| 40 namespace internal { |
| 41 |
| 42 |
| 43 #if defined(OS_WIN) |
| 44 using FileMappedForLaunch = base::HandlesToInheritVector; |
| 45 #else |
| 46 using FileMappedForLaunch = FileDescriptorInfo; |
| 47 #endif |
| 48 |
| 49 // ChildProcessLauncherHelper is used by ChildProcessLauncher to start a |
| 50 // process. Since ChildProcessLauncher can be deleted by its client at any time, |
| 51 // this class is used to keep state as the process is started asynchronously. |
| 52 // It also contains the platform specific pieces. |
| 53 class ChildProcessLauncherHelper : |
| 54 public base::RefCountedThreadSafe<ChildProcessLauncherHelper> { |
| 55 public: |
| 56 // Abstraction around a process required to deal in a platform independent way |
| 57 // between Linux (which can use zygotes) and the other platforms. |
| 58 struct Process { |
| 59 Process() {} |
| 60 Process(Process&& other); |
| 61 ~Process() {} |
| 62 Process& operator=(Process&& other); |
| 63 |
| 64 base::Process process; |
| 65 |
| 66 #if defined(OS_LINUX) |
| 67 ZygoteHandle zygote = nullptr; |
| 68 #endif |
| 69 }; |
| 70 |
| 71 ChildProcessLauncherHelper( |
| 72 int child_process_id, |
| 73 BrowserThread::ID client_thread_id, |
| 74 std::unique_ptr<base::CommandLine> command_line, |
| 75 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 76 const base::WeakPtr<ChildProcessLauncher>& child_process_launcher, |
| 77 bool terminate_on_shutdown); |
| 78 |
| 79 // The methods below are defined in the order they are called. |
| 80 |
| 81 // Starts the flow of launching the process. |
| 82 void StartLaunchOnClientThread(); |
| 83 |
| 84 // Platform specific. |
| 85 void BeforeLaunchOnClientThread(); |
| 86 |
| 87 // Called in to give implementors a chance at creating a server pipe. |
| 88 // Platform specific. |
| 89 mojo::edk::ScopedPlatformHandle PrepareMojoPipeHandlesOnClientThread(); |
| 90 |
| 91 // Returns the list of files that should be mapped in the child process. |
| 92 // Platform specific. |
| 93 std::unique_ptr<FileMappedForLaunch> GetFilesToMap(); |
| 94 |
| 95 // Platform specific. |
| 96 void BeforeLaunchOnLauncherThread( |
| 97 const FileMappedForLaunch& files_to_register, |
| 98 base::LaunchOptions* options); |
| 99 |
| 100 // Does the actual starting of the process. |
| 101 // |is_synchronous_launch| is set to false if the starting of the process is |
| 102 // asynchonous (this is the case on Android), in which case the returned |
| 103 // Process is not valid (and PostLaunchOnLauncherThread() will provide the |
| 104 // process once it is available). |
| 105 // Platform specific. |
| 106 ChildProcessLauncherHelper::Process LaunchProcessOnLauncherThread( |
| 107 const base::LaunchOptions& options, |
| 108 std::unique_ptr<FileMappedForLaunch> files_to_register, |
| 109 bool* is_synchronous_launch, |
| 110 int* launch_result); |
| 111 |
| 112 // Called right after the process has been launched, whether it was created |
| 113 // yet or not. |
| 114 // Platform specific. |
| 115 void AfterLaunchOnLauncherThread( |
| 116 const ChildProcessLauncherHelper::Process& process, |
| 117 const base::LaunchOptions& options); |
| 118 |
| 119 // Called once the process has been created, successfully or not. |
| 120 // If |post_launch_on_client_thread_called| is false, |
| 121 // this calls PostLaunchOnClientThread on the client thread. |
| 122 void PostLaunchOnLauncherThread(ChildProcessLauncherHelper::Process process, |
| 123 int launch_result, |
| 124 bool post_launch_on_client_thread_called); |
| 125 |
| 126 // Note that this could be called before PostLaunchOnLauncherThread() is |
| 127 // called. |
| 128 void PostLaunchOnClientThread(ChildProcessLauncherHelper::Process process, |
| 129 int error_code); |
| 130 |
| 131 int client_thread_id() const { return client_thread_id_; } |
| 132 |
| 133 // Returns the termination status and sets |exit_code| if non null. |
| 134 // See ChildProcessLauncher::GetChildTerminationStatus for more info. |
| 135 static base::TerminationStatus GetTerminationStatus( |
| 136 const ChildProcessLauncherHelper::Process& process, |
| 137 bool known_dead, |
| 138 int* exit_code); |
| 139 |
| 140 // Terminates |process|. |
| 141 // Returns true if the process was stopped, false if the process had not been |
| 142 // started yet or could not be stopped. |
| 143 // Note that |exit_code| and |wait| are not used on Android. |
| 144 static bool TerminateProcess(const base::Process& process, |
| 145 int exit_code, |
| 146 bool wait); |
| 147 |
| 148 // Terminates the process with the normal exit code and ensures it has been |
| 149 // stopped. By returning a normal exit code this ensures UMA won't treat this |
| 150 // as a crash. |
| 151 // Returns immediately and perform the work on the launcher thread. |
| 152 static void ForceNormalProcessTerminationAsync( |
| 153 ChildProcessLauncherHelper::Process process); |
| 154 |
| 155 static void SetProcessBackgroundedOnLauncherThread( |
| 156 base::Process process, bool background); |
| 157 |
| 158 private: |
| 159 friend class base::RefCountedThreadSafe<ChildProcessLauncherHelper>; |
| 160 |
| 161 ~ChildProcessLauncherHelper(); |
| 162 |
| 163 void LaunchOnLauncherThread(); |
| 164 |
| 165 const mojo::edk::PlatformHandle& mojo_client_handle() const { |
| 166 return mojo_client_handle_.get(); |
| 167 } |
| 168 base::CommandLine* command_line() { return command_line_.get(); } |
| 169 int child_process_id() const { return child_process_id_; } |
| 170 |
| 171 std::string GetProcessType(); |
| 172 |
| 173 static void ForceNormalProcessTerminationSync( |
| 174 ChildProcessLauncherHelper::Process process); |
| 175 |
| 176 const int child_process_id_; |
| 177 const BrowserThread::ID client_thread_id_; |
| 178 base::TimeTicks begin_launch_time_; |
| 179 std::unique_ptr<base::CommandLine> command_line_; |
| 180 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_; |
| 181 base::WeakPtr<ChildProcessLauncher> child_process_launcher_; |
| 182 mojo::edk::ScopedPlatformHandle mojo_client_handle_; |
| 183 mojo::edk::ScopedPlatformHandle mojo_server_handle_; |
| 184 bool terminate_on_shutdown_; |
| 185 }; |
| 186 |
| 187 } // namespace internal |
| 188 |
| 189 } // namespace content |
| 190 |
| 191 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_HELPER_H_ |
| OLD | NEW |