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_agent_service_provider.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/platform_thread.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/fake_bluetooth_agent_service_provider.h"
14 #include "dbus/exported_object.h"
15 #include "dbus/message.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
20 // The BluetoothAgentServiceProvider implementation used in production.
21 class BluetoothAgentServiceProviderImpl
22 : public BluetoothAgentServiceProvider
{
24 BluetoothAgentServiceProviderImpl(dbus::Bus
* bus
,
25 const dbus::ObjectPath
& object_path
,
27 : origin_thread_id_(base::PlatformThread::CurrentId()),
30 object_path_(object_path
),
31 weak_ptr_factory_(this) {
32 VLOG(1) << "Creating Bluetooth Agent: " << object_path_
.value();
34 exported_object_
= bus_
->GetExportedObject(object_path_
);
36 exported_object_
->ExportMethod(
37 bluetooth_agent::kBluetoothAgentInterface
,
38 bluetooth_agent::kRelease
,
39 base::Bind(&BluetoothAgentServiceProviderImpl::Release
,
40 weak_ptr_factory_
.GetWeakPtr()),
41 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
42 weak_ptr_factory_
.GetWeakPtr()));
44 exported_object_
->ExportMethod(
45 bluetooth_agent::kBluetoothAgentInterface
,
46 bluetooth_agent::kRequestPinCode
,
47 base::Bind(&BluetoothAgentServiceProviderImpl::RequestPinCode
,
48 weak_ptr_factory_
.GetWeakPtr()),
49 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
50 weak_ptr_factory_
.GetWeakPtr()));
52 exported_object_
->ExportMethod(
53 bluetooth_agent::kBluetoothAgentInterface
,
54 bluetooth_agent::kDisplayPinCode
,
55 base::Bind(&BluetoothAgentServiceProviderImpl::DisplayPinCode
,
56 weak_ptr_factory_
.GetWeakPtr()),
57 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
58 weak_ptr_factory_
.GetWeakPtr()));
60 exported_object_
->ExportMethod(
61 bluetooth_agent::kBluetoothAgentInterface
,
62 bluetooth_agent::kRequestPasskey
,
63 base::Bind(&BluetoothAgentServiceProviderImpl::RequestPasskey
,
64 weak_ptr_factory_
.GetWeakPtr()),
65 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
66 weak_ptr_factory_
.GetWeakPtr()));
68 exported_object_
->ExportMethod(
69 bluetooth_agent::kBluetoothAgentInterface
,
70 bluetooth_agent::kDisplayPasskey
,
71 base::Bind(&BluetoothAgentServiceProviderImpl::DisplayPasskey
,
72 weak_ptr_factory_
.GetWeakPtr()),
73 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
74 weak_ptr_factory_
.GetWeakPtr()));
76 exported_object_
->ExportMethod(
77 bluetooth_agent::kBluetoothAgentInterface
,
78 bluetooth_agent::kRequestConfirmation
,
79 base::Bind(&BluetoothAgentServiceProviderImpl::RequestConfirmation
,
80 weak_ptr_factory_
.GetWeakPtr()),
81 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
82 weak_ptr_factory_
.GetWeakPtr()));
84 exported_object_
->ExportMethod(
85 bluetooth_agent::kBluetoothAgentInterface
,
86 bluetooth_agent::kRequestAuthorization
,
87 base::Bind(&BluetoothAgentServiceProviderImpl::RequestAuthorization
,
88 weak_ptr_factory_
.GetWeakPtr()),
89 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
90 weak_ptr_factory_
.GetWeakPtr()));
92 exported_object_
->ExportMethod(
93 bluetooth_agent::kBluetoothAgentInterface
,
94 bluetooth_agent::kAuthorizeService
,
95 base::Bind(&BluetoothAgentServiceProviderImpl::AuthorizeService
,
96 weak_ptr_factory_
.GetWeakPtr()),
97 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
98 weak_ptr_factory_
.GetWeakPtr()));
100 exported_object_
->ExportMethod(
101 bluetooth_agent::kBluetoothAgentInterface
,
102 bluetooth_agent::kCancel
,
103 base::Bind(&BluetoothAgentServiceProviderImpl::Cancel
,
104 weak_ptr_factory_
.GetWeakPtr()),
105 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported
,
106 weak_ptr_factory_
.GetWeakPtr()));
109 ~BluetoothAgentServiceProviderImpl() override
{
110 VLOG(1) << "Cleaning up Bluetooth Agent: " << object_path_
.value();
112 // Unregister the object path so we can reuse with a new agent.
113 bus_
->UnregisterExportedObject(object_path_
);
117 // Returns true if the current thread is on the origin thread.
118 bool OnOriginThread() {
119 return base::PlatformThread::CurrentId() == origin_thread_id_
;
122 // Called by dbus:: when the agent is unregistered from the Bluetooth
123 // daemon, generally at the end of a pairing request.
124 void Release(dbus::MethodCall
* method_call
,
125 dbus::ExportedObject::ResponseSender response_sender
) {
126 DCHECK(OnOriginThread());
129 delegate_
->Released();
131 response_sender
.Run(dbus::Response::FromMethodCall(method_call
));
134 // Called by dbus:: when the Bluetooth daemon requires a PIN Code for
135 // device authentication.
136 void RequestPinCode(dbus::MethodCall
* method_call
,
137 dbus::ExportedObject::ResponseSender response_sender
) {
138 DCHECK(OnOriginThread());
141 dbus::MessageReader
reader(method_call
);
142 dbus::ObjectPath device_path
;
143 if (!reader
.PopObjectPath(&device_path
)) {
144 LOG(WARNING
) << "RequestPinCode called with incorrect paramters: "
145 << method_call
->ToString();
149 Delegate::PinCodeCallback callback
= base::Bind(
150 &BluetoothAgentServiceProviderImpl::OnPinCode
,
151 weak_ptr_factory_
.GetWeakPtr(),
155 delegate_
->RequestPinCode(device_path
, callback
);
158 // Called by dbus:: when the Bluetooth daemon requires that the user
159 // enter a PIN Code into the remote device so that it may be
161 void DisplayPinCode(dbus::MethodCall
* method_call
,
162 dbus::ExportedObject::ResponseSender response_sender
) {
163 DCHECK(OnOriginThread());
166 dbus::MessageReader
reader(method_call
);
167 dbus::ObjectPath device_path
;
169 if (!reader
.PopObjectPath(&device_path
) ||
170 !reader
.PopString(&pincode
)) {
171 LOG(WARNING
) << "DisplayPinCode called with incorrect paramters: "
172 << method_call
->ToString();
176 delegate_
->DisplayPinCode(device_path
, pincode
);
178 response_sender
.Run(dbus::Response::FromMethodCall(method_call
));
181 // Called by dbus:: when the Bluetooth daemon requires a Passkey for
182 // device authentication.
183 void RequestPasskey(dbus::MethodCall
* method_call
,
184 dbus::ExportedObject::ResponseSender response_sender
) {
185 DCHECK(OnOriginThread());
188 dbus::MessageReader
reader(method_call
);
189 dbus::ObjectPath device_path
;
190 if (!reader
.PopObjectPath(&device_path
)) {
191 LOG(WARNING
) << "RequestPasskey called with incorrect paramters: "
192 << method_call
->ToString();
196 Delegate::PasskeyCallback callback
= base::Bind(
197 &BluetoothAgentServiceProviderImpl::OnPasskey
,
198 weak_ptr_factory_
.GetWeakPtr(),
202 delegate_
->RequestPasskey(device_path
, callback
);
205 // Called by dbus:: when the Bluetooth daemon requires that the user
206 // enter a Passkey into the remote device so that it may be
208 void DisplayPasskey(dbus::MethodCall
* method_call
,
209 dbus::ExportedObject::ResponseSender response_sender
) {
210 DCHECK(OnOriginThread());
213 dbus::MessageReader
reader(method_call
);
214 dbus::ObjectPath device_path
;
217 if (!reader
.PopObjectPath(&device_path
) ||
218 !reader
.PopUint32(&passkey
) ||
219 !reader
.PopUint16(&entered
)) {
220 LOG(WARNING
) << "DisplayPasskey called with incorrect paramters: "
221 << method_call
->ToString();
225 delegate_
->DisplayPasskey(device_path
, passkey
, entered
);
227 response_sender
.Run(dbus::Response::FromMethodCall(method_call
));
230 // Called by dbus:: when the Bluetooth daemon requires that the user
231 // confirm that a Passkey is displayed on the screen of the remote
232 // device so that it may be authenticated.
233 void RequestConfirmation(
234 dbus::MethodCall
* method_call
,
235 dbus::ExportedObject::ResponseSender response_sender
) {
236 DCHECK(OnOriginThread());
239 dbus::MessageReader
reader(method_call
);
240 dbus::ObjectPath device_path
;
242 if (!reader
.PopObjectPath(&device_path
) ||
243 !reader
.PopUint32(&passkey
)) {
244 LOG(WARNING
) << "RequestConfirmation called with incorrect paramters: "
245 << method_call
->ToString();
249 Delegate::ConfirmationCallback callback
= base::Bind(
250 &BluetoothAgentServiceProviderImpl::OnConfirmation
,
251 weak_ptr_factory_
.GetWeakPtr(),
255 delegate_
->RequestConfirmation(device_path
, passkey
, callback
);
258 // Called by dbus:: when the Bluetooth daemon requires that the user
259 // confirm an incoming just-works pairing.
260 void RequestAuthorization(
261 dbus::MethodCall
* method_call
,
262 dbus::ExportedObject::ResponseSender response_sender
) {
263 DCHECK(OnOriginThread());
266 dbus::MessageReader
reader(method_call
);
267 dbus::ObjectPath device_path
;
268 if (!reader
.PopObjectPath(&device_path
)) {
269 LOG(WARNING
) << "RequestAuthorization called with incorrect paramters: "
270 << method_call
->ToString();
274 Delegate::ConfirmationCallback callback
= base::Bind(
275 &BluetoothAgentServiceProviderImpl::OnConfirmation
,
276 weak_ptr_factory_
.GetWeakPtr(),
280 delegate_
->RequestAuthorization(device_path
, callback
);
283 // Called by dbus:: when the Bluetooth daemon requires that the user
284 // confirm that that a remote device is authorized to connect to a service
286 void AuthorizeService(dbus::MethodCall
* method_call
,
287 dbus::ExportedObject::ResponseSender response_sender
) {
288 DCHECK(OnOriginThread());
291 dbus::MessageReader
reader(method_call
);
292 dbus::ObjectPath device_path
;
294 if (!reader
.PopObjectPath(&device_path
) ||
295 !reader
.PopString(&uuid
)) {
296 LOG(WARNING
) << "AuthorizeService called with incorrect paramters: "
297 << method_call
->ToString();
301 Delegate::ConfirmationCallback callback
= base::Bind(
302 &BluetoothAgentServiceProviderImpl::OnConfirmation
,
303 weak_ptr_factory_
.GetWeakPtr(),
307 delegate_
->AuthorizeService(device_path
, uuid
, callback
);
310 // Called by dbus:: when the request failed before a reply was returned
312 void Cancel(dbus::MethodCall
* method_call
,
313 dbus::ExportedObject::ResponseSender response_sender
) {
314 DCHECK(OnOriginThread());
319 response_sender
.Run(dbus::Response::FromMethodCall(method_call
));
322 // Called by dbus:: when a method is exported.
323 void OnExported(const std::string
& interface_name
,
324 const std::string
& method_name
,
326 LOG_IF(WARNING
, !success
) << "Failed to export "
327 << interface_name
<< "." << method_name
;
330 // Called by the Delegate to response to a method requesting a PIN code.
331 void OnPinCode(dbus::MethodCall
* method_call
,
332 dbus::ExportedObject::ResponseSender response_sender
,
333 Delegate::Status status
,
334 const std::string
& pincode
) {
335 DCHECK(OnOriginThread());
338 case Delegate::SUCCESS
: {
339 scoped_ptr
<dbus::Response
> response(
340 dbus::Response::FromMethodCall(method_call
));
341 dbus::MessageWriter
writer(response
.get());
342 writer
.AppendString(pincode
);
343 response_sender
.Run(response
.Pass());
346 case Delegate::REJECTED
: {
347 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
348 method_call
, bluetooth_agent::kErrorRejected
, "rejected"));
351 case Delegate::CANCELLED
: {
352 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
353 method_call
, bluetooth_agent::kErrorCanceled
, "canceled"));
357 NOTREACHED() << "Unexpected status code from delegate: " << status
;
361 // Called by the Delegate to response to a method requesting a Passkey.
362 void OnPasskey(dbus::MethodCall
* method_call
,
363 dbus::ExportedObject::ResponseSender response_sender
,
364 Delegate::Status status
,
366 DCHECK(OnOriginThread());
369 case Delegate::SUCCESS
: {
370 scoped_ptr
<dbus::Response
> response(
371 dbus::Response::FromMethodCall(method_call
));
372 dbus::MessageWriter
writer(response
.get());
373 writer
.AppendUint32(passkey
);
374 response_sender
.Run(response
.Pass());
377 case Delegate::REJECTED
: {
378 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
379 method_call
, bluetooth_agent::kErrorRejected
, "rejected"));
382 case Delegate::CANCELLED
: {
383 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
384 method_call
, bluetooth_agent::kErrorCanceled
, "canceled"));
388 NOTREACHED() << "Unexpected status code from delegate: " << status
;
392 // Called by the Delegate in response to a method requiring confirmation.
393 void OnConfirmation(dbus::MethodCall
* method_call
,
394 dbus::ExportedObject::ResponseSender response_sender
,
395 Delegate::Status status
) {
396 DCHECK(OnOriginThread());
399 case Delegate::SUCCESS
: {
400 response_sender
.Run(dbus::Response::FromMethodCall(method_call
));
403 case Delegate::REJECTED
: {
404 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
405 method_call
, bluetooth_agent::kErrorRejected
, "rejected"));
408 case Delegate::CANCELLED
: {
409 response_sender
.Run(dbus::ErrorResponse::FromMethodCall(
410 method_call
, bluetooth_agent::kErrorCanceled
, "canceled"));
414 NOTREACHED() << "Unexpected status code from delegate: " << status
;
418 // Origin thread (i.e. the UI thread in production).
419 base::PlatformThreadId origin_thread_id_
;
421 // D-Bus bus object is exported on, not owned by this object and must
425 // All incoming method calls are passed on to the Delegate and a callback
426 // passed to generate the reply. |delegate_| is generally the object that
427 // owns this one, and must outlive it.
430 // D-Bus object path of object we are exporting, kept so we can unregister
431 // again in our destructor.
432 dbus::ObjectPath object_path_
;
434 // D-Bus object we are exporting, owned by this object.
435 scoped_refptr
<dbus::ExportedObject
> exported_object_
;
437 // Weak pointer factory for generating 'this' pointers that might live longer
439 // Note: This should remain the last member so it'll be destroyed and
440 // invalidate its weak pointers before any other members are destroyed.
441 base::WeakPtrFactory
<BluetoothAgentServiceProviderImpl
> weak_ptr_factory_
;
443 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentServiceProviderImpl
);
446 BluetoothAgentServiceProvider::BluetoothAgentServiceProvider() {
449 BluetoothAgentServiceProvider::~BluetoothAgentServiceProvider() {
453 BluetoothAgentServiceProvider
* BluetoothAgentServiceProvider::Create(
455 const dbus::ObjectPath
& object_path
,
456 Delegate
* delegate
) {
457 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH
)) {
458 return new BluetoothAgentServiceProviderImpl(bus
, object_path
, delegate
);
460 return new FakeBluetoothAgentServiceProvider(object_path
, delegate
);
464 } // namespace chromeos