Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / bluetooth_gatt_service_client.cc
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.
4
5 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
6
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"
13
14 namespace chromeos {
15
16 namespace {
17
18 // TODO(armansito): Add these to service_constants.h when they are defined
19 // in the BlueZ doc.
20 const char kDeviceProperty[] = "Device";
21 const char kPrimaryProperty[] = "Primary";
22
23 }  // namespace
24
25 BluetoothGattServiceClient::Properties::Properties(
26     dbus::ObjectProxy* object_proxy,
27     const std::string& interface_name,
28     const PropertyChangedCallback&callback)
29     : dbus::PropertySet(object_proxy, interface_name, callback) {
30   RegisterProperty(bluetooth_gatt_service::kUUIDProperty, &uuid);
31   RegisterProperty(bluetooth_gatt_service::kIncludesProperty, &includes);
32   RegisterProperty(kDeviceProperty, &device);
33   RegisterProperty(kPrimaryProperty, &primary);
34 }
35
36 BluetoothGattServiceClient::Properties::~Properties() {
37 }
38
39 // The BluetoothGattServiceClient implementation used in production.
40 class BluetoothGattServiceClientImpl : public BluetoothGattServiceClient,
41                                        public dbus::ObjectManager::Interface {
42  public:
43   BluetoothGattServiceClientImpl()
44       : object_manager_(NULL),
45         weak_ptr_factory_(this) {
46   }
47
48   virtual ~BluetoothGattServiceClientImpl() {
49     object_manager_->UnregisterInterface(
50         bluetooth_gatt_service::kBluetoothGattServiceInterface);
51   }
52
53   // BluetoothGattServiceClientImpl override.
54   virtual void AddObserver(
55       BluetoothGattServiceClient::Observer* observer) OVERRIDE {
56     DCHECK(observer);
57     observers_.AddObserver(observer);
58   }
59
60   // BluetoothGattServiceClientImpl override.
61   virtual void RemoveObserver(
62       BluetoothGattServiceClient::Observer* observer) OVERRIDE {
63     DCHECK(observer);
64     observers_.RemoveObserver(observer);
65   }
66
67   // BluetoothGattServiceClientImpl override.
68   virtual std::vector<dbus::ObjectPath> GetServices() OVERRIDE {
69     DCHECK(object_manager_);
70     return object_manager_->GetObjectsWithInterface(
71         bluetooth_gatt_service::kBluetoothGattServiceInterface);
72   }
73
74   // BluetoothGattServiceClientImpl override.
75   virtual Properties* GetProperties(
76       const dbus::ObjectPath& object_path) OVERRIDE {
77     DCHECK(object_manager_);
78     return static_cast<Properties*>(
79         object_manager_->GetProperties(
80             object_path,
81             bluetooth_gatt_service::kBluetoothGattServiceInterface));
82   }
83
84   // dbus::ObjectManager::Interface override.
85   virtual dbus::PropertySet* CreateProperties(
86       dbus::ObjectProxy *object_proxy,
87       const dbus::ObjectPath& object_path,
88       const std::string& interface_name) OVERRIDE {
89     Properties* properties = new Properties(
90         object_proxy,
91         interface_name,
92         base::Bind(&BluetoothGattServiceClientImpl::OnPropertyChanged,
93                    weak_ptr_factory_.GetWeakPtr(),
94                    object_path));
95     return static_cast<dbus::PropertySet*>(properties);
96   }
97
98   // dbus::ObjectManager::Interface override.
99   virtual void ObjectAdded(const dbus::ObjectPath& object_path,
100                            const std::string& interface_name) OVERRIDE {
101     VLOG(2) << "Remote GATT service added: " << object_path.value();
102     FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_,
103                       GattServiceAdded(object_path));
104   }
105
106   // dbus::ObjectManager::Interface override.
107   virtual void ObjectRemoved(const dbus::ObjectPath& object_path,
108                              const std::string& interface_name) OVERRIDE {
109     VLOG(2) << "Remote GATT service removed: " << object_path.value();
110     FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_,
111                       GattServiceRemoved(object_path));
112   }
113
114  protected:
115   // chromeos::DBusClient override.
116   virtual void Init(dbus::Bus* bus) OVERRIDE {
117     object_manager_ = bus->GetObjectManager(
118         bluetooth_object_manager::kBluetoothObjectManagerServiceName,
119         dbus::ObjectPath(
120             bluetooth_object_manager::kBluetoothObjectManagerServicePath));
121     object_manager_->RegisterInterface(
122         bluetooth_gatt_service::kBluetoothGattServiceInterface, this);
123   }
124
125  private:
126   // Called by dbus::PropertySet when a property value is changed, either by
127   // result of a signal or response to a GetAll() or Get() call. Informs
128   // observers.
129   virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
130                                  const std::string& property_name) {
131     VLOG(2) << "Remote GATT service property changed: " << object_path.value()
132             << ": " << property_name;
133     FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_,
134                       GattServicePropertyChanged(object_path, property_name));
135   }
136
137   dbus::ObjectManager* object_manager_;
138
139   // List of observers interested in event notifications from us.
140   ObserverList<BluetoothGattServiceClient::Observer> observers_;
141
142   // Weak pointer factory for generating 'this' pointers that might live longer
143   // than we do.
144   // Note: This should remain the last member so it'll be destroyed and
145   // invalidate its weak pointers before any other members are destroyed.
146   base::WeakPtrFactory<BluetoothGattServiceClientImpl> weak_ptr_factory_;
147
148   DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceClientImpl);
149 };
150
151 BluetoothGattServiceClient::BluetoothGattServiceClient() {
152 }
153
154 BluetoothGattServiceClient::~BluetoothGattServiceClient() {
155 }
156
157 // static
158 BluetoothGattServiceClient* BluetoothGattServiceClient::Create() {
159   return new BluetoothGattServiceClientImpl();
160 }
161
162 }  // namespace chromeos