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 ChildDiscardableSharedMemoryManager
;
40 class ChildGpuMemoryBufferManager
;
41 class ChildHistogramMessageFilter
;
42 class ChildResourceMessageFilter
;
43 class ChildSharedBitmapManager
;
44 class FileSystemDispatcher
;
45 class GeofencingMessageFilter
;
46 class NotificationDispatcher
;
47 class ServiceWorkerMessageFilter
;
48 class QuotaDispatcher
;
49 class QuotaMessageFilter
;
50 class ResourceDispatcher
;
51 class ThreadSafeSender
;
52 class WebSocketDispatcher
;
55 // The main thread of a child process derives from this class.
56 class CONTENT_EXPORT ChildThread
: public IPC::Listener
, public IPC::Sender
{
58 struct CONTENT_EXPORT Options
{
60 explicit Options(bool mojo
);
61 Options(std::string name
, bool mojo
)
62 : channel_name(name
), use_mojo_channel(mojo
) {}
64 std::string channel_name
;
65 bool use_mojo_channel
;
68 // Creates the thread.
70 // Used for single-process mode and for in process gpu mode.
71 explicit ChildThread(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 ~ChildThread() override
;
78 virtual void Shutdown();
80 // IPC::Sender implementation:
81 bool Send(IPC::Message
* msg
) override
;
83 IPC::SyncChannel
* channel() { return channel_
.get(); }
85 MessageRouter
* GetRouter();
87 // Allocates a block of shared memory of the given size and
88 // maps in into the address space. Returns NULL of failure.
89 // Note: On posix, this requires a sync IPC to the browser process,
90 // but on windows the child process directly allocates the block.
91 base::SharedMemory
* AllocateSharedMemory(size_t buf_size
);
93 // A static variant that can be called on background threads provided
94 // the |sender| passed in is safe to use on background threads.
95 static base::SharedMemory
* AllocateSharedMemory(size_t buf_size
,
98 ChildSharedBitmapManager
* shared_bitmap_manager() const {
99 return shared_bitmap_manager_
.get();
102 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
103 return gpu_memory_buffer_manager_
.get();
106 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
108 return discardable_shared_memory_manager_
.get();
111 ResourceDispatcher
* resource_dispatcher() const {
112 return resource_dispatcher_
.get();
115 WebSocketDispatcher
* websocket_dispatcher() const {
116 return websocket_dispatcher_
.get();
119 FileSystemDispatcher
* file_system_dispatcher() const {
120 return file_system_dispatcher_
.get();
123 QuotaDispatcher
* quota_dispatcher() const {
124 return quota_dispatcher_
.get();
127 NotificationDispatcher
* notification_dispatcher() const {
128 return notification_dispatcher_
.get();
131 IPC::SyncMessageFilter
* sync_message_filter() const {
132 return sync_message_filter_
.get();
135 // The getter should only be called on the main thread, however the
136 // IPC::Sender it returns may be safely called on any thread including
138 ThreadSafeSender
* thread_safe_sender() const {
139 return thread_safe_sender_
.get();
142 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
143 return histogram_message_filter_
.get();
146 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
147 return service_worker_message_filter_
.get();
150 QuotaMessageFilter
* quota_message_filter() const {
151 return quota_message_filter_
.get();
154 base::MessageLoop
* message_loop() const { return message_loop_
; }
156 // Returns the one child thread. Can only be called on the main thread.
157 static ChildThread
* current();
159 #if defined(OS_ANDROID)
160 // Called on Android's service thread to shutdown the main thread of this
162 static void ShutdownThread();
165 ServiceRegistry
* service_registry() const {
166 return mojo_application_
->service_registry();
170 friend class ChildProcess
;
172 // Called when the process refcount is 0.
173 void OnProcessFinalRelease();
175 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
177 void set_on_channel_error_called(bool on_channel_error_called
) {
178 on_channel_error_called_
= on_channel_error_called
;
181 // IPC::Listener implementation:
182 bool OnMessageReceived(const IPC::Message
& msg
) override
;
183 void OnChannelConnected(int32 peer_pid
) override
;
184 void OnChannelError() override
;
187 class ChildThreadMessageRouter
: public MessageRouter
{
189 // |sender| must outlive this object.
190 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
191 bool Send(IPC::Message
* msg
) override
;
194 IPC::Sender
* const sender_
;
197 void Init(const Options
& options
);
198 scoped_ptr
<IPC::SyncChannel
> CreateChannel(bool use_mojo_channel
);
200 // IPC message handlers.
202 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
203 void OnGetChildProfilerData(int sequence_number
);
204 void OnDumpHandles();
205 void OnProcessBackgrounded(bool background
);
206 #ifdef IPC_MESSAGE_LOG_ENABLED
207 void OnSetIPCLoggingEnabled(bool enable
);
209 #if defined(USE_TCMALLOC)
210 void OnGetTcmallocStats();
213 void EnsureConnected();
215 scoped_ptr
<MojoApplication
> mojo_application_
;
217 std::string channel_name_
;
218 scoped_ptr
<IPC::SyncChannel
> channel_
;
220 // Allows threads other than the main thread to send sync messages.
221 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
223 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
225 // Implements message routing functionality to the consumers of ChildThread.
226 ChildThreadMessageRouter router_
;
228 // Handles resource loads for this process.
229 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
231 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
233 // The OnChannelError() callback was invoked - the channel is dead, don't
234 // attempt to communicate.
235 bool on_channel_error_called_
;
237 base::MessageLoop
* message_loop_
;
239 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
241 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
243 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
245 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
247 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
249 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
251 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
253 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
255 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
257 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
258 discardable_shared_memory_manager_
;
260 // Observes the trace event system. When tracing is enabled, optionally
261 // starts profiling the tcmalloc heap.
262 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
264 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
266 scoped_refptr
<GeofencingMessageFilter
> geofencing_message_filter_
;
268 bool in_browser_process_
;
270 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
272 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
275 } // namespace content
277 #endif // CONTENT_CHILD_CHILD_THREAD_H_