OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chromeos/dbus/bluetooth_profile_manager_client.h" | 5 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
11 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 13 |
14 namespace chromeos { | 14 namespace bluez { |
15 | 15 |
16 const char BluetoothProfileManagerClient::kNoResponseError[] = | 16 const char BluetoothProfileManagerClient::kNoResponseError[] = |
17 "org.chromium.Error.NoResponse"; | 17 "org.chromium.Error.NoResponse"; |
18 | 18 |
| 19 BluetoothProfileManagerClient::Options::Options() {} |
19 | 20 |
20 BluetoothProfileManagerClient::Options::Options() { | 21 BluetoothProfileManagerClient::Options::~Options() {} |
21 } | |
22 | |
23 BluetoothProfileManagerClient::Options::~Options() { | |
24 } | |
25 | 22 |
26 // The BluetoothProfileManagerClient implementation used in production. | 23 // The BluetoothProfileManagerClient implementation used in production. |
27 class BluetoothProfileManagerClientImpl | 24 class BluetoothProfileManagerClientImpl : public BluetoothProfileManagerClient { |
28 : public BluetoothProfileManagerClient { | |
29 public: | 25 public: |
30 BluetoothProfileManagerClientImpl() : weak_ptr_factory_(this) {} | 26 BluetoothProfileManagerClientImpl() : weak_ptr_factory_(this) {} |
31 | 27 |
32 ~BluetoothProfileManagerClientImpl() override {} | 28 ~BluetoothProfileManagerClientImpl() override {} |
33 | 29 |
34 // BluetoothProfileManagerClient override. | 30 // BluetoothProfileManagerClient override. |
35 void RegisterProfile(const dbus::ObjectPath& profile_path, | 31 void RegisterProfile(const dbus::ObjectPath& profile_path, |
36 const std::string& uuid, | 32 const std::string& uuid, |
37 const Options& options, | 33 const Options& options, |
38 const base::Closure& callback, | 34 const base::Closure& callback, |
39 const ErrorCallback& error_callback) override { | 35 const ErrorCallback& error_callback) override { |
40 dbus::MethodCall method_call( | 36 dbus::MethodCall method_call( |
41 bluetooth_profile_manager::kBluetoothProfileManagerInterface, | 37 bluetooth_profile_manager::kBluetoothProfileManagerInterface, |
42 bluetooth_profile_manager::kRegisterProfile); | 38 bluetooth_profile_manager::kRegisterProfile); |
43 | 39 |
44 dbus::MessageWriter writer(&method_call); | 40 dbus::MessageWriter writer(&method_call); |
45 writer.AppendObjectPath(profile_path); | 41 writer.AppendObjectPath(profile_path); |
46 writer.AppendString(uuid); | 42 writer.AppendString(uuid); |
47 | 43 |
48 dbus::MessageWriter array_writer(NULL); | 44 dbus::MessageWriter array_writer(NULL); |
49 writer.OpenArray("{sv}", &array_writer); | 45 writer.OpenArray("{sv}", &array_writer); |
50 | 46 |
51 dbus::MessageWriter dict_writer(NULL); | 47 dbus::MessageWriter dict_writer(NULL); |
52 | 48 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 array_writer.OpenDictEntry(&dict_writer); | 111 array_writer.OpenDictEntry(&dict_writer); |
116 dict_writer.AppendString( | 112 dict_writer.AppendString( |
117 bluetooth_profile_manager::kRequireAuthorizationOption); | 113 bluetooth_profile_manager::kRequireAuthorizationOption); |
118 dict_writer.AppendVariantOfBool(*(options.require_authorization)); | 114 dict_writer.AppendVariantOfBool(*(options.require_authorization)); |
119 array_writer.CloseContainer(&dict_writer); | 115 array_writer.CloseContainer(&dict_writer); |
120 } | 116 } |
121 | 117 |
122 // Send AutoConnect if provided. | 118 // Send AutoConnect if provided. |
123 if (options.auto_connect.get() != NULL) { | 119 if (options.auto_connect.get() != NULL) { |
124 array_writer.OpenDictEntry(&dict_writer); | 120 array_writer.OpenDictEntry(&dict_writer); |
125 dict_writer.AppendString( | 121 dict_writer.AppendString(bluetooth_profile_manager::kAutoConnectOption); |
126 bluetooth_profile_manager::kAutoConnectOption); | |
127 dict_writer.AppendVariantOfBool(*(options.auto_connect)); | 122 dict_writer.AppendVariantOfBool(*(options.auto_connect)); |
128 array_writer.CloseContainer(&dict_writer); | 123 array_writer.CloseContainer(&dict_writer); |
129 } | 124 } |
130 | 125 |
131 // Send ServiceRecord if provided. | 126 // Send ServiceRecord if provided. |
132 if (options.service_record.get() != NULL) { | 127 if (options.service_record.get() != NULL) { |
133 dbus::MessageWriter dict_writer(NULL); | 128 dbus::MessageWriter dict_writer(NULL); |
134 array_writer.OpenDictEntry(&dict_writer); | 129 array_writer.OpenDictEntry(&dict_writer); |
135 dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption); | 130 dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption); |
136 dict_writer.AppendVariantOfString(*(options.service_record)); | 131 dict_writer.AppendVariantOfString(*(options.service_record)); |
(...skipping 14 matching lines...) Expand all Loading... |
151 dbus::MessageWriter dict_writer(NULL); | 146 dbus::MessageWriter dict_writer(NULL); |
152 array_writer.OpenDictEntry(&dict_writer); | 147 array_writer.OpenDictEntry(&dict_writer); |
153 dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption); | 148 dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption); |
154 dict_writer.AppendVariantOfUint16(*(options.features)); | 149 dict_writer.AppendVariantOfUint16(*(options.features)); |
155 array_writer.CloseContainer(&dict_writer); | 150 array_writer.CloseContainer(&dict_writer); |
156 } | 151 } |
157 | 152 |
158 writer.CloseContainer(&array_writer); | 153 writer.CloseContainer(&array_writer); |
159 | 154 |
160 object_proxy_->CallMethodWithErrorCallback( | 155 object_proxy_->CallMethodWithErrorCallback( |
161 &method_call, | 156 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
162 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
163 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess, | 157 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess, |
164 weak_ptr_factory_.GetWeakPtr(), callback), | 158 weak_ptr_factory_.GetWeakPtr(), callback), |
165 base::Bind(&BluetoothProfileManagerClientImpl::OnError, | 159 base::Bind(&BluetoothProfileManagerClientImpl::OnError, |
166 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 160 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
167 } | 161 } |
168 | 162 |
169 // BluetoothProfileManagerClient override. | 163 // BluetoothProfileManagerClient override. |
170 void UnregisterProfile(const dbus::ObjectPath& profile_path, | 164 void UnregisterProfile(const dbus::ObjectPath& profile_path, |
171 const base::Closure& callback, | 165 const base::Closure& callback, |
172 const ErrorCallback& error_callback) override { | 166 const ErrorCallback& error_callback) override { |
173 dbus::MethodCall method_call( | 167 dbus::MethodCall method_call( |
174 bluetooth_profile_manager::kBluetoothProfileManagerInterface, | 168 bluetooth_profile_manager::kBluetoothProfileManagerInterface, |
175 bluetooth_profile_manager::kUnregisterProfile); | 169 bluetooth_profile_manager::kUnregisterProfile); |
176 | 170 |
177 dbus::MessageWriter writer(&method_call); | 171 dbus::MessageWriter writer(&method_call); |
178 writer.AppendObjectPath(profile_path); | 172 writer.AppendObjectPath(profile_path); |
179 | 173 |
180 object_proxy_->CallMethodWithErrorCallback( | 174 object_proxy_->CallMethodWithErrorCallback( |
181 &method_call, | 175 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
183 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess, | 176 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess, |
184 weak_ptr_factory_.GetWeakPtr(), callback), | 177 weak_ptr_factory_.GetWeakPtr(), callback), |
185 base::Bind(&BluetoothProfileManagerClientImpl::OnError, | 178 base::Bind(&BluetoothProfileManagerClientImpl::OnError, |
186 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 179 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
187 } | 180 } |
188 | 181 |
189 protected: | 182 protected: |
190 void Init(dbus::Bus* bus) override { | 183 void Init(dbus::Bus* bus) override { |
191 DCHECK(bus); | 184 DCHECK(bus); |
192 object_proxy_ = bus->GetObjectProxy( | 185 object_proxy_ = bus->GetObjectProxy( |
193 bluetooth_profile_manager::kBluetoothProfileManagerServiceName, | 186 bluetooth_profile_manager::kBluetoothProfileManagerServiceName, |
194 dbus::ObjectPath( | 187 dbus::ObjectPath( |
195 bluetooth_profile_manager::kBluetoothProfileManagerServicePath)); | 188 bluetooth_profile_manager::kBluetoothProfileManagerServicePath)); |
196 } | 189 } |
197 | 190 |
198 private: | 191 private: |
199 // Called when a response for successful method call is received. | 192 // Called when a response for successful method call is received. |
200 void OnSuccess(const base::Closure& callback, | 193 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
201 dbus::Response* response) { | |
202 DCHECK(response); | 194 DCHECK(response); |
203 callback.Run(); | 195 callback.Run(); |
204 } | 196 } |
205 | 197 |
206 // Called when a response for a failed method call is received. | 198 // Called when a response for a failed method call is received. |
207 void OnError(const ErrorCallback& error_callback, | 199 void OnError(const ErrorCallback& error_callback, |
208 dbus::ErrorResponse* response) { | 200 dbus::ErrorResponse* response) { |
209 // Error response has optional error message argument. | 201 // Error response has optional error message argument. |
210 std::string error_name; | 202 std::string error_name; |
211 std::string error_message; | 203 std::string error_message; |
(...skipping 12 matching lines...) Expand all Loading... |
224 | 216 |
225 // Weak pointer factory for generating 'this' pointers that might live longer | 217 // Weak pointer factory for generating 'this' pointers that might live longer |
226 // than we do. | 218 // than we do. |
227 // Note: This should remain the last member so it'll be destroyed and | 219 // Note: This should remain the last member so it'll be destroyed and |
228 // invalidate its weak pointers before any other members are destroyed. | 220 // invalidate its weak pointers before any other members are destroyed. |
229 base::WeakPtrFactory<BluetoothProfileManagerClientImpl> weak_ptr_factory_; | 221 base::WeakPtrFactory<BluetoothProfileManagerClientImpl> weak_ptr_factory_; |
230 | 222 |
231 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileManagerClientImpl); | 223 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileManagerClientImpl); |
232 }; | 224 }; |
233 | 225 |
234 BluetoothProfileManagerClient::BluetoothProfileManagerClient() { | 226 BluetoothProfileManagerClient::BluetoothProfileManagerClient() {} |
235 } | |
236 | 227 |
237 BluetoothProfileManagerClient::~BluetoothProfileManagerClient() { | 228 BluetoothProfileManagerClient::~BluetoothProfileManagerClient() {} |
238 } | |
239 | 229 |
240 BluetoothProfileManagerClient* BluetoothProfileManagerClient::Create() { | 230 BluetoothProfileManagerClient* BluetoothProfileManagerClient::Create() { |
241 return new BluetoothProfileManagerClientImpl(); | 231 return new BluetoothProfileManagerClientImpl(); |
242 } | 232 } |
243 | 233 |
244 } // namespace chromeos | 234 } // namespace bluez |
OLD | NEW |