base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / child / child_thread.h
blob8dc2630e28b8c9cb2b6986c5293f5598bde2efc8
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_
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/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.
21 namespace base {
22 class MessageLoop;
24 namespace debug {
25 class TraceMemoryController;
26 } // namespace debug
27 } // namespace base
29 namespace IPC {
30 class SyncChannel;
31 class SyncMessageFilter;
32 } // namespace IPC
34 namespace blink {
35 class WebFrame;
36 } // namespace blink
38 namespace content {
39 class BluetoothMessageFilter;
40 class ChildDiscardableSharedMemoryManager;
41 class ChildGpuMemoryBufferManager;
42 class ChildHistogramMessageFilter;
43 class ChildResourceMessageFilter;
44 class ChildSharedBitmapManager;
45 class FileSystemDispatcher;
46 class GeofencingMessageFilter;
47 class NavigatorConnectDispatcher;
48 class NotificationDispatcher;
49 class PushDispatcher;
50 class ServiceWorkerMessageFilter;
51 class QuotaDispatcher;
52 class QuotaMessageFilter;
53 class ResourceDispatcher;
54 class ThreadSafeSender;
55 class WebSocketDispatcher;
56 struct RequestInfo;
58 // The main thread of a child process derives from this class.
59 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender {
60 public:
61 struct CONTENT_EXPORT Options {
62 Options();
63 explicit Options(bool mojo);
64 Options(std::string name, bool mojo)
65 : channel_name(name), use_mojo_channel(mojo) {}
67 std::string channel_name;
68 bool use_mojo_channel;
71 // Creates the thread.
72 ChildThread();
73 // Used for single-process mode and for in process gpu mode.
74 explicit ChildThread(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 ~ChildThread() override;
81 virtual void Shutdown();
83 // IPC::Sender implementation:
84 bool Send(IPC::Message* msg) override;
86 IPC::SyncChannel* channel() { return channel_.get(); }
88 MessageRouter* GetRouter();
90 // Allocates a block of shared memory of the given size. Returns NULL on
91 // failure.
92 // Note: On posix, this requires a sync IPC to the browser process,
93 // but on windows the child process directly allocates the block.
94 scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size);
96 // A static variant that can be called on background threads provided
97 // the |sender| passed in is safe to use on background threads.
98 static scoped_ptr<base::SharedMemory> AllocateSharedMemory(
99 size_t buf_size,
100 IPC::Sender* sender);
102 ChildSharedBitmapManager* shared_bitmap_manager() const {
103 return shared_bitmap_manager_.get();
106 ChildGpuMemoryBufferManager* gpu_memory_buffer_manager() const {
107 return gpu_memory_buffer_manager_.get();
110 ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager()
111 const {
112 return discardable_shared_memory_manager_.get();
115 ResourceDispatcher* resource_dispatcher() const {
116 return resource_dispatcher_.get();
119 WebSocketDispatcher* websocket_dispatcher() const {
120 return websocket_dispatcher_.get();
123 FileSystemDispatcher* file_system_dispatcher() const {
124 return file_system_dispatcher_.get();
127 QuotaDispatcher* quota_dispatcher() const {
128 return quota_dispatcher_.get();
131 NotificationDispatcher* notification_dispatcher() const {
132 return notification_dispatcher_.get();
135 PushDispatcher* push_dispatcher() const {
136 return push_dispatcher_.get();
139 IPC::SyncMessageFilter* sync_message_filter() const {
140 return sync_message_filter_.get();
143 // The getter should only be called on the main thread, however the
144 // IPC::Sender it returns may be safely called on any thread including
145 // the main thread.
146 ThreadSafeSender* thread_safe_sender() const {
147 return thread_safe_sender_.get();
150 ChildHistogramMessageFilter* child_histogram_message_filter() const {
151 return histogram_message_filter_.get();
154 ServiceWorkerMessageFilter* service_worker_message_filter() const {
155 return service_worker_message_filter_.get();
158 QuotaMessageFilter* quota_message_filter() const {
159 return quota_message_filter_.get();
162 ChildResourceMessageFilter* child_resource_message_filter() const {
163 return resource_message_filter_.get();
166 base::MessageLoop* message_loop() const { return message_loop_; }
168 // Returns the one child thread. Can only be called on the main thread.
169 static ChildThread* current();
171 #if defined(OS_ANDROID)
172 // Called on Android's service thread to shutdown the main thread of this
173 // process.
174 static void ShutdownThread();
175 #endif
177 ServiceRegistry* service_registry() const {
178 return mojo_application_->service_registry();
181 protected:
182 friend class ChildProcess;
184 // Called when the process refcount is 0.
185 void OnProcessFinalRelease();
187 virtual bool OnControlMessageReceived(const IPC::Message& msg);
189 void set_on_channel_error_called(bool on_channel_error_called) {
190 on_channel_error_called_ = on_channel_error_called;
193 // IPC::Listener implementation:
194 bool OnMessageReceived(const IPC::Message& msg) override;
195 void OnChannelConnected(int32 peer_pid) override;
196 void OnChannelError() override;
198 private:
199 class ChildThreadMessageRouter : public MessageRouter {
200 public:
201 // |sender| must outlive this object.
202 explicit ChildThreadMessageRouter(IPC::Sender* sender);
203 bool Send(IPC::Message* msg) override;
205 private:
206 IPC::Sender* const sender_;
209 void Init(const Options& options);
210 scoped_ptr<IPC::SyncChannel> CreateChannel(bool use_mojo_channel);
212 // IPC message handlers.
213 void OnShutdown();
214 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
215 void OnGetChildProfilerData(int sequence_number);
216 void OnDumpHandles();
217 void OnProcessBackgrounded(bool background);
218 #ifdef IPC_MESSAGE_LOG_ENABLED
219 void OnSetIPCLoggingEnabled(bool enable);
220 #endif
221 #if defined(USE_TCMALLOC)
222 void OnGetTcmallocStats();
223 #endif
225 void EnsureConnected();
227 scoped_ptr<MojoApplication> mojo_application_;
229 std::string channel_name_;
230 scoped_ptr<IPC::SyncChannel> channel_;
232 // Allows threads other than the main thread to send sync messages.
233 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
235 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
237 // Implements message routing functionality to the consumers of ChildThread.
238 ChildThreadMessageRouter router_;
240 // Handles resource loads for this process.
241 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
243 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
245 // The OnChannelError() callback was invoked - the channel is dead, don't
246 // attempt to communicate.
247 bool on_channel_error_called_;
249 base::MessageLoop* message_loop_;
251 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
253 scoped_ptr<QuotaDispatcher> quota_dispatcher_;
255 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
257 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
259 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
261 scoped_refptr<QuotaMessageFilter> quota_message_filter_;
263 scoped_refptr<NotificationDispatcher> notification_dispatcher_;
265 scoped_refptr<PushDispatcher> push_dispatcher_;
267 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
269 scoped_ptr<ChildGpuMemoryBufferManager> gpu_memory_buffer_manager_;
271 scoped_ptr<ChildDiscardableSharedMemoryManager>
272 discardable_shared_memory_manager_;
274 // Observes the trace event system. When tracing is enabled, optionally
275 // starts profiling the tcmalloc heap.
276 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
278 scoped_ptr<base::PowerMonitor> power_monitor_;
280 scoped_refptr<GeofencingMessageFilter> geofencing_message_filter_;
282 scoped_refptr<BluetoothMessageFilter> bluetooth_message_filter_;
284 scoped_refptr<NavigatorConnectDispatcher> navigator_connect_dispatcher_;
286 bool in_browser_process_;
288 base::WeakPtrFactory<ChildThread> channel_connected_factory_;
290 DISALLOW_COPY_AND_ASSIGN(ChildThread);
293 } // namespace content
295 #endif // CONTENT_CHILD_CHILD_THREAD_H_