OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/system/chromeos/bluetooth/bluetooth_notification_controller.h" | 5 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "ash/common/system/system_notifier.h" | 10 #include "ash/common/system/system_notifier.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 protected: | 59 protected: |
60 ~BluetoothPairingNotificationDelegate() override; | 60 ~BluetoothPairingNotificationDelegate() override; |
61 | 61 |
62 // message_center::NotificationDelegate overrides. | 62 // message_center::NotificationDelegate overrides. |
63 void Close(bool by_user) override; | 63 void Close(bool by_user) override; |
64 void ButtonClick(int button_index) override; | 64 void ButtonClick(int button_index) override; |
65 | 65 |
66 private: | 66 private: |
67 // Buttons that appear in notifications. | 67 // Buttons that appear in notifications. |
68 enum Button { | 68 enum Button { BUTTON_ACCEPT, BUTTON_REJECT }; |
69 BUTTON_ACCEPT, | |
70 BUTTON_REJECT | |
71 }; | |
72 | 69 |
73 // Reference to the underlying Bluetooth Adapter, holding onto this | 70 // Reference to the underlying Bluetooth Adapter, holding onto this |
74 // reference ensures the adapter object doesn't go out of scope while we have | 71 // reference ensures the adapter object doesn't go out of scope while we have |
75 // a pending request and user interaction. | 72 // a pending request and user interaction. |
76 scoped_refptr<BluetoothAdapter> adapter_; | 73 scoped_refptr<BluetoothAdapter> adapter_; |
77 | 74 |
78 // Address of the device being paired. | 75 // Address of the device being paired. |
79 const std::string address_; | 76 const std::string address_; |
80 | 77 |
81 DISALLOW_COPY_AND_ASSIGN(BluetoothPairingNotificationDelegate); | 78 DISALLOW_COPY_AND_ASSIGN(BluetoothPairingNotificationDelegate); |
82 }; | 79 }; |
83 | 80 |
84 BluetoothPairingNotificationDelegate::BluetoothPairingNotificationDelegate( | 81 BluetoothPairingNotificationDelegate::BluetoothPairingNotificationDelegate( |
85 scoped_refptr<BluetoothAdapter> adapter, | 82 scoped_refptr<BluetoothAdapter> adapter, |
86 const std::string& address) | 83 const std::string& address) |
87 : adapter_(adapter), | 84 : adapter_(adapter), address_(address) {} |
88 address_(address) { | |
89 } | |
90 | 85 |
91 BluetoothPairingNotificationDelegate::~BluetoothPairingNotificationDelegate() { | 86 BluetoothPairingNotificationDelegate::~BluetoothPairingNotificationDelegate() {} |
92 } | |
93 | 87 |
94 void BluetoothPairingNotificationDelegate::Close(bool by_user) { | 88 void BluetoothPairingNotificationDelegate::Close(bool by_user) { |
95 VLOG(1) << "Pairing notification closed. by_user = " << by_user; | 89 VLOG(1) << "Pairing notification closed. by_user = " << by_user; |
96 // Ignore notification closes generated as a result of pairing completion. | 90 // Ignore notification closes generated as a result of pairing completion. |
97 if (!by_user) | 91 if (!by_user) |
98 return; | 92 return; |
99 | 93 |
100 // Cancel the pairing of the device, if the object still exists. | 94 // Cancel the pairing of the device, if the object still exists. |
101 BluetoothDevice* device = adapter_->GetDevice(address_); | 95 BluetoothDevice* device = adapter_->GetDevice(address_); |
102 if (device) | 96 if (device) |
(...skipping 16 matching lines...) Expand all Loading... |
119 } | 113 } |
120 } | 114 } |
121 | 115 |
122 // In any case, remove this pairing notification. | 116 // In any case, remove this pairing notification. |
123 message_center::MessageCenter::Get()->RemoveNotification( | 117 message_center::MessageCenter::Get()->RemoveNotification( |
124 kBluetoothDevicePairingNotificationId, false /* by_user */); | 118 kBluetoothDevicePairingNotificationId, false /* by_user */); |
125 } | 119 } |
126 | 120 |
127 } // namespace | 121 } // namespace |
128 | 122 |
129 | |
130 namespace ash { | 123 namespace ash { |
131 | 124 |
132 BluetoothNotificationController::BluetoothNotificationController() | 125 BluetoothNotificationController::BluetoothNotificationController() |
133 : weak_ptr_factory_(this) { | 126 : weak_ptr_factory_(this) { |
134 BluetoothAdapterFactory::GetAdapter( | 127 BluetoothAdapterFactory::GetAdapter( |
135 base::Bind(&BluetoothNotificationController::OnGetAdapter, | 128 base::Bind(&BluetoothNotificationController::OnGetAdapter, |
136 weak_ptr_factory_.GetWeakPtr())); | 129 weak_ptr_factory_.GetWeakPtr())); |
137 } | 130 } |
138 | 131 |
139 BluetoothNotificationController::~BluetoothNotificationController() { | 132 BluetoothNotificationController::~BluetoothNotificationController() { |
140 if (adapter_.get()) { | 133 if (adapter_.get()) { |
141 adapter_->RemoveObserver(this); | 134 adapter_->RemoveObserver(this); |
142 adapter_->RemovePairingDelegate(this); | 135 adapter_->RemovePairingDelegate(this); |
143 adapter_ = NULL; | 136 adapter_ = NULL; |
144 } | 137 } |
145 } | 138 } |
146 | 139 |
147 | |
148 void BluetoothNotificationController::AdapterDiscoverableChanged( | 140 void BluetoothNotificationController::AdapterDiscoverableChanged( |
149 BluetoothAdapter* adapter, | 141 BluetoothAdapter* adapter, |
150 bool discoverable) { | 142 bool discoverable) { |
151 if (discoverable) { | 143 if (discoverable) { |
152 NotifyAdapterDiscoverable(); | 144 NotifyAdapterDiscoverable(); |
153 } else { | 145 } else { |
154 // Clear any previous discoverable notification. | 146 // Clear any previous discoverable notification. |
155 message_center::MessageCenter::Get()->RemoveNotification( | 147 message_center::MessageCenter::Get()->RemoveNotification( |
156 kBluetoothDeviceDiscoverableNotificationId, false /* by_user */); | 148 kBluetoothDeviceDiscoverableNotificationId, false /* by_user */); |
157 } | 149 } |
(...skipping 20 matching lines...) Expand all Loading... |
178 paired_devices_.insert(device->GetAddress()); | 170 paired_devices_.insert(device->GetAddress()); |
179 NotifyPairedDevice(device); | 171 NotifyPairedDevice(device); |
180 } | 172 } |
181 } | 173 } |
182 | 174 |
183 void BluetoothNotificationController::DeviceRemoved(BluetoothAdapter* adapter, | 175 void BluetoothNotificationController::DeviceRemoved(BluetoothAdapter* adapter, |
184 BluetoothDevice* device) { | 176 BluetoothDevice* device) { |
185 paired_devices_.erase(device->GetAddress()); | 177 paired_devices_.erase(device->GetAddress()); |
186 } | 178 } |
187 | 179 |
188 | |
189 void BluetoothNotificationController::RequestPinCode(BluetoothDevice* device) { | 180 void BluetoothNotificationController::RequestPinCode(BluetoothDevice* device) { |
190 // Cannot provide keyboard entry in a notification; these devices (old car | 181 // Cannot provide keyboard entry in a notification; these devices (old car |
191 // audio systems for the most part) will need pairing to be initiated from | 182 // audio systems for the most part) will need pairing to be initiated from |
192 // the Chromebook. | 183 // the Chromebook. |
193 device->CancelPairing(); | 184 device->CancelPairing(); |
194 } | 185 } |
195 | 186 |
196 void BluetoothNotificationController::RequestPasskey(BluetoothDevice* device) { | 187 void BluetoothNotificationController::RequestPasskey(BluetoothDevice* device) { |
197 // Cannot provide keyboard entry in a notification; fortunately the spec | 188 // Cannot provide keyboard entry in a notification; fortunately the spec |
198 // doesn't allow for this to be an option when we're receiving the pairing | 189 // doesn't allow for this to be an option when we're receiving the pairing |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 228 |
238 void BluetoothNotificationController::AuthorizePairing( | 229 void BluetoothNotificationController::AuthorizePairing( |
239 BluetoothDevice* device) { | 230 BluetoothDevice* device) { |
240 base::string16 message = l10n_util::GetStringFUTF16( | 231 base::string16 message = l10n_util::GetStringFUTF16( |
241 IDS_ASH_STATUS_TRAY_BLUETOOTH_AUTHORIZE_PAIRING, | 232 IDS_ASH_STATUS_TRAY_BLUETOOTH_AUTHORIZE_PAIRING, |
242 device->GetNameForDisplay()); | 233 device->GetNameForDisplay()); |
243 | 234 |
244 NotifyPairing(device, message, true); | 235 NotifyPairing(device, message, true); |
245 } | 236 } |
246 | 237 |
247 | |
248 void BluetoothNotificationController::OnGetAdapter( | 238 void BluetoothNotificationController::OnGetAdapter( |
249 scoped_refptr<BluetoothAdapter> adapter) { | 239 scoped_refptr<BluetoothAdapter> adapter) { |
250 DCHECK(!adapter_.get()); | 240 DCHECK(!adapter_.get()); |
251 adapter_ = adapter; | 241 adapter_ = adapter; |
252 adapter_->AddObserver(this); | 242 adapter_->AddObserver(this); |
253 adapter_->AddPairingDelegate(this, | 243 adapter_->AddPairingDelegate(this, |
254 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW); | 244 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW); |
255 | 245 |
256 // Notify a user if the adapter is already in the discoverable state. | 246 // Notify a user if the adapter is already in the discoverable state. |
257 if (adapter_->IsDiscoverable()) | 247 if (adapter_->IsDiscoverable()) |
258 NotifyAdapterDiscoverable(); | 248 NotifyAdapterDiscoverable(); |
259 | 249 |
260 // Build a list of the currently paired devices; these don't receive | 250 // Build a list of the currently paired devices; these don't receive |
261 // notifications since it's assumed they were previously notified. | 251 // notifications since it's assumed they were previously notified. |
262 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 252 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
263 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); | 253 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); |
264 iter != devices.end(); ++iter) { | 254 iter != devices.end(); ++iter) { |
265 const BluetoothDevice* device = *iter; | 255 const BluetoothDevice* device = *iter; |
266 if (device->IsPaired()) | 256 if (device->IsPaired()) |
267 paired_devices_.insert(device->GetAddress()); | 257 paired_devices_.insert(device->GetAddress()); |
268 } | 258 } |
269 } | 259 } |
270 | 260 |
271 | |
272 void BluetoothNotificationController::NotifyAdapterDiscoverable() { | 261 void BluetoothNotificationController::NotifyAdapterDiscoverable() { |
273 message_center::RichNotificationData optional; | 262 message_center::RichNotificationData optional; |
274 | 263 |
275 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 264 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
276 | 265 |
277 std::unique_ptr<Notification> notification(new Notification( | 266 std::unique_ptr<Notification> notification(new Notification( |
278 message_center::NOTIFICATION_TYPE_SIMPLE, | 267 message_center::NOTIFICATION_TYPE_SIMPLE, |
279 kBluetoothDeviceDiscoverableNotificationId, base::string16() /* title */, | 268 kBluetoothDeviceDiscoverableNotificationId, base::string16() /* title */, |
280 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCOVERABLE, | 269 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCOVERABLE, |
281 base::UTF8ToUTF16(adapter_->GetName()), | 270 base::UTF8ToUTF16(adapter_->GetName()), |
282 base::UTF8ToUTF16(adapter_->GetAddress())), | 271 base::UTF8ToUTF16(adapter_->GetAddress())), |
283 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), | 272 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), |
284 base::string16() /* display source */, GURL(), | 273 base::string16() /* display source */, GURL(), |
285 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 274 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
286 system_notifier::kNotifierBluetooth), | 275 system_notifier::kNotifierBluetooth), |
287 optional, NULL)); | 276 optional, NULL)); |
288 message_center::MessageCenter::Get()->AddNotification( | 277 message_center::MessageCenter::Get()->AddNotification( |
289 std::move(notification)); | 278 std::move(notification)); |
290 } | 279 } |
291 | 280 |
292 void BluetoothNotificationController::NotifyPairing( | 281 void BluetoothNotificationController::NotifyPairing( |
293 BluetoothDevice* device, | 282 BluetoothDevice* device, |
294 const base::string16& message, | 283 const base::string16& message, |
295 bool with_buttons) { | 284 bool with_buttons) { |
296 message_center::RichNotificationData optional; | 285 message_center::RichNotificationData optional; |
297 if (with_buttons) { | 286 if (with_buttons) { |
298 optional.buttons.push_back(message_center::ButtonInfo( | 287 optional.buttons.push_back(message_center::ButtonInfo( |
299 l10n_util::GetStringUTF16( | 288 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_ACCEPT))); |
300 IDS_ASH_STATUS_TRAY_BLUETOOTH_ACCEPT))); | |
301 optional.buttons.push_back(message_center::ButtonInfo( | 289 optional.buttons.push_back(message_center::ButtonInfo( |
302 l10n_util::GetStringUTF16( | 290 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_REJECT))); |
303 IDS_ASH_STATUS_TRAY_BLUETOOTH_REJECT))); | |
304 } | 291 } |
305 | 292 |
306 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 293 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
307 | 294 |
308 std::unique_ptr<Notification> notification(new Notification( | 295 std::unique_ptr<Notification> notification(new Notification( |
309 message_center::NOTIFICATION_TYPE_SIMPLE, | 296 message_center::NOTIFICATION_TYPE_SIMPLE, |
310 kBluetoothDevicePairingNotificationId, base::string16() /* title */, | 297 kBluetoothDevicePairingNotificationId, base::string16() /* title */, |
311 message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), | 298 message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), |
312 base::string16() /* display source */, GURL(), | 299 base::string16() /* display source */, GURL(), |
313 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 300 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
(...skipping 24 matching lines...) Expand all Loading... |
338 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), | 325 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_BLUETOOTH), |
339 base::string16() /* display source */, GURL(), | 326 base::string16() /* display source */, GURL(), |
340 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 327 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
341 system_notifier::kNotifierBluetooth), | 328 system_notifier::kNotifierBluetooth), |
342 optional, NULL)); | 329 optional, NULL)); |
343 message_center::MessageCenter::Get()->AddNotification( | 330 message_center::MessageCenter::Get()->AddNotification( |
344 std::move(notification)); | 331 std::move(notification)); |
345 } | 332 } |
346 | 333 |
347 } // namespace ash | 334 } // namespace ash |
OLD | NEW |