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/chromeos/accessibility/switch_access_event_handler.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | |
| 10 #include "chrome/browser/profiles/profile_manager.h" | |
| 11 #include "chrome/common/extensions/extension_constants.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/render_widget_host.h" | |
| 14 #include "extensions/browser/extension_host.h" | |
| 15 #include "extensions/browser/extension_registry.h" | |
| 16 #include "extensions/browser/process_manager.h" | |
| 17 #include "ui/events/event.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 SwitchAccessEventHandler::SwitchAccessEventHandler() { | |
| 22 if (ash::Shell::HasInstance()) | |
| 23 ash::Shell::GetInstance()->AddPreTargetHandler(this); | |
| 24 } | |
| 25 | |
| 26 SwitchAccessEventHandler::~SwitchAccessEventHandler() { | |
| 27 if (ash::Shell::HasInstance()) | |
| 28 ash::Shell::GetInstance()->RemovePreTargetHandler(this); | |
| 29 } | |
| 30 | |
| 31 void SwitchAccessEventHandler::OnKeyEvent(ui::KeyEvent* event) { | |
| 32 DCHECK(event); | |
| 33 if (event->key_code() == ui::VKEY_1) { | |
| 34 CancelEvent(event); | |
| 35 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | |
| 36 state_ = ONE_KEY_DOWN; | |
|
dmazzoni
2017/02/24 23:06:54
The state machine made a lot of sense for select-t
elichtenberg
2017/02/28 00:28:42
Done.
| |
| 37 LOG(ERROR) << "One key was pressed!"; | |
| 38 } else if (event->type() == ui::ET_KEY_RELEASED && state_ == ONE_KEY_DOWN) { | |
| 39 state_ = INACTIVE; | |
| 40 LOG(ERROR) << "One key was released!"; | |
| 41 bool result = DispatchKeyEventToSwitchAccess(*event); | |
| 42 LOG(ERROR) << "Result of dispatching one key was: " << std::boolalpha | |
| 43 << result; | |
| 44 } | |
| 45 } else if (event->key_code() == ui::VKEY_2) { | |
| 46 CancelEvent(event); | |
| 47 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | |
| 48 state_ = TWO_KEY_DOWN; | |
| 49 LOG(ERROR) << "Two key was pressed!"; | |
| 50 } else if (event->type() == ui::ET_KEY_RELEASED && state_ == TWO_KEY_DOWN) { | |
| 51 state_ = INACTIVE; | |
| 52 LOG(ERROR) << "Two key was released!"; | |
| 53 bool result = DispatchKeyEventToSwitchAccess(*event); | |
| 54 LOG(ERROR) << "Result of dispatching two key was: " << std::boolalpha | |
| 55 << result; | |
| 56 } | |
| 57 } else if (event->key_code() == ui::VKEY_3) { | |
| 58 CancelEvent(event); | |
| 59 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | |
| 60 state_ = THREE_KEY_DOWN; | |
| 61 LOG(ERROR) << "Three key was pressed!"; | |
| 62 } else if (event->type() == ui::ET_KEY_RELEASED && | |
| 63 state_ == THREE_KEY_DOWN) { | |
| 64 state_ = INACTIVE; | |
| 65 LOG(ERROR) << "Three key was released!"; | |
| 66 bool result = DispatchKeyEventToSwitchAccess(*event); | |
| 67 LOG(ERROR) << "Result of dispatching three key was: " << std::boolalpha | |
| 68 << result; | |
| 69 } | |
| 70 } else if (event->key_code() == ui::VKEY_4) { | |
| 71 CancelEvent(event); | |
| 72 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | |
| 73 state_ = FOUR_KEY_DOWN; | |
| 74 LOG(ERROR) << "Four key was pressed!"; | |
| 75 } else if (event->type() == ui::ET_KEY_RELEASED && | |
| 76 state_ == FOUR_KEY_DOWN) { | |
| 77 state_ = INACTIVE; | |
| 78 LOG(ERROR) << "Four key was released!"; | |
| 79 bool result = DispatchKeyEventToSwitchAccess(*event); | |
| 80 LOG(ERROR) << "Result of dispatching four key was: " << std::boolalpha | |
| 81 << result; | |
| 82 } | |
| 83 } else if (event->key_code() == ui::VKEY_5) { | |
| 84 CancelEvent(event); | |
| 85 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | |
| 86 state_ = FIVE_KEY_DOWN; | |
| 87 LOG(ERROR) << "Five key was pressed!"; | |
| 88 } else if (event->type() == ui::ET_KEY_RELEASED && | |
| 89 state_ == FIVE_KEY_DOWN) { | |
| 90 state_ = INACTIVE; | |
| 91 LOG(ERROR) << "Five key was released!"; | |
| 92 bool result = DispatchKeyEventToSwitchAccess(*event); | |
| 93 LOG(ERROR) << "Result of dispatching five key was: " << std::boolalpha | |
| 94 << result; | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void SwitchAccessEventHandler::CancelEvent(ui::Event* event) { | |
| 100 DCHECK(event); | |
| 101 if (event->cancelable()) { | |
| 102 event->SetHandled(); | |
| 103 event->StopPropagation(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 bool SwitchAccessEventHandler::DispatchKeyEventToSwitchAccess( | |
|
dmazzoni
2017/02/24 23:06:54
Please refactor this into a common utility functio
elichtenberg
2017/02/28 00:28:42
Done. Did event_handler_common.h and event_handler
| |
| 108 const ui::KeyEvent& event) { | |
| 109 if (!AccessibilityManager::Get()) | |
| 110 return false; | |
| 111 | |
| 112 content::BrowserContext* context = ProfileManager::GetActiveUserProfile(); | |
| 113 if (!context) | |
| 114 return false; | |
| 115 | |
| 116 const extensions::Extension* extension = | |
| 117 extensions::ExtensionRegistry::Get(context)->enabled_extensions().GetByID( | |
| 118 extension_misc::kSwitchAccessExtensionId); | |
| 119 if (!extension) | |
| 120 return false; | |
| 121 | |
| 122 extensions::ExtensionHost* host = | |
| 123 extensions::ProcessManager::Get(context)->GetBackgroundHostForExtension( | |
| 124 extension->id()); | |
| 125 if (!host) | |
| 126 return false; | |
| 127 | |
| 128 content::RenderViewHost* rvh = host->render_view_host(); | |
| 129 if (!rvh) | |
| 130 return false; | |
| 131 | |
| 132 const content::NativeWebKeyboardEvent web_event(event); | |
| 133 rvh->GetWidget()->ForwardKeyboardEvent(web_event); | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 } // namespace chromeos | |
| OLD | NEW |