| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop_proxy.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_channel_handle.h" | 16 #include "ipc/ipc_channel_handle.h" |
| 16 | 17 |
| 17 class MessageLoop; | |
| 18 | |
| 19 namespace IPC { | 18 namespace IPC { |
| 20 | 19 |
| 21 class SendTask; | 20 class SendTask; |
| 22 | 21 |
| 23 //----------------------------------------------------------------------------- | 22 //----------------------------------------------------------------------------- |
| 24 // IPC::ChannelProxy | 23 // IPC::ChannelProxy |
| 25 // | 24 // |
| 26 // This class is a helper class that is useful when you wish to run an IPC | 25 // This class is a helper class that is useful when you wish to run an IPC |
| 27 // channel on a background thread. It provides you with the option of either | 26 // channel on a background thread. It provides you with the option of either |
| 28 // handling IPC messages on that background thread or having them dispatched to | 27 // handling IPC messages on that background thread or having them dispatched to |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // 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 |
| 104 // the thread that creates the ChannelProxy. The filter's OnMessageReceived | 103 // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
| 105 // 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 |
| 106 // 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 |
| 107 // 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 |
| 108 // dispatched to the listener. The given message loop indicates where the | 107 // dispatched to the listener. The given message loop indicates where the |
| 109 // IPC::Channel should be created. | 108 // IPC::Channel should be created. |
| 110 ChannelProxy(const IPC::ChannelHandle& channel_handle, | 109 ChannelProxy(const IPC::ChannelHandle& channel_handle, |
| 111 Channel::Mode mode, | 110 Channel::Mode mode, |
| 112 Channel::Listener* listener, | 111 Channel::Listener* listener, |
| 113 MessageLoop* ipc_thread_loop); | 112 base::MessageLoopProxy* ipc_thread_loop); |
| 114 | 113 |
| 115 virtual ~ChannelProxy(); | 114 virtual ~ChannelProxy(); |
| 116 | 115 |
| 117 // Close the IPC::Channel. This operation completes asynchronously, once the | 116 // Close the IPC::Channel. This operation completes asynchronously, once the |
| 118 // background thread processes the command to close the channel. It is ok to | 117 // background thread processes the command to close the channel. It is ok to |
| 119 // call this method multiple times. Redundant calls are ignored. | 118 // call this method multiple times. Redundant calls are ignored. |
| 120 // | 119 // |
| 121 // WARNING: The MessageFilter object held by the ChannelProxy is also | 120 // WARNING: The MessageFilter object held by the ChannelProxy is also |
| 122 // released asynchronously, and it may in fact have its final reference | 121 // released asynchronously, and it may in fact have its final reference |
| 123 // released on the background thread. The caller should be careful to deal | 122 // released on the background thread. The caller should be careful to deal |
| (...skipping 25 matching lines...) Expand all Loading... |
| 149 bool GetClientEuid(uid_t* client_euid) const; | 148 bool GetClientEuid(uid_t* client_euid) const; |
| 150 #endif // defined(OS_POSIX) | 149 #endif // defined(OS_POSIX) |
| 151 | 150 |
| 152 protected: | 151 protected: |
| 153 class Context; | 152 class Context; |
| 154 // A subclass uses this constructor if it needs to add more information | 153 // A subclass uses this constructor if it needs to add more information |
| 155 // to the internal state. If create_pipe_now is true, the pipe is created | 154 // to the internal state. If create_pipe_now is true, the pipe is created |
| 156 // immediately. Otherwise it's created on the IO thread. | 155 // immediately. Otherwise it's created on the IO thread. |
| 157 ChannelProxy(const IPC::ChannelHandle& channel_handle, | 156 ChannelProxy(const IPC::ChannelHandle& channel_handle, |
| 158 Channel::Mode mode, | 157 Channel::Mode mode, |
| 159 MessageLoop* ipc_thread_loop, | 158 base::MessageLoopProxy* ipc_thread_loop, |
| 160 Context* context, | 159 Context* context, |
| 161 bool create_pipe_now); | 160 bool create_pipe_now); |
| 162 | 161 |
| 163 // Used internally to hold state that is referenced on the IPC thread. | 162 // Used internally to hold state that is referenced on the IPC thread. |
| 164 class Context : public base::RefCountedThreadSafe<Context>, | 163 class Context : public base::RefCountedThreadSafe<Context>, |
| 165 public Channel::Listener { | 164 public Channel::Listener { |
| 166 public: | 165 public: |
| 167 Context(Channel::Listener* listener, MessageLoop* ipc_thread); | 166 Context(Channel::Listener* listener, base::MessageLoopProxy* ipc_thread); |
| 168 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } | 167 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } |
| 169 MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } | 168 base::MessageLoopProxy* ipc_message_loop() const { |
| 169 return ipc_message_loop_.get(); |
| 170 } |
| 170 const std::string& channel_id() const { return channel_id_; } | 171 const std::string& channel_id() const { return channel_id_; } |
| 171 | 172 |
| 172 // Dispatches a message on the listener thread. | 173 // Dispatches a message on the listener thread. |
| 173 void OnDispatchMessage(const Message& message); | 174 void OnDispatchMessage(const Message& message); |
| 174 | 175 |
| 175 protected: | 176 protected: |
| 176 friend class base::RefCountedThreadSafe<Context>; | 177 friend class base::RefCountedThreadSafe<Context>; |
| 177 virtual ~Context() { } | 178 virtual ~Context(); |
| 178 | 179 |
| 179 // IPC::Channel::Listener methods: | 180 // IPC::Channel::Listener methods: |
| 180 virtual bool OnMessageReceived(const Message& message); | 181 virtual bool OnMessageReceived(const Message& message); |
| 181 virtual void OnChannelConnected(int32 peer_pid); | 182 virtual void OnChannelConnected(int32 peer_pid); |
| 182 virtual void OnChannelError(); | 183 virtual void OnChannelError(); |
| 183 | 184 |
| 184 // Like OnMessageReceived but doesn't try the filters. | 185 // Like OnMessageReceived but doesn't try the filters. |
| 185 bool OnMessageReceivedNoFilter(const Message& message); | 186 bool OnMessageReceivedNoFilter(const Message& message); |
| 186 | 187 |
| 187 // Gives the filters a chance at processing |message|. | 188 // Gives the filters a chance at processing |message|. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 208 // Methods called on the IO thread. | 209 // Methods called on the IO thread. |
| 209 void OnSendMessage(Message* message_ptr); | 210 void OnSendMessage(Message* message_ptr); |
| 210 void OnAddFilter(); | 211 void OnAddFilter(); |
| 211 void OnRemoveFilter(MessageFilter* filter); | 212 void OnRemoveFilter(MessageFilter* filter); |
| 212 | 213 |
| 213 // Methods called on the listener thread. | 214 // Methods called on the listener thread. |
| 214 void AddFilter(MessageFilter* filter); | 215 void AddFilter(MessageFilter* filter); |
| 215 void OnDispatchConnected(); | 216 void OnDispatchConnected(); |
| 216 void OnDispatchError(); | 217 void OnDispatchError(); |
| 217 | 218 |
| 218 MessageLoop* listener_message_loop_; | 219 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; |
| 219 Channel::Listener* listener_; | 220 Channel::Listener* listener_; |
| 220 | 221 |
| 221 // List of filters. This is only accessed on the IPC thread. | 222 // List of filters. This is only accessed on the IPC thread. |
| 222 std::vector<scoped_refptr<MessageFilter> > filters_; | 223 std::vector<scoped_refptr<MessageFilter> > filters_; |
| 223 MessageLoop* ipc_message_loop_; | 224 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; |
| 224 scoped_ptr<Channel> channel_; | 225 scoped_ptr<Channel> channel_; |
| 225 std::string channel_id_; | 226 std::string channel_id_; |
| 226 int peer_pid_; | 227 int peer_pid_; |
| 227 bool channel_connected_called_; | 228 bool channel_connected_called_; |
| 228 | 229 |
| 229 // Holds filters between the AddFilter call on the listerner thread and the | 230 // Holds filters between the AddFilter call on the listerner thread and the |
| 230 // IPC thread when they're added to filters_. | 231 // IPC thread when they're added to filters_. |
| 231 std::vector<scoped_refptr<MessageFilter> > pending_filters_; | 232 std::vector<scoped_refptr<MessageFilter> > pending_filters_; |
| 232 // Lock for pending_filters_. | 233 // Lock for pending_filters_. |
| 233 base::Lock pending_filters_lock_; | 234 base::Lock pending_filters_lock_; |
| 234 }; | 235 }; |
| 235 | 236 |
| 236 Context* context() { return context_; } | 237 Context* context() { return context_; } |
| 237 | 238 |
| 238 private: | 239 private: |
| 239 friend class SendTask; | 240 friend class SendTask; |
| 240 | 241 |
| 241 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, | 242 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, |
| 242 MessageLoop* ipc_thread_loop, bool create_pipe_now); | 243 base::MessageLoopProxy* ipc_thread_loop, bool create_pipe_now); |
| 243 | 244 |
| 244 // By maintaining this indirection (ref-counted) to our internal state, we | 245 // By maintaining this indirection (ref-counted) to our internal state, we |
| 245 // can safely be destroyed while the background thread continues to do stuff | 246 // can safely be destroyed while the background thread continues to do stuff |
| 246 // that involves this data. | 247 // that involves this data. |
| 247 scoped_refptr<Context> context_; | 248 scoped_refptr<Context> context_; |
| 248 }; | 249 }; |
| 249 | 250 |
| 250 } // namespace IPC | 251 } // namespace IPC |
| 251 | 252 |
| 252 #endif // IPC_IPC_CHANNEL_PROXY_H__ | 253 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
| OLD | NEW |