OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | 5 #ifndef CHROME_COMMON_CHILD_PROCESS_HOST_H_ |
6 #define CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | 6 #define CHROME_COMMON_CHILD_PROCESS_HOST_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 // Must be included early (e.g. before chrome/common/plugin_messages.h) | 11 // Must be included early (e.g. before chrome/common/plugin_messages.h) |
12 #include "ipc/ipc_logging.h" | 12 #include "ipc/ipc_logging.h" |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/file_path.h" |
15 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
16 #include "chrome/browser/child_process_launcher.h" | 17 #include "chrome/common/notification_type.h" |
17 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
18 #include "ipc/ipc_channel.h" | 18 #include "ipc/ipc_channel.h" |
19 | 19 |
20 class CommandLine; | 20 class CommandLine; |
21 class NotificationType; | |
22 | 21 |
23 // Plugins/workers and other child processes that live on the IO thread should | 22 // Provides common functionality for hosting a child process and processing IPC |
24 // derive from this class. | 23 // messages between the host and the child process. Subclasses are responsible |
| 24 // for the actual launching and terminating of the child processes. |
25 // | 25 // |
26 // [Browser]RenderProcessHost is the main exception that doesn't derive from | 26 class ChildProcessHost : public IPC::Channel::Listener { |
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: | 27 public: |
32 virtual ~ChildProcessHost(); | 28 virtual ~ChildProcessHost(); |
33 | 29 |
34 // Returns the pathname to be used for a child process. If a subprocess | 30 // 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, | 31 // pathname was specified on the command line, that will be used. Otherwise, |
36 // the default child process pathname will be returned. On most platforms, | 32 // the default child process pathname will be returned. On most platforms, |
37 // this will be the same as the currently-executing process. | 33 // this will be the same as the currently-executing process. |
38 // | 34 // |
39 // The argument allow_self is used on Linux to indicate that we allow us to | 35 // 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 | 36 // 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 | 37 // 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 | 38 // 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 | 39 // 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 | 40 // 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. | 41 // file name, and then prepend the GDB command to the command line. |
46 // | 42 // |
47 // On failure, returns an empty FilePath. | 43 // On failure, returns an empty FilePath. |
48 static FilePath GetChildPath(bool allow_self); | 44 static FilePath GetChildPath(bool allow_self); |
49 | 45 |
50 // Prepares command_line for crash reporting as appropriate. On Linux and | 46 protected: |
51 // Mac, a command-line flag to enable crash reporting in the child process | 47 ChildProcessHost(); |
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 | 48 |
57 // Terminates all child processes and deletes each ChildProcessHost instance. | 49 // A helper method to send an IPC message to the child on the channel. |
58 static void TerminateAll(); | 50 // It behavies just like IPC::Message::Sender::Send. The implementor takes |
59 | 51 // ownership of the given Message regardless of whether or not this method |
60 // ResourceDispatcherHost::Receiver implementation: | 52 // succeeds. This class does not implement IPC::Message::Sender to prevent |
61 virtual bool Send(IPC::Message* msg); | 53 // conflicts with subclasses which indirectly could inherit from |
62 | 54 // IPC::Message::Sender. |
63 // The Iterator class allows iteration through either all child processes, or | 55 bool SendOnChannel(IPC::Message* msg); |
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 | 56 |
99 // Derived classes return true if it's ok to shut down the child process. | 57 // Derived classes return true if it's ok to shut down the child process. |
100 virtual bool CanShutdown() = 0; | 58 virtual bool CanShutdown() = 0; |
101 | 59 |
102 // Send the shutdown message to the child process, and remove this host from | 60 // Send the shutdown message to the child process. |
103 // the host list. Does not check if CanShutdown is true. | 61 // Does not check if CanShutdown is true. |
104 void ForceShutdown(); | 62 virtual void ForceShutdown(); |
105 | 63 |
106 // Creates the IPC channel. Returns true iff it succeeded. | 64 // Creates the IPC channel. Returns true iff it succeeded. |
107 bool CreateChannel(); | 65 virtual bool CreateChannel(); |
108 | 66 |
109 // Notifies us that an instance has been created on this child process. | 67 // Notifies us that an instance has been created on this child process. |
110 void InstanceCreated(); | 68 virtual void InstanceCreated(); |
111 | 69 |
112 // IPC::Channel::Listener implementation: | 70 // IPC::Channel::Listener implementation: |
113 virtual void OnMessageReceived(const IPC::Message& msg) { } | 71 virtual void OnMessageReceived(const IPC::Message& msg) { } |
114 virtual void OnChannelConnected(int32 peer_pid) { } | 72 virtual void OnChannelConnected(int32 peer_pid) { } |
115 virtual void OnChannelError() { } | 73 virtual void OnChannelError() { } |
116 | 74 |
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_; } | 75 bool opening_channel() { return opening_channel_; } |
124 const std::string& channel_id() { return channel_id_; } | 76 const std::string& channel_id() { return channel_id_; } |
125 | 77 IPC::Channel* channel() { return channel_.get(); } |
126 virtual bool DidChildCrash(); | |
127 | 78 |
128 // Called when the child process goes away. | 79 // Called when the child process goes away. |
129 virtual void OnChildDied(); | 80 virtual void OnChildDied(); |
| 81 // Allows the derived implementation to intercept a message before it is |
| 82 // handed to the IPC::Channel::Listener::OnMessageReceived implementation. |
| 83 virtual bool InterceptMessageFromChild(const IPC::Message& msg) { |
| 84 return false; |
| 85 } |
| 86 // Subclasses can implement specific notification methods. |
| 87 virtual void Notify(NotificationType type) { } |
130 | 88 |
131 private: | 89 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 | 90 // By using an internal class as the IPC::Channel::Listener, we can intercept |
136 // OnMessageReceived/OnChannelConnected and do our own processing before | 91 // OnMessageReceived/OnChannelConnected and do our own processing before |
137 // calling the subclass' implementation. | 92 // calling the subclass' implementation. |
138 class ListenerHook : public IPC::Channel::Listener, | 93 class ListenerHook : public IPC::Channel::Listener { |
139 public ChildProcessLauncher::Client { | |
140 public: | 94 public: |
141 explicit ListenerHook(ChildProcessHost* host); | 95 explicit ListenerHook(ChildProcessHost* host); |
142 virtual void OnMessageReceived(const IPC::Message& msg); | 96 virtual void OnMessageReceived(const IPC::Message& msg); |
143 virtual void OnChannelConnected(int32 peer_pid); | 97 virtual void OnChannelConnected(int32 peer_pid); |
144 virtual void OnChannelError(); | 98 virtual void OnChannelError(); |
145 virtual void OnProcessLaunched(); | |
146 private: | 99 private: |
147 ChildProcessHost* host_; | 100 ChildProcessHost* host_; |
148 }; | 101 }; |
149 | 102 |
150 ListenerHook listener_; | 103 ListenerHook listener_; |
151 | 104 |
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. | 105 bool opening_channel_; // True while we're waiting the channel to be opened. |
156 scoped_ptr<IPC::Channel> channel_; | 106 scoped_ptr<IPC::Channel> channel_; |
157 std::string channel_id_; | 107 std::string channel_id_; |
158 scoped_ptr<ChildProcessLauncher> child_process_; | |
159 }; | 108 }; |
160 | 109 |
161 #endif // CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | 110 #endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_ |
| 111 |
OLD | NEW |