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_H_
6 #define CONTENT_CHILD_CHILD_THREAD_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 "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
25 class TraceMemoryController
;
31 class SyncMessageFilter
;
39 class BluetoothMessageFilter
;
40 class ChildDiscardableSharedMemoryManager
;
41 class ChildGpuMemoryBufferManager
;
42 class ChildHistogramMessageFilter
;
43 class ChildResourceMessageFilter
;
44 class ChildSharedBitmapManager
;
45 class FileSystemDispatcher
;
46 class GeofencingMessageFilter
;
47 class NotificationDispatcher
;
49 class ServiceWorkerMessageFilter
;
50 class QuotaDispatcher
;
51 class QuotaMessageFilter
;
52 class ResourceDispatcher
;
53 class ThreadSafeSender
;
54 class WebSocketDispatcher
;
57 // The main thread of a child process derives from this class.
58 class CONTENT_EXPORT ChildThread
: public IPC::Listener
, public IPC::Sender
{
60 struct CONTENT_EXPORT Options
{
62 explicit Options(bool mojo
);
63 Options(std::string name
, bool mojo
)
64 : channel_name(name
), use_mojo_channel(mojo
) {}
66 std::string channel_name
;
67 bool use_mojo_channel
;
70 // Creates the thread.
72 // Used for single-process mode and for in process gpu mode.
73 explicit ChildThread(const Options
& options
);
74 // ChildProcess::main_thread() is reset after Shutdown(), and before the
75 // destructor, so any subsystem that relies on ChildProcess::main_thread()
76 // must be terminated before Shutdown returns. In particular, if a subsystem
77 // has a thread that post tasks to ChildProcess::main_thread(), that thread
78 // should be joined in Shutdown().
79 ~ChildThread() override
;
80 virtual void Shutdown();
82 // IPC::Sender implementation:
83 bool Send(IPC::Message
* msg
) override
;
85 IPC::SyncChannel
* channel() { return channel_
.get(); }
87 MessageRouter
* GetRouter();
89 // Allocates a block of shared memory of the given size. Returns NULL on
91 // Note: On posix, this requires a sync IPC to the browser process,
92 // but on windows the child process directly allocates the block.
93 scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t buf_size
);
95 // A static variant that can be called on background threads provided
96 // the |sender| passed in is safe to use on background threads.
97 static scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
101 ChildSharedBitmapManager
* shared_bitmap_manager() const {
102 return shared_bitmap_manager_
.get();
105 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
106 return gpu_memory_buffer_manager_
.get();
109 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
111 return discardable_shared_memory_manager_
.get();
114 ResourceDispatcher
* resource_dispatcher() const {
115 return resource_dispatcher_
.get();
118 WebSocketDispatcher
* websocket_dispatcher() const {
119 return websocket_dispatcher_
.get();
122 FileSystemDispatcher
* file_system_dispatcher() const {
123 return file_system_dispatcher_
.get();
126 QuotaDispatcher
* quota_dispatcher() const {
127 return quota_dispatcher_
.get();
130 NotificationDispatcher
* notification_dispatcher() const {
131 return notification_dispatcher_
.get();
134 PushDispatcher
* push_dispatcher() const {
135 return push_dispatcher_
.get();
138 IPC::SyncMessageFilter
* sync_message_filter() const {
139 return sync_message_filter_
.get();
142 // The getter should only be called on the main thread, however the
143 // IPC::Sender it returns may be safely called on any thread including
145 ThreadSafeSender
* thread_safe_sender() const {
146 return thread_safe_sender_
.get();
149 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
150 return histogram_message_filter_
.get();
153 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
154 return service_worker_message_filter_
.get();
157 QuotaMessageFilter
* quota_message_filter() const {
158 return quota_message_filter_
.get();
161 base::MessageLoop
* message_loop() const { return message_loop_
; }
163 // Returns the one child thread. Can only be called on the main thread.
164 static ChildThread
* current();
166 #if defined(OS_ANDROID)
167 // Called on Android's service thread to shutdown the main thread of this
169 static void ShutdownThread();
172 ServiceRegistry
* service_registry() const {
173 return mojo_application_
->service_registry();
177 friend class ChildProcess
;
179 // Called when the process refcount is 0.
180 void OnProcessFinalRelease();
182 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
184 void set_on_channel_error_called(bool on_channel_error_called
) {
185 on_channel_error_called_
= on_channel_error_called
;
188 // IPC::Listener implementation:
189 bool OnMessageReceived(const IPC::Message
& msg
) override
;
190 void OnChannelConnected(int32 peer_pid
) override
;
191 void OnChannelError() override
;
194 class ChildThreadMessageRouter
: public MessageRouter
{
196 // |sender| must outlive this object.
197 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
198 bool Send(IPC::Message
* msg
) override
;
201 IPC::Sender
* const sender_
;
204 void Init(const Options
& options
);
205 scoped_ptr
<IPC::SyncChannel
> CreateChannel(bool use_mojo_channel
);
207 // IPC message handlers.
209 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
210 void OnGetChildProfilerData(int sequence_number
);
211 void OnDumpHandles();
212 void OnProcessBackgrounded(bool background
);
213 #ifdef IPC_MESSAGE_LOG_ENABLED
214 void OnSetIPCLoggingEnabled(bool enable
);
216 #if defined(USE_TCMALLOC)
217 void OnGetTcmallocStats();
220 void EnsureConnected();
222 scoped_ptr
<MojoApplication
> mojo_application_
;
224 std::string channel_name_
;
225 scoped_ptr
<IPC::SyncChannel
> channel_
;
227 // Allows threads other than the main thread to send sync messages.
228 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
230 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
232 // Implements message routing functionality to the consumers of ChildThread.
233 ChildThreadMessageRouter router_
;
235 // Handles resource loads for this process.
236 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
238 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
240 // The OnChannelError() callback was invoked - the channel is dead, don't
241 // attempt to communicate.
242 bool on_channel_error_called_
;
244 base::MessageLoop
* message_loop_
;
246 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
248 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
250 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
252 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
254 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
256 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
258 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
260 scoped_refptr
<PushDispatcher
> push_dispatcher_
;
262 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
264 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
266 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
267 discardable_shared_memory_manager_
;
269 // Observes the trace event system. When tracing is enabled, optionally
270 // starts profiling the tcmalloc heap.
271 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
273 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
275 scoped_refptr
<GeofencingMessageFilter
> geofencing_message_filter_
;
277 scoped_refptr
<BluetoothMessageFilter
> bluetooth_message_filter_
;
279 bool in_browser_process_
;
281 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
283 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
286 } // namespace content
288 #endif // CONTENT_CHILD_CHILD_THREAD_H_