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 // TODO(satorux): | 5 // TODO(satorux): |
6 // - Handle "disconnected" signal. | 6 // - Handle "disconnected" signal. |
7 | 7 |
8 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 | 580 |
581 dbus_connection_remove_filter(connection_, filter_function, user_data); | 581 dbus_connection_remove_filter(connection_, filter_function, user_data); |
582 filter_functions_added_.erase(filter_data_pair); | 582 filter_functions_added_.erase(filter_data_pair); |
583 return true; | 583 return true; |
584 } | 584 } |
585 | 585 |
586 void Bus::AddMatch(const std::string& match_rule, DBusError* error) { | 586 void Bus::AddMatch(const std::string& match_rule, DBusError* error) { |
587 DCHECK(connection_); | 587 DCHECK(connection_); |
588 AssertOnDBusThread(); | 588 AssertOnDBusThread(); |
589 | 589 |
590 if (match_rules_added_.find(match_rule) != match_rules_added_.end()) { | 590 std::map<std::string, int>::iterator iter = |
| 591 match_rules_added_.find(match_rule); |
| 592 if (iter != match_rules_added_.end()) { |
| 593 // The already existing rule's counter is incremented. |
| 594 iter->second++; |
| 595 |
591 VLOG(1) << "Match rule already exists: " << match_rule; | 596 VLOG(1) << "Match rule already exists: " << match_rule; |
592 return; | 597 return; |
593 } | 598 } |
594 | 599 |
595 dbus_bus_add_match(connection_, match_rule.c_str(), error); | 600 dbus_bus_add_match(connection_, match_rule.c_str(), error); |
596 match_rules_added_.insert(match_rule); | 601 match_rules_added_[match_rule] = 1; |
597 } | 602 } |
598 | 603 |
599 void Bus::RemoveMatch(const std::string& match_rule, DBusError* error) { | 604 bool Bus::RemoveMatch(const std::string& match_rule, DBusError* error) { |
600 DCHECK(connection_); | 605 DCHECK(connection_); |
601 AssertOnDBusThread(); | 606 AssertOnDBusThread(); |
602 | 607 |
603 if (match_rules_added_.find(match_rule) == match_rules_added_.end()) { | 608 std::map<std::string, int>::iterator iter = |
| 609 match_rules_added_.find(match_rule); |
| 610 if (iter == match_rules_added_.end()) { |
604 LOG(ERROR) << "Requested to remove an unknown match rule: " << match_rule; | 611 LOG(ERROR) << "Requested to remove an unknown match rule: " << match_rule; |
605 return; | 612 return false; |
606 } | 613 } |
607 | 614 |
608 dbus_bus_remove_match(connection_, match_rule.c_str(), error); | 615 // The rule's counter is decremented and the rule is deleted when reachs 0. |
609 match_rules_added_.erase(match_rule); | 616 iter->second--; |
| 617 if (iter->second == 0) { |
| 618 dbus_bus_remove_match(connection_, match_rule.c_str(), error); |
| 619 match_rules_added_.erase(match_rule); |
| 620 } |
| 621 return true; |
610 } | 622 } |
611 | 623 |
612 bool Bus::TryRegisterObjectPath(const ObjectPath& object_path, | 624 bool Bus::TryRegisterObjectPath(const ObjectPath& object_path, |
613 const DBusObjectPathVTable* vtable, | 625 const DBusObjectPathVTable* vtable, |
614 void* user_data, | 626 void* user_data, |
615 DBusError* error) { | 627 DBusError* error) { |
616 DCHECK(connection_); | 628 DCHECK(connection_); |
617 AssertOnDBusThread(); | 629 AssertOnDBusThread(); |
618 | 630 |
619 if (registered_object_paths_.find(object_path) != | 631 if (registered_object_paths_.find(object_path) != |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 } | 855 } |
844 | 856 |
845 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, | 857 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
846 DBusDispatchStatus status, | 858 DBusDispatchStatus status, |
847 void* data) { | 859 void* data) { |
848 Bus* self = static_cast<Bus*>(data); | 860 Bus* self = static_cast<Bus*>(data); |
849 self->OnDispatchStatusChanged(connection, status); | 861 self->OnDispatchStatusChanged(connection, status); |
850 } | 862 } |
851 | 863 |
852 } // namespace dbus | 864 } // namespace dbus |
OLD | NEW |