OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 BASE_MP_CHILD_THREAD_H_ |
| 6 #define BASE_MP_CHILD_THREAD_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "base/mp/message_router.h" |
| 11 #include "ipc/ipc_sync_channel.h" |
| 12 #include "ipc/ipc_message.h" |
| 13 |
| 14 namespace IPC { |
| 15 class SyncMessageFilter; |
| 16 } |
| 17 |
| 18 namespace base { |
| 19 |
| 20 // The main thread of a child process derives from this class. |
| 21 class MpChildThread : public IPC::Channel::Listener, |
| 22 public IPC::Message::Sender { |
| 23 public: |
| 24 // Creates the thread. |
| 25 MpChildThread(); |
| 26 // Used for single-process mode. |
| 27 explicit MpChildThread(const std::string& channel_name); |
| 28 virtual ~MpChildThread(); |
| 29 |
| 30 // IPC::Message::Sender implementation: |
| 31 virtual bool Send(IPC::Message* msg); |
| 32 |
| 33 // IPC::Channel::Listener implementation: |
| 34 virtual void OnMessageReceived(const IPC::Message& msg); |
| 35 |
| 36 // See documentation on MessageRouter for AddRoute and RemoveRoute |
| 37 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); |
| 38 void RemoveRoute(int32 routing_id); |
| 39 |
| 40 IPC::Channel::Listener* ResolveRoute(int32 routing_id); |
| 41 |
| 42 // Safe to call on any thread, as long as it's guaranteed that the thread's |
| 43 // lifetime is less than the main thread. |
| 44 IPC::SyncMessageFilter* sync_message_filter() { return sync_message_filter_; } |
| 45 |
| 46 MessageLoop* message_loop() { return message_loop_; } |
| 47 |
| 48 // Returns the one child thread. |
| 49 static MpChildThread* current(); |
| 50 |
| 51 protected: |
| 52 friend class MpChildProcess; |
| 53 |
| 54 // Called when the process refcount is 0. |
| 55 void OnProcessFinalRelease(); |
| 56 |
| 57 virtual void OnControlMessageReceived(const IPC::Message& msg) { } |
| 58 virtual void OnAskBeforeShutdown(); |
| 59 virtual void OnShutdown(); |
| 60 |
| 61 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 62 virtual void OnSetIPCLoggingEnabled(bool enable); |
| 63 #endif |
| 64 |
| 65 IPC::SyncChannel* channel() { return channel_.get(); } |
| 66 |
| 67 void set_on_channel_error_called(bool on_channel_error_called) { |
| 68 on_channel_error_called_ = on_channel_error_called; |
| 69 } |
| 70 |
| 71 private: |
| 72 void Init(); |
| 73 |
| 74 // IPC::Channel::Listener implementation: |
| 75 virtual void OnChannelError(); |
| 76 |
| 77 std::string channel_name_; |
| 78 scoped_ptr<IPC::SyncChannel> channel_; |
| 79 |
| 80 // Allows threads other than the main thread to send sync messages. |
| 81 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_; |
| 82 |
| 83 // Implements message routing functionality to the consumers of MpChildThread. |
| 84 MessageRouter router_; |
| 85 |
| 86 // If true, checks with the parent process before shutdown. This avoids race |
| 87 // conditions if the process refcount is 0 but there's an IPC message inflight |
| 88 // that would addref it. |
| 89 bool check_with_parent_before_shutdown_; |
| 90 |
| 91 // The OnChannelError() callback was invoked - the channel is dead, don't |
| 92 // attempt to communicate. |
| 93 bool on_channel_error_called_; |
| 94 |
| 95 MessageLoop* message_loop_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(MpChildThread); |
| 98 }; |
| 99 |
| 100 } // namespace base |
| 101 |
| 102 #endif // BASE_MP_CHILD_THREAD_H_ |
OLD | NEW |