Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "ui/events/devices/input_device_observer_win.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/memory/singleton.h" | |
| 10 #include "base/strings/string16.h" | |
| 11 | |
| 12 // This macro provides the implementation for the observer notification methods. | |
| 13 #define NOTIFY_OBSERVERS(method_decl, observer_call) \ | |
| 14 void InputDeviceObserverWin::method_decl { \ | |
| 15 for (InputDeviceEventObserver & observer : observers_) \ | |
| 16 observer.observer_call; \ | |
| 17 } | |
| 18 | |
| 19 namespace ui { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // The registry subkey that contains information about the state of the | |
| 24 // detachable/convertible laptop, it tells if the device has an accessible | |
| 25 // keyboard. | |
| 26 // OEMs are expected to follow this guidelines to report docked/undocked state | |
| 27 // https://msdn.microsoft.com/en-us/library/windows/hardware/dn922653(v=vs.85).a spx. | |
|
mustaq
2017/03/23 20:09:10
Nit: Perhaps replace this URL with the redirected
| |
| 28 const base::char16 kRegistryPriorityControl[] = | |
| 29 L"System\\CurrentControlSet\\Control\\PriorityControl"; | |
| 30 | |
| 31 const base::char16 kRegistryConvertibleSlateModeKey[] = L"ConvertibleSlateMode"; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 InputDeviceObserverWin::InputDeviceObserverWin() : weak_factory_(this) { | |
| 36 registry_key_.reset(new base::win::RegKey( | |
| 37 HKEY_LOCAL_MACHINE, kRegistryPriorityControl, KEY_NOTIFY | KEY_READ)); | |
| 38 | |
| 39 if (registry_key_->Valid()) { | |
| 40 slate_mode_enabled_ = IsSlateModeEnabled(registry_key_.get()); | |
| 41 // Start watching the registry for changes. | |
| 42 base::win::RegKey::ChangeCallback callback = | |
| 43 base::Bind(&InputDeviceObserverWin::OnRegistryKeyChanged, | |
| 44 weak_factory_.GetWeakPtr(), registry_key_.get()); | |
| 45 registry_key_->StartWatching(callback); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 InputDeviceObserverWin* InputDeviceObserverWin::GetInstance() { | |
| 50 return base::Singleton< | |
| 51 InputDeviceObserverWin, | |
| 52 base::LeakySingletonTraits<InputDeviceObserverWin>>::get(); | |
| 53 } | |
| 54 | |
| 55 InputDeviceObserverWin::~InputDeviceObserverWin() {} | |
| 56 | |
| 57 void InputDeviceObserverWin::OnRegistryKeyChanged(base::win::RegKey* key) { | |
| 58 if (!key) | |
| 59 return; | |
| 60 | |
| 61 // |OnRegistryKeyChanged| is removed as an observer when the ChangeCallback is | |
| 62 // called, so we need to re-register. | |
| 63 key->StartWatching(base::Bind(&InputDeviceObserverWin::OnRegistryKeyChanged, | |
| 64 base::Unretained(this), base::Unretained(key))); | |
| 65 | |
| 66 bool new_slate_mode = IsSlateModeEnabled(key); | |
| 67 if (slate_mode_enabled_ == new_slate_mode) | |
| 68 return; | |
| 69 | |
| 70 NotifyObserversTouchpadDeviceConfigurationChanged(); | |
| 71 NotifyObserversMouseDeviceConfigurationChanged(); | |
| 72 NotifyObserversKeyboardDeviceConfigurationChanged(); | |
| 73 NotifyObserversTouchscreenDeviceConfigurationChanged(); | |
|
mustaq
2017/03/23 20:09:10
Another nit: now that you have focused the "watch"
| |
| 74 slate_mode_enabled_ = new_slate_mode; | |
| 75 } | |
| 76 | |
| 77 bool InputDeviceObserverWin::IsSlateModeEnabled(base::win::RegKey* key) { | |
| 78 DWORD slate_enabled; | |
| 79 if (key->ReadValueDW(kRegistryConvertibleSlateModeKey, &slate_enabled) != | |
| 80 ERROR_SUCCESS) | |
| 81 return false; | |
| 82 return slate_enabled == 1; | |
| 83 } | |
| 84 | |
| 85 void InputDeviceObserverWin::AddObserver(InputDeviceEventObserver* observer) { | |
| 86 observers_.AddObserver(observer); | |
| 87 } | |
| 88 | |
| 89 void InputDeviceObserverWin::RemoveObserver( | |
| 90 InputDeviceEventObserver* observer) { | |
| 91 observers_.RemoveObserver(observer); | |
| 92 } | |
| 93 | |
| 94 NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged(), | |
| 95 OnKeyboardDeviceConfigurationChanged()); | |
| 96 | |
| 97 NOTIFY_OBSERVERS(NotifyObserversMouseDeviceConfigurationChanged(), | |
| 98 OnMouseDeviceConfigurationChanged()); | |
| 99 | |
| 100 NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged(), | |
| 101 OnTouchpadDeviceConfigurationChanged()); | |
| 102 | |
| 103 NOTIFY_OBSERVERS(NotifyObserversTouchscreenDeviceConfigurationChanged(), | |
| 104 OnTouchscreenDeviceConfigurationChanged()); | |
| 105 | |
| 106 } // namespace ui | |
| OLD | NEW |