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