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