Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / child_process_launcher.h
blobde890a92e404cbcd15688bf6a6fe7dc9d5245a6a
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.
5 #ifndef CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_
6 #define CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_
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 "base/process/process.h"
13 #include "content/common/content_export.h"
15 namespace base {
16 class CommandLine;
19 namespace content {
20 class SandboxedProcessLauncherDelegate;
22 // 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
24 // on the OS since often it can take > 100 ms to create the process.
25 class CONTENT_EXPORT ChildProcessLauncher {
26 public:
27 class CONTENT_EXPORT Client {
28 public:
29 // Will be called on the thread that the ChildProcessLauncher was
30 // constructed on.
31 virtual void OnProcessLaunched() = 0;
33 virtual void OnProcessLaunchFailed() {};
35 protected:
36 virtual ~Client() {}
39 // Launches the process asynchronously, calling the client when the result is
40 // 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
42 // this object destructs, it will be terminated.
43 // Takes ownership of cmd_line.
44 ChildProcessLauncher(
45 SandboxedProcessLauncherDelegate* delegate,
46 base::CommandLine* cmd_line,
47 int child_process_id,
48 Client* client);
49 ~ChildProcessLauncher();
51 // True if the process is being launched and so the handle isn't available.
52 bool IsStarting();
54 // Getter for the process. Only call after the process has started.
55 const base::Process& GetProcess() const;
57 // 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
59 // help the implemention figure the proper TerminationStatus.
60 // 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
62 // 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
64 // more discussion of Linux implementation details.
65 // |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
67 // be NULL.
68 base::TerminationStatus GetChildTerminationStatus(bool known_dead,
69 int* exit_code);
71 // Changes whether the process runs in the background or not. Only call
72 // this after the process has started.
73 void SetProcessBackgrounded(bool background);
75 // Controls whether the child process should be terminated on browser
76 // shutdown.
77 void SetTerminateChildOnShutdown(bool terminate_on_shutdown);
79 private:
80 class Context;
82 scoped_refptr<Context> context_;
84 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher);
87 } // namespace content
89 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_