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