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 ServiceWorkerMessageFilter
;
47 class QuotaDispatcher
;
48 class QuotaMessageFilter
;
49 class ResourceDispatcher
;
50 class ThreadSafeSender
;
51 class WebSocketDispatcher
;
54 // The main thread of a child process derives from this class.
55 class CONTENT_EXPORT ChildThread
: public IPC::Listener
, public IPC::Sender
{
57 struct CONTENT_EXPORT Options
{
59 explicit Options(bool mojo
);
60 Options(std::string name
, bool mojo
)
61 : channel_name(name
), use_mojo_channel(mojo
) {}
63 std::string channel_name
;
64 bool use_mojo_channel
;
67 // Creates the thread.
69 // Used for single-process mode and for in process gpu mode.
70 explicit ChildThread(const Options
& options
);
71 // ChildProcess::main_thread() is reset after Shutdown(), and before the
72 // destructor, so any subsystem that relies on ChildProcess::main_thread()
73 // must be terminated before Shutdown returns. In particular, if a subsystem
74 // has a thread that post tasks to ChildProcess::main_thread(), that thread
75 // should be joined in Shutdown().
76 ~ChildThread() override
;
77 virtual void Shutdown();
79 // IPC::Sender implementation:
80 bool Send(IPC::Message
* msg
) override
;
82 IPC::SyncChannel
* channel() { return channel_
.get(); }
84 MessageRouter
* GetRouter();
86 // Allocates a block of shared memory of the given size and
87 // maps in into the address space. Returns NULL of failure.
88 // Note: On posix, this requires a sync IPC to the browser process,
89 // but on windows the child process directly allocates the block.
90 base::SharedMemory
* AllocateSharedMemory(size_t buf_size
);
92 // A static variant that can be called on background threads provided
93 // the |sender| passed in is safe to use on background threads.
94 static base::SharedMemory
* AllocateSharedMemory(size_t buf_size
,
97 ChildSharedBitmapManager
* shared_bitmap_manager() const {
98 return shared_bitmap_manager_
.get();
101 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
102 return gpu_memory_buffer_manager_
.get();
105 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
107 return discardable_shared_memory_manager_
.get();
110 ResourceDispatcher
* resource_dispatcher() const {
111 return resource_dispatcher_
.get();
114 WebSocketDispatcher
* websocket_dispatcher() const {
115 return websocket_dispatcher_
.get();
118 FileSystemDispatcher
* file_system_dispatcher() const {
119 return file_system_dispatcher_
.get();
122 QuotaDispatcher
* quota_dispatcher() const {
123 return quota_dispatcher_
.get();
126 IPC::SyncMessageFilter
* sync_message_filter() const {
127 return sync_message_filter_
.get();
130 // The getter should only be called on the main thread, however the
131 // IPC::Sender it returns may be safely called on any thread including
133 ThreadSafeSender
* thread_safe_sender() const {
134 return thread_safe_sender_
.get();
137 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
138 return histogram_message_filter_
.get();
141 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
142 return service_worker_message_filter_
.get();
145 QuotaMessageFilter
* quota_message_filter() const {
146 return quota_message_filter_
.get();
149 base::MessageLoop
* message_loop() const { return message_loop_
; }
151 // Returns the one child thread. Can only be called on the main thread.
152 static ChildThread
* current();
154 #if defined(OS_ANDROID)
155 // Called on Android's service thread to shutdown the main thread of this
157 static void ShutdownThread();
160 ServiceRegistry
* service_registry() const {
161 return mojo_application_
->service_registry();
165 friend class ChildProcess
;
167 // Called when the process refcount is 0.
168 void OnProcessFinalRelease();
170 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
172 void set_on_channel_error_called(bool on_channel_error_called
) {
173 on_channel_error_called_
= on_channel_error_called
;
176 // IPC::Listener implementation:
177 bool OnMessageReceived(const IPC::Message
& msg
) override
;
178 void OnChannelConnected(int32 peer_pid
) override
;
179 void OnChannelError() override
;
182 class ChildThreadMessageRouter
: public MessageRouter
{
184 // |sender| must outlive this object.
185 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
186 bool Send(IPC::Message
* msg
) override
;
189 IPC::Sender
* const sender_
;
192 void Init(const Options
& options
);
193 scoped_ptr
<IPC::SyncChannel
> CreateChannel(bool use_mojo_channel
);
195 // IPC message handlers.
197 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
198 void OnGetChildProfilerData(int sequence_number
);
199 void OnDumpHandles();
200 void OnProcessBackgrounded(bool background
);
201 #ifdef IPC_MESSAGE_LOG_ENABLED
202 void OnSetIPCLoggingEnabled(bool enable
);
204 #if defined(USE_TCMALLOC)
205 void OnGetTcmallocStats();
208 void EnsureConnected();
210 scoped_ptr
<MojoApplication
> mojo_application_
;
212 std::string channel_name_
;
213 scoped_ptr
<IPC::SyncChannel
> channel_
;
215 // Allows threads other than the main thread to send sync messages.
216 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
218 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
220 // Implements message routing functionality to the consumers of ChildThread.
221 ChildThreadMessageRouter router_
;
223 // Handles resource loads for this process.
224 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
226 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
228 // The OnChannelError() callback was invoked - the channel is dead, don't
229 // attempt to communicate.
230 bool on_channel_error_called_
;
232 base::MessageLoop
* message_loop_
;
234 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
236 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
238 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
240 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
242 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
244 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
246 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
248 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
250 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
251 discardable_shared_memory_manager_
;
253 // Observes the trace event system. When tracing is enabled, optionally
254 // starts profiling the tcmalloc heap.
255 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
257 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
259 scoped_refptr
<GeofencingMessageFilter
> geofencing_message_filter_
;
261 bool in_browser_process_
;
263 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
265 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
268 } // namespace content
270 #endif // CONTENT_CHILD_CHILD_THREAD_H_