[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef CHROME_COMMON_CHILD_THREAD_H_ | ||||
6 | #define CHROME_COMMON_CHILD_THREAD_H_ | ||||
7 | |||||
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 8 | #include "base/basictypes.h" |
9 | #include "base/scoped_ptr.h" | ||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 10 | #include "base/thread.h" |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 11 | #include "chrome/common/ipc_sync_channel.h" |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 12 | #include "chrome/common/message_router.h" |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 13 | #include "chrome/common/resource_dispatcher.h" |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 14 | |
15 | // Child processes's background thread should derive from this class. | ||||
16 | class ChildThread : public IPC::Channel::Listener, | ||||
17 | public IPC::Message::Sender, | ||||
18 | public base::Thread { | ||||
19 | public: | ||||
20 | // Creates the thread. | ||||
21 | ChildThread(Thread::Options options); | ||||
22 | virtual ~ChildThread(); | ||||
23 | |||||
24 | // IPC::Message::Sender implementation: | ||||
25 | virtual bool Send(IPC::Message* msg); | ||||
26 | |||||
27 | // See documentation on MessageRouter for AddRoute and RemoveRoute | ||||
28 | void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); | ||||
29 | void RemoveRoute(int32 routing_id); | ||||
30 | |||||
31 | MessageLoop* owner_loop() { return owner_loop_; } | ||||
32 | |||||
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 33 | ResourceDispatcher* resource_dispatcher() { |
34 | return resource_dispatcher_.get(); | ||||
35 | } | ||||
36 | |||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 37 | protected: |
38 | friend class ChildProcess; | ||||
39 | |||||
40 | // Starts the thread. | ||||
41 | bool Run(); | ||||
42 | |||||
43 | // Overrides the channel name. Used for --single-process mode. | ||||
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame^] | 44 | void SetChannelName(const std::string& name) { channel_name_ = name; } |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 45 | |
[email protected] | 51d70e0 | 2009-03-27 20:45:59 | [diff] [blame] | 46 | // Called when the process refcount is 0. |
47 | void OnProcessFinalRelease(); | ||||
48 | |||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 49 | protected: |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 50 | // The required stack size if V8 runs on a thread. |
51 | static const size_t kV8StackSize; | ||||
52 | |||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 53 | virtual void OnControlMessageReceived(const IPC::Message& msg) { } |
54 | |||||
55 | // Returns the one child thread. | ||||
56 | static ChildThread* current(); | ||||
57 | |||||
58 | IPC::SyncChannel* channel() { return channel_.get(); } | ||||
59 | |||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 60 | // Thread implementation. |
61 | virtual void Init(); | ||||
62 | virtual void CleanUp(); | ||||
63 | |||||
64 | private: | ||||
65 | // IPC::Channel::Listener implementation: | ||||
66 | virtual void OnMessageReceived(const IPC::Message& msg); | ||||
67 | virtual void OnChannelError(); | ||||
68 | |||||
69 | // The message loop used to run tasks on the thread that started this thread. | ||||
70 | MessageLoop* owner_loop_; | ||||
71 | |||||
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame^] | 72 | std::string channel_name_; |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 73 | scoped_ptr<IPC::SyncChannel> channel_; |
74 | |||||
75 | // Used only on the background render thread to implement message routing | ||||
76 | // functionality to the consumers of the ChildThread. | ||||
77 | MessageRouter router_; | ||||
78 | |||||
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 79 | Thread::Options options_; |
80 | |||||
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 81 | // Handles resource loads for this process. |
82 | // NOTE: this object lives on the owner thread. | ||||
83 | scoped_ptr<ResourceDispatcher> resource_dispatcher_; | ||||
84 | |||||
[email protected] | 51d70e0 | 2009-03-27 20:45:59 | [diff] [blame] | 85 | // If true, checks with the browser process before shutdown. This avoids race |
86 | // conditions if the process refcount is 0 but there's an IPC message inflight | ||||
87 | // that would addref it. | ||||
88 | bool check_with_browser_before_shutdown_; | ||||
89 | |||||
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 90 | DISALLOW_COPY_AND_ASSIGN(ChildThread); |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 91 | }; |
92 | |||||
93 | #endif // CHROME_COMMON_CHILD_THREAD_H_ |