| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_COMMON_CHILD_THREAD_H_ | 5 #ifndef CHROME_COMMON_CHILD_THREAD_H_ |
| 6 #define CHROME_COMMON_CHILD_THREAD_H_ | 6 #define CHROME_COMMON_CHILD_THREAD_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "chrome/common/message_router.h" | 10 #include "base/mp/mp_child_thread.h" |
| 11 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/common/resource_dispatcher.h" | 12 #include "chrome/common/resource_dispatcher.h" |
| 12 #include "ipc/ipc_sync_channel.h" | 13 #include "chrome/common/socket_stream_dispatcher.h" |
| 13 #include "ipc/ipc_message.h" | |
| 14 | 14 |
| 15 class NotificationService; | |
| 16 class SocketStreamDispatcher; | |
| 17 | |
| 18 namespace IPC { | |
| 19 class SyncMessageFilter; | |
| 20 } | |
| 21 | 15 |
| 22 // The main thread of a child process derives from this class. | 16 // The main thread of a child process derives from this class. |
| 23 class ChildThread : public IPC::Channel::Listener, | 17 class ChildThread : public base::MpChildThread { |
| 24 public IPC::Message::Sender { | |
| 25 public: | 18 public: |
| 26 // Creates the thread. | 19 // Creates the thread. |
| 27 ChildThread(); | 20 ChildThread(); |
| 28 // Used for single-process mode. | 21 // Used for single-process mode. |
| 29 explicit ChildThread(const std::string& channel_name); | 22 explicit ChildThread(const std::string& channel_name); |
| 30 virtual ~ChildThread(); | 23 virtual ~ChildThread(); |
| 31 | 24 |
| 32 // IPC::Message::Sender implementation: | |
| 33 virtual bool Send(IPC::Message* msg); | |
| 34 | |
| 35 // See documentation on MessageRouter for AddRoute and RemoveRoute | |
| 36 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); | |
| 37 void RemoveRoute(int32 routing_id); | |
| 38 | |
| 39 IPC::Channel::Listener* ResolveRoute(int32 routing_id); | |
| 40 | |
| 41 // Creates a ResourceLoaderBridge. | 25 // Creates a ResourceLoaderBridge. |
| 42 // Tests can override this method if they want a custom loading behavior. | 26 // Tests can override this method if they want a custom loading behavior. |
| 43 virtual webkit_glue::ResourceLoaderBridge* CreateBridge( | 27 virtual webkit_glue::ResourceLoaderBridge* CreateBridge( |
| 44 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info, | 28 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info, |
| 45 int host_renderer_id, | 29 int host_renderer_id, |
| 46 int host_render_view_id); | 30 int host_render_view_id); |
| 47 | 31 |
| 48 ResourceDispatcher* resource_dispatcher() { | 32 ResourceDispatcher* resource_dispatcher() { |
| 49 return resource_dispatcher_.get(); | 33 return resource_dispatcher_.get(); |
| 50 } | 34 } |
| 51 | 35 |
| 52 SocketStreamDispatcher* socket_stream_dispatcher() { | 36 SocketStreamDispatcher* socket_stream_dispatcher() { |
| 53 return socket_stream_dispatcher_.get(); | 37 return socket_stream_dispatcher_.get(); |
| 54 } | 38 } |
| 55 | 39 |
| 56 // Safe to call on any thread, as long as it's guaranteed that the thread's | |
| 57 // lifetime is less than the main thread. | |
| 58 IPC::SyncMessageFilter* sync_message_filter() { return sync_message_filter_; } | |
| 59 | |
| 60 MessageLoop* message_loop() { return message_loop_; } | |
| 61 | |
| 62 // Returns the one child thread. | |
| 63 static ChildThread* current(); | |
| 64 | |
| 65 protected: | |
| 66 friend class ChildProcess; | |
| 67 | |
| 68 // Called when the process refcount is 0. | |
| 69 void OnProcessFinalRelease(); | |
| 70 | |
| 71 virtual void OnControlMessageReceived(const IPC::Message& msg) { } | |
| 72 virtual void OnAskBeforeShutdown(); | |
| 73 virtual void OnShutdown(); | |
| 74 | |
| 75 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 76 virtual void OnSetIPCLoggingEnabled(bool enable); | |
| 77 #endif | |
| 78 | |
| 79 IPC::SyncChannel* channel() { return channel_.get(); } | |
| 80 | |
| 81 void set_on_channel_error_called(bool on_channel_error_called) { | |
| 82 on_channel_error_called_ = on_channel_error_called; | |
| 83 } | |
| 84 | |
| 85 private: | 40 private: |
| 86 void Init(); | 41 void Init(); |
| 87 | 42 |
| 88 // IPC::Channel::Listener implementation: | 43 // IPC::Channel::Listener implementation: |
| 89 virtual void OnMessageReceived(const IPC::Message& msg); | 44 virtual void OnMessageReceived(const IPC::Message& msg); |
| 90 virtual void OnChannelError(); | |
| 91 | |
| 92 std::string channel_name_; | |
| 93 scoped_ptr<IPC::SyncChannel> channel_; | |
| 94 | |
| 95 // Allows threads other than the main thread to send sync messages. | |
| 96 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_; | |
| 97 | |
| 98 // Implements message routing functionality to the consumers of ChildThread. | |
| 99 MessageRouter router_; | |
| 100 | 45 |
| 101 // Handles resource loads for this process. | 46 // Handles resource loads for this process. |
| 102 scoped_ptr<ResourceDispatcher> resource_dispatcher_; | 47 scoped_ptr<ResourceDispatcher> resource_dispatcher_; |
| 103 | 48 |
| 104 // Handles SocketStream for this process. | 49 // Handles SocketStream for this process. |
| 105 scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_; | 50 scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_; |
| 106 | 51 |
| 107 // If true, checks with the browser process before shutdown. This avoids race | |
| 108 // conditions if the process refcount is 0 but there's an IPC message inflight | |
| 109 // that would addref it. | |
| 110 bool check_with_browser_before_shutdown_; | |
| 111 | |
| 112 // The OnChannelError() callback was invoked - the channel is dead, don't | |
| 113 // attempt to communicate. | |
| 114 bool on_channel_error_called_; | |
| 115 | |
| 116 MessageLoop* message_loop_; | |
| 117 | |
| 118 scoped_ptr<NotificationService> notification_service_; | 52 scoped_ptr<NotificationService> notification_service_; |
| 119 | 53 |
| 120 DISALLOW_COPY_AND_ASSIGN(ChildThread); | 54 DISALLOW_COPY_AND_ASSIGN(ChildThread); |
| 121 }; | 55 }; |
| 122 | 56 |
| 123 #endif // CHROME_COMMON_CHILD_THREAD_H_ | 57 #endif // CHROME_COMMON_CHILD_THREAD_H_ |
| OLD | NEW |