OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h" | 5 #include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/logging.h" |
9 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
10 #include "dbus/bus.h" | 11 #include "dbus/bus.h" |
11 #include "dbus/message.h" | 12 #include "dbus/message.h" |
| 13 #include "dbus/object_manager.h" |
12 #include "dbus/object_proxy.h" | 14 #include "dbus/object_proxy.h" |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
14 | 16 |
15 namespace bluez { | 17 namespace bluez { |
16 | 18 |
17 const char BluetoothGattManagerClient::kNoResponseError[] = | 19 const char BluetoothGattManagerClient::kNoResponseError[] = |
18 "org.chromium.Error.NoResponse"; | 20 "org.chromium.Error.NoResponse"; |
19 | 21 |
20 // The BluetoothGattManagerClient implementation used in production. | 22 // The BluetoothGattManagerClient implementation used in production. |
21 class BluetoothGattManagerClientImpl : public BluetoothGattManagerClient { | 23 class BluetoothGattManagerClientImpl : public BluetoothGattManagerClient { |
22 public: | 24 public: |
23 BluetoothGattManagerClientImpl() | 25 BluetoothGattManagerClientImpl() |
24 : object_proxy_(NULL), weak_ptr_factory_(this) {} | 26 : object_manager_(nullptr), weak_ptr_factory_(this) {} |
25 | 27 |
26 ~BluetoothGattManagerClientImpl() override {} | 28 ~BluetoothGattManagerClientImpl() override {} |
27 | 29 |
28 // BluetoothGattManagerClient override. | 30 // BluetoothGattManagerClient override. |
29 void RegisterApplication(const dbus::ObjectPath& application_path, | 31 void RegisterApplication(const dbus::ObjectPath& adapter_object_path, |
| 32 const dbus::ObjectPath& application_path, |
30 const Options& options, | 33 const Options& options, |
31 const base::Closure& callback, | 34 const base::Closure& callback, |
32 const ErrorCallback& error_callback) override { | 35 const ErrorCallback& error_callback) override { |
33 dbus::MethodCall method_call( | 36 dbus::MethodCall method_call( |
34 bluetooth_gatt_manager::kBluetoothGattManagerInterface, | 37 bluetooth_gatt_manager::kBluetoothGattManagerInterface, |
35 bluetooth_gatt_manager::kRegisterService); | 38 bluetooth_gatt_manager::kRegisterService); |
36 | 39 |
37 dbus::MessageWriter writer(&method_call); | 40 dbus::MessageWriter writer(&method_call); |
38 writer.AppendObjectPath(application_path); | 41 writer.AppendObjectPath(application_path); |
39 | 42 |
40 // The parameters of the Options dictionary are undefined but the method | 43 // The parameters of the Options dictionary are undefined but the method |
41 // signature still requires a value dictionary. Pass an empty dictionary | 44 // signature still requires a value dictionary. Pass an empty dictionary |
42 // and fill in the contents later if and when we add any options. | 45 // and fill in the contents later if and when we add any options. |
43 dbus::MessageWriter array_writer(NULL); | 46 dbus::MessageWriter array_writer(NULL); |
44 writer.OpenArray("{sv}", &array_writer); | 47 writer.OpenArray("{sv}", &array_writer); |
45 writer.CloseContainer(&array_writer); | 48 writer.CloseContainer(&array_writer); |
46 | 49 |
47 DCHECK(object_proxy_); | 50 DCHECK(object_manager_); |
48 object_proxy_->CallMethodWithErrorCallback( | 51 dbus::ObjectProxy* object_proxy = |
| 52 object_manager_->GetObjectProxy(adapter_object_path); |
| 53 DCHECK(object_proxy); |
| 54 object_proxy->CallMethodWithErrorCallback( |
49 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 55 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
50 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, | 56 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, |
51 weak_ptr_factory_.GetWeakPtr(), callback), | 57 weak_ptr_factory_.GetWeakPtr(), callback), |
52 base::Bind(&BluetoothGattManagerClientImpl::OnError, | 58 base::Bind(&BluetoothGattManagerClientImpl::OnError, |
53 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 59 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
54 } | 60 } |
55 | 61 |
56 // BluetoothGattManagerClient override. | 62 // BluetoothGattManagerClient override. |
57 void UnregisterApplication(const dbus::ObjectPath& application_path, | 63 void UnregisterApplication(const dbus::ObjectPath& adapter_object_path, |
| 64 const dbus::ObjectPath& application_path, |
58 const base::Closure& callback, | 65 const base::Closure& callback, |
59 const ErrorCallback& error_callback) override { | 66 const ErrorCallback& error_callback) override { |
60 dbus::MethodCall method_call( | 67 dbus::MethodCall method_call( |
61 bluetooth_gatt_manager::kBluetoothGattManagerInterface, | 68 bluetooth_gatt_manager::kBluetoothGattManagerInterface, |
62 bluetooth_gatt_manager::kUnregisterService); | 69 bluetooth_gatt_manager::kUnregisterService); |
63 | 70 |
64 dbus::MessageWriter writer(&method_call); | 71 dbus::MessageWriter writer(&method_call); |
65 writer.AppendObjectPath(application_path); | 72 writer.AppendObjectPath(application_path); |
66 | 73 |
67 DCHECK(object_proxy_); | 74 DCHECK(object_manager_); |
68 object_proxy_->CallMethodWithErrorCallback( | 75 dbus::ObjectProxy* object_proxy = |
| 76 object_manager_->GetObjectProxy(adapter_object_path); |
| 77 DCHECK(object_proxy); |
| 78 object_proxy->CallMethodWithErrorCallback( |
69 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 79 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
70 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, | 80 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, |
71 weak_ptr_factory_.GetWeakPtr(), callback), | 81 weak_ptr_factory_.GetWeakPtr(), callback), |
72 base::Bind(&BluetoothGattManagerClientImpl::OnError, | 82 base::Bind(&BluetoothGattManagerClientImpl::OnError, |
73 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 83 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
74 } | 84 } |
75 | 85 |
76 protected: | 86 protected: |
77 // bluez::DBusClient override. | 87 // bluez::DBusClient override. |
78 void Init(dbus::Bus* bus) override { | 88 void Init(dbus::Bus* bus) override { |
79 DCHECK(bus); | 89 DCHECK(bus); |
80 object_proxy_ = bus->GetObjectProxy( | 90 DCHECK(bus); |
81 bluetooth_gatt_manager::kBluetoothGattManagerServiceName, | 91 object_manager_ = bus->GetObjectManager( |
| 92 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
82 dbus::ObjectPath( | 93 dbus::ObjectPath( |
83 bluetooth_gatt_manager::kBluetoothGattManagerInterface)); | 94 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
84 } | 95 } |
85 | 96 |
86 private: | 97 private: |
87 // Called when a response for a successful method call is received. | 98 // Called when a response for a successful method call is received. |
88 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 99 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
89 DCHECK(response); | 100 DCHECK(response); |
90 callback.Run(); | 101 callback.Run(); |
91 } | 102 } |
92 | 103 |
93 // Called when a response for a failed method call is received. | 104 // Called when a response for a failed method call is received. |
94 void OnError(const ErrorCallback& error_callback, | 105 void OnError(const ErrorCallback& error_callback, |
95 dbus::ErrorResponse* response) { | 106 dbus::ErrorResponse* response) { |
96 // Error response has optional error message argument. | 107 // Error response has optional error message argument. |
97 std::string error_name; | 108 std::string error_name; |
98 std::string error_message; | 109 std::string error_message; |
99 if (response) { | 110 if (response) { |
100 dbus::MessageReader reader(response); | 111 dbus::MessageReader reader(response); |
101 error_name = response->GetErrorName(); | 112 error_name = response->GetErrorName(); |
102 reader.PopString(&error_message); | 113 reader.PopString(&error_message); |
103 } else { | 114 } else { |
104 error_name = kNoResponseError; | 115 error_name = kNoResponseError; |
105 } | 116 } |
106 error_callback.Run(error_name, error_message); | 117 error_callback.Run(error_name, error_message); |
107 } | 118 } |
108 | 119 |
109 // The proxy to the remote GATT manager object. | 120 // The proxy to the bluez object manager. |
110 dbus::ObjectProxy* object_proxy_; | 121 dbus::ObjectManager* object_manager_; |
111 | 122 |
112 // Weak pointer factory for generating 'this' pointers that might live longer | 123 // Weak pointer factory for generating 'this' pointers that might live longer |
113 // than we do. | 124 // than we do. |
114 // Note: This should remain the last member so it'll be destroyed and | 125 // Note: This should remain the last member so it'll be destroyed and |
115 // invalidate its weak pointers before any other members are destroyed. | 126 // invalidate its weak pointers before any other members are destroyed. |
116 base::WeakPtrFactory<BluetoothGattManagerClientImpl> weak_ptr_factory_; | 127 base::WeakPtrFactory<BluetoothGattManagerClientImpl> weak_ptr_factory_; |
117 | 128 |
118 DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl); | 129 DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl); |
119 }; | 130 }; |
120 | 131 |
121 BluetoothGattManagerClient::BluetoothGattManagerClient() {} | 132 BluetoothGattManagerClient::BluetoothGattManagerClient() {} |
122 | 133 |
123 BluetoothGattManagerClient::~BluetoothGattManagerClient() {} | 134 BluetoothGattManagerClient::~BluetoothGattManagerClient() {} |
124 | 135 |
125 // static | 136 // static |
126 BluetoothGattManagerClient* BluetoothGattManagerClient::Create() { | 137 BluetoothGattManagerClient* BluetoothGattManagerClient::Create() { |
127 return new BluetoothGattManagerClientImpl(); | 138 return new BluetoothGattManagerClientImpl(); |
128 } | 139 } |
129 | 140 |
130 } // namespace bluez | 141 } // namespace bluez |
OLD | NEW |