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