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