| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/system/chromeos/bluetooth/tray_bluetooth_helper.h" | 5 #include "ash/common/system/chromeos/bluetooth/tray_bluetooth_helper.h" |
| 6 | 6 |
| 7 #include "ash/common/system/tray/system_tray_controller.h" | 7 #include "ash/common/system/tray/system_tray_controller.h" |
| 8 #include "ash/common/system/tray/system_tray_delegate.h" // BluetoothDeviceInfo | |
| 9 #include "ash/common/system/tray/system_tray_notifier.h" | 8 #include "ash/common/system/tray/system_tray_notifier.h" |
| 10 #include "ash/common/wm_shell.h" | 9 #include "ash/common/wm_shell.h" |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 13 #include "base/metrics/user_metrics.h" | 12 #include "base/metrics/user_metrics.h" |
| 14 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 15 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 16 #include "device/bluetooth/bluetooth_device.h" | 15 #include "device/bluetooth/bluetooth_device.h" |
| 17 #include "device/bluetooth/bluetooth_discovery_session.h" | 16 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 18 | 17 |
| 19 namespace ash { | 18 namespace ash { |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 void BluetoothSetDiscoveringError() { | 21 void BluetoothSetDiscoveringError() { |
| 23 LOG(ERROR) << "BluetoothSetDiscovering failed."; | 22 LOG(ERROR) << "BluetoothSetDiscovering failed."; |
| 24 } | 23 } |
| 25 | 24 |
| 26 void BluetoothDeviceConnectError( | 25 void BluetoothDeviceConnectError( |
| 27 device::BluetoothDevice::ConnectErrorCode error_code) {} | 26 device::BluetoothDevice::ConnectErrorCode error_code) {} |
| 28 | 27 |
| 29 ash::SystemTrayNotifier* GetSystemTrayNotifier() { | 28 ash::SystemTrayNotifier* GetSystemTrayNotifier() { |
| 30 return WmShell::Get()->system_tray_notifier(); | 29 return WmShell::Get()->system_tray_notifier(); |
| 31 } | 30 } |
| 32 | 31 |
| 33 } // namespace | 32 } // namespace |
| 34 | 33 |
| 34 BluetoothDeviceInfo::BluetoothDeviceInfo() |
| 35 : connected(false), connecting(false), paired(false) {} |
| 36 |
| 37 BluetoothDeviceInfo::BluetoothDeviceInfo(const BluetoothDeviceInfo& other) = |
| 38 default; |
| 39 |
| 40 BluetoothDeviceInfo::~BluetoothDeviceInfo() {} |
| 41 |
| 35 TrayBluetoothHelper::TrayBluetoothHelper() : weak_ptr_factory_(this) {} | 42 TrayBluetoothHelper::TrayBluetoothHelper() : weak_ptr_factory_(this) {} |
| 36 | 43 |
| 37 TrayBluetoothHelper::~TrayBluetoothHelper() { | 44 TrayBluetoothHelper::~TrayBluetoothHelper() { |
| 38 if (adapter_) | 45 if (adapter_) |
| 39 adapter_->RemoveObserver(this); | 46 adapter_->RemoveObserver(this); |
| 40 } | 47 } |
| 41 | 48 |
| 42 void TrayBluetoothHelper::Initialize() { | 49 void TrayBluetoothHelper::Initialize() { |
| 43 device::BluetoothAdapterFactory::GetAdapter( | 50 device::BluetoothAdapterFactory::GetAdapter( |
| 44 base::Bind(&TrayBluetoothHelper::InitializeOnAdapterReady, | 51 base::Bind(&TrayBluetoothHelper::InitializeOnAdapterReady, |
| 45 weak_ptr_factory_.GetWeakPtr())); | 52 weak_ptr_factory_.GetWeakPtr())); |
| 46 } | 53 } |
| 47 | 54 |
| 48 void TrayBluetoothHelper::InitializeOnAdapterReady( | 55 void TrayBluetoothHelper::InitializeOnAdapterReady( |
| 49 scoped_refptr<device::BluetoothAdapter> adapter) { | 56 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 50 adapter_ = adapter; | 57 adapter_ = adapter; |
| 51 CHECK(adapter_); | 58 CHECK(adapter_); |
| 52 adapter_->AddObserver(this); | 59 adapter_->AddObserver(this); |
| 53 } | 60 } |
| 54 | 61 |
| 55 void TrayBluetoothHelper::GetAvailableDevices( | 62 void TrayBluetoothHelper::GetAvailableBluetoothDevices( |
| 56 std::vector<BluetoothDeviceInfo>* list) { | 63 std::vector<BluetoothDeviceInfo>* list) { |
| 57 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 64 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 58 for (device::BluetoothDevice* device : devices) { | 65 for (device::BluetoothDevice* device : devices) { |
| 59 BluetoothDeviceInfo info; | 66 BluetoothDeviceInfo info; |
| 60 info.address = device->GetAddress(); | 67 info.address = device->GetAddress(); |
| 61 info.display_name = device->GetNameForDisplay(); | 68 info.display_name = device->GetNameForDisplay(); |
| 62 info.connected = device->IsConnected(); | 69 info.connected = device->IsConnected(); |
| 63 info.connecting = device->IsConnecting(); | 70 info.connecting = device->IsConnecting(); |
| 64 info.paired = device->IsPaired(); | 71 info.paired = device->IsPaired(); |
| 65 info.device_type = device->GetDeviceType(); | 72 info.device_type = device->GetDeviceType(); |
| 66 list->push_back(info); | 73 list->push_back(info); |
| 67 } | 74 } |
| 68 } | 75 } |
| 69 | 76 |
| 70 void TrayBluetoothHelper::StartDiscovering() { | 77 void TrayBluetoothHelper::StartBluetoothDiscovering() { |
| 71 if (HasDiscoverySession()) { | 78 if (HasBluetoothDiscoverySession()) { |
| 72 LOG(WARNING) << "Already have active Bluetooth device discovery session."; | 79 LOG(WARNING) << "Already have active Bluetooth device discovery session."; |
| 73 return; | 80 return; |
| 74 } | 81 } |
| 75 VLOG(1) << "Requesting new Bluetooth device discovery session."; | 82 VLOG(1) << "Requesting new Bluetooth device discovery session."; |
| 76 should_run_discovery_ = true; | 83 should_run_discovery_ = true; |
| 77 adapter_->StartDiscoverySession( | 84 adapter_->StartDiscoverySession( |
| 78 base::Bind(&TrayBluetoothHelper::OnStartDiscoverySession, | 85 base::Bind(&TrayBluetoothHelper::OnStartDiscoverySession, |
| 79 weak_ptr_factory_.GetWeakPtr()), | 86 weak_ptr_factory_.GetWeakPtr()), |
| 80 base::Bind(&BluetoothSetDiscoveringError)); | 87 base::Bind(&BluetoothSetDiscoveringError)); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void TrayBluetoothHelper::StopDiscovering() { | 90 void TrayBluetoothHelper::StopBluetoothDiscovering() { |
| 84 should_run_discovery_ = false; | 91 should_run_discovery_ = false; |
| 85 if (!HasDiscoverySession()) { | 92 if (!HasBluetoothDiscoverySession()) { |
| 86 LOG(WARNING) << "No active Bluetooth device discovery session."; | 93 LOG(WARNING) << "No active Bluetooth device discovery session."; |
| 87 return; | 94 return; |
| 88 } | 95 } |
| 89 VLOG(1) << "Stopping Bluetooth device discovery session."; | 96 VLOG(1) << "Stopping Bluetooth device discovery session."; |
| 90 discovery_session_->Stop(base::Bind(&base::DoNothing), | 97 discovery_session_->Stop(base::Bind(&base::DoNothing), |
| 91 base::Bind(&BluetoothSetDiscoveringError)); | 98 base::Bind(&BluetoothSetDiscoveringError)); |
| 92 } | 99 } |
| 93 | 100 |
| 94 void TrayBluetoothHelper::ConnectToDevice(const std::string& address) { | 101 void TrayBluetoothHelper::ConnectToBluetoothDevice(const std::string& address) { |
| 95 device::BluetoothDevice* device = adapter_->GetDevice(address); | 102 device::BluetoothDevice* device = adapter_->GetDevice(address); |
| 96 if (!device || device->IsConnecting() || | 103 if (!device || device->IsConnecting() || |
| 97 (device->IsConnected() && device->IsPaired())) { | 104 (device->IsConnected() && device->IsPaired())) { |
| 98 return; | 105 return; |
| 99 } | 106 } |
| 100 if (device->IsPaired() && !device->IsConnectable()) | 107 if (device->IsPaired() && !device->IsConnectable()) |
| 101 return; | 108 return; |
| 102 if (device->IsPaired() || !device->IsPairable()) { | 109 if (device->IsPaired() || !device->IsPairable()) { |
| 103 base::RecordAction( | 110 base::RecordAction( |
| 104 base::UserMetricsAction("StatusArea_Bluetooth_Connect_Known")); | 111 base::UserMetricsAction("StatusArea_Bluetooth_Connect_Known")); |
| 105 device->Connect(NULL, base::Bind(&base::DoNothing), | 112 device->Connect(NULL, base::Bind(&base::DoNothing), |
| 106 base::Bind(&BluetoothDeviceConnectError)); | 113 base::Bind(&BluetoothDeviceConnectError)); |
| 107 return; | 114 return; |
| 108 } | 115 } |
| 109 // Show pairing dialog for the unpaired device. | 116 // Show pairing dialog for the unpaired device. |
| 110 WmShell::Get()->system_tray_controller()->ShowBluetoothPairingDialog( | 117 WmShell::Get()->system_tray_controller()->ShowBluetoothPairingDialog( |
| 111 device->GetAddress(), device->GetNameForDisplay(), device->IsPaired(), | 118 device->GetAddress(), device->GetNameForDisplay(), device->IsPaired(), |
| 112 device->IsConnected()); | 119 device->IsConnected()); |
| 113 } | 120 } |
| 114 | 121 |
| 115 bool TrayBluetoothHelper::IsDiscovering() const { | 122 bool TrayBluetoothHelper::IsBluetoothDiscovering() const { |
| 116 return adapter_ && adapter_->IsDiscovering(); | 123 return adapter_ && adapter_->IsDiscovering(); |
| 117 } | 124 } |
| 118 | 125 |
| 119 void TrayBluetoothHelper::ToggleEnabled() { | 126 void TrayBluetoothHelper::ToggleBluetoothEnabled() { |
| 120 adapter_->SetPowered(!adapter_->IsPowered(), base::Bind(&base::DoNothing), | 127 adapter_->SetPowered(!adapter_->IsPowered(), base::Bind(&base::DoNothing), |
| 121 base::Bind(&base::DoNothing)); | 128 base::Bind(&base::DoNothing)); |
| 122 } | 129 } |
| 123 | 130 |
| 124 bool TrayBluetoothHelper::GetAvailable() { | 131 bool TrayBluetoothHelper::GetBluetoothAvailable() { |
| 125 return adapter_ && adapter_->IsPresent(); | 132 return adapter_ && adapter_->IsPresent(); |
| 126 } | 133 } |
| 127 | 134 |
| 128 bool TrayBluetoothHelper::GetEnabled() { | 135 bool TrayBluetoothHelper::GetBluetoothEnabled() { |
| 129 return adapter_ && adapter_->IsPowered(); | 136 return adapter_ && adapter_->IsPowered(); |
| 130 } | 137 } |
| 131 | 138 |
| 132 bool TrayBluetoothHelper::HasDiscoverySession() { | 139 bool TrayBluetoothHelper::HasBluetoothDiscoverySession() { |
| 133 return discovery_session_ && discovery_session_->IsActive(); | 140 return discovery_session_ && discovery_session_->IsActive(); |
| 134 } | 141 } |
| 135 | 142 |
| 136 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
| 137 // BluetoothAdapter::Observer: | 144 // BluetoothAdapter::Observer: |
| 138 | 145 |
| 139 void TrayBluetoothHelper::AdapterPresentChanged( | 146 void TrayBluetoothHelper::AdapterPresentChanged( |
| 140 device::BluetoothAdapter* adapter, | 147 device::BluetoothAdapter* adapter, |
| 141 bool present) { | 148 bool present) { |
| 142 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | 149 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // (e.g. the user dismissed the Bluetooth detailed view before the call | 182 // (e.g. the user dismissed the Bluetooth detailed view before the call |
| 176 // returned), don't claim the discovery session and let it clean up. | 183 // returned), don't claim the discovery session and let it clean up. |
| 177 if (!should_run_discovery_) | 184 if (!should_run_discovery_) |
| 178 return; | 185 return; |
| 179 VLOG(1) << "Claiming new Bluetooth device discovery session."; | 186 VLOG(1) << "Claiming new Bluetooth device discovery session."; |
| 180 discovery_session_ = std::move(discovery_session); | 187 discovery_session_ = std::move(discovery_session); |
| 181 GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); | 188 GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); |
| 182 } | 189 } |
| 183 | 190 |
| 184 } // namespace ash | 191 } // namespace ash |
| OLD | NEW |