Remove unnecessary message_loop_proxy.h includes
[chromium-blink-merge.git] / device / bluetooth / bluetooth_advertisement_chromeos.cc
blob9b5f1bb682ec30e33911c5ad496beca257959f6b
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"
7 #include <string>
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"
21 namespace {
23 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) {
24 LOG(ERROR)
25 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = "
26 << error;
29 void ErrorCallbackConnector(
30 const device::BluetoothAdapter::CreateAdvertisementErrorCallback&
31 error_callback,
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);
53 } // namespace
55 namespace chromeos {
57 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS(
58 scoped_ptr<device::BluetoothAdvertisement::Data> data,
59 scoped_refptr<BluetoothAdapterChromeOS> adapter)
60 : adapter_(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>(
67 data->type()),
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&
75 error_callback) {
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,
92 // return an error.
93 if (!provider_) {
94 error_callback.Run(device::BluetoothAdvertisement::ErrorCode::
95 ERROR_ADVERTISEMENT_DOES_NOT_EXIST);
96 return;
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));
105 provider_.reset();
108 void BluetoothAdvertisementChromeOS::Released() {
109 LOG(WARNING) << "Advertisement released.";
110 provider_.reset();
111 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_,
112 AdvertisementReleased(this));
115 } // namespace chromeos