Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / child_thread.h
blob9b63fb8eeef21f67fb81c6aa29aa643866e93146
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 ChildHistogramMessageFilter;
40 class ChildResourceMessageFilter;
41 class ChildSharedBitmapManager;
42 class FileSystemDispatcher;
43 class ServiceWorkerMessageFilter;
44 class QuotaDispatcher;
45 class QuotaMessageFilter;
46 class ResourceDispatcher;
47 class SocketStreamDispatcher;
48 class ThreadSafeSender;
49 class WebSocketDispatcher;
50 struct RequestInfo;
52 // The main thread of a child process derives from this class.
53 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender {
54 public:
55 struct CONTENT_EXPORT Options {
56 Options();
57 explicit Options(bool mojo);
58 Options(std::string name, bool mojo)
59 : channel_name(name), use_mojo_channel(mojo) {}
61 std::string channel_name;
62 bool use_mojo_channel;
65 // Creates the thread.
66 ChildThread();
67 // Used for single-process mode and for in process gpu mode.
68 explicit ChildThread(const Options& options);
69 // ChildProcess::main_thread() is reset after Shutdown(), and before the
70 // destructor, so any subsystem that relies on ChildProcess::main_thread()
71 // must be terminated before Shutdown returns. In particular, if a subsystem
72 // has a thread that post tasks to ChildProcess::main_thread(), that thread
73 // should be joined in Shutdown().
74 virtual ~ChildThread();
75 virtual void Shutdown();
77 // IPC::Sender implementation:
78 virtual bool Send(IPC::Message* msg) OVERRIDE;
80 IPC::SyncChannel* channel() { return channel_.get(); }
82 MessageRouter* GetRouter();
84 // Allocates a block of shared memory of the given size and
85 // maps in into the address space. Returns NULL of failure.
86 // Note: On posix, this requires a sync IPC to the browser process,
87 // but on windows the child process directly allocates the block.
88 base::SharedMemory* AllocateSharedMemory(size_t buf_size);
90 // A static variant that can be called on background threads provided
91 // the |sender| passed in is safe to use on background threads.
92 static base::SharedMemory* AllocateSharedMemory(size_t buf_size,
93 IPC::Sender* sender);
95 ChildSharedBitmapManager* shared_bitmap_manager() const {
96 return shared_bitmap_manager_.get();
99 ResourceDispatcher* resource_dispatcher() const {
100 return resource_dispatcher_.get();
103 SocketStreamDispatcher* socket_stream_dispatcher() const {
104 return socket_stream_dispatcher_.get();
107 WebSocketDispatcher* websocket_dispatcher() const {
108 return websocket_dispatcher_.get();
111 FileSystemDispatcher* file_system_dispatcher() const {
112 return file_system_dispatcher_.get();
115 QuotaDispatcher* quota_dispatcher() const {
116 return quota_dispatcher_.get();
119 IPC::SyncMessageFilter* sync_message_filter() const {
120 return sync_message_filter_.get();
123 // The getter should only be called on the main thread, however the
124 // IPC::Sender it returns may be safely called on any thread including
125 // the main thread.
126 ThreadSafeSender* thread_safe_sender() const {
127 return thread_safe_sender_.get();
130 ChildHistogramMessageFilter* child_histogram_message_filter() const {
131 return histogram_message_filter_.get();
134 ServiceWorkerMessageFilter* service_worker_message_filter() const {
135 return service_worker_message_filter_.get();
138 QuotaMessageFilter* quota_message_filter() const {
139 return quota_message_filter_.get();
142 base::MessageLoop* message_loop() const { return message_loop_; }
144 // Returns the one child thread. Can only be called on the main thread.
145 static ChildThread* current();
147 #if defined(OS_ANDROID)
148 // Called on Android's service thread to shutdown the main thread of this
149 // process.
150 static void ShutdownThread();
151 #endif
153 ServiceRegistry* service_registry() const {
154 return mojo_application_->service_registry();
157 protected:
158 friend class ChildProcess;
160 // Called when the process refcount is 0.
161 void OnProcessFinalRelease();
163 virtual bool OnControlMessageReceived(const IPC::Message& msg);
165 void set_on_channel_error_called(bool on_channel_error_called) {
166 on_channel_error_called_ = on_channel_error_called;
169 // IPC::Listener implementation:
170 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
171 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
172 virtual void OnChannelError() OVERRIDE;
174 private:
175 class ChildThreadMessageRouter : public MessageRouter {
176 public:
177 // |sender| must outlive this object.
178 explicit ChildThreadMessageRouter(IPC::Sender* sender);
179 virtual bool Send(IPC::Message* msg) OVERRIDE;
181 private:
182 IPC::Sender* const sender_;
185 void Init(const Options& options);
186 scoped_ptr<IPC::SyncChannel> CreateChannel(bool use_mojo_channel);
188 // IPC message handlers.
189 void OnShutdown();
190 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
191 void OnGetChildProfilerData(int sequence_number);
192 void OnDumpHandles();
193 void OnProcessBackgrounded(bool background);
194 #ifdef IPC_MESSAGE_LOG_ENABLED
195 void OnSetIPCLoggingEnabled(bool enable);
196 #endif
197 #if defined(USE_TCMALLOC)
198 void OnGetTcmallocStats();
199 #endif
201 void EnsureConnected();
203 scoped_ptr<MojoApplication> mojo_application_;
205 std::string channel_name_;
206 scoped_ptr<IPC::SyncChannel> channel_;
208 // Allows threads other than the main thread to send sync messages.
209 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
211 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
213 // Implements message routing functionality to the consumers of ChildThread.
214 ChildThreadMessageRouter router_;
216 // Handles resource loads for this process.
217 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
219 // Handles SocketStream for this process.
220 scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_;
222 scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
224 // The OnChannelError() callback was invoked - the channel is dead, don't
225 // attempt to communicate.
226 bool on_channel_error_called_;
228 base::MessageLoop* message_loop_;
230 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
232 scoped_ptr<QuotaDispatcher> quota_dispatcher_;
234 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
236 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
238 scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
240 scoped_refptr<QuotaMessageFilter> quota_message_filter_;
242 scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
244 base::WeakPtrFactory<ChildThread> channel_connected_factory_;
246 // Observes the trace event system. When tracing is enabled, optionally
247 // starts profiling the tcmalloc heap.
248 scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
250 scoped_ptr<base::PowerMonitor> power_monitor_;
252 bool in_browser_process_;
254 DISALLOW_COPY_AND_ASSIGN(ChildThread);
257 } // namespace content
259 #endif // CONTENT_CHILD_CHILD_THREAD_H_