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.
23 #if defined(OS_MACOSX) && !defined(OS_IOS)
24 #include "content/common/mac/io_surface_manager_token.h"
30 namespace trace_event
{
31 class TraceMemoryController
;
32 } // namespace trace_event
37 class ScopedIPCSupport
;
39 class SyncMessageFilter
;
47 class ChildMessageFilter
;
48 class ChildDiscardableSharedMemoryManager
;
49 class ChildGpuMemoryBufferManager
;
50 class ChildHistogramMessageFilter
;
51 class ChildResourceMessageFilter
;
52 class ChildSharedBitmapManager
;
53 class FileSystemDispatcher
;
54 class InProcessChildThreadParams
;
55 class NavigatorConnectDispatcher
;
56 class NotificationDispatcher
;
58 class ServiceWorkerMessageFilter
;
59 class QuotaDispatcher
;
60 class QuotaMessageFilter
;
61 class ResourceDispatcher
;
62 class ThreadSafeSender
;
63 class WebSocketDispatcher
;
66 // The main thread of a child process derives from this class.
67 class CONTENT_EXPORT ChildThreadImpl
68 : public IPC::Listener
,
69 virtual public ChildThread
{
71 struct CONTENT_EXPORT Options
;
73 // Creates the thread.
75 // Allow to be used for single-process mode and for in process gpu mode via
77 explicit ChildThreadImpl(const Options
& options
);
78 // ChildProcess::main_thread() is reset after Shutdown(), and before the
79 // destructor, so any subsystem that relies on ChildProcess::main_thread()
80 // must be terminated before Shutdown returns. In particular, if a subsystem
81 // has a thread that post tasks to ChildProcess::main_thread(), that thread
82 // should be joined in Shutdown().
83 ~ChildThreadImpl() override
;
84 virtual void Shutdown();
86 // IPC::Sender implementation:
87 bool Send(IPC::Message
* msg
) override
;
89 // ChildThread implementation:
91 void PreCacheFont(const LOGFONT
& log_font
) override
;
92 void ReleaseCachedFonts() override
;
95 IPC::SyncChannel
* channel() { return channel_
.get(); }
97 MessageRouter
* GetRouter();
99 // Allocates a block of shared memory of the given size. Returns NULL on
101 // Note: On posix, this requires a sync IPC to the browser process,
102 // but on windows the child process directly allocates the block.
103 scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t buf_size
);
105 // A static variant that can be called on background threads provided
106 // the |sender| passed in is safe to use on background threads.
107 static scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
109 IPC::Sender
* sender
);
111 ChildSharedBitmapManager
* shared_bitmap_manager() const {
112 return shared_bitmap_manager_
.get();
115 ChildGpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
116 return gpu_memory_buffer_manager_
.get();
119 ChildDiscardableSharedMemoryManager
* discardable_shared_memory_manager()
121 return discardable_shared_memory_manager_
.get();
124 ResourceDispatcher
* resource_dispatcher() const {
125 return resource_dispatcher_
.get();
128 WebSocketDispatcher
* websocket_dispatcher() const {
129 return websocket_dispatcher_
.get();
132 FileSystemDispatcher
* file_system_dispatcher() const {
133 return file_system_dispatcher_
.get();
136 QuotaDispatcher
* quota_dispatcher() const {
137 return quota_dispatcher_
.get();
140 NotificationDispatcher
* notification_dispatcher() const {
141 return notification_dispatcher_
.get();
144 PushDispatcher
* push_dispatcher() const {
145 return push_dispatcher_
.get();
148 IPC::SyncMessageFilter
* sync_message_filter() const {
149 return sync_message_filter_
.get();
152 // The getter should only be called on the main thread, however the
153 // IPC::Sender it returns may be safely called on any thread including
155 ThreadSafeSender
* thread_safe_sender() const {
156 return thread_safe_sender_
.get();
159 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
160 return histogram_message_filter_
.get();
163 ServiceWorkerMessageFilter
* service_worker_message_filter() const {
164 return service_worker_message_filter_
.get();
167 QuotaMessageFilter
* quota_message_filter() const {
168 return quota_message_filter_
.get();
171 ChildResourceMessageFilter
* child_resource_message_filter() const {
172 return resource_message_filter_
.get();
175 base::MessageLoop
* message_loop() const { return message_loop_
; }
177 // Returns the one child thread. Can only be called on the main thread.
178 static ChildThreadImpl
* current();
180 #if defined(OS_ANDROID)
181 // Called on Android's service thread to shutdown the main thread of this
183 static void ShutdownThread();
186 ServiceRegistry
* service_registry() const {
187 return mojo_application_
->service_registry();
191 friend class ChildProcess
;
193 // Called when the process refcount is 0.
194 void OnProcessFinalRelease();
196 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
198 void set_on_channel_error_called(bool on_channel_error_called
) {
199 on_channel_error_called_
= on_channel_error_called
;
202 // IPC::Listener implementation:
203 bool OnMessageReceived(const IPC::Message
& msg
) override
;
204 void OnChannelConnected(int32 peer_pid
) override
;
205 void OnChannelError() override
;
207 bool IsInBrowserProcess() const;
208 scoped_refptr
<base::SequencedTaskRunner
> GetIOTaskRunner();
211 class ChildThreadMessageRouter
: public MessageRouter
{
213 // |sender| must outlive this object.
214 explicit ChildThreadMessageRouter(IPC::Sender
* sender
);
215 bool Send(IPC::Message
* msg
) override
;
218 IPC::Sender
* const sender_
;
221 void Init(const Options
& options
);
223 // We create the channel first without connecting it so we can add filters
224 // prior to any messages being received, then connect it afterwards.
225 void ConnectChannel(bool use_mojo_channel
);
227 // IPC message handlers.
229 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
230 void OnGetChildProfilerData(int sequence_number
, int current_profiling_phase
);
231 void OnProfilingPhaseCompleted(int profiling_phase
);
232 void OnProcessBackgrounded(bool background
);
233 #ifdef IPC_MESSAGE_LOG_ENABLED
234 void OnSetIPCLoggingEnabled(bool enable
);
236 #if defined(USE_TCMALLOC)
237 void OnGetTcmallocStats();
239 #if defined(OS_MACOSX) && !defined(OS_IOS)
240 void OnSetIOSurfaceManagerToken(const IOSurfaceManagerToken
& token
);
243 void EnsureConnected();
245 scoped_ptr
<MojoApplication
> mojo_application_
;
247 std::string channel_name_
;
248 scoped_ptr
<IPC::SyncChannel
> channel_
;
250 // Allows threads other than the main thread to send sync messages.
251 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
253 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
255 // Implements message routing functionality to the consumers of
257 ChildThreadMessageRouter router_
;
259 // Handles resource loads for this process.
260 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
262 scoped_ptr
<WebSocketDispatcher
> websocket_dispatcher_
;
264 // The OnChannelError() callback was invoked - the channel is dead, don't
265 // attempt to communicate.
266 bool on_channel_error_called_
;
268 base::MessageLoop
* message_loop_
;
270 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
272 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
274 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
276 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
278 scoped_refptr
<ServiceWorkerMessageFilter
> service_worker_message_filter_
;
280 scoped_refptr
<QuotaMessageFilter
> quota_message_filter_
;
282 scoped_refptr
<NotificationDispatcher
> notification_dispatcher_
;
284 scoped_refptr
<PushDispatcher
> push_dispatcher_
;
286 scoped_ptr
<ChildSharedBitmapManager
> shared_bitmap_manager_
;
288 scoped_ptr
<ChildGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
290 scoped_ptr
<ChildDiscardableSharedMemoryManager
>
291 discardable_shared_memory_manager_
;
293 // Observes the trace event system. When tracing is enabled, optionally
294 // starts profiling the tcmalloc heap.
295 scoped_ptr
<base::trace_event::TraceMemoryController
> trace_memory_controller_
;
297 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
299 scoped_refptr
<ChildMessageFilter
> geofencing_message_filter_
;
300 scoped_refptr
<ChildMessageFilter
> bluetooth_message_filter_
;
302 scoped_refptr
<NavigatorConnectDispatcher
> navigator_connect_dispatcher_
;
304 scoped_refptr
<base::SequencedTaskRunner
> browser_process_io_runner_
;
306 base::WeakPtrFactory
<ChildThreadImpl
> channel_connected_factory_
;
308 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl
);
311 struct ChildThreadImpl::Options
{
316 std::string channel_name
;
317 bool use_mojo_channel
;
318 scoped_refptr
<base::SequencedTaskRunner
> browser_process_io_runner
;
319 std::vector
<IPC::MessageFilter
*> startup_filters
;
325 class ChildThreadImpl::Options::Builder
{
329 Builder
& InBrowserProcess(const InProcessChildThreadParams
& params
);
330 Builder
& UseMojoChannel(bool use_mojo_channel
);
331 Builder
& WithChannelName(const std::string
& channel_name
);
332 Builder
& AddStartupFilter(IPC::MessageFilter
* filter
);
337 struct Options options_
;
339 DISALLOW_COPY_AND_ASSIGN(Builder
);
342 } // namespace content
344 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_