[M108 Migration] Support standard build for armv7hl architecture
[platform/framework/web/chromium-efl.git] / ash / bluetooth_devices_observer.cc
1 // Copyright 2018 The Chromium Authors
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 "ash/bluetooth_devices_observer.h"
6
7 #include "base/bind.h"
8 #include "device/bluetooth/bluetooth_adapter_factory.h"
9
10 namespace ash {
11
12 BluetoothDevicesObserver::BluetoothDevicesObserver(
13     const AdapterOrDeviceChangedCallback& device_changed_callback)
14     : adapter_or_device_changed_callback_(device_changed_callback) {
15   if (device::BluetoothAdapterFactory::IsBluetoothSupported()) {
16     device::BluetoothAdapterFactory::Get()->GetAdapter(
17         base::BindOnce(&BluetoothDevicesObserver::InitializeOnAdapterReady,
18                        weak_factory_.GetWeakPtr()));
19   } else {
20     adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
21   }
22 }
23
24 BluetoothDevicesObserver::~BluetoothDevicesObserver() {
25   if (bluetooth_adapter_)
26     bluetooth_adapter_->RemoveObserver(this);
27 }
28
29 void BluetoothDevicesObserver::AdapterPresentChanged(
30     device::BluetoothAdapter* adapter,
31     bool present) {
32   adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
33 }
34
35 void BluetoothDevicesObserver::AdapterPoweredChanged(
36     device::BluetoothAdapter* adapter,
37     bool powered) {
38   adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
39 }
40
41 void BluetoothDevicesObserver::DeviceChanged(device::BluetoothAdapter* adapter,
42                                              device::BluetoothDevice* device) {
43   adapter_or_device_changed_callback_.Run(device);
44 }
45
46 void BluetoothDevicesObserver::InitializeOnAdapterReady(
47     scoped_refptr<device::BluetoothAdapter> adapter) {
48   bluetooth_adapter_ = std::move(adapter);
49   bluetooth_adapter_->AddObserver(this);
50 }
51
52 bool BluetoothDevicesObserver::IsConnectedBluetoothDevice(
53     const ui::InputDevice& input_device) const {
54   if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPresent() ||
55       !bluetooth_adapter_->IsInitialized() ||
56       !bluetooth_adapter_->IsPowered()) {
57     return false;
58   }
59
60   // Since there is no map from an InputDevice to a BluetoothDevice. We just
61   // comparing their vendor id and product id to guess a match.
62   for (auto* device : bluetooth_adapter_->GetDevices()) {
63     if (!device->IsConnected())
64       continue;
65
66     if (device->GetVendorID() == input_device.vendor_id &&
67         device->GetProductID() == input_device.product_id) {
68       return true;
69     }
70   }
71
72   return false;
73 }
74
75 }  // namespace ash