Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_le_advertising_manager_client.cc
blob88dafcbd332771a6219e7e8442127f737a649b5b
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 "base/location.h"
6 #include "base/logging.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "dbus/bus.h"
10 #include "dbus/message.h"
11 #include "dbus/object_proxy.h"
12 #include "fake_bluetooth_le_advertisement_service_provider.h"
13 #include "fake_bluetooth_le_advertising_manager_client.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 namespace chromeos {
18 const char FakeBluetoothLEAdvertisingManagerClient::kAdvertisingManagerPath[] =
19 "/fake/hci0";
21 FakeBluetoothLEAdvertisingManagerClient::
22 FakeBluetoothLEAdvertisingManagerClient() {
25 FakeBluetoothLEAdvertisingManagerClient::
26 ~FakeBluetoothLEAdvertisingManagerClient() {
29 void FakeBluetoothLEAdvertisingManagerClient::Init(dbus::Bus* bus) {
32 void FakeBluetoothLEAdvertisingManagerClient::AddObserver(Observer* observer) {
35 void FakeBluetoothLEAdvertisingManagerClient::RemoveObserver(
36 Observer* observer) {
39 void FakeBluetoothLEAdvertisingManagerClient::RegisterAdvertisement(
40 const dbus::ObjectPath& manager_object_path,
41 const dbus::ObjectPath& advertisement_object_path,
42 const base::Closure& callback,
43 const ErrorCallback& error_callback) {
44 VLOG(1) << "RegisterAdvertisment: " << advertisement_object_path.value();
46 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) {
47 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path.");
48 return;
51 ServiceProviderMap::iterator iter =
52 service_provider_map_.find(advertisement_object_path);
53 if (iter == service_provider_map_.end()) {
54 error_callback.Run(bluetooth_advertising_manager::kErrorInvalidArguments,
55 "Advertisement object not registered");
56 } else if (!currently_registered_.value().empty()) {
57 error_callback.Run(bluetooth_advertising_manager::kErrorFailed,
58 "Maximum advertisements reached");
59 } else {
60 currently_registered_ = advertisement_object_path;
61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
65 void FakeBluetoothLEAdvertisingManagerClient::UnregisterAdvertisement(
66 const dbus::ObjectPath& manager_object_path,
67 const dbus::ObjectPath& advertisement_object_path,
68 const base::Closure& callback,
69 const ErrorCallback& error_callback) {
70 VLOG(1) << "UnregisterAdvertisment: " << advertisement_object_path.value();
72 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) {
73 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path.");
74 return;
77 ServiceProviderMap::iterator iter =
78 service_provider_map_.find(advertisement_object_path);
79 if (iter == service_provider_map_.end()) {
80 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist,
81 "Advertisement not registered");
82 } else if (advertisement_object_path != currently_registered_) {
83 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist,
84 "Does not exist");
85 } else {
86 currently_registered_ = dbus::ObjectPath("");
87 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
91 void FakeBluetoothLEAdvertisingManagerClient::
92 RegisterAdvertisementServiceProvider(
93 FakeBluetoothLEAdvertisementServiceProvider* service_provider) {
94 service_provider_map_[service_provider->object_path_] = service_provider;
97 void FakeBluetoothLEAdvertisingManagerClient::
98 UnregisterAdvertisementServiceProvider(
99 FakeBluetoothLEAdvertisementServiceProvider* service_provider) {
100 ServiceProviderMap::iterator iter =
101 service_provider_map_.find(service_provider->object_path_);
102 if (iter != service_provider_map_.end() && iter->second == service_provider)
103 service_provider_map_.erase(iter);
106 } // namespace chromeos