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