Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 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( |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 13 | const AdapterOrDeviceChangedCallback& device_changed_callback) |
| 14 | : adapter_or_device_changed_callback_(device_changed_callback), |
| 15 | weak_factory_(this) { |
| 16 | if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
| 17 | device::BluetoothAdapterFactory::GetAdapter( |
Reilly Grant | d833b30 | 2019-01-08 01:39:02 | [diff] [blame] | 18 | base::BindOnce(&BluetoothDevicesObserver::InitializeOnAdapterReady, |
| 19 | weak_factory_.GetWeakPtr())); |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 20 | } else { |
| 21 | adapter_or_device_changed_callback_.Run(/*device=*/nullptr); |
| 22 | } |
Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | BluetoothDevicesObserver::~BluetoothDevicesObserver() { |
| 26 | if (bluetooth_adapter_) |
| 27 | bluetooth_adapter_->RemoveObserver(this); |
| 28 | } |
| 29 | |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 30 | void BluetoothDevicesObserver::AdapterPresentChanged( |
| 31 | device::BluetoothAdapter* adapter, |
| 32 | bool present) { |
| 33 | adapter_or_device_changed_callback_.Run(/*device=*/nullptr); |
| 34 | } |
| 35 | |
| 36 | void BluetoothDevicesObserver::AdapterPoweredChanged( |
| 37 | device::BluetoothAdapter* adapter, |
| 38 | bool powered) { |
| 39 | adapter_or_device_changed_callback_.Run(/*device=*/nullptr); |
| 40 | } |
| 41 | |
Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 42 | void BluetoothDevicesObserver::DeviceChanged(device::BluetoothAdapter* adapter, |
| 43 | device::BluetoothDevice* device) { |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 44 | adapter_or_device_changed_callback_.Run(device); |
Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void BluetoothDevicesObserver::InitializeOnAdapterReady( |
| 48 | scoped_refptr<device::BluetoothAdapter> adapter) { |
| 49 | bluetooth_adapter_ = std::move(adapter); |
| 50 | bluetooth_adapter_->AddObserver(this); |
| 51 | } |
| 52 | |
| 53 | bool BluetoothDevicesObserver::IsConnectedBluetoothDevice( |
| 54 | const ui::InputDevice& input_device) const { |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 55 | if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPresent() || |
| 56 | !bluetooth_adapter_->IsInitialized() || |
| 57 | !bluetooth_adapter_->IsPowered()) { |
Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 58 | return false; |
Xiaoqian Dai | f631084 | 2018-11-15 05:07:12 | [diff] [blame] | 59 | } |
Xiaoqian Dai | 45fbc04 | 2018-09-25 21:00:55 | [diff] [blame] | 60 | |
| 61 | // Since there is no map from an InputDevice to a BluetoothDevice. We just |
| 62 | // comparing their vendor id and product id to guess a match. |
| 63 | for (auto* device : bluetooth_adapter_->GetDevices()) { |
| 64 | if (!device->IsConnected()) |
| 65 | continue; |
| 66 | |
| 67 | if (device->GetVendorID() == input_device.vendor_id && |
| 68 | device->GetProductID() == input_device.product_id) { |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | } // namespace ash |