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 "chrome/browser/ui/webui/chromeos/login/hid_detection_screen_handler.h" | 5 #include "chrome/browser/ui/webui/chromeos/login/hid_detection_screen_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/metrics/histogram.h" | |
11 #include "base/prefs/pref_service.h" | |
10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" | 16 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" |
17 #include "chrome/common/pref_names.h" | |
14 #include "device/bluetooth/bluetooth_adapter_factory.h" | 18 #include "device/bluetooth/bluetooth_adapter_factory.h" |
15 #include "grit/chromium_strings.h" | 19 #include "grit/chromium_strings.h" |
16 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 const char kJsScreenPath[] = "login.HIDDetectionScreen"; | 25 const char kJsScreenPath[] = "login.HIDDetectionScreen"; |
22 | 26 |
23 // Variants of pairing state. | 27 // Variants of pairing state. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 | 64 |
61 namespace chromeos { | 65 namespace chromeos { |
62 | 66 |
63 HIDDetectionScreenHandler::HIDDetectionScreenHandler() | 67 HIDDetectionScreenHandler::HIDDetectionScreenHandler() |
64 : BaseScreenHandler(kJsScreenPath), | 68 : BaseScreenHandler(kJsScreenPath), |
65 delegate_(NULL), | 69 delegate_(NULL), |
66 show_on_init_(false), | 70 show_on_init_(false), |
67 mouse_is_pairing_(false), | 71 mouse_is_pairing_(false), |
68 keyboard_is_pairing_(false), | 72 keyboard_is_pairing_(false), |
69 switch_on_adapter_when_ready_(false), | 73 switch_on_adapter_when_ready_(false), |
70 skip_screen_if_devices_present_(true), | 74 first_time_screen_show_(true), |
71 weak_ptr_factory_(this) { | 75 weak_ptr_factory_(this) { |
72 } | 76 } |
73 | 77 |
74 HIDDetectionScreenHandler::~HIDDetectionScreenHandler() { | 78 HIDDetectionScreenHandler::~HIDDetectionScreenHandler() { |
75 if (adapter_.get()) | 79 if (adapter_.get()) |
76 adapter_->RemoveObserver(this); | 80 adapter_->RemoveObserver(this); |
77 input_service_proxy_.RemoveObserver(this); | 81 input_service_proxy_.RemoveObserver(this); |
78 if (delegate_) | 82 if (delegate_) |
79 delegate_->OnActorDestroyed(this); | 83 delegate_->OnActorDestroyed(this); |
80 } | 84 } |
(...skipping 12 matching lines...) Expand all Loading... | |
93 void HIDDetectionScreenHandler::FindDevicesError() { | 97 void HIDDetectionScreenHandler::FindDevicesError() { |
94 VLOG(1) << "Failed to start Bluetooth discovery."; | 98 VLOG(1) << "Failed to start Bluetooth discovery."; |
95 } | 99 } |
96 | 100 |
97 void HIDDetectionScreenHandler::Show() { | 101 void HIDDetectionScreenHandler::Show() { |
98 if (!page_is_ready()) { | 102 if (!page_is_ready()) { |
99 show_on_init_ = true; | 103 show_on_init_ = true; |
100 return; | 104 return; |
101 } | 105 } |
102 input_service_proxy_.AddObserver(this); | 106 input_service_proxy_.AddObserver(this); |
103 skip_screen_if_devices_present_ = true; | 107 first_time_screen_show_ = true; |
104 UpdateDevices(); | 108 GetDevicesFirstTime(); |
105 ShowScreen(OobeUI::kScreenHIDDetection, NULL); | 109 ShowScreen(OobeUI::kScreenHIDDetection, NULL); |
106 } | 110 } |
107 | 111 |
108 void HIDDetectionScreenHandler::Hide() { | 112 void HIDDetectionScreenHandler::Hide() { |
109 if (adapter_.get()) | 113 if (adapter_.get()) |
110 adapter_->RemoveObserver(this); | 114 adapter_->RemoveObserver(this); |
111 input_service_proxy_.RemoveObserver(this); | 115 input_service_proxy_.RemoveObserver(this); |
112 } | 116 } |
113 | 117 |
114 void HIDDetectionScreenHandler::SetDelegate(Delegate* delegate) { | 118 void HIDDetectionScreenHandler::SetDelegate(Delegate* delegate) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 show_on_init_ = false; | 152 show_on_init_ = false; |
149 } | 153 } |
150 } | 154 } |
151 | 155 |
152 void HIDDetectionScreenHandler::RegisterMessages() { | 156 void HIDDetectionScreenHandler::RegisterMessages() { |
153 AddCallback( | 157 AddCallback( |
154 "HIDDetectionOnContinue", &HIDDetectionScreenHandler::HandleOnContinue); | 158 "HIDDetectionOnContinue", &HIDDetectionScreenHandler::HandleOnContinue); |
155 } | 159 } |
156 | 160 |
157 void HIDDetectionScreenHandler::HandleOnContinue() { | 161 void HIDDetectionScreenHandler::HandleOnContinue() { |
162 if (!first_time_screen_show_) { | |
163 // Continue button pressed. | |
164 int scenario_type; | |
Alexei Svitkine (slow)
2014/05/14 14:57:48
Nit: ContinueScenarioType instead of int.
merkulova
2014/05/22 12:02:32
Done.
| |
165 if (!pointing_device_id_.empty() && !keyboard_device_id_.empty()) | |
166 scenario_type = All_DEVICES_DETECTED; | |
167 else if (pointing_device_id_.empty()) | |
168 scenario_type = KEYBOARD_DEVICE_ONLY_DETECTED; | |
169 else | |
170 scenario_type = POINTING_DEVICE_ONLY_DETECTED; | |
171 | |
172 UMA_HISTOGRAM_ENUMERATION( | |
173 "HIDDetection.OOBEDevicesDetectedOnContinuePressed", | |
174 scenario_type, | |
175 CONTINUE_SCENARIO_TYPE_SIZE); | |
176 } | |
158 if (delegate_) | 177 if (delegate_) |
159 delegate_->OnExit(); | 178 delegate_->OnExit(); |
160 } | 179 } |
161 | 180 |
162 void HIDDetectionScreenHandler::InitializeAdapter( | 181 void HIDDetectionScreenHandler::InitializeAdapter( |
163 scoped_refptr<device::BluetoothAdapter> adapter) { | 182 scoped_refptr<device::BluetoothAdapter> adapter) { |
164 adapter_ = adapter; | 183 adapter_ = adapter; |
165 CHECK(adapter_.get()); | 184 CHECK(adapter_.get()); |
166 | 185 |
167 adapter_->AddObserver(this); | 186 adapter_->AddObserver(this); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 UpdateDevices(); | 342 UpdateDevices(); |
324 } else if (id == pointing_device_id_) { | 343 } else if (id == pointing_device_id_) { |
325 pointing_device_id_.clear(); | 344 pointing_device_id_.clear(); |
326 pointing_device_name_.clear(); | 345 pointing_device_name_.clear(); |
327 pointing_device_connect_type_ = InputDeviceInfo::TYPE_UNKNOWN; | 346 pointing_device_connect_type_ = InputDeviceInfo::TYPE_UNKNOWN; |
328 SendPointingDeviceNotification(); | 347 SendPointingDeviceNotification(); |
329 UpdateDevices(); | 348 UpdateDevices(); |
330 } | 349 } |
331 } | 350 } |
332 | 351 |
352 // static | |
353 void HIDDetectionScreenHandler::RegisterPrefs(PrefRegistrySimple* registry) { | |
354 registry->RegisterIntegerPref(prefs::kTimesHIDDialogShown, 0); | |
355 } | |
356 | |
357 void HIDDetectionScreenHandler::GetDevicesFirstTime() { | |
358 input_service_proxy_.GetDevices( | |
359 base::Bind(&HIDDetectionScreenHandler::OnGetInputDevicesListFirstTime, | |
360 base::Unretained(this))); | |
Alexei Svitkine (slow)
2014/05/14 14:57:48
Why not use weak_ptr_factory_.GetWeakPtr() instead
merkulova
2014/05/22 12:02:32
Done.
| |
361 } | |
362 | |
333 void HIDDetectionScreenHandler::UpdateDevices() { | 363 void HIDDetectionScreenHandler::UpdateDevices() { |
334 input_service_proxy_.GetDevices( | 364 input_service_proxy_.GetDevices( |
335 base::Bind(&HIDDetectionScreenHandler::OnGetInputDevicesList, | 365 base::Bind(&HIDDetectionScreenHandler::OnGetInputDevicesList, |
336 base::Unretained(this))); | 366 base::Unretained(this))); |
337 } | 367 } |
338 | 368 |
339 void HIDDetectionScreenHandler::UpdateBTDevices() { | 369 void HIDDetectionScreenHandler::UpdateBTDevices() { |
340 if (!adapter_ || !adapter_->IsPresent() || !adapter_->IsPowered()) | 370 if (!adapter_ || !adapter_->IsPresent() || !adapter_->IsPowered()) |
341 return; | 371 return; |
342 | 372 |
343 // If no connected devices found as pointing device and keyboard, we try to | 373 // If no connected devices found as pointing device and keyboard, we try to |
344 // connect some type-suitable active bluetooth device. | 374 // connect some type-suitable active bluetooth device. |
345 std::vector<device::BluetoothDevice*> bt_devices = adapter_->GetDevices(); | 375 std::vector<device::BluetoothDevice*> bt_devices = adapter_->GetDevices(); |
346 for (std::vector<device::BluetoothDevice*>::const_iterator it = | 376 for (std::vector<device::BluetoothDevice*>::const_iterator it = |
347 bt_devices.begin(); | 377 bt_devices.begin(); |
348 it != bt_devices.end() && | 378 it != bt_devices.end() && |
349 (keyboard_device_id_.empty() || pointing_device_id_.empty()); | 379 (keyboard_device_id_.empty() || pointing_device_id_.empty()); |
350 ++it) { | 380 ++it) { |
351 TryPairingAsPointingDevice(*it); | 381 TryPairingAsPointingDevice(*it); |
352 TryPairingAsKeyboardDevice(*it); | 382 TryPairingAsKeyboardDevice(*it); |
353 } | 383 } |
354 } | 384 } |
355 | 385 |
356 void HIDDetectionScreenHandler::OnGetInputDevicesList( | 386 void HIDDetectionScreenHandler::ProcessConnectedDevicesList( |
357 const std::vector<InputDeviceInfo>& devices) { | 387 const std::vector<InputDeviceInfo>& devices) { |
358 for (std::vector<InputDeviceInfo>::const_iterator it = devices.begin(); | 388 for (std::vector<InputDeviceInfo>::const_iterator it = devices.begin(); |
359 it != devices.end() && | 389 it != devices.end() && |
360 (pointing_device_id_.empty() || keyboard_device_id_.empty()); | 390 (pointing_device_id_.empty() || keyboard_device_id_.empty()); |
361 ++it) { | 391 ++it) { |
362 if (pointing_device_id_.empty() && DeviceIsPointing(*it)) { | 392 if (pointing_device_id_.empty() && DeviceIsPointing(*it)) { |
363 pointing_device_id_ = it->id; | 393 pointing_device_id_ = it->id; |
364 pointing_device_name_ = it->name; | 394 pointing_device_name_ = it->name; |
365 pointing_device_connect_type_ = it->type; | 395 pointing_device_connect_type_ = it->type; |
366 SendPointingDeviceNotification(); | 396 SendPointingDeviceNotification(); |
367 } | 397 } |
368 if (keyboard_device_id_.empty() && it->is_keyboard) { | 398 if (keyboard_device_id_.empty() && it->is_keyboard) { |
369 keyboard_device_id_ = it->id; | 399 keyboard_device_id_ = it->id; |
370 keyboard_device_name_ = it->name; | 400 keyboard_device_name_ = it->name; |
371 keyboard_device_connect_type_ = it->type; | 401 keyboard_device_connect_type_ = it->type; |
372 SendKeyboardDeviceNotification(NULL); | 402 SendKeyboardDeviceNotification(NULL); |
373 } | 403 } |
374 } | 404 } |
375 // Skip screen if both devices are present and skip was requested. | 405 } |
376 if (!pointing_device_id_.empty() && | 406 |
377 !keyboard_device_id_.empty() && | 407 void HIDDetectionScreenHandler::TryInitiateBTDevicesUpdate() { |
378 skip_screen_if_devices_present_) { | |
379 HandleOnContinue(); | |
380 } | |
381 // Skip requested only once on dialog show. | |
382 skip_screen_if_devices_present_ = false; | |
383 if ((pointing_device_id_.empty() || keyboard_device_id_.empty()) && | 408 if ((pointing_device_id_.empty() || keyboard_device_id_.empty()) && |
384 adapter_) { | 409 adapter_) { |
385 if (!adapter_->IsPresent()) { | 410 if (!adapter_->IsPresent()) { |
386 // Switch on BT adapter later when it's available. | 411 // Switch on BT adapter later when it's available. |
387 switch_on_adapter_when_ready_ = true; | 412 switch_on_adapter_when_ready_ = true; |
388 } else if (!adapter_->IsPowered()) { | 413 } else if (!adapter_->IsPowered()) { |
389 adapter_->SetPowered( | 414 adapter_->SetPowered( |
390 true, | 415 true, |
391 base::Bind(&HIDDetectionScreenHandler::StartBTDiscoverySession, | 416 base::Bind(&HIDDetectionScreenHandler::StartBTDiscoverySession, |
392 weak_ptr_factory_.GetWeakPtr()), | 417 weak_ptr_factory_.GetWeakPtr()), |
393 base::Bind(&HIDDetectionScreenHandler::SetPoweredError, | 418 base::Bind(&HIDDetectionScreenHandler::SetPoweredError, |
394 weak_ptr_factory_.GetWeakPtr())); | 419 weak_ptr_factory_.GetWeakPtr())); |
395 } else { | 420 } else { |
396 UpdateBTDevices(); | 421 UpdateBTDevices(); |
397 } | 422 } |
398 } | 423 } |
399 } | 424 } |
400 | 425 |
426 void HIDDetectionScreenHandler::OnGetInputDevicesListFirstTime( | |
427 const std::vector<InputDeviceInfo>& devices) { | |
428 ProcessConnectedDevicesList(devices); | |
429 | |
430 // Skip screen if both devices are present. | |
431 bool all_devices_autodetected = !pointing_device_id_.empty() && | |
432 !keyboard_device_id_.empty(); | |
433 UMA_HISTOGRAM_BOOLEAN("HIDDetection.OOBEDialogShown", | |
434 !all_devices_autodetected); | |
435 if (all_devices_autodetected) { | |
436 HandleOnContinue(); | |
Alexei Svitkine (slow)
2014/05/14 14:57:48
Nit: prefer an early return and no else block.
merkulova
2014/05/22 12:02:32
Done.
| |
437 } else { | |
438 PrefService* const local_state = g_browser_process->local_state(); | |
Alexei Svitkine (slow)
2014/05/14 14:57:48
Nit: Chromium style doesn't usually use const in t
merkulova
2014/05/22 12:02:32
Done.
| |
439 int num_of_times_dialog_was_shown = local_state->GetInteger( | |
440 prefs::kTimesHIDDialogShown); | |
441 local_state->SetInteger(prefs::kTimesHIDDialogShown, | |
442 num_of_times_dialog_was_shown + 1); | |
443 first_time_screen_show_ = false; | |
444 | |
445 TryInitiateBTDevicesUpdate(); | |
446 } | |
447 } | |
448 | |
449 void HIDDetectionScreenHandler::OnGetInputDevicesList( | |
450 const std::vector<InputDeviceInfo>& devices) { | |
451 ProcessConnectedDevicesList(devices); | |
452 TryInitiateBTDevicesUpdate(); | |
453 } | |
454 | |
401 void HIDDetectionScreenHandler::ConnectBTDevice( | 455 void HIDDetectionScreenHandler::ConnectBTDevice( |
402 device::BluetoothDevice* device) { | 456 device::BluetoothDevice* device) { |
403 if (!device->IsPairable() || device->IsPaired()) | 457 if (!device->IsPairable() || device->IsPaired()) |
404 return; | 458 return; |
405 device::BluetoothDevice::DeviceType device_type = device->GetDeviceType(); | 459 device::BluetoothDevice::DeviceType device_type = device->GetDeviceType(); |
406 | 460 |
407 if (device_type == device::BluetoothDevice::DEVICE_MOUSE || | 461 if (device_type == device::BluetoothDevice::DEVICE_MOUSE || |
408 device_type == device::BluetoothDevice::DEVICE_TABLET) { | 462 device_type == device::BluetoothDevice::DEVICE_TABLET) { |
409 if (mouse_is_pairing_) | 463 if (mouse_is_pairing_) |
410 return; | 464 return; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 l10n_util::GetStringFUTF16( | 546 l10n_util::GetStringFUTF16( |
493 IDS_HID_DETECTION_PAIRED_BLUETOOTH_KEYBOARD, | 547 IDS_HID_DETECTION_PAIRED_BLUETOOTH_KEYBOARD, |
494 base::UTF8ToUTF16(keyboard_device_name_))); | 548 base::UTF8ToUTF16(keyboard_device_name_))); |
495 } else { | 549 } else { |
496 state_info.SetString("state", kUSBConnectedState); | 550 state_info.SetString("state", kUSBConnectedState); |
497 } | 551 } |
498 CallJS("setKeyboardDeviceState", state_info); | 552 CallJS("setKeyboardDeviceState", state_info); |
499 } | 553 } |
500 | 554 |
501 } // namespace chromeos | 555 } // namespace chromeos |
OLD | NEW |