OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "ipc/ipc_channel_proxy.h" | 13 #include "ipc/ipc_channel_proxy.h" |
14 #include "ipc/ipc_listener.h" | 14 #include "ipc/ipc_listener.h" |
15 #include "ipc/ipc_logging.h" | 15 #include "ipc/ipc_logging.h" |
16 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
17 #include "ipc/ipc_message_utils.h" | 17 #include "ipc/ipc_message_utils.h" |
18 | 18 |
19 namespace IPC { | 19 namespace IPC { |
20 namespace { | |
21 bool EraseFilter(const ChannelProxy::MessageFilter* filter, | |
22 std::vector<ChannelProxy::MessageFilter*>& filters) { | |
23 std::vector<ChannelProxy::MessageFilter*>::iterator it = | |
24 std::find(filters.begin(), filters.end(), filter); | |
25 if (it == filters.end()) | |
26 return false; | |
27 | |
28 filters.erase(it); | |
29 return true; | |
30 } | |
31 | |
32 bool ApplyFilters(std::vector<ChannelProxy::MessageFilter*>& filters, | |
33 const IPC::Message& message, | |
34 const std::string& channel_id) { | |
35 #ifdef IPC_MESSAGE_LOG_ENABLED | |
36 Logging* logger = Logging::GetInstance(); | |
37 #endif | |
38 | |
39 for (size_t i = 0; i < filters.size(); ++i) { | |
40 if (filters[i]->OnMessageReceived(message)) { | |
41 #ifdef IPC_MESSAGE_LOG_ENABLED | |
42 if (logger->Enabled()) | |
43 logger->OnPostDispatchMessage(message, channel_id); | |
44 #endif | |
45 return true; | |
46 } | |
47 } | |
48 return false; | |
49 } | |
50 | |
51 } // namespace | |
20 | 52 |
21 //------------------------------------------------------------------------------ | 53 //------------------------------------------------------------------------------ |
22 | 54 |
23 ChannelProxy::MessageFilter::MessageFilter() {} | 55 ChannelProxy::MessageFilter::MessageFilter() {} |
24 | 56 |
25 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {} | 57 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {} |
26 | 58 |
27 void ChannelProxy::MessageFilter::OnFilterRemoved() {} | 59 void ChannelProxy::MessageFilter::OnFilterRemoved() {} |
28 | 60 |
29 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {} | 61 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {} |
30 | 62 |
31 void ChannelProxy::MessageFilter::OnChannelError() {} | 63 void ChannelProxy::MessageFilter::OnChannelError() {} |
32 | 64 |
33 void ChannelProxy::MessageFilter::OnChannelClosing() {} | 65 void ChannelProxy::MessageFilter::OnChannelClosing() {} |
34 | 66 |
35 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) { | 67 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) { |
36 return false; | 68 return false; |
37 } | 69 } |
38 | 70 |
71 bool ChannelProxy::MessageFilter::GetSupportedMessageClasses( | |
72 std::vector<uint32>* /*supported_message_classes*/) const { | |
73 return false; | |
74 } | |
75 | |
39 ChannelProxy::MessageFilter::~MessageFilter() {} | 76 ChannelProxy::MessageFilter::~MessageFilter() {} |
40 | 77 |
41 //------------------------------------------------------------------------------ | 78 //------------------------------------------------------------------------------ |
42 | 79 |
43 ChannelProxy::Context::Context(Listener* listener, | 80 ChannelProxy::Context::Context(Listener* listener, |
44 base::SingleThreadTaskRunner* ipc_task_runner) | 81 base::SingleThreadTaskRunner* ipc_task_runner) |
45 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 82 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
46 listener_(listener), | 83 listener_(listener), |
47 ipc_task_runner_(ipc_task_runner), | 84 ipc_task_runner_(ipc_task_runner), |
48 channel_connected_called_(false), | 85 channel_connected_called_(false), |
(...skipping 15 matching lines...) Expand all Loading... | |
64 channel_.reset(new Channel(handle, mode, this)); | 101 channel_.reset(new Channel(handle, mode, this)); |
65 } | 102 } |
66 | 103 |
67 bool ChannelProxy::Context::TryFilters(const Message& message) { | 104 bool ChannelProxy::Context::TryFilters(const Message& message) { |
68 #ifdef IPC_MESSAGE_LOG_ENABLED | 105 #ifdef IPC_MESSAGE_LOG_ENABLED |
69 Logging* logger = Logging::GetInstance(); | 106 Logging* logger = Logging::GetInstance(); |
70 if (logger->Enabled()) | 107 if (logger->Enabled()) |
71 logger->OnPreDispatchMessage(message); | 108 logger->OnPreDispatchMessage(message); |
72 #endif | 109 #endif |
73 | 110 |
74 for (size_t i = 0; i < filters_.size(); ++i) { | 111 if (ApplyFilters(message_global_filters_, message, channel_id_)) |
75 if (filters_[i]->OnMessageReceived(message)) { | 112 return true; |
76 #ifdef IPC_MESSAGE_LOG_ENABLED | 113 |
77 if (logger->Enabled()) | 114 MessageClassFilterMap::iterator message_class_filter_it = |
78 logger->OnPostDispatchMessage(message, channel_id_); | 115 message_class_filters_.find(IPC_MESSAGE_CLASS(message)); |
79 #endif | 116 if (message_class_filter_it != message_class_filters_.end() && |
80 return true; | 117 ApplyFilters(message_class_filter_it->second, message, channel_id_)) { |
81 } | 118 return true; |
82 } | 119 } |
piman
2014/02/11 21:48:46
So, there is an assumption that filters are run in
jdduke (slow)
2014/02/11 23:01:48
Good call. I *think* we're OK in our current form
| |
120 | |
83 return false; | 121 return false; |
84 } | 122 } |
85 | 123 |
86 // Called on the IPC::Channel thread | 124 // Called on the IPC::Channel thread |
87 bool ChannelProxy::Context::OnMessageReceived(const Message& message) { | 125 bool ChannelProxy::Context::OnMessageReceived(const Message& message) { |
88 // First give a chance to the filters to process this message. | 126 // First give a chance to the filters to process this message. |
89 if (!TryFilters(message)) | 127 if (!TryFilters(message)) |
90 OnMessageReceivedNoFilter(message); | 128 OnMessageReceivedNoFilter(message); |
91 return true; | 129 return true; |
92 } | 130 } |
93 | 131 |
94 // Called on the IPC::Channel thread | 132 // Called on the IPC::Channel thread |
95 bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { | 133 bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { |
96 listener_task_runner_->PostTask( | 134 listener_task_runner_->PostTask( |
97 FROM_HERE, base::Bind(&Context::OnDispatchMessage, this, message)); | 135 FROM_HERE, base::Bind(&Context::OnDispatchMessage, this, message)); |
98 return true; | 136 return true; |
99 } | 137 } |
100 | 138 |
101 // Called on the IPC::Channel thread | 139 // Called on the IPC::Channel thread |
102 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { | 140 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { |
103 // Add any pending filters. This avoids a race condition where someone | 141 // Add any pending filters. This avoids a race condition where someone |
104 // creates a ChannelProxy, calls AddFilter, and then right after starts the | 142 // creates a ChannelProxy, calls AddFilter, and then right after starts the |
105 // peer process. The IO thread could receive a message before the task to add | 143 // peer process. The IO thread could receive a message before the task to add |
106 // the filter is run on the IO thread. | 144 // the filter is run on the IO thread. |
107 OnAddFilter(); | 145 OnAddFilter(); |
108 | 146 |
109 // We cache off the peer_pid so it can be safely accessed from both threads. | 147 // We cache off the peer_pid so it can be safely accessed from both threads. |
110 peer_pid_ = channel_->peer_pid(); | 148 peer_pid_ = channel_->peer_pid(); |
111 for (size_t i = 0; i < filters_.size(); ++i) | 149 for (size_t i = 0; i < all_filters_.size(); ++i) |
112 filters_[i]->OnChannelConnected(peer_pid); | 150 all_filters_[i]->OnChannelConnected(peer_pid); |
113 | 151 |
114 // See above comment about using listener_task_runner_ here. | 152 // See above comment about using listener_task_runner_ here. |
115 listener_task_runner_->PostTask( | 153 listener_task_runner_->PostTask( |
116 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this)); | 154 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this)); |
117 } | 155 } |
118 | 156 |
119 // Called on the IPC::Channel thread | 157 // Called on the IPC::Channel thread |
120 void ChannelProxy::Context::OnChannelError() { | 158 void ChannelProxy::Context::OnChannelError() { |
121 for (size_t i = 0; i < filters_.size(); ++i) | 159 for (size_t i = 0; i < all_filters_.size(); ++i) |
122 filters_[i]->OnChannelError(); | 160 all_filters_[i]->OnChannelError(); |
123 | 161 |
124 // See above comment about using listener_task_runner_ here. | 162 // See above comment about using listener_task_runner_ here. |
125 listener_task_runner_->PostTask( | 163 listener_task_runner_->PostTask( |
126 FROM_HERE, base::Bind(&Context::OnDispatchError, this)); | 164 FROM_HERE, base::Bind(&Context::OnDispatchError, this)); |
127 } | 165 } |
128 | 166 |
129 // Called on the IPC::Channel thread | 167 // Called on the IPC::Channel thread |
130 void ChannelProxy::Context::OnChannelOpened() { | 168 void ChannelProxy::Context::OnChannelOpened() { |
131 DCHECK(channel_ != NULL); | 169 DCHECK(channel_ != NULL); |
132 | 170 |
133 // Assume a reference to ourselves on behalf of this thread. This reference | 171 // Assume a reference to ourselves on behalf of this thread. This reference |
134 // will be released when we are closed. | 172 // will be released when we are closed. |
135 AddRef(); | 173 AddRef(); |
136 | 174 |
137 if (!channel_->Connect()) { | 175 if (!channel_->Connect()) { |
138 OnChannelError(); | 176 OnChannelError(); |
139 return; | 177 return; |
140 } | 178 } |
141 | 179 |
142 for (size_t i = 0; i < filters_.size(); ++i) | 180 for (size_t i = 0; i < all_filters_.size(); ++i) |
143 filters_[i]->OnFilterAdded(channel_.get()); | 181 all_filters_[i]->OnFilterAdded(channel_.get()); |
144 } | 182 } |
145 | 183 |
146 // Called on the IPC::Channel thread | 184 // Called on the IPC::Channel thread |
147 void ChannelProxy::Context::OnChannelClosed() { | 185 void ChannelProxy::Context::OnChannelClosed() { |
148 // It's okay for IPC::ChannelProxy::Close to be called more than once, which | 186 // It's okay for IPC::ChannelProxy::Close to be called more than once, which |
149 // would result in this branch being taken. | 187 // would result in this branch being taken. |
150 if (!channel_.get()) | 188 if (!channel_.get()) |
151 return; | 189 return; |
152 | 190 |
153 for (size_t i = 0; i < filters_.size(); ++i) { | 191 for (size_t i = 0; i < all_filters_.size(); ++i) { |
154 filters_[i]->OnChannelClosing(); | 192 all_filters_[i]->OnChannelClosing(); |
155 filters_[i]->OnFilterRemoved(); | 193 all_filters_[i]->OnFilterRemoved(); |
156 } | 194 } |
157 | 195 |
158 // We don't need the filters anymore. | 196 // We don't need the filters anymore. |
159 filters_.clear(); | 197 all_filters_.clear(); |
198 message_global_filters_.clear(); | |
199 message_class_filters_.clear(); | |
160 | 200 |
161 channel_.reset(); | 201 channel_.reset(); |
162 | 202 |
163 // Balance with the reference taken during startup. This may result in | 203 // Balance with the reference taken during startup. This may result in |
164 // self-destruction. | 204 // self-destruction. |
165 Release(); | 205 Release(); |
166 } | 206 } |
167 | 207 |
168 void ChannelProxy::Context::Clear() { | 208 void ChannelProxy::Context::Clear() { |
169 listener_ = NULL; | 209 listener_ = NULL; |
(...skipping 10 matching lines...) Expand all Loading... | |
180 } | 220 } |
181 | 221 |
182 // Called on the IPC::Channel thread | 222 // Called on the IPC::Channel thread |
183 void ChannelProxy::Context::OnAddFilter() { | 223 void ChannelProxy::Context::OnAddFilter() { |
184 std::vector<scoped_refptr<MessageFilter> > new_filters; | 224 std::vector<scoped_refptr<MessageFilter> > new_filters; |
185 { | 225 { |
186 base::AutoLock auto_lock(pending_filters_lock_); | 226 base::AutoLock auto_lock(pending_filters_lock_); |
187 new_filters.swap(pending_filters_); | 227 new_filters.swap(pending_filters_); |
188 } | 228 } |
189 | 229 |
230 std::vector<uint32> supported_message_classes; | |
190 for (size_t i = 0; i < new_filters.size(); ++i) { | 231 for (size_t i = 0; i < new_filters.size(); ++i) { |
191 filters_.push_back(new_filters[i]); | 232 all_filters_.push_back(new_filters[i]); |
233 | |
234 // Determine if the filter should be applied to all messages, or only | |
235 // messages of a certain class. | |
236 MessageFilter* filter = new_filters[i].get(); | |
237 if (filter->GetSupportedMessageClasses(&supported_message_classes)) { | |
238 DCHECK(!supported_message_classes.empty()); | |
239 for (size_t i = 0; i < supported_message_classes.size(); ++i) | |
240 message_class_filters_[supported_message_classes[i]].push_back(filter); | |
241 } else { | |
242 message_global_filters_.push_back(filter); | |
243 } | |
244 supported_message_classes.clear(); | |
192 | 245 |
193 // If the channel has already been created, then we need to send this | 246 // If the channel has already been created, then we need to send this |
194 // message so that the filter gets access to the Channel. | 247 // message so that the filter gets access to the Channel. |
195 if (channel_.get()) | 248 if (channel_.get()) |
196 new_filters[i]->OnFilterAdded(channel_.get()); | 249 filter->OnFilterAdded(channel_.get()); |
197 // Ditto for if the channel has been connected. | 250 // Ditto for if the channel has been connected. |
198 if (peer_pid_) | 251 if (peer_pid_) |
199 new_filters[i]->OnChannelConnected(peer_pid_); | 252 filter->OnChannelConnected(peer_pid_); |
200 } | 253 } |
201 } | 254 } |
202 | 255 |
203 // Called on the IPC::Channel thread | 256 // Called on the IPC::Channel thread |
204 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { | 257 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { |
205 if (!channel_.get()) | 258 if (!channel_.get()) |
206 return; // The filters have already been deleted. | 259 return; // The filters have already been deleted. |
207 | 260 |
208 for (size_t i = 0; i < filters_.size(); ++i) { | 261 bool filter_exists = false; |
209 if (filters_[i].get() == filter) { | 262 for (size_t i = 0; i < all_filters_.size(); ++i) { |
263 if (all_filters_[i].get() == filter) { | |
210 filter->OnFilterRemoved(); | 264 filter->OnFilterRemoved(); |
211 filters_.erase(filters_.begin() + i); | 265 all_filters_.erase(all_filters_.begin() + i); |
212 return; | 266 filter_exists = true; |
267 break; | |
213 } | 268 } |
214 } | 269 } |
215 | 270 |
216 NOTREACHED() << "filter to be removed not found"; | 271 if (!filter_exists) { |
272 NOTREACHED() << "filter to be removed not found"; | |
273 return; | |
274 } | |
275 | |
276 if (EraseFilter(filter, message_global_filters_)) | |
277 return; | |
278 | |
279 for (MessageClassFilterMap::iterator it = message_class_filters_.begin(); | |
280 it != message_class_filters_.end(); | |
281 ++it) { | |
282 EraseFilter(filter, it->second); | |
283 } | |
217 } | 284 } |
218 | 285 |
219 // Called on the listener's thread | 286 // Called on the listener's thread |
220 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { | 287 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { |
221 base::AutoLock auto_lock(pending_filters_lock_); | 288 base::AutoLock auto_lock(pending_filters_lock_); |
222 pending_filters_.push_back(make_scoped_refptr(filter)); | 289 pending_filters_.push_back(make_scoped_refptr(filter)); |
223 ipc_task_runner_->PostTask( | 290 ipc_task_runner_->PostTask( |
224 FROM_HERE, base::Bind(&Context::OnAddFilter, this)); | 291 FROM_HERE, base::Bind(&Context::OnAddFilter, this)); |
225 } | 292 } |
226 | 293 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 Channel* channel = context_.get()->channel_.get(); | 478 Channel* channel = context_.get()->channel_.get(); |
412 // Channel must have been created first. | 479 // Channel must have been created first. |
413 DCHECK(channel) << context_.get()->channel_id_; | 480 DCHECK(channel) << context_.get()->channel_id_; |
414 return channel->GetPeerEuid(peer_euid); | 481 return channel->GetPeerEuid(peer_euid); |
415 } | 482 } |
416 #endif | 483 #endif |
417 | 484 |
418 //----------------------------------------------------------------------------- | 485 //----------------------------------------------------------------------------- |
419 | 486 |
420 } // namespace IPC | 487 } // namespace IPC |
OLD | NEW |