Add default implementations for AppWindowRegistry::Observer notifications.
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_gatt_characteristic_client.cc
blobe2e7c746ab00b055376749ef976ed322f2ffc918
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
7 #include "base/bind.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/observer_list.h"
10 #include "dbus/bus.h"
11 #include "dbus/object_manager.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
14 namespace chromeos {
16 BluetoothGattCharacteristicClient::Properties::Properties(
17 dbus::ObjectProxy* object_proxy,
18 const std::string& interface_name,
19 const PropertyChangedCallback& callback)
20 : dbus::PropertySet(object_proxy, interface_name, callback) {
21 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid);
22 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service);
23 RegisterProperty(bluetooth_gatt_characteristic::kValueProperty, &value);
24 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags);
27 BluetoothGattCharacteristicClient::Properties::~Properties() {
30 // The BluetoothGattCharacteristicClient implementation used in production.
31 class BluetoothGattCharacteristicClientImpl
32 : public BluetoothGattCharacteristicClient,
33 public dbus::ObjectManager::Interface {
34 public:
35 BluetoothGattCharacteristicClientImpl()
36 : object_manager_(NULL),
37 weak_ptr_factory_(this) {
40 virtual ~BluetoothGattCharacteristicClientImpl() {
41 object_manager_->UnregisterInterface(
42 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface);
45 // BluetoothGattCharacteristicClient override.
46 virtual void AddObserver(
47 BluetoothGattCharacteristicClient::Observer* observer) OVERRIDE {
48 DCHECK(observer);
49 observers_.AddObserver(observer);
52 // BluetoothGattCharacteristicClient override.
53 virtual void RemoveObserver(
54 BluetoothGattCharacteristicClient::Observer* observer) OVERRIDE {
55 DCHECK(observer);
56 observers_.RemoveObserver(observer);
59 // BluetoothGattCharacteristicClient override.
60 virtual std::vector<dbus::ObjectPath> GetCharacteristics() OVERRIDE {
61 DCHECK(object_manager_);
62 return object_manager_->GetObjectsWithInterface(
63 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface);
66 // BluetoothGattCharacteristicClient override.
67 virtual Properties* GetProperties(
68 const dbus::ObjectPath& object_path) OVERRIDE {
69 DCHECK(object_manager_);
70 return static_cast<Properties*>(
71 object_manager_->GetProperties(
72 object_path,
73 bluetooth_gatt_characteristic::
74 kBluetoothGattCharacteristicInterface));
77 // dbus::ObjectManager::Interface override.
78 virtual dbus::PropertySet* CreateProperties(
79 dbus::ObjectProxy *object_proxy,
80 const dbus::ObjectPath& object_path,
81 const std::string& interface_name) OVERRIDE {
82 Properties* properties = new Properties(
83 object_proxy,
84 interface_name,
85 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged,
86 weak_ptr_factory_.GetWeakPtr(),
87 object_path));
88 return static_cast<dbus::PropertySet*>(properties);
91 // dbus::ObjectManager::Interface override.
92 virtual void ObjectAdded(const dbus::ObjectPath& object_path,
93 const std::string& interface_name) OVERRIDE {
94 VLOG(2) << "Remote GATT characteristic added: " << object_path.value();
95 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
96 GattCharacteristicAdded(object_path));
99 // dbus::ObjectManager::Interface override.
100 virtual void ObjectRemoved(const dbus::ObjectPath& object_path,
101 const std::string& interface_name) OVERRIDE {
102 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value();
103 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
104 GattCharacteristicRemoved(object_path));
107 protected:
108 // chromeos::DBusClient override.
109 virtual void Init(dbus::Bus* bus) OVERRIDE {
110 object_manager_ = bus->GetObjectManager(
111 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
112 dbus::ObjectPath(
113 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
114 object_manager_->RegisterInterface(
115 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
116 this);
119 private:
120 // Called by dbus::PropertySet when a property value is changed, either by
121 // result of a signal or response to a GetAll() or Get() call. Informs
122 // observers.
123 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
124 const std::string& property_name) {
125 VLOG(2) << "Remote GATT characteristic property changed: "
126 << object_path.value() << ": " << property_name;
127 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
128 GattCharacteristicPropertyChanged(object_path,
129 property_name));
132 dbus::ObjectManager* object_manager_;
134 // List of observers interested in event notifications from us.
135 ObserverList<BluetoothGattCharacteristicClient::Observer> observers_;
137 // Weak pointer factory for generating 'this' pointers that might live longer
138 // than we do.
139 // Note: This should remain the last member so it'll be destroyed and
140 // invalidate its weak pointers before any other members are destroyed.
141 base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl>
142 weak_ptr_factory_;
144 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl);
147 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() {
150 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() {
153 // static
154 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() {
155 return new BluetoothGattCharacteristicClientImpl();
158 } // namespace chromeos