Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / child_process_launcher.h
blob2d647f4087766203f4a3519432b2ff29e041d4b8
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/files/scoped_file.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/process/kill.h"
12 #include "base/process/launch.h"
13 #include "base/process/process.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/browser_thread.h"
18 namespace base {
19 class CommandLine;
22 namespace content {
23 class SandboxedProcessLauncherDelegate;
25 // Launches a process asynchronously and notifies the client of the process
26 // handle when it's available. It's used to avoid blocking the calling thread
27 // on the OS since often it can take > 100 ms to create the process.
28 class CONTENT_EXPORT ChildProcessLauncher : public base::NonThreadSafe {
29 public:
30 class CONTENT_EXPORT Client {
31 public:
32 // Will be called on the thread that the ChildProcessLauncher was
33 // constructed on.
34 virtual void OnProcessLaunched() = 0;
36 virtual void OnProcessLaunchFailed() {};
38 protected:
39 virtual ~Client() {}
42 // Launches the process asynchronously, calling the client when the result is
43 // ready. Deleting this object before the process is created is safe, since
44 // the callback won't be called. If the process is still running by the time
45 // this object destructs, it will be terminated.
46 // Takes ownership of cmd_line.
47 ChildProcessLauncher(
48 SandboxedProcessLauncherDelegate* delegate,
49 base::CommandLine* cmd_line,
50 int child_process_id,
51 Client* client,
52 bool terminate_on_shutdown = true);
53 ~ChildProcessLauncher();
55 // True if the process is being launched and so the handle isn't available.
56 bool IsStarting();
58 // Getter for the process. Only call after the process has started.
59 const base::Process& GetProcess() const;
61 // Call this when the child process exits to know what happened to it.
62 // |known_dead| can be true if we already know the process is dead as it can
63 // help the implemention figure the proper TerminationStatus.
64 // On Linux, the use of |known_dead| is subtle and can be crucial if an
65 // accurate status is important. With |known_dead| set to false, a dead
66 // process could be seen as running. With |known_dead| set to true, the
67 // process will be killed if it was still running. See ZygoteHostImpl for
68 // more discussion of Linux implementation details.
69 // |exit_code| is the exit code of the process if it exited (e.g. status from
70 // waitpid if on posix, from GetExitCodeProcess on Windows). |exit_code| may
71 // be NULL.
72 base::TerminationStatus GetChildTerminationStatus(bool known_dead,
73 int* exit_code);
75 // Changes whether the process runs in the background or not. Only call
76 // this after the process has started.
77 void SetProcessBackgrounded(bool background);
79 // Replaces the ChildProcessLauncher::Client for testing purposes. Returns the
80 // previous client.
81 Client* ReplaceClientForTest(Client* client);
83 private:
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);
89 void UpdateTerminationStatus(bool known_dead);
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);
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);
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_;
121 base::WeakPtrFactory<ChildProcessLauncher> weak_factory_;
123 DISALLOW_COPY_AND_ASSIGN(ChildProcessLauncher);
126 } // namespace content
128 #endif // CONTENT_BROWSER_CHILD_PROCESS_LAUNCHER_H_