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_start.h" |
17 #include "ipc/ipc_message_utils.h" | 18 #include "ipc/ipc_message_utils.h" |
18 | 19 |
19 namespace IPC { | 20 namespace IPC { |
20 | 21 |
21 //------------------------------------------------------------------------------ | 22 //------------------------------------------------------------------------------ |
22 | 23 |
| 24 class ChannelProxy::Context::MessageFilterRouter { |
| 25 public: |
| 26 typedef std::vector<MessageFilter*> MessageFilters; |
| 27 |
| 28 MessageFilterRouter() {} |
| 29 ~MessageFilterRouter() {} |
| 30 |
| 31 void AddFilter(MessageFilter* filter) { |
| 32 // Determine if the filter should be applied to all messages, or only |
| 33 // messages of a certain class. |
| 34 std::vector<uint32> supported_message_classes; |
| 35 if (filter->GetSupportedMessageClasses(&supported_message_classes)) { |
| 36 DCHECK(!supported_message_classes.empty()); |
| 37 for (size_t i = 0; i < supported_message_classes.size(); ++i) { |
| 38 const int message_class = supported_message_classes[i]; |
| 39 DCHECK(ValidMessageClass(message_class)); |
| 40 // Safely ignore repeated subscriptions to a given message class for the |
| 41 // current filter being added. |
| 42 if (!message_class_filters_[message_class].empty() && |
| 43 message_class_filters_[message_class].back() == filter) { |
| 44 continue; |
| 45 } |
| 46 message_class_filters_[message_class].push_back(filter); |
| 47 } |
| 48 } else { |
| 49 global_filters_.push_back(filter); |
| 50 } |
| 51 } |
| 52 |
| 53 void RemoveFilter(MessageFilter* filter) { |
| 54 if (RemoveFilter(global_filters_, filter)) |
| 55 return; |
| 56 |
| 57 for (size_t i = 0; i < arraysize(message_class_filters_); ++i) |
| 58 RemoveFilter(message_class_filters_[i], filter); |
| 59 } |
| 60 |
| 61 bool TryFilters(const Message& message) { |
| 62 if (TryFilters(global_filters_, message)) |
| 63 return true; |
| 64 |
| 65 const int message_class = IPC_MESSAGE_CLASS(message); |
| 66 if (!ValidMessageClass(message_class)) |
| 67 return false; |
| 68 |
| 69 return TryFilters(message_class_filters_[message_class], message); |
| 70 } |
| 71 |
| 72 void Clear() { |
| 73 global_filters_.clear(); |
| 74 for (size_t i = 0; i < arraysize(message_class_filters_); ++i) |
| 75 message_class_filters_[i].clear(); |
| 76 } |
| 77 |
| 78 private: |
| 79 static bool TryFilters(MessageFilters& filters, const IPC::Message& message) { |
| 80 for (size_t i = 0; i < filters.size(); ++i) { |
| 81 if (filters[i]->OnMessageReceived(message)) { |
| 82 return true; |
| 83 } |
| 84 } |
| 85 return false; |
| 86 } |
| 87 |
| 88 static bool RemoveFilter(MessageFilters& filters, MessageFilter* filter) { |
| 89 MessageFilters::iterator it = |
| 90 std::remove(filters.begin(), filters.end(), filter); |
| 91 if (it == filters.end()) |
| 92 return false; |
| 93 |
| 94 filters.erase(it, filters.end()); |
| 95 return true; |
| 96 } |
| 97 |
| 98 static bool ValidMessageClass(int message_class) { |
| 99 return message_class >= 0 && message_class < LastIPCMsgStart; |
| 100 } |
| 101 |
| 102 // List of global and selective filters; a given filter will exist in either |
| 103 // |message_global_filters_| OR |message_class_filters_|, but not both. |
| 104 // Note that |message_global_filters_| will be given first offering of any |
| 105 // given message. It's the filter implementer and installer's |
| 106 // responsibility to ensure that a filter is either global or selective to |
| 107 // ensure proper message filtering order. |
| 108 MessageFilters global_filters_; |
| 109 MessageFilters message_class_filters_[LastIPCMsgStart]; |
| 110 }; |
| 111 |
| 112 //------------------------------------------------------------------------------ |
| 113 |
23 ChannelProxy::MessageFilter::MessageFilter() {} | 114 ChannelProxy::MessageFilter::MessageFilter() {} |
24 | 115 |
25 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {} | 116 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {} |
26 | 117 |
27 void ChannelProxy::MessageFilter::OnFilterRemoved() {} | 118 void ChannelProxy::MessageFilter::OnFilterRemoved() {} |
28 | 119 |
29 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {} | 120 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {} |
30 | 121 |
31 void ChannelProxy::MessageFilter::OnChannelError() {} | 122 void ChannelProxy::MessageFilter::OnChannelError() {} |
32 | 123 |
33 void ChannelProxy::MessageFilter::OnChannelClosing() {} | 124 void ChannelProxy::MessageFilter::OnChannelClosing() {} |
34 | 125 |
35 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) { | 126 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) { |
36 return false; | 127 return false; |
37 } | 128 } |
38 | 129 |
| 130 bool ChannelProxy::MessageFilter::GetSupportedMessageClasses( |
| 131 std::vector<uint32>* /*supported_message_classes*/) const { |
| 132 return false; |
| 133 } |
| 134 |
39 ChannelProxy::MessageFilter::~MessageFilter() {} | 135 ChannelProxy::MessageFilter::~MessageFilter() {} |
40 | 136 |
41 //------------------------------------------------------------------------------ | 137 //------------------------------------------------------------------------------ |
42 | 138 |
43 ChannelProxy::Context::Context(Listener* listener, | 139 ChannelProxy::Context::Context(Listener* listener, |
44 base::SingleThreadTaskRunner* ipc_task_runner) | 140 base::SingleThreadTaskRunner* ipc_task_runner) |
45 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 141 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
46 listener_(listener), | 142 listener_(listener), |
47 ipc_task_runner_(ipc_task_runner), | 143 ipc_task_runner_(ipc_task_runner), |
48 channel_connected_called_(false), | 144 channel_connected_called_(false), |
| 145 message_filter_router_(new MessageFilterRouter()), |
49 peer_pid_(base::kNullProcessId) { | 146 peer_pid_(base::kNullProcessId) { |
50 DCHECK(ipc_task_runner_.get()); | 147 DCHECK(ipc_task_runner_.get()); |
51 } | 148 } |
52 | 149 |
53 ChannelProxy::Context::~Context() { | 150 ChannelProxy::Context::~Context() { |
54 } | 151 } |
55 | 152 |
56 void ChannelProxy::Context::ClearIPCTaskRunner() { | 153 void ChannelProxy::Context::ClearIPCTaskRunner() { |
57 ipc_task_runner_ = NULL; | 154 ipc_task_runner_ = NULL; |
58 } | 155 } |
59 | 156 |
60 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, | 157 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, |
61 const Channel::Mode& mode) { | 158 const Channel::Mode& mode) { |
62 DCHECK(channel_.get() == NULL); | 159 DCHECK(channel_.get() == NULL); |
63 channel_id_ = handle.name; | 160 channel_id_ = handle.name; |
64 channel_.reset(new Channel(handle, mode, this)); | 161 channel_.reset(new Channel(handle, mode, this)); |
65 } | 162 } |
66 | 163 |
67 bool ChannelProxy::Context::TryFilters(const Message& message) { | 164 bool ChannelProxy::Context::TryFilters(const Message& message) { |
| 165 DCHECK(message_filter_router_); |
68 #ifdef IPC_MESSAGE_LOG_ENABLED | 166 #ifdef IPC_MESSAGE_LOG_ENABLED |
69 Logging* logger = Logging::GetInstance(); | 167 Logging* logger = Logging::GetInstance(); |
70 if (logger->Enabled()) | 168 if (logger->Enabled()) |
71 logger->OnPreDispatchMessage(message); | 169 logger->OnPreDispatchMessage(message); |
72 #endif | 170 #endif |
73 | 171 |
74 for (size_t i = 0; i < filters_.size(); ++i) { | 172 if (message_filter_router_->TryFilters(message)) { |
75 if (filters_[i]->OnMessageReceived(message)) { | |
76 #ifdef IPC_MESSAGE_LOG_ENABLED | 173 #ifdef IPC_MESSAGE_LOG_ENABLED |
77 if (logger->Enabled()) | 174 if (logger->Enabled()) |
78 logger->OnPostDispatchMessage(message, channel_id_); | 175 logger->OnPostDispatchMessage(message, channel_id_); |
79 #endif | 176 #endif |
80 return true; | 177 return true; |
81 } | |
82 } | 178 } |
83 return false; | 179 return false; |
84 } | 180 } |
85 | 181 |
86 // Called on the IPC::Channel thread | 182 // Called on the IPC::Channel thread |
87 bool ChannelProxy::Context::OnMessageReceived(const Message& message) { | 183 bool ChannelProxy::Context::OnMessageReceived(const Message& message) { |
88 // First give a chance to the filters to process this message. | 184 // First give a chance to the filters to process this message. |
89 if (!TryFilters(message)) | 185 if (!TryFilters(message)) |
90 OnMessageReceivedNoFilter(message); | 186 OnMessageReceivedNoFilter(message); |
91 return true; | 187 return true; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // would result in this branch being taken. | 245 // would result in this branch being taken. |
150 if (!channel_.get()) | 246 if (!channel_.get()) |
151 return; | 247 return; |
152 | 248 |
153 for (size_t i = 0; i < filters_.size(); ++i) { | 249 for (size_t i = 0; i < filters_.size(); ++i) { |
154 filters_[i]->OnChannelClosing(); | 250 filters_[i]->OnChannelClosing(); |
155 filters_[i]->OnFilterRemoved(); | 251 filters_[i]->OnFilterRemoved(); |
156 } | 252 } |
157 | 253 |
158 // We don't need the filters anymore. | 254 // We don't need the filters anymore. |
| 255 message_filter_router_->Clear(); |
159 filters_.clear(); | 256 filters_.clear(); |
160 | 257 |
161 channel_.reset(); | 258 channel_.reset(); |
162 | 259 |
163 // Balance with the reference taken during startup. This may result in | 260 // Balance with the reference taken during startup. This may result in |
164 // self-destruction. | 261 // self-destruction. |
165 Release(); | 262 Release(); |
166 } | 263 } |
167 | 264 |
168 void ChannelProxy::Context::Clear() { | 265 void ChannelProxy::Context::Clear() { |
(...skipping 14 matching lines...) Expand all Loading... |
183 void ChannelProxy::Context::OnAddFilter() { | 280 void ChannelProxy::Context::OnAddFilter() { |
184 std::vector<scoped_refptr<MessageFilter> > new_filters; | 281 std::vector<scoped_refptr<MessageFilter> > new_filters; |
185 { | 282 { |
186 base::AutoLock auto_lock(pending_filters_lock_); | 283 base::AutoLock auto_lock(pending_filters_lock_); |
187 new_filters.swap(pending_filters_); | 284 new_filters.swap(pending_filters_); |
188 } | 285 } |
189 | 286 |
190 for (size_t i = 0; i < new_filters.size(); ++i) { | 287 for (size_t i = 0; i < new_filters.size(); ++i) { |
191 filters_.push_back(new_filters[i]); | 288 filters_.push_back(new_filters[i]); |
192 | 289 |
| 290 message_filter_router_->AddFilter(new_filters[i].get()); |
| 291 |
193 // If the channel has already been created, then we need to send this | 292 // If the channel has already been created, then we need to send this |
194 // message so that the filter gets access to the Channel. | 293 // message so that the filter gets access to the Channel. |
195 if (channel_.get()) | 294 if (channel_.get()) |
196 new_filters[i]->OnFilterAdded(channel_.get()); | 295 new_filters[i]->OnFilterAdded(channel_.get()); |
197 // Ditto for if the channel has been connected. | 296 // Ditto for if the channel has been connected. |
198 if (peer_pid_) | 297 if (peer_pid_) |
199 new_filters[i]->OnChannelConnected(peer_pid_); | 298 new_filters[i]->OnChannelConnected(peer_pid_); |
200 } | 299 } |
201 } | 300 } |
202 | 301 |
203 // Called on the IPC::Channel thread | 302 // Called on the IPC::Channel thread |
204 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { | 303 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { |
205 if (!channel_.get()) | 304 if (!channel_.get()) |
206 return; // The filters have already been deleted. | 305 return; // The filters have already been deleted. |
207 | 306 |
| 307 message_filter_router_->RemoveFilter(filter); |
| 308 |
208 for (size_t i = 0; i < filters_.size(); ++i) { | 309 for (size_t i = 0; i < filters_.size(); ++i) { |
209 if (filters_[i].get() == filter) { | 310 if (filters_[i].get() == filter) { |
210 filter->OnFilterRemoved(); | 311 filter->OnFilterRemoved(); |
211 filters_.erase(filters_.begin() + i); | 312 filters_.erase(filters_.begin() + i); |
212 return; | 313 return; |
213 } | 314 } |
214 } | 315 } |
215 | 316 |
216 NOTREACHED() << "filter to be removed not found"; | 317 NOTREACHED() << "filter to be removed not found"; |
217 } | 318 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 Channel* channel = context_.get()->channel_.get(); | 512 Channel* channel = context_.get()->channel_.get(); |
412 // Channel must have been created first. | 513 // Channel must have been created first. |
413 DCHECK(channel) << context_.get()->channel_id_; | 514 DCHECK(channel) << context_.get()->channel_id_; |
414 return channel->GetPeerEuid(peer_euid); | 515 return channel->GetPeerEuid(peer_euid); |
415 } | 516 } |
416 #endif | 517 #endif |
417 | 518 |
418 //----------------------------------------------------------------------------- | 519 //----------------------------------------------------------------------------- |
419 | 520 |
420 } // namespace IPC | 521 } // namespace IPC |
OLD | NEW |