OLD | NEW |
| (Empty) |
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_CHILD_CHILD_THREAD_H_ | |
6 #define CONTENT_CHILD_CHILD_THREAD_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/shared_memory.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/power_monitor/power_monitor.h" | |
15 #include "base/tracked_objects.h" | |
16 #include "content/child/mojo/mojo_application.h" | |
17 #include "content/common/content_export.h" | |
18 #include "content/common/message_router.h" | |
19 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. | |
20 | |
21 namespace base { | |
22 class MessageLoop; | |
23 | |
24 namespace debug { | |
25 class TraceMemoryController; | |
26 } // namespace debug | |
27 } // namespace base | |
28 | |
29 namespace IPC { | |
30 class MessageFilter; | |
31 class SyncChannel; | |
32 class SyncMessageFilter; | |
33 } // namespace IPC | |
34 | |
35 namespace blink { | |
36 class WebFrame; | |
37 } // namespace blink | |
38 | |
39 namespace content { | |
40 class BluetoothMessageFilter; | |
41 class ChildDiscardableSharedMemoryManager; | |
42 class ChildGpuMemoryBufferManager; | |
43 class ChildHistogramMessageFilter; | |
44 class ChildResourceMessageFilter; | |
45 class ChildSharedBitmapManager; | |
46 class FileSystemDispatcher; | |
47 class GeofencingMessageFilter; | |
48 class NavigatorConnectDispatcher; | |
49 class NotificationDispatcher; | |
50 class PushDispatcher; | |
51 class ServiceWorkerMessageFilter; | |
52 class QuotaDispatcher; | |
53 class QuotaMessageFilter; | |
54 class ResourceDispatcher; | |
55 class ThreadSafeSender; | |
56 class WebSocketDispatcher; | |
57 struct RequestInfo; | |
58 | |
59 // The main thread of a child process derives from this class. | |
60 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender { | |
61 public: | |
62 struct CONTENT_EXPORT Options { | |
63 Options(); | |
64 explicit Options(bool mojo); | |
65 Options(std::string name, bool mojo); | |
66 ~Options(); | |
67 | |
68 std::string channel_name; | |
69 bool use_mojo_channel; | |
70 bool in_browser_process; | |
71 std::vector<IPC::MessageFilter*> startup_filters; | |
72 }; | |
73 | |
74 // Creates the thread. | |
75 ChildThread(); | |
76 // Allow to be used for single-process mode and for in process gpu mode via | |
77 // options. | |
78 explicit ChildThread(const Options& options); | |
79 // ChildProcess::main_thread() is reset after Shutdown(), and before the | |
80 // destructor, so any subsystem that relies on ChildProcess::main_thread() | |
81 // must be terminated before Shutdown returns. In particular, if a subsystem | |
82 // has a thread that post tasks to ChildProcess::main_thread(), that thread | |
83 // should be joined in Shutdown(). | |
84 ~ChildThread() override; | |
85 virtual void Shutdown(); | |
86 | |
87 // IPC::Sender implementation: | |
88 bool Send(IPC::Message* msg) override; | |
89 | |
90 IPC::SyncChannel* channel() { return channel_.get(); } | |
91 | |
92 MessageRouter* GetRouter(); | |
93 | |
94 // Allocates a block of shared memory of the given size. Returns NULL on | |
95 // failure. | |
96 // Note: On posix, this requires a sync IPC to the browser process, | |
97 // but on windows the child process directly allocates the block. | |
98 scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size); | |
99 | |
100 // A static variant that can be called on background threads provided | |
101 // the |sender| passed in is safe to use on background threads. | |
102 static scoped_ptr<base::SharedMemory> AllocateSharedMemory( | |
103 size_t buf_size, | |
104 IPC::Sender* sender); | |
105 | |
106 ChildSharedBitmapManager* shared_bitmap_manager() const { | |
107 return shared_bitmap_manager_.get(); | |
108 } | |
109 | |
110 ChildGpuMemoryBufferManager* gpu_memory_buffer_manager() const { | |
111 return gpu_memory_buffer_manager_.get(); | |
112 } | |
113 | |
114 ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager() | |
115 const { | |
116 return discardable_shared_memory_manager_.get(); | |
117 } | |
118 | |
119 ResourceDispatcher* resource_dispatcher() const { | |
120 return resource_dispatcher_.get(); | |
121 } | |
122 | |
123 WebSocketDispatcher* websocket_dispatcher() const { | |
124 return websocket_dispatcher_.get(); | |
125 } | |
126 | |
127 FileSystemDispatcher* file_system_dispatcher() const { | |
128 return file_system_dispatcher_.get(); | |
129 } | |
130 | |
131 QuotaDispatcher* quota_dispatcher() const { | |
132 return quota_dispatcher_.get(); | |
133 } | |
134 | |
135 NotificationDispatcher* notification_dispatcher() const { | |
136 return notification_dispatcher_.get(); | |
137 } | |
138 | |
139 PushDispatcher* push_dispatcher() const { | |
140 return push_dispatcher_.get(); | |
141 } | |
142 | |
143 IPC::SyncMessageFilter* sync_message_filter() const { | |
144 return sync_message_filter_.get(); | |
145 } | |
146 | |
147 // The getter should only be called on the main thread, however the | |
148 // IPC::Sender it returns may be safely called on any thread including | |
149 // the main thread. | |
150 ThreadSafeSender* thread_safe_sender() const { | |
151 return thread_safe_sender_.get(); | |
152 } | |
153 | |
154 ChildHistogramMessageFilter* child_histogram_message_filter() const { | |
155 return histogram_message_filter_.get(); | |
156 } | |
157 | |
158 ServiceWorkerMessageFilter* service_worker_message_filter() const { | |
159 return service_worker_message_filter_.get(); | |
160 } | |
161 | |
162 QuotaMessageFilter* quota_message_filter() const { | |
163 return quota_message_filter_.get(); | |
164 } | |
165 | |
166 ChildResourceMessageFilter* child_resource_message_filter() const { | |
167 return resource_message_filter_.get(); | |
168 } | |
169 | |
170 base::MessageLoop* message_loop() const { return message_loop_; } | |
171 | |
172 // Returns the one child thread. Can only be called on the main thread. | |
173 static ChildThread* current(); | |
174 | |
175 #if defined(OS_ANDROID) | |
176 // Called on Android's service thread to shutdown the main thread of this | |
177 // process. | |
178 static void ShutdownThread(); | |
179 #endif | |
180 | |
181 ServiceRegistry* service_registry() const { | |
182 return mojo_application_->service_registry(); | |
183 } | |
184 | |
185 protected: | |
186 friend class ChildProcess; | |
187 | |
188 // Called when the process refcount is 0. | |
189 void OnProcessFinalRelease(); | |
190 | |
191 virtual bool OnControlMessageReceived(const IPC::Message& msg); | |
192 | |
193 void set_on_channel_error_called(bool on_channel_error_called) { | |
194 on_channel_error_called_ = on_channel_error_called; | |
195 } | |
196 | |
197 // IPC::Listener implementation: | |
198 bool OnMessageReceived(const IPC::Message& msg) override; | |
199 void OnChannelConnected(int32 peer_pid) override; | |
200 void OnChannelError() override; | |
201 | |
202 private: | |
203 class ChildThreadMessageRouter : public MessageRouter { | |
204 public: | |
205 // |sender| must outlive this object. | |
206 explicit ChildThreadMessageRouter(IPC::Sender* sender); | |
207 bool Send(IPC::Message* msg) override; | |
208 | |
209 private: | |
210 IPC::Sender* const sender_; | |
211 }; | |
212 | |
213 void Init(const Options& options); | |
214 | |
215 // We create the channel first without connecting it so we can add filters | |
216 // prior to any messages being received, then connect it afterwards. | |
217 void ConnectChannel(bool use_mojo_channel); | |
218 | |
219 // IPC message handlers. | |
220 void OnShutdown(); | |
221 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status); | |
222 void OnGetChildProfilerData(int sequence_number); | |
223 void OnDumpHandles(); | |
224 void OnProcessBackgrounded(bool background); | |
225 #ifdef IPC_MESSAGE_LOG_ENABLED | |
226 void OnSetIPCLoggingEnabled(bool enable); | |
227 #endif | |
228 #if defined(USE_TCMALLOC) | |
229 void OnGetTcmallocStats(); | |
230 #endif | |
231 | |
232 void EnsureConnected(); | |
233 | |
234 scoped_ptr<MojoApplication> mojo_application_; | |
235 | |
236 std::string channel_name_; | |
237 scoped_ptr<IPC::SyncChannel> channel_; | |
238 | |
239 // Allows threads other than the main thread to send sync messages. | |
240 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_; | |
241 | |
242 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | |
243 | |
244 // Implements message routing functionality to the consumers of ChildThread. | |
245 ChildThreadMessageRouter router_; | |
246 | |
247 // Handles resource loads for this process. | |
248 scoped_ptr<ResourceDispatcher> resource_dispatcher_; | |
249 | |
250 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_; | |
251 | |
252 // The OnChannelError() callback was invoked - the channel is dead, don't | |
253 // attempt to communicate. | |
254 bool on_channel_error_called_; | |
255 | |
256 base::MessageLoop* message_loop_; | |
257 | |
258 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_; | |
259 | |
260 scoped_ptr<QuotaDispatcher> quota_dispatcher_; | |
261 | |
262 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_; | |
263 | |
264 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_; | |
265 | |
266 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_; | |
267 | |
268 scoped_refptr<QuotaMessageFilter> quota_message_filter_; | |
269 | |
270 scoped_refptr<NotificationDispatcher> notification_dispatcher_; | |
271 | |
272 scoped_refptr<PushDispatcher> push_dispatcher_; | |
273 | |
274 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_; | |
275 | |
276 scoped_ptr<ChildGpuMemoryBufferManager> gpu_memory_buffer_manager_; | |
277 | |
278 scoped_ptr<ChildDiscardableSharedMemoryManager> | |
279 discardable_shared_memory_manager_; | |
280 | |
281 // Observes the trace event system. When tracing is enabled, optionally | |
282 // starts profiling the tcmalloc heap. | |
283 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_; | |
284 | |
285 scoped_ptr<base::PowerMonitor> power_monitor_; | |
286 | |
287 scoped_refptr<GeofencingMessageFilter> geofencing_message_filter_; | |
288 | |
289 scoped_refptr<BluetoothMessageFilter> bluetooth_message_filter_; | |
290 | |
291 scoped_refptr<NavigatorConnectDispatcher> navigator_connect_dispatcher_; | |
292 | |
293 bool in_browser_process_; | |
294 | |
295 base::WeakPtrFactory<ChildThread> channel_connected_factory_; | |
296 | |
297 DISALLOW_COPY_AND_ASSIGN(ChildThread); | |
298 }; | |
299 | |
300 } // namespace content | |
301 | |
302 #endif // CONTENT_CHILD_CHILD_THREAD_H_ | |
OLD | NEW |