Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 IPC_IPC_CHANNEL_PROXY_H__ | 5 #ifndef IPC_IPC_CHANNEL_PROXY_H__ |
| 6 #define IPC_IPC_CHANNEL_PROXY_H__ | 6 #define IPC_IPC_CHANNEL_PROXY_H__ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 // messages on this background thread instead of on their own thread, which may | 40 // messages on this background thread instead of on their own thread, which may |
| 41 // be bogged down with other processing. The result can be greatly improved | 41 // be bogged down with other processing. The result can be greatly improved |
| 42 // latency for messages that can be handled on a background thread. | 42 // latency for messages that can be handled on a background thread. |
| 43 // | 43 // |
| 44 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread | 44 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread |
| 45 // instance where the IPC::Channel will be created and operated. | 45 // instance where the IPC::Channel will be created and operated. |
| 46 // | 46 // |
| 47 class ChannelProxy : public Message::Sender { | 47 class ChannelProxy : public Message::Sender { |
| 48 public: | 48 public: |
| 49 | 49 |
| 50 class MessageFilter; | 50 struct MessageFilterTraits; |
| 51 struct MessageFilterTraits { | |
| 52 static void Destruct(MessageFilter* filter); | |
| 53 }; | |
| 54 | 51 |
| 55 // A class that receives messages on the thread where the IPC channel is | 52 // A class that receives messages on the thread where the IPC channel is |
| 56 // running. It can choose to prevent the default action for an IPC message. | 53 // running. It can choose to prevent the default action for an IPC message. |
| 57 class MessageFilter | 54 class MessageFilter |
| 58 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { | 55 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { |
| 59 public: | 56 public: |
| 60 virtual ~MessageFilter() {} | 57 virtual ~MessageFilter() {} |
| 61 | 58 |
| 62 // Called on the background thread to provide the filter with access to the | 59 // Called on the background thread to provide the filter with access to the |
| 63 // channel. Called when the IPC channel is initialized or when AddFilter | 60 // channel. Called when the IPC channel is initialized or when AddFilter |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 88 } | 85 } |
| 89 | 86 |
| 90 // Called when the message filter is about to be deleted. This gives | 87 // Called when the message filter is about to be deleted. This gives |
| 91 // derived classes the option of controlling which thread they're deleted | 88 // derived classes the option of controlling which thread they're deleted |
| 92 // on etc. | 89 // on etc. |
| 93 virtual void OnDestruct() { | 90 virtual void OnDestruct() { |
| 94 delete this; | 91 delete this; |
| 95 } | 92 } |
| 96 }; | 93 }; |
| 97 | 94 |
| 95 struct MessageFilterTraits { | |
| 96 static void Destruct(MessageFilter* filter) { | |
| 97 filter->OnDestruct(); | |
| 98 } | |
| 99 }; | |
| 100 | |
| 98 // Initializes a channel proxy. The channel_id and mode parameters are | 101 // Initializes a channel proxy. The channel_id and mode parameters are |
| 99 // passed directly to the underlying IPC::Channel. The listener is called on | 102 // passed directly to the underlying IPC::Channel. The listener is called on |
| 100 // the thread that creates the ChannelProxy. The filter's OnMessageReceived | 103 // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
| 101 // method is called on the thread where the IPC::Channel is running. The | 104 // method is called on the thread where the IPC::Channel is running. The |
| 102 // filter may be null if the consumer is not interested in handling messages | 105 // filter may be null if the consumer is not interested in handling messages |
| 103 // on the background thread. Any message not handled by the filter will be | 106 // on the background thread. Any message not handled by the filter will be |
| 104 // dispatched to the listener. The given message loop indicates where the | 107 // dispatched to the listener. The given message loop indicates where the |
| 105 // IPC::Channel should be created. | 108 // IPC::Channel should be created. |
| 106 ChannelProxy(const std::string& channel_id, Channel::Mode mode, | 109 ChannelProxy(const std::string& channel_id, Channel::Mode mode, |
| 107 Channel::Listener* listener, MessageFilter* filter, | 110 Channel::Listener* listener, MessageFilter* filter, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 ChannelProxy(const std::string& channel_id, Channel::Mode mode, | 154 ChannelProxy(const std::string& channel_id, Channel::Mode mode, |
| 152 MessageLoop* ipc_thread_loop, Context* context, | 155 MessageLoop* ipc_thread_loop, Context* context, |
| 153 bool create_pipe_now); | 156 bool create_pipe_now); |
| 154 | 157 |
| 155 // Used internally to hold state that is referenced on the IPC thread. | 158 // Used internally to hold state that is referenced on the IPC thread. |
| 156 class Context : public base::RefCountedThreadSafe<Context>, | 159 class Context : public base::RefCountedThreadSafe<Context>, |
| 157 public Channel::Listener { | 160 public Channel::Listener { |
| 158 public: | 161 public: |
| 159 Context(Channel::Listener* listener, MessageFilter* filter, | 162 Context(Channel::Listener* listener, MessageFilter* filter, |
| 160 MessageLoop* ipc_thread); | 163 MessageLoop* ipc_thread); |
| 161 virtual ~Context() { } | |
| 162 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } | 164 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } |
| 163 MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } | 165 MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } |
| 164 const std::string& channel_id() const { return channel_id_; } | 166 const std::string& channel_id() const { return channel_id_; } |
| 165 | 167 |
| 166 // Dispatches a message on the listener thread. | 168 // Dispatches a message on the listener thread. |
| 167 void OnDispatchMessage(const Message& message); | 169 void OnDispatchMessage(const Message& message); |
| 168 | 170 |
| 169 protected: | 171 protected: |
| 172 friend class base::RefCountedThreadSafe<Context>; | |
|
M-A Ruel
2009/11/05 20:31:28
protected?
jam
2009/11/05 21:52:37
yeah, because SyncContext derives from it.
| |
| 173 virtual ~Context() { } | |
| 174 | |
| 170 // IPC::Channel::Listener methods: | 175 // IPC::Channel::Listener methods: |
| 171 virtual void OnMessageReceived(const Message& message); | 176 virtual void OnMessageReceived(const Message& message); |
| 172 virtual void OnChannelConnected(int32 peer_pid); | 177 virtual void OnChannelConnected(int32 peer_pid); |
| 173 virtual void OnChannelError(); | 178 virtual void OnChannelError(); |
| 174 | 179 |
| 175 // Like OnMessageReceived but doesn't try the filters. | 180 // Like OnMessageReceived but doesn't try the filters. |
| 176 void OnMessageReceivedNoFilter(const Message& message); | 181 void OnMessageReceivedNoFilter(const Message& message); |
| 177 | 182 |
| 178 // Gives the filters a chance at processing |message|. | 183 // Gives the filters a chance at processing |message|. |
| 179 // Returns true if the message was processed, false otherwise. | 184 // Returns true if the message was processed, false otherwise. |
| 180 bool TryFilters(const Message& message); | 185 bool TryFilters(const Message& message); |
| 181 | 186 |
| 182 // Like Open and Close, but called on the IPC thread. | 187 // Like Open and Close, but called on the IPC thread. |
| 183 virtual void OnChannelOpened(); | 188 virtual void OnChannelOpened(); |
| 184 virtual void OnChannelClosed(); | 189 virtual void OnChannelClosed(); |
| 185 | 190 |
| 186 // Called on the consumers thread when the ChannelProxy is closed. At that | 191 // Called on the consumers thread when the ChannelProxy is closed. At that |
| 187 // point the consumer is telling us that they don't want to receive any | 192 // point the consumer is telling us that they don't want to receive any |
| 188 // more messages, so we honor that wish by forgetting them! | 193 // more messages, so we honor that wish by forgetting them! |
| 189 virtual void Clear() { listener_ = NULL; } | 194 virtual void Clear() { listener_ = NULL; } |
| 190 | 195 |
| 191 private: | 196 private: |
| 192 friend class ChannelProxy; | 197 friend class ChannelProxy; |
| 193 friend class SendTask; | 198 friend class SendTask; |
| 199 | |
| 194 // Create the Channel | 200 // Create the Channel |
| 195 void CreateChannel(const std::string& id, const Channel::Mode& mode); | 201 void CreateChannel(const std::string& id, const Channel::Mode& mode); |
| 196 | 202 |
| 197 // Methods called via InvokeLater: | 203 // Methods called via InvokeLater: |
| 198 void OnSendMessage(Message* message_ptr); | 204 void OnSendMessage(Message* message_ptr); |
| 199 void OnAddFilter(MessageFilter* filter); | 205 void OnAddFilter(MessageFilter* filter); |
| 200 void OnRemoveFilter(MessageFilter* filter); | 206 void OnRemoveFilter(MessageFilter* filter); |
| 201 void OnDispatchConnected(); | 207 void OnDispatchConnected(); |
| 202 void OnDispatchError(); | 208 void OnDispatchError(); |
| 203 | 209 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 223 | 229 |
| 224 // By maintaining this indirection (ref-counted) to our internal state, we | 230 // By maintaining this indirection (ref-counted) to our internal state, we |
| 225 // can safely be destroyed while the background thread continues to do stuff | 231 // can safely be destroyed while the background thread continues to do stuff |
| 226 // that involves this data. | 232 // that involves this data. |
| 227 scoped_refptr<Context> context_; | 233 scoped_refptr<Context> context_; |
| 228 }; | 234 }; |
| 229 | 235 |
| 230 } // namespace IPC | 236 } // namespace IPC |
| 231 | 237 |
| 232 #endif // IPC_IPC_CHANNEL_PROXY_H__ | 238 #endif // IPC_IPC_CHANNEL_PROXY_H__ |
| OLD | NEW |