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