[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_gatt_descriptor_client.cc
blobbb8c3f8e0b869b947bdc7bf008354f6a5ad4ba56
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_descriptor_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 // static
17 const char BluetoothGattDescriptorClient::kNoResponseError[] =
18 "org.chromium.Error.NoResponse";
19 // static
20 const char BluetoothGattDescriptorClient::kUnknownDescriptorError[] =
21 "org.chromium.Error.UnknownDescriptor";
23 BluetoothGattDescriptorClient::Properties::Properties(
24 dbus::ObjectProxy* object_proxy,
25 const std::string& interface_name,
26 const PropertyChangedCallback&callback)
27 : dbus::PropertySet(object_proxy, interface_name, callback) {
28 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid);
29 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty,
30 &characteristic);
33 BluetoothGattDescriptorClient::Properties::~Properties() {
36 // The BluetoothGattDescriptorClient implementation used in production.
37 class BluetoothGattDescriptorClientImpl
38 : public BluetoothGattDescriptorClient,
39 public dbus::ObjectManager::Interface {
40 public:
41 BluetoothGattDescriptorClientImpl()
42 : object_manager_(NULL),
43 weak_ptr_factory_(this) {
46 ~BluetoothGattDescriptorClientImpl() override {
47 object_manager_->UnregisterInterface(
48 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface);
51 // BluetoothGattDescriptorClientImpl override.
52 void AddObserver(BluetoothGattDescriptorClient::Observer* observer) override {
53 DCHECK(observer);
54 observers_.AddObserver(observer);
57 // BluetoothGattDescriptorClientImpl override.
58 void RemoveObserver(
59 BluetoothGattDescriptorClient::Observer* observer) override {
60 DCHECK(observer);
61 observers_.RemoveObserver(observer);
64 // BluetoothGattDescriptorClientImpl override.
65 std::vector<dbus::ObjectPath> GetDescriptors() override {
66 DCHECK(object_manager_);
67 return object_manager_->GetObjectsWithInterface(
68 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface);
71 // BluetoothGattDescriptorClientImpl override.
72 Properties* GetProperties(const dbus::ObjectPath& object_path) override {
73 DCHECK(object_manager_);
74 return static_cast<Properties*>(
75 object_manager_->GetProperties(
76 object_path,
77 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface));
80 // BluetoothGattDescriptorClientImpl override.
81 void ReadValue(const dbus::ObjectPath& object_path,
82 const ValueCallback& callback,
83 const ErrorCallback& error_callback) override {
84 dbus::ObjectProxy* object_proxy =
85 object_manager_->GetObjectProxy(object_path);
86 if (!object_proxy) {
87 error_callback.Run(kUnknownDescriptorError, "");
88 return;
91 dbus::MethodCall method_call(
92 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
93 bluetooth_gatt_descriptor::kReadValue);
95 object_proxy->CallMethodWithErrorCallback(
96 &method_call,
97 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
98 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess,
99 weak_ptr_factory_.GetWeakPtr(),
100 callback),
101 base::Bind(&BluetoothGattDescriptorClientImpl::OnError,
102 weak_ptr_factory_.GetWeakPtr(),
103 error_callback));
106 // BluetoothGattDescriptorClientImpl override.
107 void WriteValue(const dbus::ObjectPath& object_path,
108 const std::vector<uint8>& value,
109 const base::Closure& callback,
110 const ErrorCallback& error_callback) override {
111 dbus::ObjectProxy* object_proxy =
112 object_manager_->GetObjectProxy(object_path);
113 if (!object_proxy) {
114 error_callback.Run(kUnknownDescriptorError, "");
115 return;
118 dbus::MethodCall method_call(
119 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
120 bluetooth_gatt_descriptor::kWriteValue);
121 dbus::MessageWriter writer(&method_call);
122 writer.AppendArrayOfBytes(value.data(), value.size());
124 object_proxy->CallMethodWithErrorCallback(
125 &method_call,
126 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
127 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess,
128 weak_ptr_factory_.GetWeakPtr(),
129 callback),
130 base::Bind(&BluetoothGattDescriptorClientImpl::OnError,
131 weak_ptr_factory_.GetWeakPtr(),
132 error_callback));
135 // dbus::ObjectManager::Interface override.
136 dbus::PropertySet* CreateProperties(
137 dbus::ObjectProxy* object_proxy,
138 const dbus::ObjectPath& object_path,
139 const std::string& interface_name) override {
140 Properties* properties = new Properties(
141 object_proxy,
142 interface_name,
143 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged,
144 weak_ptr_factory_.GetWeakPtr(),
145 object_path));
146 return static_cast<dbus::PropertySet*>(properties);
149 // dbus::ObjectManager::Interface override.
150 void ObjectAdded(const dbus::ObjectPath& object_path,
151 const std::string& interface_name) override {
152 VLOG(2) << "Remote GATT descriptor added: " << object_path.value();
153 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
154 GattDescriptorAdded(object_path));
157 // dbus::ObjectManager::Interface override.
158 void ObjectRemoved(const dbus::ObjectPath& object_path,
159 const std::string& interface_name) override {
160 VLOG(2) << "Remote GATT descriptor removed: " << object_path.value();
161 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
162 GattDescriptorRemoved(object_path));
165 protected:
166 // chromeos::DBusClient override.
167 void Init(dbus::Bus* bus) override {
168 object_manager_ = bus->GetObjectManager(
169 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
170 dbus::ObjectPath(
171 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
172 object_manager_->RegisterInterface(
173 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
174 this);
177 private:
178 // Called by dbus::PropertySet when a property value is changed, either by
179 // result of a signal or response to a GetAll() or Get() call. Informs
180 // observers.
181 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
182 const std::string& property_name) {
183 VLOG(2) << "Remote GATT descriptor property changed: "
184 << object_path.value() << ": " << property_name;
185 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_,
186 GattDescriptorPropertyChanged(object_path,
187 property_name));
190 // Called when a response for a successful method call is received.
191 void OnSuccess(const base::Closure& callback, dbus::Response* response) {
192 DCHECK(response);
193 callback.Run();
196 // Called when a descriptor value response for a successful method call is
197 // received.
198 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) {
199 DCHECK(response);
200 dbus::MessageReader reader(response);
202 const uint8* bytes = NULL;
203 size_t length = 0;
205 if (!reader.PopArrayOfBytes(&bytes, &length))
206 VLOG(2) << "Error reading array of bytes in ValueCallback";
208 std::vector<uint8> value;
210 if (bytes)
211 value.assign(bytes, bytes + length);
213 callback.Run(value);
216 // Called when a response for a failed method call is received.
217 void OnError(const ErrorCallback& error_callback,
218 dbus::ErrorResponse* response) {
219 // Error response has optional error message argument.
220 std::string error_name;
221 std::string error_message;
222 if (response) {
223 dbus::MessageReader reader(response);
224 error_name = response->GetErrorName();
225 reader.PopString(&error_message);
226 } else {
227 error_name = kNoResponseError;
228 error_message = "";
230 error_callback.Run(error_name, error_message);
233 dbus::ObjectManager* object_manager_;
235 // List of observers interested in event notifications from us.
236 ObserverList<BluetoothGattDescriptorClient::Observer> observers_;
238 // Weak pointer factory for generating 'this' pointers that might live longer
239 // than we do.
240 // Note: This should remain the last member so it'll be destroyed and
241 // invalidate its weak pointers before any other members are destroyed.
242 base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_;
244 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl);
247 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() {
250 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() {
253 // static
254 BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() {
255 return new BluetoothGattDescriptorClientImpl();
258 } // namespace chromeos