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 CHROME_COMMON_IPC_CHANNEL_PROXY_H__ | 5 #ifndef CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |
6 #define CHROME_COMMON_IPC_CHANNEL_PROXY_H__ | 6 #define CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 #include "base/lock.h" | 9 #include "base/lock.h" |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 // Used to intercept messages as they are received on the background thread. | 106 // Used to intercept messages as they are received on the background thread. |
107 // | 107 // |
108 // Ordinarily, messages sent to the ChannelProxy are routed to the matching | 108 // Ordinarily, messages sent to the ChannelProxy are routed to the matching |
109 // listener on the worker thread. This API allows code to intercept messages | 109 // listener on the worker thread. This API allows code to intercept messages |
110 // before they are sent to the worker thread. | 110 // before they are sent to the worker thread. |
111 void AddFilter(MessageFilter* filter); | 111 void AddFilter(MessageFilter* filter); |
112 void RemoveFilter(MessageFilter* filter); | 112 void RemoveFilter(MessageFilter* filter); |
113 | 113 |
114 protected: | 114 protected: |
115 Channel::Listener* listener() const { return context_->listener(); } | |
116 | |
117 class Context; | 115 class Context; |
118 // A subclass uses this constructor if it needs to add more information | 116 // A subclass uses this constructor if it needs to add more information |
119 // to the internal state. If create_pipe_now is true, the pipe is created | 117 // to the internal state. If create_pipe_now is true, the pipe is created |
120 // immediately. Otherwise it's created on the IO thread. | 118 // immediately. Otherwise it's created on the IO thread. |
121 ChannelProxy(const std::wstring& channel_id, Channel::Mode mode, | 119 ChannelProxy(const std::wstring& channel_id, Channel::Mode mode, |
122 MessageLoop* ipc_thread_loop, Context* context, | 120 MessageLoop* ipc_thread_loop, Context* context, |
123 bool create_pipe_now); | 121 bool create_pipe_now); |
124 | 122 |
125 // Used internally to hold state that is referenced on the IPC thread. | 123 // Used internally to hold state that is referenced on the IPC thread. |
126 class Context : public base::RefCountedThreadSafe<Context>, | 124 class Context : public base::RefCountedThreadSafe<Context>, |
127 public Channel::Listener { | 125 public Channel::Listener { |
128 public: | 126 public: |
129 Context(Channel::Listener* listener, MessageFilter* filter, | 127 Context(Channel::Listener* listener, MessageFilter* filter, |
130 MessageLoop* ipc_thread); | 128 MessageLoop* ipc_thread); |
131 virtual ~Context() { } | 129 virtual ~Context() { } |
| 130 MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } |
132 | 131 |
133 protected: | 132 protected: |
134 // IPC::Channel::Listener methods: | 133 // IPC::Channel::Listener methods: |
135 virtual void OnMessageReceived(const Message& message); | 134 virtual void OnMessageReceived(const Message& message); |
136 virtual void OnChannelConnected(int32 peer_pid); | 135 virtual void OnChannelConnected(int32 peer_pid); |
137 virtual void OnChannelError(); | 136 virtual void OnChannelError(); |
138 | 137 |
| 138 // Like OnMessageReceived but doesn't try the filters. |
| 139 void OnMessageReceivedNoFilter(const Message& message); |
| 140 |
139 Channel::Listener* listener() const { return listener_; } | 141 Channel::Listener* listener() const { return listener_; } |
140 const std::wstring& channel_id() const { return channel_id_; } | 142 const std::wstring& channel_id() const { return channel_id_; } |
141 | 143 |
142 // Gives the filters a chance at processing |message|. | 144 // Gives the filters a chance at processing |message|. |
143 // Returns true if the message was processed, false otherwise. | 145 // Returns true if the message was processed, false otherwise. |
144 bool TryFilters(const Message& message); | 146 bool TryFilters(const Message& message); |
145 | 147 |
| 148 // Like Open and Close, but called on the IPC thread. |
| 149 virtual void OnChannelOpened(); |
| 150 virtual void OnChannelClosed(); |
| 151 |
| 152 // Called on the consumers thread when the ChannelProxy is closed. At that |
| 153 // point the consumer is telling us that they don't want to receive any |
| 154 // more messages, so we honor that wish by forgetting them! |
| 155 virtual void Clear() { listener_ = NULL; } |
| 156 |
146 private: | 157 private: |
147 friend class ChannelProxy; | 158 friend class ChannelProxy; |
148 // Create the Channel | 159 // Create the Channel |
149 void CreateChannel(const std::wstring& id, const Channel::Mode& mode); | 160 void CreateChannel(const std::wstring& id, const Channel::Mode& mode); |
150 | 161 |
151 // Methods called via InvokeLater: | 162 // Methods called via InvokeLater: |
152 void OnOpenChannel(); | |
153 void OnCloseChannel(); | |
154 void OnSendMessage(Message* message_ptr); | 163 void OnSendMessage(Message* message_ptr); |
155 void OnAddFilter(MessageFilter* filter); | 164 void OnAddFilter(MessageFilter* filter); |
156 void OnRemoveFilter(MessageFilter* filter); | 165 void OnRemoveFilter(MessageFilter* filter); |
157 void OnDispatchMessage(const Message& message); | 166 void OnDispatchMessage(const Message& message); |
158 void OnDispatchConnected(int32 peer_pid); | 167 void OnDispatchConnected(int32 peer_pid); |
159 void OnDispatchError(); | 168 void OnDispatchError(); |
160 | 169 |
161 MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } | |
162 | |
163 // Called on the consumers thread when the ChannelProxy is closed. At that | |
164 // point the consumer is telling us that they don't want to receive any | |
165 // more messages, so we honor that wish by forgetting them! | |
166 void clear() { listener_ = NULL; } | |
167 | |
168 MessageLoop* listener_message_loop_; | 170 MessageLoop* listener_message_loop_; |
169 Channel::Listener* listener_; | 171 Channel::Listener* listener_; |
170 | 172 |
171 // List of filters. This is only accessed on the IPC thread. | 173 // List of filters. This is only accessed on the IPC thread. |
172 std::vector<scoped_refptr<MessageFilter>> filters_; | 174 std::vector<scoped_refptr<MessageFilter>> filters_; |
173 MessageLoop* ipc_message_loop_; | 175 MessageLoop* ipc_message_loop_; |
174 Channel* channel_; | 176 Channel* channel_; |
175 std::wstring channel_id_; | 177 std::wstring channel_id_; |
176 }; | 178 }; |
177 | 179 |
178 Context* context() { return context_; } | 180 Context* context() { return context_; } |
179 | 181 |
180 private: | 182 private: |
181 void Init(const std::wstring& channel_id, Channel::Mode mode, | 183 void Init(const std::wstring& channel_id, Channel::Mode mode, |
182 MessageLoop* ipc_thread_loop, bool create_pipe_now); | 184 MessageLoop* ipc_thread_loop, bool create_pipe_now); |
183 | 185 |
184 // By maintaining this indirection (ref-counted) to our internal state, we | 186 // By maintaining this indirection (ref-counted) to our internal state, we |
185 // can safely be destroyed while the background thread continues to do stuff | 187 // can safely be destroyed while the background thread continues to do stuff |
186 // that involves this data. | 188 // that involves this data. |
187 scoped_refptr<Context> context_; | 189 scoped_refptr<Context> context_; |
188 }; | 190 }; |
189 | 191 |
190 } // namespace IPC | 192 } // namespace IPC |
191 | 193 |
192 #endif // CHROME_COMMON_IPC_CHANNEL_PROXY_H__ | 194 #endif // CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |
193 | 195 |
OLD | NEW |