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

Side by Side Diff: dbus/object_manager.cc

Issue 510863002: dbus::ObjectManager: Add a match rule for properties before GetManagedObjects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Round 2 comments Created 6 years, 3 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "dbus/object_manager.h" 5 #include "dbus/object_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/task_runner_util.h"
9 #include "dbus/bus.h" 13 #include "dbus/bus.h"
14 #include "dbus/dbus_statistics.h"
10 #include "dbus/message.h" 15 #include "dbus/message.h"
11 #include "dbus/object_proxy.h" 16 #include "dbus/object_proxy.h"
12 #include "dbus/property.h" 17 #include "dbus/property.h"
18 #include "dbus/scoped_dbus_error.h"
19 #include "dbus/util.h"
13 20
14 namespace dbus { 21 namespace dbus {
15 22
16 ObjectManager::Object::Object() 23 ObjectManager::Object::Object()
17 : object_proxy(NULL) { 24 : object_proxy(NULL) {
18 } 25 }
19 26
20 ObjectManager::Object::~Object() { 27 ObjectManager::Object::~Object() {
21 } 28 }
22 29
23 ObjectManager::ObjectManager(Bus* bus, 30 ObjectManager::ObjectManager(Bus* bus,
24 const std::string& service_name, 31 const std::string& service_name,
25 const ObjectPath& object_path) 32 const ObjectPath& object_path)
26 : bus_(bus), 33 : bus_(bus),
27 service_name_(service_name), 34 service_name_(service_name),
28 object_path_(object_path), 35 object_path_(object_path),
36 setup_success_(false),
29 weak_ptr_factory_(this) { 37 weak_ptr_factory_(this) {
30 DVLOG(1) << "Creating ObjectManager for " << service_name_ 38 DVLOG(1) << "Creating ObjectManager for " << service_name_
31 << " " << object_path_.value(); 39 << " " << object_path_.value();
32
33 DCHECK(bus_); 40 DCHECK(bus_);
41 bus_->AssertOnOriginThread();
34 object_proxy_ = bus_->GetObjectProxy(service_name_, object_path_); 42 object_proxy_ = bus_->GetObjectProxy(service_name_, object_path_);
35 object_proxy_->SetNameOwnerChangedCallback( 43 object_proxy_->SetNameOwnerChangedCallback(
36 base::Bind(&ObjectManager::NameOwnerChanged, 44 base::Bind(&ObjectManager::NameOwnerChanged,
37 weak_ptr_factory_.GetWeakPtr())); 45 weak_ptr_factory_.GetWeakPtr()));
38 46
39 object_proxy_->ConnectToSignal( 47 // Set up a match rule and a filter function to handle PropertiesChanged
40 kObjectManagerInterface, 48 // signals from the service. This is important to avoid any race conditions
41 kObjectManagerInterfacesAdded, 49 // that might cause us to miss PropertiesChanged signals once all objects are
42 base::Bind(&ObjectManager::InterfacesAddedReceived, 50 // initialized via GetManagedObjects.
43 weak_ptr_factory_.GetWeakPtr()), 51 base::PostTaskAndReplyWithResult(
44 base::Bind(&ObjectManager::InterfacesAddedConnected, 52 bus_->GetDBusTaskRunner(),
45 weak_ptr_factory_.GetWeakPtr())); 53 FROM_HERE,
46 54 base::Bind(&ObjectManager::SetupMatchRuleAndFilter, this),
47 object_proxy_->ConnectToSignal( 55 base::Bind(&ObjectManager::OnSetupMatchRuleAndFilterComplete, this));
48 kObjectManagerInterface,
49 kObjectManagerInterfacesRemoved,
50 base::Bind(&ObjectManager::InterfacesRemovedReceived,
51 weak_ptr_factory_.GetWeakPtr()),
52 base::Bind(&ObjectManager::InterfacesRemovedConnected,
53 weak_ptr_factory_.GetWeakPtr()));
54
55 GetManagedObjects();
56 } 56 }
57 57
58 ObjectManager::~ObjectManager() { 58 ObjectManager::~ObjectManager() {
59 // Clean up Object structures 59 // Clean up Object structures
60 for (ObjectMap::iterator iter = object_map_.begin(); 60 for (ObjectMap::iterator iter = object_map_.begin();
61 iter != object_map_.end(); ++iter) { 61 iter != object_map_.end(); ++iter) {
62 Object* object = iter->second; 62 Object* object = iter->second;
63 63
64 for (Object::PropertiesMap::iterator piter = object->properties_map.begin(); 64 for (Object::PropertiesMap::iterator piter = object->properties_map.begin();
65 piter != object->properties_map.end(); ++piter) { 65 piter != object->properties_map.end(); ++piter) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 MethodCall method_call(kObjectManagerInterface, 137 MethodCall method_call(kObjectManagerInterface,
138 kObjectManagerGetManagedObjects); 138 kObjectManagerGetManagedObjects);
139 139
140 object_proxy_->CallMethod( 140 object_proxy_->CallMethod(
141 &method_call, 141 &method_call,
142 ObjectProxy::TIMEOUT_USE_DEFAULT, 142 ObjectProxy::TIMEOUT_USE_DEFAULT,
143 base::Bind(&ObjectManager::OnGetManagedObjects, 143 base::Bind(&ObjectManager::OnGetManagedObjects,
144 weak_ptr_factory_.GetWeakPtr())); 144 weak_ptr_factory_.GetWeakPtr()));
145 } 145 }
146 146
147 void ObjectManager::CleanUp() {
148 DCHECK(bus_);
149 bus_->AssertOnDBusThread();
150
151 if (!setup_success_)
152 return;
153
154 if (!bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this))
155 LOG(ERROR) << "Failed to remove filter function";
156
157 ScopedDBusError error;
158 bus_->RemoveMatch(match_rule_, error.get());
159 if (error.is_set())
160 LOG(ERROR) << "Failed to remove match rule: " << match_rule_;
161
162 match_rule_.clear();
163 }
164
165 void ObjectManager::InitializeObjects() {
166 DCHECK(bus_);
167 object_proxy_->ConnectToSignal(
168 kObjectManagerInterface,
169 kObjectManagerInterfacesAdded,
170 base::Bind(&ObjectManager::InterfacesAddedReceived,
171 weak_ptr_factory_.GetWeakPtr()),
172 base::Bind(&ObjectManager::InterfacesAddedConnected,
173 weak_ptr_factory_.GetWeakPtr()));
174
175 object_proxy_->ConnectToSignal(
176 kObjectManagerInterface,
177 kObjectManagerInterfacesRemoved,
178 base::Bind(&ObjectManager::InterfacesRemovedReceived,
179 weak_ptr_factory_.GetWeakPtr()),
180 base::Bind(&ObjectManager::InterfacesRemovedConnected,
181 weak_ptr_factory_.GetWeakPtr()));
182
183 GetManagedObjects();
184 }
185
186 bool ObjectManager::SetupMatchRuleAndFilter() {
187 DCHECK(bus_);
188 DCHECK(!setup_success_);
189 bus_->AssertOnDBusThread();
190
191 if (!bus_->Connect() || !bus_->SetUpAsyncOperations())
192 return false;
193
194 service_name_owner_ =
195 bus_->GetServiceOwnerAndBlock(service_name_, Bus::SUPPRESS_ERRORS);
196
197 const std::string match_rule =
198 base::StringPrintf(
199 "type='signal', sender='%s', interface='%s', member='%s'",
200 service_name_.c_str(),
201 kPropertiesInterface,
202 kPropertiesChanged);
203
204 if (!bus_->AddFilterFunction(&ObjectManager::HandleMessageThunk, this)) {
205 LOG(ERROR) << "ObjectManager failed to add filter function";
206 return false;
207 }
208
209 ScopedDBusError error;
210 bus_->AddMatch(match_rule, error.get());
211 if (error.is_set()) {
212 LOG(ERROR) << "ObjectManager failed to add match rule \"" << match_rule
213 << "\". Got " << error.name() << ": " << error.message();
214 bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this);
215 return false;
216 }
217
218 match_rule_ = match_rule;
219 setup_success_ = true;
220
221 return true;
222 }
223
224 void ObjectManager::OnSetupMatchRuleAndFilterComplete(bool success) {
225 LOG_IF(WARNING, !success) << service_name_ << " " << object_path_.value()
226 << ": Failed to set up match rule.";
227 if (success)
228 InitializeObjects();
229 }
230
231 // static
232 DBusHandlerResult ObjectManager::HandleMessageThunk(DBusConnection* connection,
233 DBusMessage* raw_message,
234 void* user_data) {
235 ObjectManager* self = reinterpret_cast<ObjectManager*>(user_data);
236 return self->HandleMessage(connection, raw_message);
237 }
238
239 DBusHandlerResult ObjectManager::HandleMessage(DBusConnection* connection,
240 DBusMessage* raw_message) {
241 DCHECK(bus_);
242 bus_->AssertOnDBusThread();
243
244 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL)
245 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
246
247 // raw_message will be unrefed on exit of the function. Increment the
248 // reference so we can use it in Signal.
249 dbus_message_ref(raw_message);
250 scoped_ptr<Signal> signal(
251 Signal::FromRawMessage(raw_message));
252
253 const std::string interface = signal->GetInterface();
254 const std::string member = signal->GetMember();
255
256 statistics::AddReceivedSignal(service_name_, interface, member);
257
258 // Only handle the PropertiesChanged signal.
259 const std::string absolute_signal_name =
260 GetAbsoluteMemberName(interface, member);
261 const std::string properties_changed_signal_name =
262 GetAbsoluteMemberName(kPropertiesInterface, kPropertiesChanged);
263 if (absolute_signal_name != properties_changed_signal_name)
264 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
265
266 VLOG(1) << "Signal received: " << signal->ToString();
267
268 // Make sure that the signal originated from the correct sender.
269 std::string sender = signal->GetSender();
270 if (service_name_owner_ != sender) {
271 LOG(ERROR) << "Rejecting a message from a wrong sender.";
272 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1);
273 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
274 }
275
276 const ObjectPath path = signal->GetPath();
277
278 if (bus_->HasDBusThread()) {
279 // Post a task to run the method in the origin thread. Transfer ownership of
280 // |signal| to NotifyPropertiesChanged, which will handle the clean up.
281 Signal* released_signal = signal.release();
282 bus_->GetOriginTaskRunner()->PostTask(
283 FROM_HERE,
284 base::Bind(&ObjectManager::NotifyPropertiesChanged,
285 this, path,
286 released_signal));
287 } else {
288 // If the D-Bus thread is not used, just call the callback on the
289 // current thread. Transfer the ownership of |signal| to
290 // NotifyPropertiesChanged.
291 NotifyPropertiesChanged(path, signal.release());
292 }
293
294 // We don't return DBUS_HANDLER_RESULT_HANDLED for signals because other
295 // objects may be interested in them. (e.g. Signals from org.freedesktop.DBus)
296 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
297 }
298
299 void ObjectManager::NotifyPropertiesChanged(
300 const dbus::ObjectPath object_path,
301 Signal* signal) {
302 DCHECK(bus_);
303 bus_->AssertOnOriginThread();
304
305 NotifyPropertiesChangedHelper(object_path, signal);
306
307 // Delete the message on the D-Bus thread. See comments in HandleMessage.
308 bus_->GetDBusTaskRunner()->PostTask(
309 FROM_HERE,
310 base::Bind(&base::DeletePointer<Signal>, signal));
311 }
312
313 void ObjectManager::NotifyPropertiesChangedHelper(
314 const dbus::ObjectPath object_path,
315 Signal* signal) {
316 DCHECK(bus_);
317 bus_->AssertOnOriginThread();
318
319 MessageReader reader(signal);
320 std::string interface;
321 if (!reader.PopString(&interface)) {
322 LOG(WARNING) << "Property changed signal has wrong parameters: "
323 << "expected interface name: " << signal->ToString();
324 return;
325 }
326
327 PropertySet* properties = GetProperties(object_path, interface);
328 if (properties)
329 properties->ChangedReceived(signal);
330 }
331
147 void ObjectManager::OnGetManagedObjects(Response* response) { 332 void ObjectManager::OnGetManagedObjects(Response* response) {
148 if (response != NULL) { 333 if (response != NULL) {
149 MessageReader reader(response); 334 MessageReader reader(response);
150 MessageReader array_reader(NULL); 335 MessageReader array_reader(NULL);
151 if (!reader.PopArray(&array_reader)) 336 if (!reader.PopArray(&array_reader))
152 return; 337 return;
153 338
154 while (array_reader.HasMoreData()) { 339 while (array_reader.HasMoreData()) {
155 MessageReader dict_entry_reader(NULL); 340 MessageReader dict_entry_reader(NULL);
156 ObjectPath object_path; 341 ObjectPath object_path;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 object = oiter->second; 435 object = oiter->second;
251 436
252 Object::PropertiesMap::iterator piter = 437 Object::PropertiesMap::iterator piter =
253 object->properties_map.find(interface_name); 438 object->properties_map.find(interface_name);
254 PropertySet* property_set; 439 PropertySet* property_set;
255 const bool interface_added = (piter == object->properties_map.end()); 440 const bool interface_added = (piter == object->properties_map.end());
256 if (interface_added) { 441 if (interface_added) {
257 property_set = object->properties_map[interface_name] = 442 property_set = object->properties_map[interface_name] =
258 interface->CreateProperties(object->object_proxy, 443 interface->CreateProperties(object->object_proxy,
259 object_path, interface_name); 444 object_path, interface_name);
260 property_set->ConnectSignals();
261 } else 445 } else
262 property_set = piter->second; 446 property_set = piter->second;
263 447
264 property_set->UpdatePropertiesFromReader(reader); 448 property_set->UpdatePropertiesFromReader(reader);
265 449
266 if (interface_added) 450 if (interface_added)
267 interface->ObjectAdded(object_path, interface_name); 451 interface->ObjectAdded(object_path, interface_name);
268 } 452 }
269 453
270 void ObjectManager::RemoveInterface(const ObjectPath& object_path, 454 void ObjectManager::RemoveInterface(const ObjectPath& object_path,
(...skipping 19 matching lines...) Expand all
290 object->properties_map.erase(piter); 474 object->properties_map.erase(piter);
291 475
292 if (object->properties_map.empty()) { 476 if (object->properties_map.empty()) {
293 object_map_.erase(oiter); 477 object_map_.erase(oiter);
294 delete object; 478 delete object;
295 } 479 }
296 } 480 }
297 481
298 void ObjectManager::NameOwnerChanged(const std::string& old_owner, 482 void ObjectManager::NameOwnerChanged(const std::string& old_owner,
299 const std::string& new_owner) { 483 const std::string& new_owner) {
484 service_name_owner_ = new_owner;
485
300 if (!old_owner.empty()) { 486 if (!old_owner.empty()) {
301 ObjectMap::iterator iter = object_map_.begin(); 487 ObjectMap::iterator iter = object_map_.begin();
302 while (iter != object_map_.end()) { 488 while (iter != object_map_.end()) {
303 ObjectMap::iterator tmp = iter; 489 ObjectMap::iterator tmp = iter;
304 ++iter; 490 ++iter;
305 491
306 // PropertiesMap is mutated by RemoveInterface, and also Object is 492 // PropertiesMap is mutated by RemoveInterface, and also Object is
307 // destroyed; easier to collect the object path and interface names 493 // destroyed; easier to collect the object path and interface names
308 // and remove them safely. 494 // and remove them safely.
309 const dbus::ObjectPath object_path = tmp->first; 495 const dbus::ObjectPath object_path = tmp->first;
(...skipping 10 matching lines...) Expand all
320 RemoveInterface(object_path, *iiter); 506 RemoveInterface(object_path, *iiter);
321 } 507 }
322 508
323 } 509 }
324 510
325 if (!new_owner.empty()) 511 if (!new_owner.empty())
326 GetManagedObjects(); 512 GetManagedObjects();
327 } 513 }
328 514
329 } // namespace dbus 515 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/object_manager.h ('k') | dbus/object_manager_unittest.cc » ('j') | dbus/util.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698