| OLD | NEW |
| (Empty) |
| 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_COMMON_CHILD_THREAD_H_ | |
| 6 #define CONTENT_COMMON_CHILD_THREAD_H_ | |
| 7 | |
| 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" | |
| 17 | |
| 18 namespace base { | |
| 19 class MessageLoop; | |
| 20 } | |
| 21 | |
| 22 namespace IPC { | |
| 23 class SyncChannel; | |
| 24 class SyncMessageFilter; | |
| 25 } | |
| 26 | |
| 27 namespace WebKit { | |
| 28 class WebFrame; | |
| 29 } | |
| 30 | |
| 31 namespace content { | |
| 32 class ChildHistogramMessageFilter; | |
| 33 class ChildResourceMessageFilter; | |
| 34 class FileSystemDispatcher; | |
| 35 class QuotaDispatcher; | |
| 36 class ResourceDispatcher; | |
| 37 class SocketStreamDispatcher; | |
| 38 class ThreadSafeSender; | |
| 39 | |
| 40 // The main thread of a child process derives from this class. | |
| 41 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender { | |
| 42 public: | |
| 43 // Creates the thread. | |
| 44 ChildThread(); | |
| 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; | |
| 54 | |
| 55 // IPC::Sender implementation: | |
| 56 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 57 | |
| 58 // See documentation on MessageRouter for AddRoute and RemoveRoute | |
| 59 void AddRoute(int32 routing_id, IPC::Listener* listener); | |
| 60 void RemoveRoute(int32 routing_id); | |
| 61 | |
| 62 IPC::SyncChannel* channel() { return channel_.get(); } | |
| 63 | |
| 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); | |
| 68 | |
| 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); | |
| 74 | |
| 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, | |
| 78 IPC::Sender* sender); | |
| 79 | |
| 80 ResourceDispatcher* resource_dispatcher() const { | |
| 81 return resource_dispatcher_.get(); | |
| 82 } | |
| 83 | |
| 84 SocketStreamDispatcher* socket_stream_dispatcher() const { | |
| 85 return socket_stream_dispatcher_.get(); | |
| 86 } | |
| 87 | |
| 88 FileSystemDispatcher* file_system_dispatcher() const { | |
| 89 return file_system_dispatcher_.get(); | |
| 90 } | |
| 91 | |
| 92 QuotaDispatcher* quota_dispatcher() const { | |
| 93 return quota_dispatcher_.get(); | |
| 94 } | |
| 95 | |
| 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(); | |
| 101 } | |
| 102 | |
| 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 | |
| 105 // the main thread. | |
| 106 ThreadSafeSender* thread_safe_sender() const { | |
| 107 return thread_safe_sender_.get(); | |
| 108 } | |
| 109 | |
| 110 ChildHistogramMessageFilter* child_histogram_message_filter() const { | |
| 111 return histogram_message_filter_.get(); | |
| 112 } | |
| 113 | |
| 114 base::MessageLoop* message_loop() const { return message_loop_; } | |
| 115 | |
| 116 // Returns the one child thread. | |
| 117 static ChildThread* current(); | |
| 118 | |
| 119 virtual bool IsWebFrameValid(WebKit::WebFrame* frame); | |
| 120 | |
| 121 protected: | |
| 122 friend class ChildProcess; | |
| 123 | |
| 124 // Called when the process refcount is 0. | |
| 125 void OnProcessFinalRelease(); | |
| 126 | |
| 127 virtual bool OnControlMessageReceived(const IPC::Message& msg); | |
| 128 | |
| 129 void set_on_channel_error_called(bool on_channel_error_called) { | |
| 130 on_channel_error_called_ = on_channel_error_called; | |
| 131 } | |
| 132 | |
| 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; | |
| 137 | |
| 138 private: | |
| 139 void Init(); | |
| 140 | |
| 141 // IPC message handlers. | |
| 142 void OnShutdown(); | |
| 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); | |
| 148 #endif | |
| 149 #if defined(USE_TCMALLOC) | |
| 150 void OnGetTcmallocStats(); | |
| 151 #endif | |
| 152 | |
| 153 void EnsureConnected(); | |
| 154 | |
| 155 std::string channel_name_; | |
| 156 scoped_ptr<IPC::SyncChannel> channel_; | |
| 157 | |
| 158 // Allows threads other than the main thread to send sync messages. | |
| 159 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_; | |
| 160 | |
| 161 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | |
| 162 | |
| 163 // Implements message routing functionality to the consumers of ChildThread. | |
| 164 MessageRouter router_; | |
| 165 | |
| 166 // Handles resource loads for this process. | |
| 167 scoped_ptr<ResourceDispatcher> resource_dispatcher_; | |
| 168 | |
| 169 // Handles SocketStream for this process. | |
| 170 scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_; | |
| 171 | |
| 172 // The OnChannelError() callback was invoked - the channel is dead, don't | |
| 173 // attempt to communicate. | |
| 174 bool on_channel_error_called_; | |
| 175 | |
| 176 base::MessageLoop* message_loop_; | |
| 177 | |
| 178 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_; | |
| 179 | |
| 180 scoped_ptr<QuotaDispatcher> quota_dispatcher_; | |
| 181 | |
| 182 scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_; | |
| 183 | |
| 184 scoped_refptr<ChildResourceMessageFilter> resource_message_filter_; | |
| 185 | |
| 186 base::WeakPtrFactory<ChildThread> channel_connected_factory_; | |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(ChildThread); | |
| 189 }; | |
| 190 | |
| 191 } // namespace content | |
| 192 | |
| 193 #endif // CONTENT_COMMON_CHILD_THREAD_H_ | |
| OLD | NEW |