OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/bluetooth_advertisement_chromeos.h" | 5 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/guid.h" | 11 #include "base/guid.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string_util.h" |
14 #include "chromeos/dbus/bluetooth_le_advertising_manager_client.h" | 15 #include "chromeos/dbus/bluetooth_le_advertising_manager_client.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "dbus/bus.h" | 17 #include "dbus/bus.h" |
17 #include "dbus/object_path.h" | 18 #include "dbus/object_path.h" |
18 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 19 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) { | 24 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) { |
24 LOG(ERROR) | 25 LOG(ERROR) |
25 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = " | 26 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = " |
26 << error; | 27 << error; |
27 } | 28 } |
28 | 29 |
29 void ErrorCallbackConnector( | 30 device::BluetoothAdvertisement::ErrorCode GetErrorCodeFromErrorStrings( |
| 31 const std::string& error_name, |
| 32 const std::string& error_message) { |
| 33 if (error_name == bluetooth_advertising_manager::kErrorFailed || |
| 34 error_name == bluetooth_advertising_manager::kErrorAlreadyExists) { |
| 35 return device::BluetoothAdvertisement::ErrorCode:: |
| 36 ERROR_ADVERTISEMENT_ALREADY_EXISTS; |
| 37 } else if (error_name == |
| 38 bluetooth_advertising_manager::kErrorInvalidArguments) { |
| 39 return device::BluetoothAdvertisement::ErrorCode:: |
| 40 ERROR_ADVERTISEMENT_INVALID_LENGTH; |
| 41 } else if (error_name == bluetooth_advertising_manager::kErrorDoesNotExist) { |
| 42 return device::BluetoothAdvertisement::ErrorCode:: |
| 43 ERROR_ADVERTISEMENT_DOES_NOT_EXIST; |
| 44 } |
| 45 return device::BluetoothAdvertisement::ErrorCode:: |
| 46 INVALID_ADVERTISEMENT_ERROR_CODE; |
| 47 } |
| 48 |
| 49 void RegisterErrorCallbackConnector( |
30 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | 50 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& |
31 error_callback, | 51 error_callback, |
32 const std::string& error_name, | 52 const std::string& error_name, |
33 const std::string& error_message) { | 53 const std::string& error_message) { |
34 LOG(WARNING) << "Error while registering advertisement. error_name = " | 54 LOG(ERROR) << "Error while registering advertisement. error_name = " |
| 55 << error_name << ", error_message = " << error_message; |
| 56 error_callback.Run(GetErrorCodeFromErrorStrings(error_name, error_message)); |
| 57 } |
| 58 |
| 59 void UnregisterErrorCallbackConnector( |
| 60 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& |
| 61 error_callback, |
| 62 const std::string& error_name, |
| 63 const std::string& error_message) { |
| 64 LOG(WARNING) << "Error while unregistering advertisement. error_name = " |
35 << error_name << ", error_message = " << error_message; | 65 << error_name << ", error_message = " << error_message; |
36 device::BluetoothAdvertisement::ErrorCode error_code; | 66 error_callback.Run(GetErrorCodeFromErrorStrings(error_name, error_message)); |
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; | |
48 } | |
49 | |
50 error_callback.Run(error_code); | |
51 } | 67 } |
52 | 68 |
53 } // namespace | 69 } // namespace |
54 | 70 |
55 namespace chromeos { | 71 namespace chromeos { |
56 | 72 |
57 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS( | 73 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS( |
58 scoped_ptr<device::BluetoothAdvertisement::Data> data, | 74 scoped_ptr<device::BluetoothAdvertisement::Data> data, |
59 scoped_refptr<BluetoothAdapterChromeOS> adapter) | 75 scoped_refptr<BluetoothAdapterChromeOS> adapter) |
60 : adapter_(adapter) { | 76 : adapter_(adapter) { |
61 dbus::ObjectPath advertisement_object_path = dbus::ObjectPath( | 77 // Generate a new object path - make sure that we strip any -'s from the |
62 "/org/chromium/bluetooth_advertisement/" + base::GenerateGUID()); | 78 // generated GUID string since object paths can only contain alphanumeric |
| 79 // characters and _ characters. |
| 80 std::string GuidString = base::GenerateGUID(); |
| 81 base::RemoveChars(GuidString, "-", &GuidString); |
| 82 dbus::ObjectPath advertisement_object_path = |
| 83 dbus::ObjectPath("/org/chromium/bluetooth_advertisement/" + GuidString); |
| 84 |
63 DCHECK(DBusThreadManager::Get()); | 85 DCHECK(DBusThreadManager::Get()); |
64 provider_ = BluetoothLEAdvertisementServiceProvider::Create( | 86 provider_ = BluetoothLEAdvertisementServiceProvider::Create( |
65 DBusThreadManager::Get()->GetSystemBus(), advertisement_object_path, this, | 87 DBusThreadManager::Get()->GetSystemBus(), advertisement_object_path, this, |
66 static_cast<BluetoothLEAdvertisementServiceProvider::AdvertisementType>( | 88 static_cast<BluetoothLEAdvertisementServiceProvider::AdvertisementType>( |
67 data->type()), | 89 data->type()), |
68 data->service_uuids().Pass(), data->manufacturer_data().Pass(), | 90 data->service_uuids().Pass(), data->manufacturer_data().Pass(), |
69 data->solicit_uuids().Pass(), data->service_data().Pass()); | 91 data->solicit_uuids().Pass(), data->service_data().Pass()); |
70 } | 92 } |
71 | 93 |
72 void BluetoothAdvertisementChromeOS::Register( | 94 void BluetoothAdvertisementChromeOS::Register( |
73 const base::Closure& success_callback, | 95 const base::Closure& success_callback, |
74 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | 96 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& |
75 error_callback) { | 97 error_callback) { |
76 DCHECK(DBusThreadManager::Get()); | 98 DCHECK(DBusThreadManager::Get()); |
77 DBusThreadManager::Get() | 99 DBusThreadManager::Get() |
78 ->GetBluetoothLEAdvertisingManagerClient() | 100 ->GetBluetoothLEAdvertisingManagerClient() |
79 ->RegisterAdvertisement( | 101 ->RegisterAdvertisement( |
80 adapter_->object_path(), provider_->object_path(), success_callback, | 102 adapter_->object_path(), provider_->object_path(), success_callback, |
81 base::Bind(&ErrorCallbackConnector, error_callback)); | 103 base::Bind(&RegisterErrorCallbackConnector, error_callback)); |
82 } | 104 } |
83 | 105 |
84 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() { | 106 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() { |
85 Unregister(base::Bind(&base::DoNothing), base::Bind(&UnregisterFailure)); | 107 Unregister(base::Bind(&base::DoNothing), base::Bind(&UnregisterFailure)); |
86 } | 108 } |
87 | 109 |
88 void BluetoothAdvertisementChromeOS::Unregister( | 110 void BluetoothAdvertisementChromeOS::Unregister( |
89 const SuccessCallback& success_callback, | 111 const SuccessCallback& success_callback, |
90 const ErrorCallback& error_callback) { | 112 const ErrorCallback& error_callback) { |
91 // If we don't have a provider, that means we have already been unregistered, | 113 // If we don't have a provider, that means we have already been unregistered, |
92 // return an error. | 114 // return an error. |
93 if (!provider_) { | 115 if (!provider_) { |
94 error_callback.Run(device::BluetoothAdvertisement::ErrorCode:: | 116 error_callback.Run(device::BluetoothAdvertisement::ErrorCode:: |
95 ERROR_ADVERTISEMENT_DOES_NOT_EXIST); | 117 ERROR_ADVERTISEMENT_DOES_NOT_EXIST); |
96 return; | 118 return; |
97 } | 119 } |
98 | 120 |
99 DCHECK(DBusThreadManager::Get()); | 121 DCHECK(DBusThreadManager::Get()); |
100 DBusThreadManager::Get() | 122 DBusThreadManager::Get() |
101 ->GetBluetoothLEAdvertisingManagerClient() | 123 ->GetBluetoothLEAdvertisingManagerClient() |
102 ->UnregisterAdvertisement( | 124 ->UnregisterAdvertisement( |
103 adapter_->object_path(), provider_->object_path(), success_callback, | 125 adapter_->object_path(), provider_->object_path(), success_callback, |
104 base::Bind(&ErrorCallbackConnector, error_callback)); | 126 base::Bind(&UnregisterErrorCallbackConnector, error_callback)); |
105 provider_.reset(); | 127 provider_.reset(); |
106 } | 128 } |
107 | 129 |
108 void BluetoothAdvertisementChromeOS::Released() { | 130 void BluetoothAdvertisementChromeOS::Released() { |
109 LOG(WARNING) << "Advertisement released."; | 131 LOG(WARNING) << "Advertisement released."; |
110 provider_.reset(); | 132 provider_.reset(); |
111 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_, | 133 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_, |
112 AdvertisementReleased(this)); | 134 AdvertisementReleased(this)); |
113 } | 135 } |
114 | 136 |
115 } // namespace chromeos | 137 } // namespace chromeos |
OLD | NEW |