Convert events_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / content / child / child_thread_impl.h
blobd124a50dd56627e1ba01042e771be2b9461fe177
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 #if defined(OS_MACOSX) && !defined(OS_IOS)
24 #include "content/common/mac/io_surface_manager_token.h"
25 #endif
27 namespace base {
28 class MessageLoop;
30 namespace trace_event {
31 class TraceMemoryController;
32 } // namespace trace_event
33 } // namespace base
35 namespace IPC {
36 class AttachmentBroker;
37 class MessageFilter;
38 class ScopedIPCSupport;
39 class SyncChannel;
40 class SyncMessageFilter;
41 } // namespace IPC
43 namespace blink {
44 class WebFrame;
45 } // namespace blink
47 namespace content {
48 class ChildMessageFilter;
49 class ChildDiscardableSharedMemoryManager;
50 class ChildGpuMemoryBufferManager;
51 class ChildHistogramMessageFilter;
52 class ChildResourceMessageFilter;
53 class ChildSharedBitmapManager;
54 class FileSystemDispatcher;
55 class InProcessChildThreadParams;
56 class NavigatorConnectDispatcher;
57 class NotificationDispatcher;
58 class PushDispatcher;
59 class ServiceWorkerMessageFilter;
60 class QuotaDispatcher;
61 class QuotaMessageFilter;
62 class ResourceDispatcher;
63 class ThreadSafeSender;
64 class WebSocketDispatcher;
65 struct RequestInfo;
67 // The main thread of a child process derives from this class.
68 class CONTENT_EXPORT ChildThreadImpl
69 : public IPC::Listener,
70 virtual public ChildThread {
71 public:
72 struct CONTENT_EXPORT Options;
74 // Creates the thread.
75 ChildThreadImpl();
76 // Allow to be used for single-process mode and for in process gpu mode via
77 // options.
78 explicit ChildThreadImpl(const Options& options);
79 // ChildProcess::main_thread() is reset after Shutdown(), and before the
80 // destructor, so any subsystem that relies on ChildProcess::main_thread()
81 // must be terminated before Shutdown returns. In particular, if a subsystem
82 // has a thread that post tasks to ChildProcess::main_thread(), that thread
83 // should be joined in Shutdown().
84 ~ChildThreadImpl() override;
85 virtual void Shutdown();
87 // IPC::Sender implementation:
88 bool Send(IPC::Message* msg) override;
90 // ChildThread implementation:
91 #if defined(OS_WIN)
92 void PreCacheFont(const LOGFONT& log_font) override;
93 void ReleaseCachedFonts() override;
94 #endif
95 IPC::AttachmentBroker* GetAttachmentBroker() override;
97 IPC::SyncChannel* channel() { return channel_.get(); }
99 MessageRouter* GetRouter();
101 // Allocates a block of shared memory of the given size. Returns NULL on
102 // failure.
103 // Note: On posix, this requires a sync IPC to the browser process,
104 // but on windows the child process directly allocates the block.
105 scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size);
107 // A static variant that can be called on background threads provided
108 // the |sender| passed in is safe to use on background threads.
109 static scoped_ptr<base::SharedMemory> AllocateSharedMemory(
110 size_t buf_size,
111 IPC::Sender* sender);
113 ChildSharedBitmapManager* shared_bitmap_manager() const {
114 return shared_bitmap_manager_.get();
117 ChildGpuMemoryBufferManager* gpu_memory_buffer_manager() const {
118 return gpu_memory_buffer_manager_.get();
121 ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager()
122 const {
123 return discardable_shared_memory_manager_.get();
126 ResourceDispatcher* resource_dispatcher() const {
127 return resource_dispatcher_.get();
130 WebSocketDispatcher* websocket_dispatcher() const {
131 return websocket_dispatcher_.get();
134 FileSystemDispatcher* file_system_dispatcher() const {
135 return file_system_dispatcher_.get();
138 QuotaDispatcher* quota_dispatcher() const {
139 return quota_dispatcher_.get();
142 NotificationDispatcher* notification_dispatcher() const {
143 return notification_dispatcher_.get();
146 PushDispatcher* push_dispatcher() const {
147 return push_dispatcher_.get();
150 IPC::SyncMessageFilter* sync_message_filter() const {
151 return sync_message_filter_.get();
154 // The getter should only be called on the main thread, however the
155 // IPC::Sender it returns may be safely called on any thread including
156 // the main thread.
157 ThreadSafeSender* thread_safe_sender() const {
158 return thread_safe_sender_.get();
161 ChildHistogramMessageFilter* child_histogram_message_filter() const {
162 return histogram_message_filter_.get();
165 ServiceWorkerMessageFilter* service_worker_message_filter() const {
166 return service_worker_message_filter_.get();
169 QuotaMessageFilter* quota_message_filter() const {
170 return quota_message_filter_.get();
173 ChildResourceMessageFilter* child_resource_message_filter() const {
174 return resource_message_filter_.get();
177 base::MessageLoop* message_loop() const { return message_loop_; }
179 // Returns the one child thread. Can only be called on the main thread.
180 static ChildThreadImpl* current();
182 #if defined(OS_ANDROID)
183 // Called on Android's service thread to shutdown the main thread of this
184 // process.
185 static void ShutdownThread();
186 #endif
188 ServiceRegistry* service_registry() const {
189 return mojo_application_->service_registry();
192 protected:
193 friend class ChildProcess;
195 // Called when the process refcount is 0.
196 void OnProcessFinalRelease();
198 virtual bool OnControlMessageReceived(const IPC::Message& msg);
200 void set_on_channel_error_called(bool on_channel_error_called) {
201 on_channel_error_called_ = on_channel_error_called;
204 // IPC::Listener implementation:
205 bool OnMessageReceived(const IPC::Message& msg) override;
206 void OnChannelConnected(int32 peer_pid) override;
207 void OnChannelError() override;
209 bool IsInBrowserProcess() const;
210 scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner();
212 private:
213 class ChildThreadMessageRouter : public MessageRouter {
214 public:
215 // |sender| must outlive this object.
216 explicit ChildThreadMessageRouter(IPC::Sender* sender);
217 bool Send(IPC::Message* msg) override;
219 private:
220 IPC::Sender* const sender_;
223 void Init(const Options& options);
225 // We create the channel first without connecting it so we can add filters
226 // prior to any messages being received, then connect it afterwards.
227 void ConnectChannel(bool use_mojo_channel);
229 // IPC message handlers.
230 void OnShutdown();
231 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
232 void OnGetChildProfilerData(int sequence_number, int current_profiling_phase);
233 void OnProfilingPhaseCompleted(int profiling_phase);
234 void OnProcessBackgrounded(bool background);
235 #ifdef IPC_MESSAGE_LOG_ENABLED
236 void OnSetIPCLoggingEnabled(bool enable);
237 #endif
238 #if defined(USE_TCMALLOC)
239 void OnGetTcmallocStats();
240 #endif
241 #if defined(OS_MACOSX) && !defined(OS_IOS)
242 void OnSetIOSurfaceManagerToken(const IOSurfaceManagerToken& token);
243 #endif
245 void EnsureConnected();
247 scoped_ptr<MojoApplication> mojo_application_;
249 std::string channel_name_;
250 scoped_ptr<IPC::SyncChannel> channel_;
251 scoped_ptr<IPC::AttachmentBroker> attachment_broker_;
253 // Allows threads other than the main thread to send sync messages.
254 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
256 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
258 // Implements message routing functionality to the consumers of
259 // ChildThreadImpl.
260 ChildThreadMessageRouter router_;
262 // Handles resource loads for this process.
263 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
265 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
267 // The OnChannelError() callback was invoked - the channel is dead, don't
268 // attempt to communicate.
269 bool on_channel_error_called_;
271 base::MessageLoop* message_loop_;
273 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
275 scoped_ptr<QuotaDispatcher> quota_dispatcher_;
277 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
279 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
281 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
283 scoped_refptr<QuotaMessageFilter> quota_message_filter_;
285 scoped_refptr<NotificationDispatcher> notification_dispatcher_;
287 scoped_refptr<PushDispatcher> push_dispatcher_;
289 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
291 scoped_ptr<ChildGpuMemoryBufferManager> gpu_memory_buffer_manager_;
293 scoped_ptr<ChildDiscardableSharedMemoryManager>
294 discardable_shared_memory_manager_;
296 // Observes the trace event system. When tracing is enabled, optionally
297 // starts profiling the tcmalloc heap.
298 scoped_ptr<base::trace_event::TraceMemoryController> trace_memory_controller_;
300 scoped_ptr<base::PowerMonitor> power_monitor_;
302 scoped_refptr<ChildMessageFilter> geofencing_message_filter_;
303 scoped_refptr<ChildMessageFilter> bluetooth_message_filter_;
305 scoped_refptr<NavigatorConnectDispatcher> navigator_connect_dispatcher_;
307 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner_;
309 base::WeakPtrFactory<ChildThreadImpl> channel_connected_factory_;
311 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl);
314 struct ChildThreadImpl::Options {
315 ~Options();
317 class Builder;
319 std::string channel_name;
320 bool use_mojo_channel;
321 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner;
322 std::vector<IPC::MessageFilter*> startup_filters;
324 private:
325 Options();
328 class ChildThreadImpl::Options::Builder {
329 public:
330 Builder();
332 Builder& InBrowserProcess(const InProcessChildThreadParams& params);
333 Builder& UseMojoChannel(bool use_mojo_channel);
334 Builder& WithChannelName(const std::string& channel_name);
335 Builder& AddStartupFilter(IPC::MessageFilter* filter);
337 Options Build();
339 private:
340 struct Options options_;
342 DISALLOW_COPY_AND_ASSIGN(Builder);
345 } // namespace content
347 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_