| 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/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/files/scoped_file.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 10 #include "base/process/kill.h" | 11 #include "base/process/kill.h" |
| 11 #include "base/process/launch.h" | 12 #include "base/process/launch.h" |
| 12 #include "base/process/process.h" | 13 #include "base/process/process.h" |
| 14 #include "base/threading/non_thread_safe.h" |
| 13 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 14 | 17 |
| 15 namespace base { | 18 namespace base { |
| 16 class CommandLine; | 19 class CommandLine; |
| 17 } | 20 } |
| 18 | 21 |
| 19 namespace content { | 22 namespace content { |
| 20 class SandboxedProcessLauncherDelegate; | 23 class SandboxedProcessLauncherDelegate; |
| 21 | 24 |
| 22 // Launches a process asynchronously and notifies the client of the process | 25 // Launches a process asynchronously and notifies the client of the process |
| 23 // handle when it's available. It's used to avoid blocking the calling thread | 26 // handle when it's available. It's used to avoid blocking the calling thread |
| 24 // on the OS since often it can take > 100 ms to create the process. | 27 // on the OS since often it can take > 100 ms to create the process. |
| 25 class CONTENT_EXPORT ChildProcessLauncher { | 28 class CONTENT_EXPORT ChildProcessLauncher : public base::NonThreadSafe { |
| 26 public: | 29 public: |
| 27 class CONTENT_EXPORT Client { | 30 class CONTENT_EXPORT Client { |
| 28 public: | 31 public: |
| 29 // Will be called on the thread that the ChildProcessLauncher was | 32 // Will be called on the thread that the ChildProcessLauncher was |
| 30 // constructed on. | 33 // constructed on. |
| 31 virtual void OnProcessLaunched() = 0; | 34 virtual void OnProcessLaunched() = 0; |
| 32 | 35 |
| 33 virtual void OnProcessLaunchFailed() {}; | 36 virtual void OnProcessLaunchFailed() {}; |
| 34 | 37 |
| 35 protected: | 38 protected: |
| 36 virtual ~Client() {} | 39 virtual ~Client() {} |
| 37 }; | 40 }; |
| 38 | 41 |
| 39 // Launches the process asynchronously, calling the client when the result is | 42 // Launches the process asynchronously, calling the client when the result is |
| 40 // ready. Deleting this object before the process is created is safe, since | 43 // ready. Deleting this object before the process is created is safe, since |
| 41 // the callback won't be called. If the process is still running by the time | 44 // the callback won't be called. If the process is still running by the time |
| 42 // this object destructs, it will be terminated. | 45 // this object destructs, it will be terminated. |
| 43 // Takes ownership of cmd_line. | 46 // Takes ownership of cmd_line. |
| 44 ChildProcessLauncher( | 47 ChildProcessLauncher( |
| 45 SandboxedProcessLauncherDelegate* delegate, | 48 SandboxedProcessLauncherDelegate* delegate, |
| 46 base::CommandLine* cmd_line, | 49 base::CommandLine* cmd_line, |
| 47 int child_process_id, | 50 int child_process_id, |
| 48 Client* client); | 51 Client* client, |
| 52 bool terminate_on_shutdown = true); |
| 49 ~ChildProcessLauncher(); | 53 ~ChildProcessLauncher(); |
| 50 | 54 |
| 51 // True if the process is being launched and so the handle isn't available. | 55 // True if the process is being launched and so the handle isn't available. |
| 52 bool IsStarting(); | 56 bool IsStarting(); |
| 53 | 57 |
| 54 // Getter for the process. Only call after the process has started. | 58 // Getter for the process. Only call after the process has started. |
| 55 const base::Process& GetProcess() const; | 59 const base::Process& GetProcess() const; |
| 56 | 60 |
| 57 // Call this when the child process exits to know what happened to it. | 61 // Call this when the child process exits to know what happened to it. |
| 58 // |known_dead| can be true if we already know the process is dead as it can | 62 // |known_dead| can be true if we already know the process is dead as it can |
| 59 // help the implemention figure the proper TerminationStatus. | 63 // help the implemention figure the proper TerminationStatus. |
| 60 // On Linux, the use of |known_dead| is subtle and can be crucial if an | 64 // On Linux, the use of |known_dead| is subtle and can be crucial if an |
| 61 // accurate status is important. With |known_dead| set to false, a dead | 65 // accurate status is important. With |known_dead| set to false, a dead |
| 62 // process could be seen as running. With |known_dead| set to true, the | 66 // process could be seen as running. With |known_dead| set to true, the |
| 63 // process will be killed if it was still running. See ZygoteHostImpl for | 67 // process will be killed if it was still running. See ZygoteHostImpl for |
| 64 // more discussion of Linux implementation details. | 68 // more discussion of Linux implementation details. |
| 65 // |exit_code| is the exit code of the process if it exited (e.g. status from | 69 // |exit_code| is the exit code of the process if it exited (e.g. status from |
| 66 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may | 70 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may |
| 67 // be NULL. | 71 // be NULL. |
| 68 base::TerminationStatus GetChildTerminationStatus(bool known_dead, | 72 base::TerminationStatus GetChildTerminationStatus(bool known_dead, |
| 69 int* exit_code); | 73 int* exit_code); |
| 70 | 74 |
| 71 // Changes whether the process runs in the background or not. Only call | 75 // Changes whether the process runs in the background or not. Only call |
| 72 // this after the process has started. | 76 // this after the process has started. |
| 73 void SetProcessBackgrounded(bool background); | 77 void SetProcessBackgrounded(bool background); |
| 74 | 78 |
| 75 // Controls whether the child process should be terminated on browser | |
| 76 // shutdown. | |
| 77 void SetTerminateChildOnShutdown(bool terminate_on_shutdown); | |
| 78 | |
| 79 // Replaces the ChildProcessLauncher::Client for testing purposes. Returns the | 79 // Replaces the ChildProcessLauncher::Client for testing purposes. Returns the |
| 80 // previous client. | 80 // previous client. |
| 81 Client* ReplaceClientForTest(Client* client); | 81 Client* ReplaceClientForTest(Client* client); |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 class Context; | 84 // Posts a task to the launcher thread to do the actual work. |
| 85 void Launch(SandboxedProcessLauncherDelegate* delegate, |
| 86 base::CommandLine* cmd_line, |
| 87 int child_process_id); |
| 85 | 88 |
| 86 scoped_refptr<Context> context_; | 89 void UpdateTerminationStatus(bool known_dead); |
| 90 |
| 91 // This is always called on the client thread after an attempt |
| 92 // to launch the child process on the launcher thread. |
| 93 // It makes sure we always perform the necessary cleanup if the |
| 94 // client went away. |
| 95 static void DidLaunch(base::WeakPtr<ChildProcessLauncher> instance, |
| 96 bool terminate_on_shutdown, |
| 97 bool zygote, |
| 98 #if defined(OS_ANDROID) |
| 99 base::ScopedFD ipcfd, |
| 100 #endif |
| 101 base::Process process); |
| 102 |
| 103 // Notifies the client about the result of the operation. |
| 104 void Notify(bool zygote, |
| 105 #if defined(OS_ANDROID) |
| 106 base::ScopedFD ipcfd, |
| 107 #endif |
| 108 base::Process process); |
| 109 |
| 110 Client* client_; |
| 111 BrowserThread::ID client_thread_id_; |
| 112 base::Process process_; |
| 113 base::TerminationStatus termination_status_; |
| 114 int exit_code_; |
| 115 bool zygote_; |
| 116 bool starting_; |
| 117 // Controls whether the child process should be terminated on browser |
| 118 // shutdown. Default behavior is to terminate the child. |
| 119 const bool terminate_child_on_shutdown_; |
| 120 |
| 121 base::WeakPtrFactory<ChildProcessLauncher> weak_factory_; |
| 87 | 122 |
| 88 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher); | 123 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher); |
| 89 }; | 124 }; |
| 90 | 125 |
| 91 } // namespace content | 126 } // namespace content |
| 92 | 127 |
| 93 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ | 128 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ |
| OLD | NEW |