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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ 6 #define CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/process/kill.h" 11 #include "base/process/launch.h" 12 #include "content/common/content_export.h" 13 14 class CommandLine; 15 16 namespace content { 17 class SandboxedProcessLauncherDelegate; 18 19 // Launches a process asynchronously and notifies the client of the process 20 // handle when it's available. It's used to avoid blocking the calling thread 21 // on the OS since often it can take > 100 ms to create the process. 22 class CONTENT_EXPORT ChildProcessLauncher { 23 public: 24 class CONTENT_EXPORT Client { 25 public: 26 // Will be called on the thread that the ChildProcessLauncher was 27 // constructed on. 28 virtual void OnProcessLaunched() = 0; 29 30 protected: 31 virtual ~Client() {} 32 }; 33 34 // Launches the process asynchronously, calling the client when the result is 35 // ready. Deleting this object before the process is created is safe, since 36 // the callback won't be called. If the process is still running by the time 37 // this object destructs, it will be terminated. 38 // Takes ownership of cmd_line. 39 ChildProcessLauncher( 40 #if defined(OS_WIN) 41 SandboxedProcessLauncherDelegate* delegate, 42 #elif defined(OS_POSIX) 43 bool use_zygote, 44 const base::EnvironmentVector& environ, 45 int ipcfd, 46 #endif 47 CommandLine* cmd_line, 48 int child_process_id, 49 Client* client); 50 ~ChildProcessLauncher(); 51 52 // True if the process is being launched and so the handle isn't available. 53 bool IsStarting(); 54 55 // Getter for the process handle. Only call after the process has started. 56 base::ProcessHandle GetHandle(); 57 58 // Call this when the child process exits to know what happened to it. 59 // |known_dead| can be true if we already know the process is dead as it can 60 // help the implemention figure the proper TerminationStatus. 61 // |exit_code| is the exit code of the process if it exited (e.g. status from 62 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may 63 // be NULL. 64 base::TerminationStatus GetChildTerminationStatus(bool known_dead, 65 int* exit_code); 66 67 // Changes whether the process runs in the background or not. Only call 68 // this after the process has started. 69 void SetProcessBackgrounded(bool background); 70 71 // Controls whether the child process should be terminated on browser 72 // shutdown. 73 void SetTerminateChildOnShutdown(bool terminate_on_shutdown); 74 75 private: 76 class Context; 77 78 scoped_refptr<Context> context_; 79 80 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher); 81 }; 82 83 } // namespace content 84 85 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_ 86