| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ | 5 #ifndef CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ |
| 6 #define CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ | 6 #define CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ |
| 7 | 7 |
| 8 #include "base/files/scoped_file.h" | 8 #include <memory> |
| 9 |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
| 11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 12 #include "base/process/kill.h" | 14 #include "base/process/kill.h" |
| 13 #include "base/process/launch.h" | |
| 14 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 15 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "content/browser/child_process_launcher_helper.h" |
| 17 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/common/sandboxed_process_launcher_delegate.h" | 21 #include "content/public/common/result_codes.h" |
| 20 #include "mojo/edk/embedder/embedder.h" | 22 #include "mojo/edk/embedder/embedder.h" |
| 21 #include "mojo/edk/embedder/scoped_platform_handle.h" | 23 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 22 | 24 |
| 23 #if defined(OS_WIN) | |
| 24 #include "sandbox/win/src/sandbox_types.h" | |
| 25 #endif | |
| 26 | |
| 27 namespace base { | 25 namespace base { |
| 28 class CommandLine; | 26 class CommandLine; |
| 29 } | 27 } |
| 30 | 28 |
| 31 namespace content { | 29 namespace content { |
| 32 | 30 |
| 31 class SandboxedProcessLauncherDelegate; |
| 32 |
| 33 // Note: These codes are listed in a histogram and any new codes should be added | 33 // Note: These codes are listed in a histogram and any new codes should be added |
| 34 // at the end. | 34 // at the end. |
| 35 enum LaunchResultCode { | 35 enum LaunchResultCode { |
| 36 // Launch start code, to not overlap with sandbox::ResultCode. | 36 // Launch start code, to not overlap with sandbox::ResultCode. |
| 37 LAUNCH_RESULT_START = 1001, | 37 LAUNCH_RESULT_START = 1001, |
| 38 // Launch success. | 38 // Launch success. |
| 39 LAUNCH_RESULT_SUCCESS, | 39 LAUNCH_RESULT_SUCCESS, |
| 40 // Generic launch failure. | 40 // Generic launch failure. |
| 41 LAUNCH_RESULT_FAILURE, | 41 LAUNCH_RESULT_FAILURE, |
| 42 // Placeholder for last item of the enum. | 42 // Placeholder for last item of the enum. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // |exit_code| is the exit code of the process if it exited (e.g. status from | 102 // |exit_code| is the exit code of the process if it exited (e.g. status from |
| 103 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may | 103 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may |
| 104 // be NULL. | 104 // be NULL. |
| 105 base::TerminationStatus GetChildTerminationStatus(bool known_dead, | 105 base::TerminationStatus GetChildTerminationStatus(bool known_dead, |
| 106 int* exit_code); | 106 int* exit_code); |
| 107 | 107 |
| 108 // Changes whether the process runs in the background or not. Only call | 108 // Changes whether the process runs in the background or not. Only call |
| 109 // this after the process has started. | 109 // this after the process has started. |
| 110 void SetProcessBackgrounded(bool background); | 110 void SetProcessBackgrounded(bool background); |
| 111 | 111 |
| 112 // Terminates the process associated with this ChildProcessLauncher. |
| 113 // Returns true if the process was stopped, false if the process had not been |
| 114 // started yet or could not be stopped. |
| 115 // Note that |exit_code| and |wait| are not used on Android. |
| 116 bool Terminate(int exit_code, bool wait); |
| 117 |
| 118 // Similar to Terminate() but takes in a |process|. |
| 119 // On Android |process| must have been started by ChildProcessLauncher for |
| 120 // this method to work. |
| 121 static bool TerminateProcess(const base::Process& process, |
| 122 int exit_code, |
| 123 bool wait); |
| 124 |
| 112 // Replaces the ChildProcessLauncher::Client for testing purposes. Returns the | 125 // Replaces the ChildProcessLauncher::Client for testing purposes. Returns the |
| 113 // previous client. | 126 // previous client. |
| 114 Client* ReplaceClientForTest(Client* client); | 127 Client* ReplaceClientForTest(Client* client); |
| 115 | 128 |
| 116 private: | 129 private: |
| 117 // Posts a task to the launcher thread to do the actual work. | 130 friend class internal::ChildProcessLauncherHelper; |
| 118 void Launch(std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, | |
| 119 std::unique_ptr<base::CommandLine> cmd_line, | |
| 120 int child_process_id); | |
| 121 | 131 |
| 122 void UpdateTerminationStatus(bool known_dead); | 132 void UpdateTerminationStatus(bool known_dead); |
| 123 | 133 |
| 124 // This is always called on the client thread after an attempt | |
| 125 // to launch the child process on the launcher thread. | |
| 126 // It makes sure we always perform the necessary cleanup if the | |
| 127 // client went away. | |
| 128 static void DidLaunch(base::WeakPtr<ChildProcessLauncher> instance, | |
| 129 bool terminate_on_shutdown, | |
| 130 mojo::edk::ScopedPlatformHandle server_handle, | |
| 131 ZygoteHandle zygote, | |
| 132 #if defined(OS_ANDROID) | |
| 133 base::ScopedFD mojo_fd, | |
| 134 #endif | |
| 135 base::Process process, | |
| 136 int error_code); | |
| 137 | |
| 138 // Notifies the client about the result of the operation. | 134 // Notifies the client about the result of the operation. |
| 139 void Notify(ZygoteHandle zygote, | 135 void Notify(internal::ChildProcessLauncherHelper::Process process, |
| 140 mojo::edk::ScopedPlatformHandle server_handle, | 136 mojo::edk::ScopedPlatformHandle server_handle, |
| 141 base::Process process, | |
| 142 int error_code); | 137 int error_code); |
| 143 | 138 |
| 144 Client* client_; | 139 Client* client_; |
| 145 BrowserThread::ID client_thread_id_; | 140 BrowserThread::ID client_thread_id_; |
| 146 base::Process process_; | 141 |
| 142 // The process associated with this ChildProcessLauncher. Set in Notify by |
| 143 // ChildProcessLauncherHelper once the process was started. |
| 144 internal::ChildProcessLauncherHelper::Process process_; |
| 145 |
| 147 base::TerminationStatus termination_status_; | 146 base::TerminationStatus termination_status_; |
| 148 int exit_code_; | 147 int exit_code_; |
| 149 ZygoteHandle zygote_; | |
| 150 bool starting_; | 148 bool starting_; |
| 151 const mojo::edk::ProcessErrorCallback process_error_callback_; | 149 const mojo::edk::ProcessErrorCallback process_error_callback_; |
| 152 | 150 |
| 153 // Controls whether the child process should be terminated on browser | 151 // Controls whether the child process should be terminated on browser |
| 154 // shutdown. Default behavior is to terminate the child. | 152 // shutdown. Default behavior is to terminate the child. |
| 155 const bool terminate_child_on_shutdown_; | 153 const bool terminate_child_on_shutdown_; |
| 156 | 154 |
| 157 const std::string mojo_child_token_; | 155 const std::string mojo_child_token_; |
| 158 | 156 |
| 157 scoped_refptr<internal::ChildProcessLauncherHelper> helper_; |
| 158 |
| 159 base::WeakPtrFactory<ChildProcessLauncher> weak_factory_; | 159 base::WeakPtrFactory<ChildProcessLauncher> weak_factory_; |
| 160 | 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher); | 161 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher); |
| 162 }; | 162 }; |
| 163 | 163 |
| 164 } // namespace content | 164 } // namespace content |
| 165 | 165 |
| 166 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ | 166 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ |
| OLD | NEW |