Roll src/third_party/WebKit 6cdd902:94953d6 (svn 197985:197991)
[chromium-blink-merge.git] / content / child / child_thread_impl.h
blob3a1823711e62c6b45f9634addd082060090310e6
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_
8 #include <string>
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 namespace base {
24 class MessageLoop;
26 namespace trace_event {
27 class TraceMemoryController;
28 } // namespace trace_event
29 } // namespace base
31 namespace IPC {
32 class AttachmentBroker;
33 class MessageFilter;
34 class ScopedIPCSupport;
35 class SyncChannel;
36 class SyncMessageFilter;
37 } // namespace IPC
39 namespace blink {
40 class WebFrame;
41 } // namespace blink
43 namespace content {
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 NavigatorConnectDispatcher;
53 class NotificationDispatcher;
54 class PushDispatcher;
55 class ServiceWorkerMessageFilter;
56 class QuotaDispatcher;
57 class QuotaMessageFilter;
58 class ResourceDispatcher;
59 class ThreadSafeSender;
60 class WebSocketDispatcher;
61 struct RequestInfo;
63 // The main thread of a child process derives from this class.
64 class CONTENT_EXPORT ChildThreadImpl
65 : public IPC::Listener,
66 virtual public ChildThread {
67 public:
68 struct CONTENT_EXPORT Options;
70 // Creates the thread.
71 ChildThreadImpl();
72 // Allow to be used for single-process mode and for in process gpu mode via
73 // options.
74 explicit ChildThreadImpl(const Options& options);
75 // ChildProcess::main_thread() is reset after Shutdown(), and before the
76 // destructor, so any subsystem that relies on ChildProcess::main_thread()
77 // must be terminated before Shutdown returns. In particular, if a subsystem
78 // has a thread that post tasks to ChildProcess::main_thread(), that thread
79 // should be joined in Shutdown().
80 ~ChildThreadImpl() override;
81 virtual void Shutdown();
83 // IPC::Sender implementation:
84 bool Send(IPC::Message* msg) override;
86 // ChildThread implementation:
87 #if defined(OS_WIN)
88 void PreCacheFont(const LOGFONT& log_font) override;
89 void ReleaseCachedFonts() override;
90 #endif
91 IPC::AttachmentBroker* GetAttachmentBroker() override;
93 IPC::SyncChannel* channel() { return channel_.get(); }
95 MessageRouter* GetRouter();
97 // Allocates a block of shared memory of the given size. Returns NULL on
98 // failure.
99 // Note: On posix, this requires a sync IPC to the browser process,
100 // but on windows the child process directly allocates the block.
101 scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size);
103 // A static variant that can be called on background threads provided
104 // the |sender| passed in is safe to use on background threads.
105 static scoped_ptr<base::SharedMemory> AllocateSharedMemory(
106 size_t buf_size,
107 IPC::Sender* sender);
109 ChildSharedBitmapManager* shared_bitmap_manager() const {
110 return shared_bitmap_manager_.get();
113 ChildGpuMemoryBufferManager* gpu_memory_buffer_manager() const {
114 return gpu_memory_buffer_manager_.get();
117 ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager()
118 const {
119 return discardable_shared_memory_manager_.get();
122 ResourceDispatcher* resource_dispatcher() const {
123 return resource_dispatcher_.get();
126 WebSocketDispatcher* websocket_dispatcher() const {
127 return websocket_dispatcher_.get();
130 FileSystemDispatcher* file_system_dispatcher() const {
131 return file_system_dispatcher_.get();
134 QuotaDispatcher* quota_dispatcher() const {
135 return quota_dispatcher_.get();
138 NotificationDispatcher* notification_dispatcher() const {
139 return notification_dispatcher_.get();
142 PushDispatcher* push_dispatcher() const {
143 return push_dispatcher_.get();
146 IPC::SyncMessageFilter* sync_message_filter() const {
147 return sync_message_filter_.get();
150 // The getter should only be called on the main thread, however the
151 // IPC::Sender it returns may be safely called on any thread including
152 // the main thread.
153 ThreadSafeSender* thread_safe_sender() const {
154 return thread_safe_sender_.get();
157 ChildHistogramMessageFilter* child_histogram_message_filter() const {
158 return histogram_message_filter_.get();
161 ServiceWorkerMessageFilter* service_worker_message_filter() const {
162 return service_worker_message_filter_.get();
165 QuotaMessageFilter* quota_message_filter() const {
166 return quota_message_filter_.get();
169 ChildResourceMessageFilter* child_resource_message_filter() const {
170 return resource_message_filter_.get();
173 base::MessageLoop* message_loop() const { return message_loop_; }
175 // Returns the one child thread. Can only be called on the main thread.
176 static ChildThreadImpl* current();
178 #if defined(OS_ANDROID)
179 // Called on Android's service thread to shutdown the main thread of this
180 // process.
181 static void ShutdownThread();
182 #endif
184 ServiceRegistry* service_registry() const {
185 return mojo_application_->service_registry();
188 protected:
189 friend class ChildProcess;
191 // Called when the process refcount is 0.
192 void OnProcessFinalRelease();
194 virtual bool OnControlMessageReceived(const IPC::Message& msg);
196 void set_on_channel_error_called(bool on_channel_error_called) {
197 on_channel_error_called_ = on_channel_error_called;
200 // IPC::Listener implementation:
201 bool OnMessageReceived(const IPC::Message& msg) override;
202 void OnChannelConnected(int32 peer_pid) override;
203 void OnChannelError() override;
205 bool IsInBrowserProcess() const;
206 scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner();
208 private:
209 class ChildThreadMessageRouter : public MessageRouter {
210 public:
211 // |sender| must outlive this object.
212 explicit ChildThreadMessageRouter(IPC::Sender* sender);
213 bool Send(IPC::Message* msg) override;
215 private:
216 IPC::Sender* const sender_;
219 void Init(const Options& options);
221 // We create the channel first without connecting it so we can add filters
222 // prior to any messages being received, then connect it afterwards.
223 void ConnectChannel(bool use_mojo_channel);
225 // IPC message handlers.
226 void OnShutdown();
227 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
228 void OnGetChildProfilerData(int sequence_number, int current_profiling_phase);
229 void OnProfilingPhaseCompleted(int profiling_phase);
230 void OnProcessBackgrounded(bool background);
231 #ifdef IPC_MESSAGE_LOG_ENABLED
232 void OnSetIPCLoggingEnabled(bool enable);
233 #endif
234 #if defined(USE_TCMALLOC)
235 void OnGetTcmallocStats();
236 #endif
238 void EnsureConnected();
240 scoped_ptr<MojoApplication> mojo_application_;
242 std::string channel_name_;
243 scoped_ptr<IPC::SyncChannel> channel_;
244 scoped_ptr<IPC::AttachmentBroker> attachment_broker_;
246 // Allows threads other than the main thread to send sync messages.
247 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
249 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
251 // Implements message routing functionality to the consumers of
252 // ChildThreadImpl.
253 ChildThreadMessageRouter router_;
255 // Handles resource loads for this process.
256 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
258 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
260 // The OnChannelError() callback was invoked - the channel is dead, don't
261 // attempt to communicate.
262 bool on_channel_error_called_;
264 base::MessageLoop* message_loop_;
266 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
268 scoped_ptr<QuotaDispatcher> quota_dispatcher_;
270 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
272 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
274 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
276 scoped_refptr<QuotaMessageFilter> quota_message_filter_;
278 scoped_refptr<NotificationDispatcher> notification_dispatcher_;
280 scoped_refptr<PushDispatcher> push_dispatcher_;
282 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
284 scoped_ptr<ChildGpuMemoryBufferManager> gpu_memory_buffer_manager_;
286 scoped_ptr<ChildDiscardableSharedMemoryManager>
287 discardable_shared_memory_manager_;
289 // Observes the trace event system. When tracing is enabled, optionally
290 // starts profiling the tcmalloc heap.
291 scoped_ptr<base::trace_event::TraceMemoryController> trace_memory_controller_;
293 scoped_ptr<base::PowerMonitor> power_monitor_;
295 scoped_refptr<ChildMessageFilter> geofencing_message_filter_;
296 scoped_refptr<ChildMessageFilter> bluetooth_message_filter_;
298 scoped_refptr<NavigatorConnectDispatcher> navigator_connect_dispatcher_;
300 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner_;
302 base::WeakPtrFactory<ChildThreadImpl> channel_connected_factory_;
304 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl);
307 struct ChildThreadImpl::Options {
308 ~Options();
310 class Builder;
312 std::string channel_name;
313 bool use_mojo_channel;
314 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner;
315 std::vector<IPC::MessageFilter*> startup_filters;
317 private:
318 Options();
321 class ChildThreadImpl::Options::Builder {
322 public:
323 Builder();
325 Builder& InBrowserProcess(const InProcessChildThreadParams& params);
326 Builder& UseMojoChannel(bool use_mojo_channel);
327 Builder& WithChannelName(const std::string& channel_name);
328 Builder& AddStartupFilter(IPC::MessageFilter* filter);
330 Options Build();
332 private:
333 struct Options options_;
335 DISALLOW_COPY_AND_ASSIGN(Builder);
338 } // namespace content
340 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_