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_CHILD_CHILD_THREAD_IMPL_H_
6 #define CONTENT_CHILD_CHILD_THREAD_IMPL_H_
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 "content/public/child/child_thread.h"
20 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
25 namespace trace_event
{
26 class TraceMemoryController
;
27 } // namespace trace_event
32 class ScopedIPCSupport
;
34 class SyncMessageFilter
;
42 class ChildMessageFilter
;
43 class ChildDiscardableSharedMemoryManager
;
44 class ChildGpuMemoryBufferManager
;
45 class ChildHistogramMessageFilter
;
46 class ChildResourceMessageFilter
;
47 class ChildSharedBitmapManager
;
48 class FileSystemDispatcher
;
49 class NavigatorConnectDispatcher
;
50 class NotificationDispatcher
;
52 class ServiceWorkerMessageFilter
;
53 class QuotaDispatcher
;
54 class QuotaMessageFilter
;
55 class ResourceDispatcher
;
56 class ThreadSafeSender
;
57 class WebSocketDispatcher
;
60 // The main thread of a child process derives from this class.
61 class CONTENT_EXPORT ChildThreadImpl
62 : public IPC::Listener
,
63 virtual public ChildThread
{
65 struct CONTENT_EXPORT Options
;
67 // Creates the thread.
69 // Allow to be used for single-process mode and for in process gpu mode via
71 explicit ChildThreadImpl(const Options
& options
);
72 // ChildProcess::main_thread() is reset after Shutdown(), and before the
73 // destructor, so any subsystem that relies on ChildProcess::main_thread()
74 // must be terminated before Shutdown returns. In particular, if a subsystem
75 // has a thread that post tasks to ChildProcess::main_thread(), that thread
76 // should be joined in Shutdown().
77 ~ChildThreadImpl() override
;
78 virtual void Shutdown();
80 // IPC::Sender implementation:
81 bool Send(IPC::Message
* msg
) override
;
83 // ChildThread implementation:
85 void PreCacheFont(const LOGFONT
& log_font
) override
;
86 void ReleaseCachedFonts() override
;
89 IPC::SyncChannel
* channel() { return channel_
.get(); }
91 MessageRouter
* GetRouter();
93 // Allocates a block of shared memory of the given size. Returns NULL on
95 // Note: On posix, this requires a sync IPC to the browser process,
96 // but on windows the child process directly allocates the block.
97 scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t buf_size
);
99 // A static variant that can be called on background threads provided
100 // the |sender| passed in is safe to use on background threads.
101 static scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
103 IPC::Sender
* sender
);
105 ChildSharedBitmapManager
* shared_bitmap_manager() const {
106 return shared_bitmap_manager_
.get();
109 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
110 return gpu_memory_buffer_manager_
.get();
113 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
115 return discardable_shared_memory_manager_
.get();
118 ResourceDispatcher
* resource_dispatcher() const {
119 return resource_dispatcher_
.get();
122 WebSocketDispatcher
* websocket_dispatcher() const {
123 return websocket_dispatcher_
.get();
126 FileSystemDispatcher
* file_system_dispatcher() const {
127 return file_system_dispatcher_
.get();
130 QuotaDispatcher
* quota_dispatcher() const {
131 return quota_dispatcher_
.get();
134 NotificationDispatcher
* notification_dispatcher() const {
135 return notification_dispatcher_
.get();
138 PushDispatcher
* push_dispatcher() const {
139 return push_dispatcher_
.get();
142 IPC::SyncMessageFilter
* sync_message_filter() const {
143 return sync_message_filter_
.get();
146 // The getter should only be called on the main thread, however the
147 // IPC::Sender it returns may be safely called on any thread including
149 ThreadSafeSender
* thread_safe_sender() const {
150 return thread_safe_sender_
.get();
153 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
154 return histogram_message_filter_
.get();
157 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
158 return service_worker_message_filter_
.get();
161 QuotaMessageFilter
* quota_message_filter() const {
162 return quota_message_filter_
.get();
165 ChildResourceMessageFilter
* child_resource_message_filter() const {
166 return resource_message_filter_
.get();
169 base::MessageLoop
* message_loop() const { return message_loop_
; }
171 // Returns the one child thread. Can only be called on the main thread.
172 static ChildThreadImpl
* current();
174 #if defined(OS_ANDROID)
175 // Called on Android's service thread to shutdown the main thread of this
177 static void ShutdownThread();
180 ServiceRegistry
* service_registry() const {
181 return mojo_application_
->service_registry();
185 friend class ChildProcess
;
187 // Called when the process refcount is 0.
188 void OnProcessFinalRelease();
190 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
192 void set_on_channel_error_called(bool on_channel_error_called
) {
193 on_channel_error_called_
= on_channel_error_called
;
196 // IPC::Listener implementation:
197 bool OnMessageReceived(const IPC::Message
& msg
) override
;
198 void OnChannelConnected(int32 peer_pid
) override
;
199 void OnChannelError() override
;
202 class ChildThreadMessageRouter
: public MessageRouter
{
204 // |sender| must outlive this object.
205 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
206 bool Send(IPC::Message
* msg
) override
;
209 IPC::Sender
* const sender_
;
212 void Init(const Options
& options
);
214 // We create the channel first without connecting it so we can add filters
215 // prior to any messages being received, then connect it afterwards.
216 void ConnectChannel(bool use_mojo_channel
);
218 // IPC message handlers.
220 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
221 void OnGetChildProfilerData(int sequence_number
);
222 void OnDumpHandles();
223 void OnProcessBackgrounded(bool background
);
224 #ifdef IPC_MESSAGE_LOG_ENABLED
225 void OnSetIPCLoggingEnabled(bool enable
);
227 #if defined(USE_TCMALLOC)
228 void OnGetTcmallocStats();
231 void EnsureConnected();
233 class SingleProcessChannelDelegate
;
234 class SingleProcessChannelDelegateDeleter
{
236 void operator()(SingleProcessChannelDelegate
* delegate
) const;
239 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support_
;
240 scoped_ptr
<SingleProcessChannelDelegate
, SingleProcessChannelDelegateDeleter
>
241 single_process_channel_delegate_
;
242 scoped_ptr
<MojoApplication
> mojo_application_
;
244 std::string channel_name_
;
245 scoped_ptr
<IPC::SyncChannel
> channel_
;
247 // Allows threads other than the main thread to send sync messages.
248 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
250 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
252 // Implements message routing functionality to the consumers of
254 ChildThreadMessageRouter router_
;
256 // Handles resource loads for this process.
257 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
259 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
261 // The OnChannelError() callback was invoked - the channel is dead, don't
262 // attempt to communicate.
263 bool on_channel_error_called_
;
265 base::MessageLoop
* message_loop_
;
267 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
269 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
271 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
273 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
275 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
277 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
279 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
281 scoped_refptr
<PushDispatcher
> push_dispatcher_
;
283 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
285 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
287 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
288 discardable_shared_memory_manager_
;
290 // Observes the trace event system. When tracing is enabled, optionally
291 // starts profiling the tcmalloc heap.
292 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
294 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
296 scoped_refptr
<ChildMessageFilter
> geofencing_message_filter_
;
297 scoped_refptr
<ChildMessageFilter
> bluetooth_message_filter_
;
299 scoped_refptr
<NavigatorConnectDispatcher
> navigator_connect_dispatcher_
;
301 bool in_browser_process_
;
303 base::WeakPtrFactory
<ChildThreadImpl
> channel_connected_factory_
;
305 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl
);
308 struct ChildThreadImpl::Options
{
313 std::string channel_name
;
314 bool use_mojo_channel
;
315 bool in_browser_process
;
316 std::vector
<IPC::MessageFilter
*> startup_filters
;
322 class ChildThreadImpl::Options::Builder
{
326 Builder
& InBrowserProcess(bool in_browser_process
);
327 Builder
& UseMojoChannel(bool use_mojo_channel
);
328 Builder
& WithChannelName(const std::string
& channel_name
);
329 Builder
& AddStartupFilter(IPC::MessageFilter
* filter
);
334 struct Options options_
;
336 DISALLOW_COPY_AND_ASSIGN(Builder
);
339 } // namespace content
341 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_