• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_CHILD_CHILD_THREAD_H_
6 #define CONTENT_CHILD_CHILD_THREAD_H_
7 
8 #include <string>
9 
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/common/content_export.h"
17 #include "content/common/message_router.h"
18 #include "ipc/ipc_message.h"  // For IPC_MESSAGE_LOG_ENABLED.
19 #include "webkit/child/resource_loader_bridge.h"
20 
21 namespace base {
22 class MessageLoop;
23 
24 namespace debug {
25 class TraceMemoryController;
26 }  // namespace debug
27 }  // namespace base
28 
29 namespace IPC {
30 class SyncChannel;
31 class SyncMessageFilter;
32 }  // namespace IPC
33 
34 namespace blink {
35 class WebFrame;
36 }  // namespace blink
37 
38 namespace content {
39 class ChildHistogramMessageFilter;
40 class ChildResourceMessageFilter;
41 class FileSystemDispatcher;
42 class ServiceWorkerDispatcher;
43 class ServiceWorkerMessageFilter;
44 class QuotaDispatcher;
45 class QuotaMessageFilter;
46 class ResourceDispatcher;
47 class SocketStreamDispatcher;
48 class ThreadSafeSender;
49 class WebSocketDispatcher;
50 
51 // The main thread of a child process derives from this class.
52 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender {
53  public:
54   // Creates the thread.
55   ChildThread();
56   // Used for single-process mode and for in process gpu mode.
57   explicit ChildThread(const std::string& channel_name);
58   // ChildProcess::main_thread() is reset after Shutdown(), and before the
59   // destructor, so any subsystem that relies on ChildProcess::main_thread()
60   // must be terminated before Shutdown returns. In particular, if a subsystem
61   // has a thread that post tasks to ChildProcess::main_thread(), that thread
62   // should be joined in Shutdown().
63   virtual ~ChildThread();
64   virtual void Shutdown();
65 
66   // IPC::Sender implementation:
67   virtual bool Send(IPC::Message* msg) OVERRIDE;
68 
69   // See documentation on MessageRouter for AddRoute and RemoveRoute
70   void AddRoute(int32 routing_id, IPC::Listener* listener);
71   void RemoveRoute(int32 routing_id);
72 
channel()73   IPC::SyncChannel* channel() { return channel_.get(); }
74 
75   // Creates a ResourceLoaderBridge.
76   // Tests can override this method if they want a custom loading behavior.
77   virtual webkit_glue::ResourceLoaderBridge* CreateBridge(
78       const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info);
79 
80   // Allocates a block of shared memory of the given size and
81   // maps in into the address space. Returns NULL of failure.
82   // Note: On posix, this requires a sync IPC to the browser process,
83   // but on windows the child process directly allocates the block.
84   base::SharedMemory* AllocateSharedMemory(size_t buf_size);
85 
86   // A static variant that can be called on background threads provided
87   // the |sender| passed in is safe to use on background threads.
88   static base::SharedMemory* AllocateSharedMemory(size_t buf_size,
89                                                   IPC::Sender* sender);
90 
resource_dispatcher()91   ResourceDispatcher* resource_dispatcher() const {
92     return resource_dispatcher_.get();
93   }
94 
socket_stream_dispatcher()95   SocketStreamDispatcher* socket_stream_dispatcher() const {
96     return socket_stream_dispatcher_.get();
97   }
98 
websocket_dispatcher()99   WebSocketDispatcher* websocket_dispatcher() const {
100     return websocket_dispatcher_.get();
101   }
102 
file_system_dispatcher()103   FileSystemDispatcher* file_system_dispatcher() const {
104     return file_system_dispatcher_.get();
105   }
106 
service_worker_dispatcher()107   ServiceWorkerDispatcher* service_worker_dispatcher() const {
108     return service_worker_dispatcher_.get();
109   }
110 
quota_dispatcher()111   QuotaDispatcher* quota_dispatcher() const {
112     return quota_dispatcher_.get();
113   }
114 
sync_message_filter()115   IPC::SyncMessageFilter* sync_message_filter() const {
116     return sync_message_filter_.get();
117   }
118 
119   // The getter should only be called on the main thread, however the
120   // IPC::Sender it returns may be safely called on any thread including
121   // the main thread.
thread_safe_sender()122   ThreadSafeSender* thread_safe_sender() const {
123     return thread_safe_sender_.get();
124   }
125 
child_histogram_message_filter()126   ChildHistogramMessageFilter* child_histogram_message_filter() const {
127     return histogram_message_filter_.get();
128   }
129 
service_worker_message_filter()130   ServiceWorkerMessageFilter* service_worker_message_filter() const {
131     return service_worker_message_filter_.get();
132   }
133 
quota_message_filter()134   QuotaMessageFilter* quota_message_filter() const {
135     return quota_message_filter_.get();
136   }
137 
message_loop()138   base::MessageLoop* message_loop() const { return message_loop_; }
139 
140   // Returns the one child thread. Can only be called on the main thread.
141   static ChildThread* current();
142 
143 #if defined(OS_ANDROID)
144   // Called on Android's service thread to shutdown the main thread of this
145   // process.
146   static void ShutdownThread();
147 #endif
148 
149  protected:
150   friend class ChildProcess;
151 
152   // Called when the process refcount is 0.
153   void OnProcessFinalRelease();
154 
155   virtual bool OnControlMessageReceived(const IPC::Message& msg);
156 
set_on_channel_error_called(bool on_channel_error_called)157   void set_on_channel_error_called(bool on_channel_error_called) {
158     on_channel_error_called_ = on_channel_error_called;
159   }
160 
161   // IPC::Listener implementation:
162   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
163   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
164   virtual void OnChannelError() OVERRIDE;
165 
166  private:
167   void Init();
168 
169   // IPC message handlers.
170   void OnShutdown();
171   void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
172   void OnGetChildProfilerData(int sequence_number);
173   void OnDumpHandles();
174 #ifdef IPC_MESSAGE_LOG_ENABLED
175   void OnSetIPCLoggingEnabled(bool enable);
176 #endif
177 #if defined(USE_TCMALLOC)
178   void OnGetTcmallocStats();
179 #endif
180 
181   void EnsureConnected();
182 
183   std::string channel_name_;
184   scoped_ptr<IPC::SyncChannel> channel_;
185 
186   // Allows threads other than the main thread to send sync messages.
187   scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
188 
189   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
190 
191   // Implements message routing functionality to the consumers of ChildThread.
192   MessageRouter router_;
193 
194   // Handles resource loads for this process.
195   scoped_ptr<ResourceDispatcher> resource_dispatcher_;
196 
197   // Handles SocketStream for this process.
198   scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_;
199 
200   scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
201 
202   // The OnChannelError() callback was invoked - the channel is dead, don't
203   // attempt to communicate.
204   bool on_channel_error_called_;
205 
206   base::MessageLoop* message_loop_;
207 
208   scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
209 
210   scoped_ptr<ServiceWorkerDispatcher> service_worker_dispatcher_;
211 
212   scoped_ptr<QuotaDispatcher> quota_dispatcher_;
213 
214   scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
215 
216   scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
217 
218   scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
219 
220   scoped_refptr<QuotaMessageFilter> quota_message_filter_;
221 
222   base::WeakPtrFactory<ChildThread> channel_connected_factory_;
223 
224   // Observes the trace event system. When tracing is enabled, optionally
225   // starts profiling the tcmalloc heap.
226   scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
227 
228   scoped_ptr<base::PowerMonitor> power_monitor_;
229 
230   bool in_browser_process_;
231 
232   DISALLOW_COPY_AND_ASSIGN(ChildThread);
233 };
234 
235 }  // namespace content
236 
237 #endif  // CONTENT_CHILD_CHILD_THREAD_H_
238