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
;
38 namespace webkit_glue
{
39 class ResourceLoaderBridge
;
40 } // namespace webkit_glue
43 class ChildHistogramMessageFilter
;
44 class ChildResourceMessageFilter
;
45 class ChildSharedBitmapManager
;
46 class FileSystemDispatcher
;
47 class ServiceWorkerDispatcher
;
48 class ServiceWorkerMessageFilter
;
49 class QuotaDispatcher
;
50 class QuotaMessageFilter
;
51 class ResourceDispatcher
;
52 class SocketStreamDispatcher
;
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 // Creates the thread.
62 // Used for single-process mode and for in process gpu mode.
63 explicit ChildThread(const std::string
& channel_name
);
64 // ChildProcess::main_thread() is reset after Shutdown(), and before the
65 // destructor, so any subsystem that relies on ChildProcess::main_thread()
66 // must be terminated before Shutdown returns. In particular, if a subsystem
67 // has a thread that post tasks to ChildProcess::main_thread(), that thread
68 // should be joined in Shutdown().
69 virtual ~ChildThread();
70 virtual void Shutdown();
72 // IPC::Sender implementation:
73 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
75 IPC::SyncChannel
* channel() { return channel_
.get(); }
77 MessageRouter
* GetRouter();
79 // Allocates a block of shared memory of the given size and
80 // maps in into the address space. Returns NULL of failure.
81 // Note: On posix, this requires a sync IPC to the browser process,
82 // but on windows the child process directly allocates the block.
83 base::SharedMemory
* AllocateSharedMemory(size_t buf_size
);
85 // A static variant that can be called on background threads provided
86 // the |sender| passed in is safe to use on background threads.
87 static base::SharedMemory
* AllocateSharedMemory(size_t buf_size
,
90 ChildSharedBitmapManager
* shared_bitmap_manager() const {
91 return shared_bitmap_manager_
.get();
94 ResourceDispatcher
* resource_dispatcher() const {
95 return resource_dispatcher_
.get();
98 SocketStreamDispatcher
* socket_stream_dispatcher() const {
99 return socket_stream_dispatcher_
.get();
102 WebSocketDispatcher
* websocket_dispatcher() const {
103 return websocket_dispatcher_
.get();
106 FileSystemDispatcher
* file_system_dispatcher() const {
107 return file_system_dispatcher_
.get();
110 ServiceWorkerDispatcher
* service_worker_dispatcher() const {
111 return service_worker_dispatcher_
.get();
114 QuotaDispatcher
* quota_dispatcher() const {
115 return quota_dispatcher_
.get();
118 IPC::SyncMessageFilter
* sync_message_filter() const {
119 return sync_message_filter_
.get();
122 // The getter should only be called on the main thread, however the
123 // IPC::Sender it returns may be safely called on any thread including
125 ThreadSafeSender
* thread_safe_sender() const {
126 return thread_safe_sender_
.get();
129 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
130 return histogram_message_filter_
.get();
133 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
134 return service_worker_message_filter_
.get();
137 QuotaMessageFilter
* quota_message_filter() const {
138 return quota_message_filter_
.get();
141 base::MessageLoop
* message_loop() const { return message_loop_
; }
143 // Returns the one child thread. Can only be called on the main thread.
144 static ChildThread
* current();
146 #if defined(OS_ANDROID)
147 // Called on Android's service thread to shutdown the main thread of this
149 static void ShutdownThread();
152 ServiceRegistry
* service_registry() const {
153 return mojo_application_
->service_registry();
157 friend class ChildProcess
;
159 // Called when the process refcount is 0.
160 void OnProcessFinalRelease();
162 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
164 void set_on_channel_error_called(bool on_channel_error_called
) {
165 on_channel_error_called_
= on_channel_error_called
;
168 // IPC::Listener implementation:
169 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
170 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
171 virtual void OnChannelError() OVERRIDE
;
174 class ChildThreadMessageRouter
: public MessageRouter
{
176 // |sender| must outlive this object.
177 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
178 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
181 IPC::Sender
* const sender_
;
186 // IPC message handlers.
188 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
189 void OnGetChildProfilerData(int sequence_number
);
190 void OnDumpHandles();
191 void OnProcessBackgrounded(bool background
);
192 #ifdef IPC_MESSAGE_LOG_ENABLED
193 void OnSetIPCLoggingEnabled(bool enable
);
195 #if defined(USE_TCMALLOC)
196 void OnGetTcmallocStats();
199 void EnsureConnected();
201 scoped_ptr
<MojoApplication
> mojo_application_
;
203 std::string channel_name_
;
204 scoped_ptr
<IPC::SyncChannel
> channel_
;
206 // Allows threads other than the main thread to send sync messages.
207 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
209 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
211 // Implements message routing functionality to the consumers of ChildThread.
212 ChildThreadMessageRouter router_
;
214 // Handles resource loads for this process.
215 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
217 // Handles SocketStream for this process.
218 scoped_ptr
<SocketStreamDispatcher
> socket_stream_dispatcher_
;
220 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
222 // The OnChannelError() callback was invoked - the channel is dead, don't
223 // attempt to communicate.
224 bool on_channel_error_called_
;
226 base::MessageLoop
* message_loop_
;
228 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
230 scoped_ptr
<ServiceWorkerDispatcher
> service_worker_dispatcher_
;
232 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
234 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
236 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
238 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
240 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
242 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
244 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
246 // Observes the trace event system. When tracing is enabled, optionally
247 // starts profiling the tcmalloc heap.
248 scoped_ptr
<base::debug::TraceMemoryController
> trace_memory_controller_
;
250 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
252 bool in_browser_process_
;
254 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
257 } // namespace content
259 #endif // CONTENT_CHILD_CHILD_THREAD_H_