1 // Copyright 2015 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 "device/bluetooth/bluetooth_advertisement_chromeos.h"
10 #include "base/bind_helpers.h"
11 #include "base/guid.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chromeos/dbus/bluetooth_le_advertising_manager_client.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
23 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error
) {
25 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = "
29 void ErrorCallbackConnector(
30 const device::BluetoothAdapter::CreateAdvertisementErrorCallback
&
32 const std::string
& error_name
,
33 const std::string
& error_message
) {
34 LOG(WARNING
) << "Error while registering advertisement. error_name = "
35 << error_name
<< ", error_message = " << error_message
;
36 device::BluetoothAdvertisement::ErrorCode error_code
;
37 if (error_name
== bluetooth_advertising_manager::kErrorFailed
||
38 error_name
== bluetooth_advertising_manager::kErrorAlreadyExists
) {
39 error_code
= device::BluetoothAdvertisement::ErrorCode::
40 ERROR_ADVERTISEMENT_ALREADY_EXISTS
;
41 } else if (error_name
==
42 bluetooth_advertising_manager::kErrorInvalidArguments
) {
43 error_code
= device::BluetoothAdvertisement::ErrorCode::
44 ERROR_ADVERTISEMENT_INVALID_LENGTH
;
45 } else if (error_name
== bluetooth_advertising_manager::kErrorDoesNotExist
) {
46 error_code
= device::BluetoothAdvertisement::ErrorCode::
47 ERROR_ADVERTISEMENT_DOES_NOT_EXIST
;
50 error_callback
.Run(error_code
);
57 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS(
58 scoped_ptr
<device::BluetoothAdvertisement::Data
> data
,
59 scoped_refptr
<BluetoothAdapterChromeOS
> adapter
)
61 dbus::ObjectPath advertisement_object_path
= dbus::ObjectPath(
62 "/org/chromium/bluetooth_advertisement/" + base::GenerateGUID());
63 DCHECK(DBusThreadManager::Get());
64 provider_
= BluetoothLEAdvertisementServiceProvider::Create(
65 DBusThreadManager::Get()->GetSystemBus(), advertisement_object_path
, this,
66 static_cast<BluetoothLEAdvertisementServiceProvider::AdvertisementType
>(
68 data
->service_uuids().Pass(), data
->manufacturer_data().Pass(),
69 data
->solicit_uuids().Pass(), data
->service_data().Pass());
72 void BluetoothAdvertisementChromeOS::Register(
73 const base::Closure
& success_callback
,
74 const device::BluetoothAdapter::CreateAdvertisementErrorCallback
&
76 DCHECK(DBusThreadManager::Get());
77 DBusThreadManager::Get()
78 ->GetBluetoothLEAdvertisingManagerClient()
79 ->RegisterAdvertisement(
80 adapter_
->object_path(), provider_
->object_path(), success_callback
,
81 base::Bind(&ErrorCallbackConnector
, error_callback
));
84 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() {
85 Unregister(base::Bind(&base::DoNothing
), base::Bind(&UnregisterFailure
));
88 void BluetoothAdvertisementChromeOS::Unregister(
89 const SuccessCallback
& success_callback
,
90 const ErrorCallback
& error_callback
) {
91 // If we don't have a provider, that means we have already been unregistered,
94 error_callback
.Run(device::BluetoothAdvertisement::ErrorCode::
95 ERROR_ADVERTISEMENT_DOES_NOT_EXIST
);
99 DCHECK(DBusThreadManager::Get());
100 DBusThreadManager::Get()
101 ->GetBluetoothLEAdvertisingManagerClient()
102 ->UnregisterAdvertisement(
103 adapter_
->object_path(), provider_
->object_path(), success_callback
,
104 base::Bind(&ErrorCallbackConnector
, error_callback
));
108 void BluetoothAdvertisementChromeOS::Released() {
109 LOG(WARNING
) << "Advertisement released.";
111 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer
, observers_
,
112 AdvertisementReleased(this));
115 } // namespace chromeos