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