Don't preload rarely seen large images
[chromium-blink-merge.git] / components / proximity_auth / ble / bluetooth_low_energy_characteristics_finder.cc
blob60d15c5e19b19494d04f8ea11abacfdef5dbccec
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 "components/proximity_auth/ble/bluetooth_low_energy_characteristics_finder.h"
7 #include "components/proximity_auth/logging/logging.h"
8 #include "device/bluetooth/bluetooth_adapter.h"
9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
11 #include "device/bluetooth/bluetooth_uuid.h"
13 using device::BluetoothAdapter;
14 using device::BluetoothDevice;
15 using device::BluetoothGattCharacteristic;
16 using device::BluetoothGattService;
17 using device::BluetoothUUID;
19 namespace proximity_auth {
21 BluetoothLowEnergyCharacteristicsFinder::
22 BluetoothLowEnergyCharacteristicsFinder(
23 scoped_refptr<BluetoothAdapter> adapter,
24 BluetoothDevice* device,
25 const RemoteAttribute& remote_service,
26 const RemoteAttribute& to_peripheral_char,
27 const RemoteAttribute& from_peripheral_char,
28 const SuccessCallback& success_callback,
29 const ErrorCallback& error_callback)
30 : adapter_(adapter),
31 remote_service_(remote_service),
32 to_peripheral_char_(to_peripheral_char),
33 from_peripheral_char_(from_peripheral_char),
34 success_callback_(success_callback),
35 error_callback_(error_callback) {
36 if (!adapter_) {
37 error_callback_.Run(to_peripheral_char_, from_peripheral_char_);
38 ResetCallbacks();
39 return;
42 adapter_->AddObserver(this);
43 ScanRemoteCharacteristics(device, remote_service_.uuid);
45 // TODO(sacomoto): implement a timeout for characteristic discovery.
48 BluetoothLowEnergyCharacteristicsFinder::
49 BluetoothLowEnergyCharacteristicsFinder() {
52 BluetoothLowEnergyCharacteristicsFinder::
53 ~BluetoothLowEnergyCharacteristicsFinder() {
54 ResetCallbacks();
55 if (adapter_) {
56 adapter_->RemoveObserver(this);
57 adapter_ = NULL;
61 void BluetoothLowEnergyCharacteristicsFinder::GattCharacteristicAdded(
62 BluetoothAdapter* adapter,
63 BluetoothGattCharacteristic* characteristic) {
64 PA_LOG(INFO) << "New char found: "
65 << characteristic->GetUUID().canonical_value();
66 HandleCharacteristicUpdate(characteristic);
69 void BluetoothLowEnergyCharacteristicsFinder::GattDiscoveryCompleteForService(
70 BluetoothAdapter* adapter,
71 BluetoothGattService* service) {
72 if (service && service->GetUUID() == remote_service_.uuid) {
73 PA_LOG(INFO) << "All characteristics discovered for "
74 << remote_service_.uuid.canonical_value();
76 if (to_peripheral_char_.id.empty() || from_peripheral_char_.id.empty()) {
77 if (!error_callback_.is_null()) {
78 error_callback_.Run(to_peripheral_char_, from_peripheral_char_);
79 ResetCallbacks();
85 void BluetoothLowEnergyCharacteristicsFinder::ScanRemoteCharacteristics(
86 BluetoothDevice* device,
87 const BluetoothUUID& service_uuid) {
88 PA_LOG(INFO) << "Scanning remote characteristics.";
89 if (device) {
90 std::vector<BluetoothGattService*> services = device->GetGattServices();
91 for (const auto& service : services) {
92 if (service->GetUUID() == service_uuid) {
93 PA_LOG(INFO) << "Service " << service_uuid.canonical_value()
94 << " found.";
95 // Right service found, now scaning its characteristics.
96 std::vector<device::BluetoothGattCharacteristic*> characteristics =
97 service->GetCharacteristics();
98 for (const auto& characteristic : characteristics) {
99 PA_LOG(INFO) << "Char: "
100 << characteristic->GetUUID().canonical_value()
101 << " found.";
102 HandleCharacteristicUpdate(characteristic);
104 break;
110 void BluetoothLowEnergyCharacteristicsFinder::HandleCharacteristicUpdate(
111 BluetoothGattCharacteristic* characteristic) {
112 UpdateCharacteristicsStatus(characteristic);
114 if (!to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() &&
115 !success_callback_.is_null()) {
116 success_callback_.Run(remote_service_, to_peripheral_char_,
117 from_peripheral_char_);
118 ResetCallbacks();
122 void BluetoothLowEnergyCharacteristicsFinder::UpdateCharacteristicsStatus(
123 BluetoothGattCharacteristic* characteristic) {
124 if (characteristic) {
125 BluetoothUUID uuid = characteristic->GetUUID();
126 if (to_peripheral_char_.uuid == uuid)
127 to_peripheral_char_.id = characteristic->GetIdentifier();
128 if (from_peripheral_char_.uuid == uuid)
129 from_peripheral_char_.id = characteristic->GetIdentifier();
131 BluetoothGattService* service = characteristic->GetService();
132 if (service && service->GetUUID() == remote_service_.uuid)
133 remote_service_.id = service->GetIdentifier();
137 void BluetoothLowEnergyCharacteristicsFinder::ResetCallbacks() {
138 success_callback_.Reset();
139 error_callback_.Reset();
142 } // namespace proximity_auth