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/sequenced_task_runner.h"
16 #include "base/tracked_objects.h"
17 #include "content/child/mojo/mojo_application.h"
18 #include "content/common/content_export.h"
19 #include "content/common/message_router.h"
20 #include "content/public/child/child_thread.h"
21 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
26 namespace trace_event
{
27 class TraceMemoryController
;
28 } // namespace trace_event
32 class AttachmentBrokerUnprivileged
;
34 class ScopedIPCSupport
;
36 class SyncMessageFilter
;
44 class ChildMessageFilter
;
45 class ChildDiscardableSharedMemoryManager
;
46 class ChildGpuMemoryBufferManager
;
47 class ChildHistogramMessageFilter
;
48 class ChildResourceMessageFilter
;
49 class ChildSharedBitmapManager
;
50 class FileSystemDispatcher
;
51 class InProcessChildThreadParams
;
52 class NotificationDispatcher
;
54 class ServiceWorkerMessageFilter
;
55 class QuotaDispatcher
;
56 class QuotaMessageFilter
;
57 class ResourceDispatcher
;
58 class ThreadSafeSender
;
59 class WebSocketDispatcher
;
62 // The main thread of a child process derives from this class.
63 class CONTENT_EXPORT ChildThreadImpl
64 : public IPC::Listener
,
65 virtual public ChildThread
{
67 struct CONTENT_EXPORT Options
;
69 // Creates the thread.
71 // Allow to be used for single-process mode and for in process gpu mode via
73 explicit ChildThreadImpl(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 ~ChildThreadImpl() override
;
80 virtual void Shutdown();
82 // IPC::Sender implementation:
83 bool Send(IPC::Message
* msg
) override
;
85 // ChildThread implementation:
87 void PreCacheFont(const LOGFONT
& log_font
) override
;
88 void ReleaseCachedFonts() override
;
90 IPC::AttachmentBroker
* GetAttachmentBroker() override
;
92 IPC::SyncChannel
* channel() { return channel_
.get(); }
94 MessageRouter
* GetRouter();
96 // Allocates a block of shared memory of the given size. Returns NULL on
98 // Note: On posix, this requires a sync IPC to the browser process,
99 // but on windows the child process directly allocates the block.
100 scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t buf_size
);
102 // A static variant that can be called on background threads provided
103 // the |sender| passed in is safe to use on background threads.
104 static scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
106 IPC::Sender
* sender
);
108 ChildSharedBitmapManager
* shared_bitmap_manager() const {
109 return shared_bitmap_manager_
.get();
112 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
113 return gpu_memory_buffer_manager_
.get();
116 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
118 return discardable_shared_memory_manager_
.get();
121 ResourceDispatcher
* resource_dispatcher() const {
122 return resource_dispatcher_
.get();
125 WebSocketDispatcher
* websocket_dispatcher() const {
126 return websocket_dispatcher_
.get();
129 FileSystemDispatcher
* file_system_dispatcher() const {
130 return file_system_dispatcher_
.get();
133 QuotaDispatcher
* quota_dispatcher() const {
134 return quota_dispatcher_
.get();
137 NotificationDispatcher
* notification_dispatcher() const {
138 return notification_dispatcher_
.get();
141 PushDispatcher
* push_dispatcher() const {
142 return push_dispatcher_
.get();
145 IPC::SyncMessageFilter
* sync_message_filter() const {
146 return sync_message_filter_
.get();
149 // The getter should only be called on the main thread, however the
150 // IPC::Sender it returns may be safely called on any thread including
152 ThreadSafeSender
* thread_safe_sender() const {
153 return thread_safe_sender_
.get();
156 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
157 return histogram_message_filter_
.get();
160 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
161 return service_worker_message_filter_
.get();
164 QuotaMessageFilter
* quota_message_filter() const {
165 return quota_message_filter_
.get();
168 ChildResourceMessageFilter
* child_resource_message_filter() const {
169 return resource_message_filter_
.get();
172 base::MessageLoop
* message_loop() const { return message_loop_
; }
174 // Returns the one child thread. Can only be called on the main thread.
175 static ChildThreadImpl
* current();
177 #if defined(OS_ANDROID)
178 // Called on Android's service thread to shutdown the main thread of this
180 static void ShutdownThread();
183 ServiceRegistry
* service_registry() const {
184 return mojo_application_
->service_registry();
188 friend class ChildProcess
;
190 // Called when the process refcount is 0.
191 void OnProcessFinalRelease();
193 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
195 void set_on_channel_error_called(bool on_channel_error_called
) {
196 on_channel_error_called_
= on_channel_error_called
;
199 // IPC::Listener implementation:
200 bool OnMessageReceived(const IPC::Message
& msg
) override
;
201 void OnChannelConnected(int32 peer_pid
) override
;
202 void OnChannelError() override
;
204 bool IsInBrowserProcess() const;
205 scoped_refptr
<base::SequencedTaskRunner
> GetIOTaskRunner();
208 class ChildThreadMessageRouter
: public MessageRouter
{
210 // |sender| must outlive this object.
211 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
212 bool Send(IPC::Message
* msg
) override
;
215 IPC::Sender
* const sender_
;
218 void Init(const Options
& options
);
220 // We create the channel first without connecting it so we can add filters
221 // prior to any messages being received, then connect it afterwards.
222 void ConnectChannel(bool use_mojo_channel
);
224 // IPC message handlers.
226 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
227 void OnGetChildProfilerData(int sequence_number
, int current_profiling_phase
);
228 void OnProfilingPhaseCompleted(int profiling_phase
);
229 void OnProcessBackgrounded(bool background
);
230 #ifdef IPC_MESSAGE_LOG_ENABLED
231 void OnSetIPCLoggingEnabled(bool enable
);
233 #if defined(USE_TCMALLOC)
234 void OnGetTcmallocStats();
237 void EnsureConnected();
239 scoped_ptr
<MojoApplication
> mojo_application_
;
241 std::string channel_name_
;
242 scoped_ptr
<IPC::AttachmentBrokerUnprivileged
> attachment_broker_
;
243 scoped_ptr
<IPC::SyncChannel
> channel_
;
245 // Allows threads other than the main thread to send sync messages.
246 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
248 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
250 // Implements message routing functionality to the consumers of
252 ChildThreadMessageRouter router_
;
254 // Handles resource loads for this process.
255 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
257 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
259 // The OnChannelError() callback was invoked - the channel is dead, don't
260 // attempt to communicate.
261 bool on_channel_error_called_
;
263 base::MessageLoop
* message_loop_
;
265 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
267 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
269 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
271 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
273 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
275 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
277 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
279 scoped_refptr
<PushDispatcher
> push_dispatcher_
;
281 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
283 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
285 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
286 discardable_shared_memory_manager_
;
288 // Observes the trace event system. When tracing is enabled, optionally
289 // starts profiling the tcmalloc heap.
290 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
292 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
294 scoped_refptr
<ChildMessageFilter
> geofencing_message_filter_
;
296 scoped_refptr
<base::SequencedTaskRunner
> browser_process_io_runner_
;
298 base::WeakPtrFactory
<ChildThreadImpl
> channel_connected_factory_
;
300 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl
);
303 struct ChildThreadImpl::Options
{
308 std::string channel_name
;
309 bool use_mojo_channel
;
310 scoped_refptr
<base::SequencedTaskRunner
> browser_process_io_runner
;
311 std::vector
<IPC::MessageFilter
*> startup_filters
;
317 class ChildThreadImpl::Options::Builder
{
321 Builder
& InBrowserProcess(const InProcessChildThreadParams
& params
);
322 Builder
& UseMojoChannel(bool use_mojo_channel
);
323 Builder
& WithChannelName(const std::string
& channel_name
);
324 Builder
& AddStartupFilter(IPC::MessageFilter
* filter
);
329 struct Options options_
;
331 DISALLOW_COPY_AND_ASSIGN(Builder
);
334 } // namespace content
336 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_