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