Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1656)

Side by Side Diff: ipc/ipc_channel_proxy.cc

Issue 142923005: Allow MessageFilters to restrict listening to specific message classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 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
23 ChannelProxy::MessageFilter::MessageFilter() {} 107 ChannelProxy::MessageFilter::MessageFilter() {}
24 108
25 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {} 109 void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {}
26 110
27 void ChannelProxy::MessageFilter::OnFilterRemoved() {} 111 void ChannelProxy::MessageFilter::OnFilterRemoved() {}
28 112
29 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {} 113 void ChannelProxy::MessageFilter::OnChannelConnected(int32 peer_pid) {}
30 114
31 void ChannelProxy::MessageFilter::OnChannelError() {} 115 void ChannelProxy::MessageFilter::OnChannelError() {}
32 116
33 void ChannelProxy::MessageFilter::OnChannelClosing() {} 117 void ChannelProxy::MessageFilter::OnChannelClosing() {}
34 118
35 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) { 119 bool ChannelProxy::MessageFilter::OnMessageReceived(const Message& message) {
36 return false; 120 return false;
37 } 121 }
38 122
123 bool ChannelProxy::MessageFilter::GetSupportedMessageClasses(
124 std::vector<uint32>* /*supported_message_classes*/) const {
125 return false;
126 }
127
39 ChannelProxy::MessageFilter::~MessageFilter() {} 128 ChannelProxy::MessageFilter::~MessageFilter() {}
40 129
41 //------------------------------------------------------------------------------ 130 //------------------------------------------------------------------------------
42 131
43 ChannelProxy::Context::Context(Listener* listener, 132 ChannelProxy::Context::Context(Listener* listener,
44 base::SingleThreadTaskRunner* ipc_task_runner) 133 base::SingleThreadTaskRunner* ipc_task_runner)
45 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), 134 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
46 listener_(listener), 135 listener_(listener),
47 ipc_task_runner_(ipc_task_runner), 136 ipc_task_runner_(ipc_task_runner),
48 channel_connected_called_(false), 137 channel_connected_called_(false),
138 message_filter_router_(new MessageFilterRouter()),
49 peer_pid_(base::kNullProcessId) { 139 peer_pid_(base::kNullProcessId) {
50 DCHECK(ipc_task_runner_.get()); 140 DCHECK(ipc_task_runner_.get());
51 } 141 }
52 142
53 ChannelProxy::Context::~Context() { 143 ChannelProxy::Context::~Context() {
54 } 144 }
55 145
56 void ChannelProxy::Context::ClearIPCTaskRunner() { 146 void ChannelProxy::Context::ClearIPCTaskRunner() {
57 ipc_task_runner_ = NULL; 147 ipc_task_runner_ = NULL;
58 } 148 }
59 149
60 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, 150 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle,
61 const Channel::Mode& mode) { 151 const Channel::Mode& mode) {
62 DCHECK(channel_.get() == NULL); 152 DCHECK(channel_.get() == NULL);
63 channel_id_ = handle.name; 153 channel_id_ = handle.name;
64 channel_.reset(new Channel(handle, mode, this)); 154 channel_.reset(new Channel(handle, mode, this));
65 } 155 }
66 156
67 bool ChannelProxy::Context::TryFilters(const Message& message) { 157 bool ChannelProxy::Context::TryFilters(const Message& message) {
158 DCHECK(message_filter_router_);
68 #ifdef IPC_MESSAGE_LOG_ENABLED 159 #ifdef IPC_MESSAGE_LOG_ENABLED
69 Logging* logger = Logging::GetInstance(); 160 Logging* logger = Logging::GetInstance();
70 if (logger->Enabled()) 161 if (logger->Enabled())
71 logger->OnPreDispatchMessage(message); 162 logger->OnPreDispatchMessage(message);
72 #endif 163 #endif
73 164
74 for (size_t i = 0; i < filters_.size(); ++i) { 165 if (message_filter_router_->TryFilters(message)) {
75 if (filters_[i]->OnMessageReceived(message)) {
76 #ifdef IPC_MESSAGE_LOG_ENABLED 166 #ifdef IPC_MESSAGE_LOG_ENABLED
77 if (logger->Enabled()) 167 if (logger->Enabled())
78 logger->OnPostDispatchMessage(message, channel_id_); 168 logger->OnPostDispatchMessage(message, channel_id_);
79 #endif 169 #endif
80 return true; 170 return true;
81 }
82 } 171 }
83 return false; 172 return false;
84 } 173 }
85 174
86 // Called on the IPC::Channel thread 175 // Called on the IPC::Channel thread
87 bool ChannelProxy::Context::OnMessageReceived(const Message& message) { 176 bool ChannelProxy::Context::OnMessageReceived(const Message& message) {
88 // First give a chance to the filters to process this message. 177 // First give a chance to the filters to process this message.
89 if (!TryFilters(message)) 178 if (!TryFilters(message))
90 OnMessageReceivedNoFilter(message); 179 OnMessageReceivedNoFilter(message);
91 return true; 180 return true;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 if (!channel_.get()) 239 if (!channel_.get())
151 return; 240 return;
152 241
153 for (size_t i = 0; i < filters_.size(); ++i) { 242 for (size_t i = 0; i < filters_.size(); ++i) {
154 filters_[i]->OnChannelClosing(); 243 filters_[i]->OnChannelClosing();
155 filters_[i]->OnFilterRemoved(); 244 filters_[i]->OnFilterRemoved();
156 } 245 }
157 246
158 // We don't need the filters anymore. 247 // We don't need the filters anymore.
159 filters_.clear(); 248 filters_.clear();
249 message_filter_router_->Clear();
160 250
161 channel_.reset(); 251 channel_.reset();
162 252
163 // Balance with the reference taken during startup. This may result in 253 // Balance with the reference taken during startup. This may result in
164 // self-destruction. 254 // self-destruction.
165 Release(); 255 Release();
166 } 256 }
167 257
168 void ChannelProxy::Context::Clear() { 258 void ChannelProxy::Context::Clear() {
169 listener_ = NULL; 259 listener_ = NULL;
(...skipping 13 matching lines...) Expand all
183 void ChannelProxy::Context::OnAddFilter() { 273 void ChannelProxy::Context::OnAddFilter() {
184 std::vector<scoped_refptr<MessageFilter> > new_filters; 274 std::vector<scoped_refptr<MessageFilter> > new_filters;
185 { 275 {
186 base::AutoLock auto_lock(pending_filters_lock_); 276 base::AutoLock auto_lock(pending_filters_lock_);
187 new_filters.swap(pending_filters_); 277 new_filters.swap(pending_filters_);
188 } 278 }
189 279
190 for (size_t i = 0; i < new_filters.size(); ++i) { 280 for (size_t i = 0; i < new_filters.size(); ++i) {
191 filters_.push_back(new_filters[i]); 281 filters_.push_back(new_filters[i]);
192 282
283 message_filter_router_->AddFilter(new_filters[i].get());
284
193 // If the channel has already been created, then we need to send this 285 // If the channel has already been created, then we need to send this
194 // message so that the filter gets access to the Channel. 286 // message so that the filter gets access to the Channel.
195 if (channel_.get()) 287 if (channel_.get())
196 new_filters[i]->OnFilterAdded(channel_.get()); 288 new_filters[i]->OnFilterAdded(channel_.get());
197 // Ditto for if the channel has been connected. 289 // Ditto for if the channel has been connected.
198 if (peer_pid_) 290 if (peer_pid_)
199 new_filters[i]->OnChannelConnected(peer_pid_); 291 new_filters[i]->OnChannelConnected(peer_pid_);
200 } 292 }
201 } 293 }
202 294
203 // Called on the IPC::Channel thread 295 // Called on the IPC::Channel thread
204 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { 296 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) {
205 if (!channel_.get()) 297 if (!channel_.get())
206 return; // The filters have already been deleted. 298 return; // The filters have already been deleted.
207 299
300 message_filter_router_->RemoveFilter(filter);
301
208 for (size_t i = 0; i < filters_.size(); ++i) { 302 for (size_t i = 0; i < filters_.size(); ++i) {
209 if (filters_[i].get() == filter) { 303 if (filters_[i].get() == filter) {
210 filter->OnFilterRemoved(); 304 filter->OnFilterRemoved();
211 filters_.erase(filters_.begin() + i); 305 filters_.erase(filters_.begin() + i);
212 return; 306 return;
213 } 307 }
214 } 308 }
215 309
216 NOTREACHED() << "filter to be removed not found"; 310 NOTREACHED() << "filter to be removed not found";
217 } 311 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 Channel* channel = context_.get()->channel_.get(); 505 Channel* channel = context_.get()->channel_.get();
412 // Channel must have been created first. 506 // Channel must have been created first.
413 DCHECK(channel) << context_.get()->channel_id_; 507 DCHECK(channel) << context_.get()->channel_id_;
414 return channel->GetPeerEuid(peer_euid); 508 return channel->GetPeerEuid(peer_euid);
415 } 509 }
416 #endif 510 #endif
417 511
418 //----------------------------------------------------------------------------- 512 //-----------------------------------------------------------------------------
419 513
420 } // namespace IPC 514 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698