Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_gatt_descriptor_service_provider.cc
blob9779c3aee0eae1d0d236809bf81d0888de3f43ec
1 // Copyright 2014 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 "chromeos/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_service_provider.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_manager_client.h"
13 namespace chromeos {
15 FakeBluetoothGattDescriptorServiceProvider::
16 FakeBluetoothGattDescriptorServiceProvider(
17 const dbus::ObjectPath& object_path,
18 Delegate* delegate,
19 const std::string& uuid,
20 const std::vector<std::string>& permissions,
21 const dbus::ObjectPath& characteristic_path)
22 : object_path_(object_path),
23 uuid_(uuid),
24 characteristic_path_(characteristic_path),
25 delegate_(delegate) {
26 VLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_.value();
28 DCHECK(object_path_.IsValid());
29 DCHECK(characteristic_path_.IsValid());
30 DCHECK(!uuid.empty());
31 DCHECK(delegate_);
32 DCHECK(base::StartsWith(object_path_.value(),
33 characteristic_path_.value() + "/",
34 base::CompareCase::SENSITIVE));
36 // TODO(armansito): Do something with |permissions|.
38 FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
39 static_cast<FakeBluetoothGattManagerClient*>(
40 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
41 fake_bluetooth_gatt_manager_client->
42 RegisterDescriptorServiceProvider(this);
45 FakeBluetoothGattDescriptorServiceProvider::
46 ~FakeBluetoothGattDescriptorServiceProvider() {
47 VLOG(1) << "Cleaning up Bluetooth GATT descriptor: "
48 << object_path_.value();
50 FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
51 static_cast<FakeBluetoothGattManagerClient*>(
52 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
53 fake_bluetooth_gatt_manager_client->
54 UnregisterDescriptorServiceProvider(this);
57 void FakeBluetoothGattDescriptorServiceProvider::SendValueChanged(
58 const std::vector<uint8>& value) {
59 VLOG(1) << "Sent descriptor value changed: " << object_path_.value()
60 << " UUID: " << uuid_;
63 void FakeBluetoothGattDescriptorServiceProvider::GetValue(
64 const Delegate::ValueCallback& callback,
65 const Delegate::ErrorCallback& error_callback) {
66 VLOG(1) << "GATT descriptor value Get request: " << object_path_.value()
67 << " UUID: " << uuid_;
69 // Check if this descriptor is registered.
70 FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
71 static_cast<FakeBluetoothGattManagerClient*>(
72 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
73 FakeBluetoothGattCharacteristicServiceProvider* characteristic =
74 fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
75 characteristic_path_);
76 if (!characteristic) {
77 VLOG(1) << "GATT characteristic for descriptor does not exist: "
78 << characteristic_path_.value();
79 return;
81 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
82 characteristic->service_path())) {
83 VLOG(1) << "GATT descriptor not registered.";
84 error_callback.Run();
85 return;
88 // Pass on to the delegate.
89 DCHECK(delegate_);
90 delegate_->GetDescriptorValue(callback, error_callback);
93 void FakeBluetoothGattDescriptorServiceProvider::SetValue(
94 const std::vector<uint8>& value,
95 const base::Closure& callback,
96 const Delegate::ErrorCallback& error_callback) {
97 VLOG(1) << "GATT descriptor value Set request: " << object_path_.value()
98 << " UUID: " << uuid_;
100 // Check if this descriptor is registered.
101 FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
102 static_cast<FakeBluetoothGattManagerClient*>(
103 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
104 FakeBluetoothGattCharacteristicServiceProvider* characteristic =
105 fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
106 characteristic_path_);
107 if (!characteristic) {
108 VLOG(1) << "GATT characteristic for descriptor does not exist: "
109 << characteristic_path_.value();
110 return;
112 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
113 characteristic->service_path())) {
114 VLOG(1) << "GATT descriptor not registered.";
115 error_callback.Run();
116 return;
119 // Pass on to the delegate.
120 DCHECK(delegate_);
121 delegate_->SetDescriptorValue(value, callback, error_callback);
124 } // namespace chromeos