Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: device/bluetooth/bluetooth_advertisement_chromeos.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/strings/string_util.h"
15 #include "dbus/bus.h"
16 #include "dbus/object_path.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/dbus/bluetooth_le_advertising_manager_client.h"
19 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21
22 namespace {
23
24 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) {
25 LOG(ERROR)
26 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = "
27 << error;
28 }
29
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(
50 const device::BluetoothAdapter::CreateAdvertisementErrorCallback&
51 error_callback,
52 const std::string& error_name,
53 const std::string& error_message) {
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 = "
65 << error_name << ", error_message = " << error_message;
66 error_callback.Run(GetErrorCodeFromErrorStrings(error_name, error_message));
67 }
68
69 } // namespace
70
71 namespace chromeos {
72
73 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS(
74 scoped_ptr<device::BluetoothAdvertisement::Data> data,
75 scoped_refptr<BluetoothAdapterChromeOS> adapter)
76 : adapter_(adapter) {
77 // Generate a new object path - make sure that we strip any -'s from the
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
85 DCHECK(bluez::BluezDBusManager::Get());
86 provider_ = bluez::BluetoothLEAdvertisementServiceProvider::Create(
87 bluez::BluezDBusManager::Get()->GetSystemBus(), advertisement_object_path,
88 this,
89 static_cast<
90 bluez::BluetoothLEAdvertisementServiceProvider::AdvertisementType>(
91 data->type()),
92 data->service_uuids().Pass(), data->manufacturer_data().Pass(),
93 data->solicit_uuids().Pass(), data->service_data().Pass());
94 }
95
96 void BluetoothAdvertisementChromeOS::Register(
97 const base::Closure& success_callback,
98 const device::BluetoothAdapter::CreateAdvertisementErrorCallback&
99 error_callback) {
100 DCHECK(bluez::BluezDBusManager::Get());
101 bluez::BluezDBusManager::Get()
102 ->GetBluetoothLEAdvertisingManagerClient()
103 ->RegisterAdvertisement(
104 adapter_->object_path(), provider_->object_path(), success_callback,
105 base::Bind(&RegisterErrorCallbackConnector, error_callback));
106 }
107
108 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() {
109 Unregister(base::Bind(&base::DoNothing), base::Bind(&UnregisterFailure));
110 }
111
112 void BluetoothAdvertisementChromeOS::Unregister(
113 const SuccessCallback& success_callback,
114 const ErrorCallback& error_callback) {
115 // If we don't have a provider, that means we have already been unregistered,
116 // return an error.
117 if (!provider_) {
118 error_callback.Run(device::BluetoothAdvertisement::ErrorCode::
119 ERROR_ADVERTISEMENT_DOES_NOT_EXIST);
120 return;
121 }
122
123 DCHECK(bluez::BluezDBusManager::Get());
124 bluez::BluezDBusManager::Get()
125 ->GetBluetoothLEAdvertisingManagerClient()
126 ->UnregisterAdvertisement(
127 adapter_->object_path(), provider_->object_path(), success_callback,
128 base::Bind(&UnregisterErrorCallbackConnector, error_callback));
129 provider_.reset();
130 }
131
132 void BluetoothAdvertisementChromeOS::Released() {
133 LOG(WARNING) << "Advertisement released.";
134 provider_.reset();
135 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_,
136 AdvertisementReleased(this));
137 }
138
139 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698