GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / child / child_thread_impl.h
blob7b04d429d149f1a027df4d79133888cf1916a8fa
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 MessageFilter;
33 class ScopedIPCSupport;
34 class SyncChannel;
35 class SyncMessageFilter;
36 } // namespace IPC
38 namespace blink {
39 class WebFrame;
40 } // namespace blink
42 namespace content {
43 class ChildMessageFilter;
44 class ChildDiscardableSharedMemoryManager;
45 class ChildGpuMemoryBufferManager;
46 class ChildHistogramMessageFilter;
47 class ChildResourceMessageFilter;
48 class ChildSharedBitmapManager;
49 class FileSystemDispatcher;
50 class InProcessChildThreadParams;
51 class NavigatorConnectDispatcher;
52 class NotificationDispatcher;
53 class PushDispatcher;
54 class ServiceWorkerMessageFilter;
55 class QuotaDispatcher;
56 class QuotaMessageFilter;
57 class ResourceDispatcher;
58 class ThreadSafeSender;
59 class WebSocketDispatcher;
60 struct RequestInfo;
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 {
66 public:
67 struct CONTENT_EXPORT Options;
69 // Creates the thread.
70 ChildThreadImpl();
71 // Allow to be used for single-process mode and for in process gpu mode via
72 // options.
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:
86 #if defined(OS_WIN)
87 void PreCacheFont(const LOGFONT& log_font) override;
88 void ReleaseCachedFonts() override;
89 #endif
91 IPC::SyncChannel* channel() { return channel_.get(); }
93 MessageRouter* GetRouter();
95 // Allocates a block of shared memory of the given size. Returns NULL on
96 // failure.
97 // Note: On posix, this requires a sync IPC to the browser process,
98 // but on windows the child process directly allocates the block.
99 scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size);
101 // A static variant that can be called on background threads provided
102 // the |sender| passed in is safe to use on background threads.
103 static scoped_ptr<base::SharedMemory> AllocateSharedMemory(
104 size_t buf_size,
105 IPC::Sender* sender);
107 ChildSharedBitmapManager* shared_bitmap_manager() const {
108 return shared_bitmap_manager_.get();
111 ChildGpuMemoryBufferManager* gpu_memory_buffer_manager() const {
112 return gpu_memory_buffer_manager_.get();
115 ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager()
116 const {
117 return discardable_shared_memory_manager_.get();
120 ResourceDispatcher* resource_dispatcher() const {
121 return resource_dispatcher_.get();
124 WebSocketDispatcher* websocket_dispatcher() const {
125 return websocket_dispatcher_.get();
128 FileSystemDispatcher* file_system_dispatcher() const {
129 return file_system_dispatcher_.get();
132 QuotaDispatcher* quota_dispatcher() const {
133 return quota_dispatcher_.get();
136 NotificationDispatcher* notification_dispatcher() const {
137 return notification_dispatcher_.get();
140 PushDispatcher* push_dispatcher() const {
141 return push_dispatcher_.get();
144 IPC::SyncMessageFilter* sync_message_filter() const {
145 return sync_message_filter_.get();
148 // The getter should only be called on the main thread, however the
149 // IPC::Sender it returns may be safely called on any thread including
150 // the main thread.
151 ThreadSafeSender* thread_safe_sender() const {
152 return thread_safe_sender_.get();
155 ChildHistogramMessageFilter* child_histogram_message_filter() const {
156 return histogram_message_filter_.get();
159 ServiceWorkerMessageFilter* service_worker_message_filter() const {
160 return service_worker_message_filter_.get();
163 QuotaMessageFilter* quota_message_filter() const {
164 return quota_message_filter_.get();
167 ChildResourceMessageFilter* child_resource_message_filter() const {
168 return resource_message_filter_.get();
171 base::MessageLoop* message_loop() const { return message_loop_; }
173 // Returns the one child thread. Can only be called on the main thread.
174 static ChildThreadImpl* current();
176 #if defined(OS_ANDROID)
177 // Called on Android's service thread to shutdown the main thread of this
178 // process.
179 static void ShutdownThread();
180 #endif
182 ServiceRegistry* service_registry() const {
183 return mojo_application_->service_registry();
186 protected:
187 friend class ChildProcess;
189 // Called when the process refcount is 0.
190 void OnProcessFinalRelease();
192 virtual bool OnControlMessageReceived(const IPC::Message& msg);
194 void set_on_channel_error_called(bool on_channel_error_called) {
195 on_channel_error_called_ = on_channel_error_called;
198 // IPC::Listener implementation:
199 bool OnMessageReceived(const IPC::Message& msg) override;
200 void OnChannelConnected(int32 peer_pid) override;
201 void OnChannelError() override;
203 bool IsInBrowserProcess() const;
204 scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner();
206 private:
207 class ChildThreadMessageRouter : public MessageRouter {
208 public:
209 // |sender| must outlive this object.
210 explicit ChildThreadMessageRouter(IPC::Sender* sender);
211 bool Send(IPC::Message* msg) override;
213 private:
214 IPC::Sender* const sender_;
217 void Init(const Options& options);
219 // We create the channel first without connecting it so we can add filters
220 // prior to any messages being received, then connect it afterwards.
221 void ConnectChannel(bool use_mojo_channel);
223 // IPC message handlers.
224 void OnShutdown();
225 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
226 void OnGetChildProfilerData(int sequence_number);
227 void OnDumpHandles();
228 void OnProcessBackgrounded(bool background);
229 #ifdef IPC_MESSAGE_LOG_ENABLED
230 void OnSetIPCLoggingEnabled(bool enable);
231 #endif
232 #if defined(USE_TCMALLOC)
233 void OnGetTcmallocStats();
234 #endif
236 void EnsureConnected();
238 class SingleProcessChannelDelegate;
239 class SingleProcessChannelDelegateDeleter {
240 public:
241 void operator()(SingleProcessChannelDelegate* delegate) const;
244 scoped_ptr<IPC::ScopedIPCSupport> ipc_support_;
245 scoped_ptr<SingleProcessChannelDelegate, SingleProcessChannelDelegateDeleter>
246 single_process_channel_delegate_;
247 scoped_ptr<MojoApplication> mojo_application_;
249 std::string channel_name_;
250 scoped_ptr<IPC::SyncChannel> channel_;
252 // Allows threads other than the main thread to send sync messages.
253 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
255 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
257 // Implements message routing functionality to the consumers of
258 // ChildThreadImpl.
259 ChildThreadMessageRouter router_;
261 // Handles resource loads for this process.
262 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
264 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
266 // The OnChannelError() callback was invoked - the channel is dead, don't
267 // attempt to communicate.
268 bool on_channel_error_called_;
270 base::MessageLoop* message_loop_;
272 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
274 scoped_ptr<QuotaDispatcher> quota_dispatcher_;
276 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
278 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
280 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
282 scoped_refptr<QuotaMessageFilter> quota_message_filter_;
284 scoped_refptr<NotificationDispatcher> notification_dispatcher_;
286 scoped_refptr<PushDispatcher> push_dispatcher_;
288 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
290 scoped_ptr<ChildGpuMemoryBufferManager> gpu_memory_buffer_manager_;
292 scoped_ptr<ChildDiscardableSharedMemoryManager>
293 discardable_shared_memory_manager_;
295 // Observes the trace event system. When tracing is enabled, optionally
296 // starts profiling the tcmalloc heap.
297 scoped_ptr<base::trace_event::TraceMemoryController> trace_memory_controller_;
299 scoped_ptr<base::PowerMonitor> power_monitor_;
301 scoped_refptr<ChildMessageFilter> geofencing_message_filter_;
302 scoped_refptr<ChildMessageFilter> bluetooth_message_filter_;
304 scoped_refptr<NavigatorConnectDispatcher> navigator_connect_dispatcher_;
306 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner_;
308 base::WeakPtrFactory<ChildThreadImpl> channel_connected_factory_;
310 DISALLOW_COPY_AND_ASSIGN(ChildThreadImpl);
313 struct ChildThreadImpl::Options {
314 ~Options();
316 class Builder;
318 std::string channel_name;
319 bool use_mojo_channel;
320 scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner;
321 std::vector<IPC::MessageFilter*> startup_filters;
323 private:
324 Options();
327 class ChildThreadImpl::Options::Builder {
328 public:
329 Builder();
331 Builder& InBrowserProcess(const InProcessChildThreadParams& params);
332 Builder& UseMojoChannel(bool use_mojo_channel);
333 Builder& WithChannelName(const std::string& channel_name);
334 Builder& AddStartupFilter(IPC::MessageFilter* filter);
336 Options Build();
338 private:
339 struct Options options_;
341 DISALLOW_COPY_AND_ASSIGN(Builder);
344 } // namespace content
346 #endif // CONTENT_CHILD_CHILD_THREAD_IMPL_H_