OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | |
6 #define CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | |
7 | |
8 #include <list> | |
9 #include <string> | |
10 | |
11 // Must be included early (e.g. before chrome/common/plugin_messages.h) | |
12 #include "ipc/ipc_logging.h" | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/scoped_ptr.h" | |
16 #include "chrome/browser/child_process_launcher.h" | |
17 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
18 #include "ipc/ipc_channel.h" | |
19 | |
20 class CommandLine; | |
21 class NotificationType; | |
22 | |
23 // Plugins/workers and other child processes that live on the IO thread should | |
24 // derive from this class. | |
25 // | |
26 // [Browser]RenderProcessHost is the main exception that doesn't derive from | |
27 // this class. That project lives on the UI thread. | |
28 class ChildProcessHost : public ResourceDispatcherHost::Receiver, | |
29 public IPC::Channel::Listener, | |
30 public ChildProcessLauncher::Client { | |
31 public: | |
32 virtual ~ChildProcessHost(); | |
33 | |
34 // Returns the pathname to be used for a child process. If a subprocess | |
35 // pathname was specified on the command line, that will be used. Otherwise, | |
36 // the default child process pathname will be returned. On most platforms, | |
37 // this will be the same as the currently-executing process. | |
38 // | |
39 // The argument allow_self is used on Linux to indicate that we allow us to | |
40 // fork from /proc/self/exe rather than using the "real" app path. This | |
41 // prevents autoupdate from confusing us if it changes the file out from | |
42 // under us. You will generally want to set this to true, except when there | |
43 // is an override to the command line (for example, we're forking a renderer | |
44 // in gdb). In this case, you'd use GetChildPath to get the real executable | |
45 // file name, and then prepend the GDB command to the command line. | |
46 // | |
47 // On failure, returns an empty FilePath. | |
48 static FilePath GetChildPath(bool allow_self); | |
49 | |
50 // Prepares command_line for crash reporting as appropriate. On Linux and | |
51 // Mac, a command-line flag to enable crash reporting in the child process | |
52 // will be appended if needed, because the child process may not have access | |
53 // to the data that determines the status of crash reporting in the | |
54 // currently-executing process. This function is a no-op on Windows. | |
55 static void SetCrashReporterCommandLine(CommandLine* command_line); | |
56 | |
57 // Terminates all child processes and deletes each ChildProcessHost instance. | |
58 static void TerminateAll(); | |
59 | |
60 // ResourceDispatcherHost::Receiver implementation: | |
61 virtual bool Send(IPC::Message* msg); | |
62 | |
63 // The Iterator class allows iteration through either all child processes, or | |
64 // ones of a specific type, depending on which constructor is used. Note that | |
65 // this should be done from the IO thread and that the iterator should not be | |
66 // kept around as it may be invalidated on subsequent event processing in the | |
67 // event loop. | |
68 class Iterator { | |
69 public: | |
70 Iterator(); | |
71 explicit Iterator(ProcessType type); | |
72 ChildProcessHost* operator->() { return *iterator_; } | |
73 ChildProcessHost* operator*() { return *iterator_; } | |
74 ChildProcessHost* operator++(); | |
75 bool Done(); | |
76 | |
77 private: | |
78 bool all_; | |
79 ProcessType type_; | |
80 std::list<ChildProcessHost*>::iterator iterator_; | |
81 }; | |
82 | |
83 protected: | |
84 // The resource_dispatcher_host may be NULL to indicate none is needed for | |
85 // this process type. | |
86 ChildProcessHost(ProcessType type, | |
87 ResourceDispatcherHost* resource_dispatcher_host); | |
88 | |
89 // Derived classes call this to launch the child process asynchronously. | |
90 void Launch( | |
91 #if defined(OS_WIN) | |
92 const FilePath& exposed_dir, | |
93 #elif defined(OS_POSIX) | |
94 bool use_zygote, | |
95 const base::environment_vector& environ, | |
96 #endif | |
97 CommandLine* cmd_line); | |
98 | |
99 // Derived classes return true if it's ok to shut down the child process. | |
100 virtual bool CanShutdown() = 0; | |
101 | |
102 // Send the shutdown message to the child process, and remove this host from | |
103 // the host list. Does not check if CanShutdown is true. | |
104 void ForceShutdown(); | |
105 | |
106 // Creates the IPC channel. Returns true iff it succeeded. | |
107 bool CreateChannel(); | |
108 | |
109 // Notifies us that an instance has been created on this child process. | |
110 void InstanceCreated(); | |
111 | |
112 // IPC::Channel::Listener implementation: | |
113 virtual void OnMessageReceived(const IPC::Message& msg) { } | |
114 virtual void OnChannelConnected(int32 peer_pid) { } | |
115 virtual void OnChannelError() { } | |
116 | |
117 // ChildProcessLauncher::Client implementation. | |
118 virtual void OnProcessLaunched() {} | |
119 | |
120 // Derived classes can override this to know if the process crashed. | |
121 virtual void OnProcessCrashed() {} | |
122 | |
123 bool opening_channel() { return opening_channel_; } | |
124 const std::string& channel_id() { return channel_id_; } | |
125 | |
126 virtual bool DidChildCrash(); | |
127 | |
128 // Called when the child process goes away. | |
129 virtual void OnChildDied(); | |
130 | |
131 private: | |
132 // Sends the given notification to the notification service on the UI thread. | |
133 void Notify(NotificationType type); | |
134 | |
135 // By using an internal class as the IPC::Channel::Listener, we can intercept | |
136 // OnMessageReceived/OnChannelConnected and do our own processing before | |
137 // calling the subclass' implementation. | |
138 class ListenerHook : public IPC::Channel::Listener, | |
139 public ChildProcessLauncher::Client { | |
140 public: | |
141 explicit ListenerHook(ChildProcessHost* host); | |
142 virtual void OnMessageReceived(const IPC::Message& msg); | |
143 virtual void OnChannelConnected(int32 peer_pid); | |
144 virtual void OnChannelError(); | |
145 virtual void OnProcessLaunched(); | |
146 private: | |
147 ChildProcessHost* host_; | |
148 }; | |
149 | |
150 ListenerHook listener_; | |
151 | |
152 // May be NULL if this current process has no resource dispatcher host. | |
153 ResourceDispatcherHost* resource_dispatcher_host_; | |
154 | |
155 bool opening_channel_; // True while we're waiting the channel to be opened. | |
156 scoped_ptr<IPC::Channel> channel_; | |
157 std::string channel_id_; | |
158 scoped_ptr<ChildProcessLauncher> child_process_; | |
159 }; | |
160 | |
161 #endif // CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | |
OLD | NEW |