Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_adapter_client.cc
blobc7adcb7a6a887d221ad40c007a32b94242b36855
1 // Copyright 2013 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_adapter_client.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "dbus/bus.h"
10 #include "dbus/message.h"
11 #include "dbus/object_manager.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 namespace chromeos {
17 BluetoothAdapterClient::DiscoveryFilter::DiscoveryFilter() {
20 BluetoothAdapterClient::DiscoveryFilter::~DiscoveryFilter() {
23 const char BluetoothAdapterClient::kNoResponseError[] =
24 "org.chromium.Error.NoResponse";
25 const char BluetoothAdapterClient::kUnknownAdapterError[] =
26 "org.chromium.Error.UnknownAdapter";
28 BluetoothAdapterClient::Properties::Properties(
29 dbus::ObjectProxy* object_proxy,
30 const std::string& interface_name,
31 const PropertyChangedCallback& callback)
32 : dbus::PropertySet(object_proxy, interface_name, callback) {
33 RegisterProperty(bluetooth_adapter::kAddressProperty, &address);
34 RegisterProperty(bluetooth_adapter::kNameProperty, &name);
35 RegisterProperty(bluetooth_adapter::kAliasProperty, &alias);
36 RegisterProperty(bluetooth_adapter::kClassProperty, &bluetooth_class);
37 RegisterProperty(bluetooth_adapter::kPoweredProperty, &powered);
38 RegisterProperty(bluetooth_adapter::kDiscoverableProperty, &discoverable);
39 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable);
40 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty,
41 &pairable_timeout);
42 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty,
43 &discoverable_timeout);
44 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering);
45 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids);
46 RegisterProperty(bluetooth_adapter::kModaliasProperty, &modalias);
49 BluetoothAdapterClient::Properties::~Properties() {
53 // The BluetoothAdapterClient implementation used in production.
54 class BluetoothAdapterClientImpl
55 : public BluetoothAdapterClient,
56 public dbus::ObjectManager::Interface {
57 public:
58 BluetoothAdapterClientImpl()
59 : object_manager_(NULL), weak_ptr_factory_(this) {}
61 ~BluetoothAdapterClientImpl() override {
62 object_manager_->UnregisterInterface(
63 bluetooth_adapter::kBluetoothAdapterInterface);
66 // BluetoothAdapterClient override.
67 void AddObserver(BluetoothAdapterClient::Observer* observer) override {
68 DCHECK(observer);
69 observers_.AddObserver(observer);
72 // BluetoothAdapterClient override.
73 void RemoveObserver(BluetoothAdapterClient::Observer* observer) override {
74 DCHECK(observer);
75 observers_.RemoveObserver(observer);
78 // Returns the list of adapter object paths known to the system.
79 std::vector<dbus::ObjectPath> GetAdapters() override {
80 return object_manager_->GetObjectsWithInterface(
81 bluetooth_adapter::kBluetoothAdapterInterface);
84 // dbus::ObjectManager::Interface override.
85 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(&BluetoothAdapterClientImpl::OnPropertyChanged,
93 weak_ptr_factory_.GetWeakPtr(),
94 object_path));
95 return static_cast<dbus::PropertySet*>(properties);
98 // BluetoothAdapterClient override.
99 Properties* GetProperties(const dbus::ObjectPath& object_path) override {
100 return static_cast<Properties*>(
101 object_manager_->GetProperties(
102 object_path,
103 bluetooth_adapter::kBluetoothAdapterInterface));
106 // BluetoothAdapterClient override.
107 void StartDiscovery(const dbus::ObjectPath& object_path,
108 const base::Closure& callback,
109 const ErrorCallback& error_callback) override {
110 dbus::MethodCall method_call(
111 bluetooth_adapter::kBluetoothAdapterInterface,
112 bluetooth_adapter::kStartDiscovery);
114 dbus::ObjectProxy* object_proxy =
115 object_manager_->GetObjectProxy(object_path);
116 if (!object_proxy) {
117 error_callback.Run(kUnknownAdapterError, "");
118 return;
121 object_proxy->CallMethodWithErrorCallback(
122 &method_call,
123 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
124 base::Bind(&BluetoothAdapterClientImpl::OnSuccess,
125 weak_ptr_factory_.GetWeakPtr(), callback),
126 base::Bind(&BluetoothAdapterClientImpl::OnError,
127 weak_ptr_factory_.GetWeakPtr(), error_callback));
130 // BluetoothAdapterClient override.
131 void StopDiscovery(const dbus::ObjectPath& object_path,
132 const base::Closure& callback,
133 const ErrorCallback& error_callback) override {
134 dbus::MethodCall method_call(
135 bluetooth_adapter::kBluetoothAdapterInterface,
136 bluetooth_adapter::kStopDiscovery);
138 dbus::ObjectProxy* object_proxy =
139 object_manager_->GetObjectProxy(object_path);
140 if (!object_proxy) {
141 error_callback.Run(kUnknownAdapterError, "");
142 return;
145 object_proxy->CallMethodWithErrorCallback(
146 &method_call,
147 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
148 base::Bind(&BluetoothAdapterClientImpl::OnSuccess,
149 weak_ptr_factory_.GetWeakPtr(), callback),
150 base::Bind(&BluetoothAdapterClientImpl::OnError,
151 weak_ptr_factory_.GetWeakPtr(), error_callback));
154 // BluetoothAdapterClient override.
155 void RemoveDevice(const dbus::ObjectPath& object_path,
156 const dbus::ObjectPath& device_path,
157 const base::Closure& callback,
158 const ErrorCallback& error_callback) override {
159 dbus::MethodCall method_call(
160 bluetooth_adapter::kBluetoothAdapterInterface,
161 bluetooth_adapter::kRemoveDevice);
163 dbus::MessageWriter writer(&method_call);
164 writer.AppendObjectPath(device_path);
166 dbus::ObjectProxy* object_proxy =
167 object_manager_->GetObjectProxy(object_path);
168 if (!object_proxy) {
169 error_callback.Run(kUnknownAdapterError, "");
170 return;
173 object_proxy->CallMethodWithErrorCallback(
174 &method_call,
175 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
176 base::Bind(&BluetoothAdapterClientImpl::OnSuccess,
177 weak_ptr_factory_.GetWeakPtr(), callback),
178 base::Bind(&BluetoothAdapterClientImpl::OnError,
179 weak_ptr_factory_.GetWeakPtr(), error_callback));
182 // BluetoothAdapterClient override.
183 void SetDiscoveryFilter(const dbus::ObjectPath& object_path,
184 const DiscoveryFilter& discovery_filter,
185 const base::Closure& callback,
186 const ErrorCallback& error_callback) override {
187 dbus::MethodCall method_call(bluetooth_adapter::kBluetoothAdapterInterface,
188 bluetooth_adapter::kSetDiscoveryFilter);
190 dbus::MessageWriter writer(&method_call);
191 dbus::MessageWriter dict_writer(nullptr);
193 dbus::ObjectProxy* object_proxy =
194 object_manager_->GetObjectProxy(object_path);
195 if (!object_proxy) {
196 error_callback.Run(kUnknownAdapterError, "");
197 return;
200 writer.OpenArray("{sv}", &dict_writer);
202 if (discovery_filter.uuids.get()) {
203 std::vector<std::string>* uuids = discovery_filter.uuids.get();
204 dbus::MessageWriter uuids_entry_writer(nullptr);
205 dict_writer.OpenDictEntry(&uuids_entry_writer);
206 uuids_entry_writer.AppendString(
207 bluetooth_adapter::kDiscoveryFilterParameterUUIDs);
209 dbus::MessageWriter uuids_array_variant(nullptr);
210 uuids_entry_writer.OpenVariant("as", &uuids_array_variant);
211 dbus::MessageWriter uuids_array(nullptr);
212 uuids_array_variant.OpenArray("s", &uuids_array);
214 for (auto& it : *uuids)
215 uuids_array.AppendString(it);
217 uuids_array_variant.CloseContainer(&uuids_array);
218 uuids_entry_writer.CloseContainer(&uuids_array_variant);
219 dict_writer.CloseContainer(&uuids_entry_writer);
222 if (discovery_filter.rssi.get()) {
223 dbus::MessageWriter rssi_entry_writer(nullptr);
224 dict_writer.OpenDictEntry(&rssi_entry_writer);
225 rssi_entry_writer.AppendString(
226 bluetooth_adapter::kDiscoveryFilterParameterRSSI);
227 rssi_entry_writer.AppendVariantOfInt16(*discovery_filter.rssi.get());
228 dict_writer.CloseContainer(&rssi_entry_writer);
231 if (discovery_filter.pathloss.get()) {
232 dbus::MessageWriter pathloss_entry_writer(nullptr);
233 dict_writer.OpenDictEntry(&pathloss_entry_writer);
234 pathloss_entry_writer.AppendString(
235 bluetooth_adapter::kDiscoveryFilterParameterPathloss);
236 pathloss_entry_writer.AppendVariantOfUint16(
237 *discovery_filter.pathloss.get());
238 dict_writer.CloseContainer(&pathloss_entry_writer);
241 if (discovery_filter.transport.get()) {
242 dbus::MessageWriter transport_entry_writer(nullptr);
243 dict_writer.OpenDictEntry(&transport_entry_writer);
244 transport_entry_writer.AppendString(
245 bluetooth_adapter::kDiscoveryFilterParameterTransport);
246 transport_entry_writer.AppendVariantOfString(
247 *discovery_filter.transport.get());
248 dict_writer.CloseContainer(&transport_entry_writer);
251 writer.CloseContainer(&dict_writer);
253 object_proxy->CallMethodWithErrorCallback(
254 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
255 base::Bind(&BluetoothAdapterClientImpl::OnSuccess,
256 weak_ptr_factory_.GetWeakPtr(), callback),
257 base::Bind(&BluetoothAdapterClientImpl::OnError,
258 weak_ptr_factory_.GetWeakPtr(), error_callback));
261 protected:
262 void Init(dbus::Bus* bus) override {
263 object_manager_ = bus->GetObjectManager(
264 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
265 dbus::ObjectPath(
266 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
267 object_manager_->RegisterInterface(
268 bluetooth_adapter::kBluetoothAdapterInterface, this);
271 private:
272 // Called by dbus::ObjectManager when an object with the adapter interface
273 // is created. Informs observers.
274 void ObjectAdded(const dbus::ObjectPath& object_path,
275 const std::string& interface_name) override {
276 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
277 AdapterAdded(object_path));
280 // Called by dbus::ObjectManager when an object with the adapter interface
281 // is removed. Informs observers.
282 void ObjectRemoved(const dbus::ObjectPath& object_path,
283 const std::string& interface_name) override {
284 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
285 AdapterRemoved(object_path));
288 // Called by dbus::PropertySet when a property value is changed,
289 // either by result of a signal or response to a GetAll() or Get()
290 // call. Informs observers.
291 void OnPropertyChanged(const dbus::ObjectPath& object_path,
292 const std::string& property_name) {
293 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
294 AdapterPropertyChanged(object_path, property_name));
297 // Called when a response for successful method call is received.
298 void OnSuccess(const base::Closure& callback,
299 dbus::Response* response) {
300 DCHECK(response);
301 callback.Run();
304 // Called when a response for a failed method call is received.
305 void OnError(const ErrorCallback& error_callback,
306 dbus::ErrorResponse* response) {
307 // Error response has optional error message argument.
308 std::string error_name;
309 std::string error_message;
310 if (response) {
311 dbus::MessageReader reader(response);
312 error_name = response->GetErrorName();
313 reader.PopString(&error_message);
314 } else {
315 error_name = kNoResponseError;
316 error_message = "";
318 error_callback.Run(error_name, error_message);
321 dbus::ObjectManager* object_manager_;
323 // List of observers interested in event notifications from us.
324 ObserverList<BluetoothAdapterClient::Observer> observers_;
326 // Weak pointer factory for generating 'this' pointers that might live longer
327 // than we do.
328 // Note: This should remain the last member so it'll be destroyed and
329 // invalidate its weak pointers before any other members are destroyed.
330 base::WeakPtrFactory<BluetoothAdapterClientImpl>
331 weak_ptr_factory_;
333 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl);
336 BluetoothAdapterClient::BluetoothAdapterClient() {
339 BluetoothAdapterClient::~BluetoothAdapterClient() {
342 BluetoothAdapterClient* BluetoothAdapterClient::Create() {
343 return new BluetoothAdapterClientImpl;
346 } // namespace chromeos