Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/ash/tray_bluetooth_helper.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/system_tray_notifier.h" | |
| 8 #include "ash/common/wm_shell.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/metrics/user_metrics.h" | |
| 12 #include "chrome/browser/chromeos/bluetooth/bluetooth_pairing_dialog.h" | |
| 13 #include "chrome/browser/ui/ash/system_tray_client.h" | |
| 14 #include "chrome/browser/ui/ash/system_tray_delegate_chromeos.h" | |
| 15 #include "device/bluetooth/bluetooth_adapter.h" | |
|
msw
2017/03/17 00:11:06
nit: not needed; included in header
James Cook
2017/03/17 04:02:57
Done.
| |
| 16 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 17 #include "device/bluetooth/bluetooth_device.h" | |
| 18 #include "device/bluetooth/bluetooth_discovery_session.h" | |
| 19 | |
| 20 using chromeos::BluetoothPairingDialog; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 void BluetoothSetDiscoveringError() { | |
| 25 LOG(ERROR) << "BluetoothSetDiscovering failed."; | |
| 26 } | |
| 27 | |
| 28 void BluetoothDeviceConnectError( | |
| 29 device::BluetoothDevice::ConnectErrorCode error_code) {} | |
| 30 | |
| 31 ash::SystemTrayNotifier* GetSystemTrayNotifier() { | |
| 32 return ash::WmShell::Get()->system_tray_notifier(); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 TrayBluetoothHelper::TrayBluetoothHelper( | |
| 38 chromeos::SystemTrayDelegateChromeOS* system_tray_delegate) | |
| 39 : system_tray_delegate_(system_tray_delegate), weak_ptr_factory_(this) {} | |
| 40 | |
| 41 TrayBluetoothHelper::~TrayBluetoothHelper() { | |
| 42 if (adapter_) | |
| 43 adapter_->RemoveObserver(this); | |
| 44 } | |
| 45 | |
| 46 void TrayBluetoothHelper::Initialize() { | |
| 47 device::BluetoothAdapterFactory::GetAdapter( | |
| 48 base::Bind(&TrayBluetoothHelper::InitializeOnAdapterReady, | |
| 49 weak_ptr_factory_.GetWeakPtr())); | |
| 50 } | |
| 51 | |
| 52 void TrayBluetoothHelper::InitializeOnAdapterReady( | |
| 53 scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 54 adapter_ = adapter; | |
| 55 CHECK(adapter_); | |
| 56 adapter_->AddObserver(this); | |
| 57 | |
| 58 // May be null in tests. | |
| 59 if (system_tray_delegate_) | |
| 60 system_tray_delegate_->InitializeOnAdapterReady(); | |
| 61 } | |
| 62 | |
| 63 void TrayBluetoothHelper::GetAvailableDevices( | |
| 64 std::vector<ash::BluetoothDeviceInfo>* list) { | |
| 65 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
| 66 for (device::BluetoothDevice* device : devices) { | |
| 67 ash::BluetoothDeviceInfo info; | |
| 68 info.address = device->GetAddress(); | |
| 69 info.display_name = device->GetNameForDisplay(); | |
| 70 info.connected = device->IsConnected(); | |
| 71 info.connecting = device->IsConnecting(); | |
| 72 info.paired = device->IsPaired(); | |
| 73 info.device_type = device->GetDeviceType(); | |
| 74 list->push_back(info); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void TrayBluetoothHelper::StartDiscovering() { | |
| 79 if (HasDiscoverySession()) { | |
| 80 LOG(WARNING) << "Already have active Bluetooth device discovery session."; | |
| 81 return; | |
| 82 } | |
| 83 VLOG(1) << "Requesting new Bluetooth device discovery session."; | |
| 84 should_run_discovery_ = true; | |
| 85 adapter_->StartDiscoverySession( | |
| 86 base::Bind(&TrayBluetoothHelper::OnStartDiscoverySession, | |
| 87 weak_ptr_factory_.GetWeakPtr()), | |
| 88 base::Bind(&BluetoothSetDiscoveringError)); | |
| 89 } | |
| 90 | |
| 91 void TrayBluetoothHelper::StopDiscovering() { | |
| 92 should_run_discovery_ = false; | |
| 93 if (!HasDiscoverySession()) { | |
| 94 LOG(WARNING) << "No active Bluetooth device discovery session."; | |
| 95 return; | |
| 96 } | |
| 97 VLOG(1) << "Stopping Bluetooth device discovery session."; | |
| 98 discovery_session_->Stop(base::Bind(&base::DoNothing), | |
| 99 base::Bind(&BluetoothSetDiscoveringError)); | |
| 100 } | |
| 101 | |
| 102 void TrayBluetoothHelper::ConnectToDevice(const std::string& address) { | |
| 103 device::BluetoothDevice* device = adapter_->GetDevice(address); | |
| 104 if (!device || device->IsConnecting() || | |
| 105 (device->IsConnected() && device->IsPaired())) { | |
| 106 return; | |
| 107 } | |
| 108 if (device->IsPaired() && !device->IsConnectable()) | |
| 109 return; | |
| 110 if (device->IsPaired() || !device->IsPairable()) { | |
| 111 base::RecordAction( | |
| 112 base::UserMetricsAction("StatusArea_Bluetooth_Connect_Known")); | |
| 113 device->Connect(NULL, base::Bind(&base::DoNothing), | |
| 114 base::Bind(&BluetoothDeviceConnectError)); | |
| 115 return; | |
| 116 } | |
| 117 // Show pairing dialog for the unpaired device. | |
| 118 // TODO(jamescook): Move into SystemTrayClient and wire up with mojo. | |
| 119 base::RecordAction( | |
| 120 base::UserMetricsAction("StatusArea_Bluetooth_Connect_Unknown")); | |
| 121 BluetoothPairingDialog* dialog = new BluetoothPairingDialog(device); | |
| 122 // The dialog deletes itself on close. | |
| 123 dialog->ShowInContainer(SystemTrayClient::GetDialogParentContainerId()); | |
| 124 } | |
| 125 | |
| 126 bool TrayBluetoothHelper::IsDiscovering() const { | |
| 127 return adapter_ && adapter_->IsDiscovering(); | |
| 128 } | |
| 129 | |
| 130 void TrayBluetoothHelper::ToggleEnabled() { | |
| 131 adapter_->SetPowered(!adapter_->IsPowered(), base::Bind(&base::DoNothing), | |
| 132 base::Bind(&base::DoNothing)); | |
| 133 } | |
| 134 | |
| 135 bool TrayBluetoothHelper::GetAvailable() { | |
| 136 return adapter_ && adapter_->IsPresent(); | |
| 137 } | |
| 138 | |
| 139 bool TrayBluetoothHelper::GetEnabled() { | |
| 140 return adapter_ && adapter_->IsPowered(); | |
| 141 } | |
| 142 | |
| 143 bool TrayBluetoothHelper::HasDiscoverySession() { | |
| 144 return discovery_session_ && discovery_session_->IsActive(); | |
| 145 } | |
| 146 | |
| 147 //////////////////////////////////////////////////////////////////////////////// | |
| 148 // BluetoothAdapter::Observer: | |
| 149 | |
| 150 void TrayBluetoothHelper::AdapterPresentChanged( | |
| 151 device::BluetoothAdapter* adapter, | |
| 152 bool present) { | |
| 153 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | |
| 154 } | |
| 155 | |
| 156 void TrayBluetoothHelper::AdapterPoweredChanged( | |
| 157 device::BluetoothAdapter* adapter, | |
| 158 bool powered) { | |
| 159 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | |
| 160 } | |
| 161 | |
| 162 void TrayBluetoothHelper::AdapterDiscoveringChanged( | |
| 163 device::BluetoothAdapter* adapter, | |
| 164 bool discovering) { | |
| 165 GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); | |
| 166 } | |
| 167 | |
| 168 void TrayBluetoothHelper::DeviceAdded(device::BluetoothAdapter* adapter, | |
| 169 device::BluetoothDevice* device) { | |
| 170 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | |
| 171 } | |
| 172 | |
| 173 void TrayBluetoothHelper::DeviceChanged(device::BluetoothAdapter* adapter, | |
| 174 device::BluetoothDevice* device) { | |
| 175 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | |
| 176 } | |
| 177 | |
| 178 void TrayBluetoothHelper::DeviceRemoved(device::BluetoothAdapter* adapter, | |
| 179 device::BluetoothDevice* device) { | |
| 180 GetSystemTrayNotifier()->NotifyRefreshBluetooth(); | |
| 181 } | |
| 182 | |
| 183 void TrayBluetoothHelper::OnStartDiscoverySession( | |
| 184 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { | |
| 185 // If the discovery session was returned after a request to stop discovery | |
| 186 // (e.g. the user dismissed the Bluetooth detailed view before the call | |
| 187 // returned), don't claim the discovery session and let it clean up. | |
| 188 if (!should_run_discovery_) | |
| 189 return; | |
| 190 VLOG(1) << "Claiming new Bluetooth device discovery session."; | |
| 191 discovery_session_ = std::move(discovery_session); | |
| 192 GetSystemTrayNotifier()->NotifyBluetoothDiscoveringChanged(); | |
| 193 } | |
| OLD | NEW |