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 "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/shared_memory.h"
12 #include "base/tracked_objects.h"
13 #include "content/common/content_export.h"
14 #include "content/common/message_router.h"
15 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
16 #include "webkit/glue/resource_loader_bridge.h"
24 class SyncMessageFilter
;
32 class ChildHistogramMessageFilter
;
33 class ChildResourceMessageFilter
;
34 class FileSystemDispatcher
;
35 class QuotaDispatcher
;
36 class ResourceDispatcher
;
37 class SocketStreamDispatcher
;
38 class ThreadSafeSender
;
40 // The main thread of a child process derives from this class.
41 class CONTENT_EXPORT ChildThread
: public IPC::Listener
, public IPC::Sender
{
43 // Creates the thread.
45 // Used for single-process mode.
46 explicit ChildThread(const std::string
& channel_name
);
47 // ChildProcess::main_thread() is reset after Shutdown(), and before the
48 // destructor, so any subsystem that relies on ChildProcess::main_thread()
49 // must be terminated before Shutdown returns. In particular, if a subsystem
50 // has a thread that post tasks to ChildProcess::main_thread(), that thread
51 // should be joined in Shutdown().
52 virtual ~ChildThread();
53 virtual void Shutdown() = 0;
55 // IPC::Sender implementation:
56 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
58 // See documentation on MessageRouter for AddRoute and RemoveRoute
59 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
60 void RemoveRoute(int32 routing_id
);
62 IPC::SyncChannel
* channel() { return channel_
.get(); }
64 // Creates a ResourceLoaderBridge.
65 // Tests can override this method if they want a custom loading behavior.
66 virtual webkit_glue::ResourceLoaderBridge
* CreateBridge(
67 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
);
69 // Allocates a block of shared memory of the given size and
70 // maps in into the address space. Returns NULL of failure.
71 // Note: On posix, this requires a sync IPC to the browser process,
72 // but on windows the child process directly allocates the block.
73 base::SharedMemory
* AllocateSharedMemory(size_t buf_size
);
75 // A static variant that can be called on background threads provided
76 // the |sender| passed in is safe to use on background threads.
77 static base::SharedMemory
* AllocateSharedMemory(size_t buf_size
,
80 ResourceDispatcher
* resource_dispatcher() const {
81 return resource_dispatcher_
.get();
84 SocketStreamDispatcher
* socket_stream_dispatcher() const {
85 return socket_stream_dispatcher_
.get();
88 FileSystemDispatcher
* file_system_dispatcher() const {
89 return file_system_dispatcher_
.get();
92 QuotaDispatcher
* quota_dispatcher() const {
93 return quota_dispatcher_
.get();
96 // Safe to call on any thread, as long as it's guaranteed that the thread's
97 // lifetime is less than the main thread. The |filter| returned may only
98 // be used on background threads.
99 IPC::SyncMessageFilter
* sync_message_filter() const {
100 return sync_message_filter_
.get();
103 // The getter should only be called on the main thread, however the
104 // IPC::Sender it returns may be safely called on any thread including
106 ThreadSafeSender
* thread_safe_sender() const {
107 return thread_safe_sender_
.get();
110 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
111 return histogram_message_filter_
.get();
114 base::MessageLoop
* message_loop() const { return message_loop_
; }
116 // Returns the one child thread.
117 static ChildThread
* current();
119 virtual bool IsWebFrameValid(WebKit::WebFrame
* frame
);
122 friend class ChildProcess
;
124 // Called when the process refcount is 0.
125 void OnProcessFinalRelease();
127 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
129 void set_on_channel_error_called(bool on_channel_error_called
) {
130 on_channel_error_called_
= on_channel_error_called
;
133 // IPC::Listener implementation:
134 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
135 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
136 virtual void OnChannelError() OVERRIDE
;
141 // IPC message handlers.
143 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
144 void OnGetChildProfilerData(int sequence_number
);
145 void OnDumpHandles();
146 #ifdef IPC_MESSAGE_LOG_ENABLED
147 void OnSetIPCLoggingEnabled(bool enable
);
149 #if defined(USE_TCMALLOC)
150 void OnGetTcmallocStats();
153 void EnsureConnected();
155 std::string channel_name_
;
156 scoped_ptr
<IPC::SyncChannel
> channel_
;
158 // Allows threads other than the main thread to send sync messages.
159 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
161 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
163 // Implements message routing functionality to the consumers of ChildThread.
164 MessageRouter router_
;
166 // Handles resource loads for this process.
167 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
169 // Handles SocketStream for this process.
170 scoped_ptr
<SocketStreamDispatcher
> socket_stream_dispatcher_
;
172 // The OnChannelError() callback was invoked - the channel is dead, don't
173 // attempt to communicate.
174 bool on_channel_error_called_
;
176 base::MessageLoop
* message_loop_
;
178 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
180 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
182 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
184 scoped_refptr
<ChildResourceMessageFilter
> resource_message_filter_
;
186 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
191 } // namespace content
193 #endif // CONTENT_CHILD_CHILD_THREAD_H_