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_COMMON_CHILD_THREAD_H_
6 #define CONTENT_COMMON_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"
22 class SyncMessageFilter
;
30 class ChildHistogramMessageFilter
;
31 class FileSystemDispatcher
;
32 class QuotaDispatcher
;
33 class ResourceDispatcher
;
34 class SocketStreamDispatcher
;
36 // The main thread of a child process derives from this class.
37 class CONTENT_EXPORT ChildThread
: public IPC::Listener
, public IPC::Sender
{
39 // Creates the thread.
41 // Used for single-process mode.
42 explicit ChildThread(const std::string
& channel_name
);
43 virtual ~ChildThread();
45 // IPC::Sender implementation:
46 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
48 // See documentation on MessageRouter for AddRoute and RemoveRoute
49 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
50 void RemoveRoute(int32 routing_id
);
52 IPC::Listener
* ResolveRoute(int32 routing_id
);
54 IPC::SyncChannel
* channel() { return channel_
.get(); }
56 // Creates a ResourceLoaderBridge.
57 // Tests can override this method if they want a custom loading behavior.
58 virtual webkit_glue::ResourceLoaderBridge
* CreateBridge(
59 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
);
61 // Allocates a block of shared memory of the given size and
62 // maps in into the address space. Returns NULL of failure.
63 // Note: On posix, this requires a sync IPC to the browser process,
64 // but on windows the child process directly allocates the block.
65 base::SharedMemory
* AllocateSharedMemory(size_t buf_size
);
67 ResourceDispatcher
* resource_dispatcher();
69 SocketStreamDispatcher
* socket_stream_dispatcher() {
70 return socket_stream_dispatcher_
.get();
73 FileSystemDispatcher
* file_system_dispatcher() const {
74 return file_system_dispatcher_
.get();
77 QuotaDispatcher
* quota_dispatcher() const {
78 return quota_dispatcher_
.get();
81 // Safe to call on any thread, as long as it's guaranteed that the thread's
82 // lifetime is less than the main thread.
83 IPC::SyncMessageFilter
* sync_message_filter();
85 ChildHistogramMessageFilter
* child_histogram_message_filter() const {
86 return histogram_message_filter_
.get();
89 MessageLoop
* message_loop();
91 // Returns the one child thread.
92 static ChildThread
* current();
94 virtual bool IsWebFrameValid(WebKit::WebFrame
* frame
);
97 friend class ChildProcess
;
99 // Called when the process refcount is 0.
100 void OnProcessFinalRelease();
102 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
104 void set_on_channel_error_called(bool on_channel_error_called
) {
105 on_channel_error_called_
= on_channel_error_called
;
108 // IPC::Listener implementation:
109 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
110 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
111 virtual void OnChannelError() OVERRIDE
;
116 // IPC message handlers.
118 void OnSetProfilerStatus(tracked_objects::ThreadData::Status status
);
119 void OnGetChildProfilerData(int sequence_number
);
120 void OnDumpHandles();
121 #ifdef IPC_MESSAGE_LOG_ENABLED
122 void OnSetIPCLoggingEnabled(bool enable
);
124 #if defined(USE_TCMALLOC)
125 void OnGetTcmallocStats();
128 void EnsureConnected();
130 std::string channel_name_
;
131 scoped_ptr
<IPC::SyncChannel
> channel_
;
133 // Allows threads other than the main thread to send sync messages.
134 scoped_refptr
<IPC::SyncMessageFilter
> sync_message_filter_
;
136 // Implements message routing functionality to the consumers of ChildThread.
137 MessageRouter router_
;
139 // Handles resource loads for this process.
140 scoped_ptr
<ResourceDispatcher
> resource_dispatcher_
;
142 // Handles SocketStream for this process.
143 scoped_ptr
<SocketStreamDispatcher
> socket_stream_dispatcher_
;
145 // The OnChannelError() callback was invoked - the channel is dead, don't
146 // attempt to communicate.
147 bool on_channel_error_called_
;
149 MessageLoop
* message_loop_
;
151 scoped_ptr
<FileSystemDispatcher
> file_system_dispatcher_
;
153 scoped_ptr
<QuotaDispatcher
> quota_dispatcher_
;
155 scoped_refptr
<ChildHistogramMessageFilter
> histogram_message_filter_
;
157 base::WeakPtrFactory
<ChildThread
> channel_connected_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(ChildThread
);
162 } // namespace content
164 #endif // CONTENT_COMMON_CHILD_THREAD_H_