OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.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" | |
16 #include "dbus/bus.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" | |
20 | |
21 namespace { | |
22 | |
23 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) { | |
24 LOG(ERROR) << "Unregister failed with error code = " << error; | |
armansito
2015/04/24 22:11:00
nit: say "BluetoothAdvertisementChromeOS::Unregist
rkc
2015/04/24 22:55:25
Done.
| |
25 } | |
26 | |
27 void ErrorCallbackConnector( | |
28 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | |
29 error_callback, | |
30 const std::string& error_name, | |
31 const std::string& error_message) { | |
32 LOG(WARNING) << "Error while registering advertisement. error_name = " | |
33 << error_name << ", error_message = " << error_message; | |
34 device::BluetoothAdvertisement::ErrorCode error_code; | |
35 if (error_name == bluetooth_advertising_manager::kErrorFailed || | |
36 error_name == bluetooth_advertising_manager::kErrorAlreadyExists) { | |
37 error_code = | |
38 device::BluetoothAdvertisement::ErrorCode::ERROR_ALREADY_EXISTS; | |
39 } else if (error_name == | |
40 bluetooth_advertising_manager::kErrorInvalidArguments) { | |
41 error_code = | |
42 device::BluetoothAdvertisement::ErrorCode::ERROR_INVALID_LENGTH; | |
43 } else if (error_name == bluetooth_advertising_manager::kErrorDoesNotExist) { | |
44 error_code = | |
45 device::BluetoothAdvertisement::ErrorCode::ERROR_DOES_NOT_EXIST; | |
46 } | |
47 | |
48 error_callback.Run(error_code); | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 namespace chromeos { | |
54 | |
55 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS( | |
56 scoped_ptr<device::BluetoothAdvertisement::Data> data, | |
57 scoped_refptr<BluetoothAdapterChromeOS> adapter) | |
58 : adapter_(adapter) { | |
armansito
2015/04/24 22:11:00
Can you add DCHECKs for DBusThreadManager being in
rkc
2015/04/24 22:55:25
Done.
| |
59 dbus::ObjectPath advertisement_object_path = dbus::ObjectPath( | |
60 "/org/chromium/bluetooth_advertisement/" + base::GenerateGUID()); | |
61 provider_ = BluetoothLEAdvertisementServiceProvider::Create( | |
62 DBusThreadManager::Get()->GetSystemBus(), advertisement_object_path, this, | |
63 static_cast<BluetoothLEAdvertisementServiceProvider::AdvertisementType>( | |
64 data->type()), | |
65 data->service_uuids().Pass(), data->manufacturer_data().Pass(), | |
66 data->solicit_uuids().Pass(), data->service_data().Pass()); | |
67 } | |
68 | |
69 void BluetoothAdvertisementChromeOS::Register( | |
70 const base::Closure& success_callback, | |
71 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | |
72 error_callback) { | |
73 DBusThreadManager::Get() | |
74 ->GetBluetoothLEAdvertisingManagerClient() | |
75 ->RegisterAdvertisement( | |
76 adapter_->object_path(), provider_->object_path(), success_callback, | |
77 base::Bind(&ErrorCallbackConnector, error_callback)); | |
78 } | |
79 | |
80 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() { | |
81 Unregister(base::Bind(&base::DoNothing), base::Bind(&UnregisterFailure)); | |
82 } | |
83 | |
84 void BluetoothAdvertisementChromeOS::Unregister( | |
85 const SuccessCallback& success_callback, | |
86 const ErrorCallback& error_callback) { | |
87 // If we don't have a provider, that means we have already been unregistered, | |
88 // return an error. | |
89 if (!provider_) { | |
90 error_callback.Run( | |
91 device::BluetoothAdvertisement::ErrorCode::ERROR_DOES_NOT_EXIST); | |
92 return; | |
93 } | |
94 | |
95 DBusThreadManager::Get() | |
96 ->GetBluetoothLEAdvertisingManagerClient() | |
97 ->UnregisterAdvertisement( | |
98 adapter_->object_path(), provider_->object_path(), success_callback, | |
99 base::Bind(&ErrorCallbackConnector, error_callback)); | |
100 provider_.reset(); | |
101 } | |
102 | |
103 void BluetoothAdvertisementChromeOS::Released() { | |
104 LOG(WARNING) << "Advertisement released."; | |
105 provider_.reset(); | |
106 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_, | |
107 AdvertisementReleased(this)); | |
108 } | |
109 | |
110 } // namespace chromeos | |
OLD | NEW |