Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/browser/chromeos/accessibility/select_to_speak_event_handler.cc

Issue 2814213002: Refactor Select-to-speak so that mouse events are forwarded to the extension. (Closed)
Patch Set: Fix closure compiler warnings Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/chromeos/accessibility/select_to_speak_event_handler.h" 5 #include "chrome/browser/chromeos/accessibility/select_to_speak_event_handler.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "chrome/browser/speech/tts_controller.h" 9 #include "chrome/browser/chromeos/accessibility/event_handler_common.h"
10 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" 10 #include "chrome/common/extensions/extension_constants.h"
11 #include "chrome/common/extensions/api/automation_api_constants.h"
12 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
13 #include "ui/accessibility/ax_tree_id_registry.h" 12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host.h"
14 #include "third_party/WebKit/public/platform/WebMouseEvent.h"
15 #include "ui/aura/client/screen_position_client.h"
14 #include "ui/aura/window.h" 16 #include "ui/aura/window.h"
15 #include "ui/display/display.h" 17 #include "ui/display/display.h"
18 #include "ui/events/blink/web_input_event.h"
16 #include "ui/events/event.h" 19 #include "ui/events/event.h"
17 #include "ui/views/view.h" 20 #include "ui/views/view.h"
18 #include "ui/views/widget/widget.h" 21 #include "ui/views/widget/widget.h"
19 22
20 namespace chromeos { 23 namespace chromeos {
21 24
25 namespace {
26
27 gfx::Point GetScreenLocationFromEvent(const ui::LocatedEvent& event) {
28 aura::Window* root =
29 static_cast<aura::Window*>(event.target())->GetRootWindow();
30 aura::client::ScreenPositionClient* spc =
31 aura::client::GetScreenPositionClient(root);
32 if (!spc)
33 return event.root_location();
34
35 gfx::Point screen_location(event.root_location());
36 spc->ConvertPointToScreen(root, &screen_location);
37 return screen_location;
38 }
39 } // namespace
40
22 SelectToSpeakEventHandler::SelectToSpeakEventHandler() { 41 SelectToSpeakEventHandler::SelectToSpeakEventHandler() {
23 if (ash::Shell::HasInstance()) 42 if (ash::Shell::HasInstance())
24 ash::Shell::Get()->GetPrimaryRootWindow()->AddPreTargetHandler(this); 43 ash::Shell::Get()->GetPrimaryRootWindow()->AddPreTargetHandler(this);
25 } 44 }
26 45
27 SelectToSpeakEventHandler::~SelectToSpeakEventHandler() { 46 SelectToSpeakEventHandler::~SelectToSpeakEventHandler() {
28 if (ash::Shell::HasInstance()) 47 if (ash::Shell::HasInstance())
29 ash::Shell::Get()->GetPrimaryRootWindow()->RemovePreTargetHandler(this); 48 ash::Shell::Get()->GetPrimaryRootWindow()->RemovePreTargetHandler(this);
30 } 49 }
31 50
51 void SelectToSpeakEventHandler::CaptureForwardedEventsForTesting(
52 SelectToSpeakForwardedEventDelegateForTesting* delegate) {
53 event_delegate_for_testing_ = delegate;
54 }
55
32 void SelectToSpeakEventHandler::OnKeyEvent(ui::KeyEvent* event) { 56 void SelectToSpeakEventHandler::OnKeyEvent(ui::KeyEvent* event) {
33 DCHECK(event); 57 DCHECK(event);
34 58
35 // We can only call TtsController on the UI thread, make sure we 59 // We can only call TtsController on the UI thread, make sure we
36 // don't ever try to run this code on some other thread. 60 // don't ever try to run this code on some other thread.
37 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 61 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
38 62
39 ui::KeyboardCode key_code = event->key_code(); 63 ui::KeyboardCode key_code = event->key_code();
40 64 bool cancel_event = false;
41 // Stop speech when the user taps and releases Control or Search
42 // without pressing any other keys along the way.
43 if (state_ != MOUSE_RELEASED && event->type() == ui::ET_KEY_RELEASED &&
44 (key_code == ui::VKEY_CONTROL || key_code == ui::VKEY_LWIN) &&
45 keys_pressed_together_.find(key_code) != keys_pressed_together_.end() &&
46 keys_pressed_together_.size() == 1) {
47 TtsController::GetInstance()->Stop();
48 }
49
50 // Update keys_currently_down_ and keys_pressed_together_.
51 if (event->type() == ui::ET_KEY_PRESSED) {
52 keys_currently_down_.insert(key_code);
53 keys_pressed_together_.insert(key_code);
54 } else if (event->type() == ui::ET_KEY_RELEASED) {
55 keys_currently_down_.erase(key_code);
56 if (keys_currently_down_.empty())
57 keys_pressed_together_.clear();
58 }
59 65
60 // Update the state when pressing and releasing the Search key (VKEY_LWIN). 66 // Update the state when pressing and releasing the Search key (VKEY_LWIN).
61 if (key_code == ui::VKEY_LWIN) { 67 if (key_code == ui::VKEY_LWIN) {
62 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { 68 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) {
63 state_ = SEARCH_DOWN; 69 state_ = SEARCH_DOWN;
64 } else if (event->type() == ui::ET_KEY_RELEASED) { 70 } else if (event->type() == ui::ET_KEY_RELEASED) {
65 if (state_ == CAPTURING) { 71 if (state_ == CAPTURING) {
66 SendCancelAXEvent(); 72 cancel_event = true;
67 CancelEvent(event);
68 state_ = WAIT_FOR_MOUSE_RELEASE; 73 state_ = WAIT_FOR_MOUSE_RELEASE;
69 } else if (state_ == MOUSE_RELEASED) { 74 } else if (state_ == MOUSE_RELEASED) {
70 CancelEvent(event); 75 cancel_event = true;
71 state_ = INACTIVE; 76 state_ = INACTIVE;
72 } 77 }
73 } 78 }
74 } else if (state_ == SEARCH_DOWN) { 79 } else if (state_ == SEARCH_DOWN) {
75 state_ = INACTIVE; 80 state_ = INACTIVE;
David Tseng 2017/05/16 15:54:39 Almost there. Can we move all of this state logic
dmazzoni 2017/05/16 18:23:48 As discussed, I'd like to avoid re-injecting due t
76 } 81 }
82
83 // Forward the key to the extension.
84 extensions::ExtensionHost* host =
85 GetAccessibilityExtensionHost(extension_misc::kSelectToSpeakExtensionId);
86 if (host)
87 ForwardKeyToExtension(*event, host);
88
89 if (cancel_event)
90 CancelEvent(event);
77 } 91 }
78 92
79 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) { 93 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) {
80 DCHECK(event); 94 DCHECK(event);
81 if (state_ == INACTIVE) 95 if (state_ == INACTIVE)
82 return; 96 return;
83 97
84 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) && 98 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) &&
85 event->type() == ui::ET_MOUSE_PRESSED) { 99 event->type() == ui::ET_MOUSE_PRESSED) {
86 state_ = CAPTURING; 100 state_ = CAPTURING;
87 } 101 }
88 102
89 if (state_ == WAIT_FOR_MOUSE_RELEASE && 103 if (state_ == WAIT_FOR_MOUSE_RELEASE &&
90 event->type() == ui::ET_MOUSE_RELEASED) { 104 event->type() == ui::ET_MOUSE_RELEASED) {
91 CancelEvent(event); 105 CancelEvent(event);
92 state_ = INACTIVE; 106 state_ = INACTIVE;
93 return; 107 return;
94 } 108 }
95 109
96 if (state_ != CAPTURING) 110 if (state_ != CAPTURING)
97 return; 111 return;
98 112
99 // If we're in the capturing state, send accessibility events to 113 if (event->type() == ui::ET_MOUSE_RELEASED)
100 // the Select-to-speak extension based on the mouse event. 114 state_ = MOUSE_RELEASED;
101 // First, figure out what event to send. 115
102 ui::AXEvent ax_event = ui::AX_EVENT_NONE; 116 // If we're in the capturing state, forward the mouse event to
103 switch (event->type()) { 117 // select-to-speak.
104 case ui::ET_MOUSE_PRESSED: 118 if (event_delegate_for_testing_) {
105 ax_event = ui::AX_EVENT_MOUSE_PRESSED; 119 event_delegate_for_testing_->OnForwardEventToSelectToSpeakExtension(*event);
106 break; 120 } else {
107 case ui::ET_MOUSE_DRAGGED: 121 extensions::ExtensionHost* host = GetAccessibilityExtensionHost(
108 ax_event = ui::AX_EVENT_MOUSE_DRAGGED; 122 extension_misc::kSelectToSpeakExtensionId);
109 break; 123 if (!host)
110 case ui::ET_MOUSE_RELEASED:
111 state_ = MOUSE_RELEASED;
112 ax_event = ui::AX_EVENT_MOUSE_RELEASED;
113 break;
114 case ui::ET_MOUSE_MOVED:
115 case ui::ET_MOUSE_ENTERED:
116 case ui::ET_MOUSE_EXITED:
117 ax_event = ui::AX_EVENT_MOUSE_MOVED;
118 break;
119 default:
120 return; 124 return;
125
126 content::RenderViewHost* rvh = host->render_view_host();
127 if (!rvh)
128 return;
129
130 const blink::WebMouseEvent web_event =
131 ui::MakeWebMouseEvent(*event, base::Bind(&GetScreenLocationFromEvent));
132 rvh->GetWidget()->ForwardMouseEvent(web_event);
121 } 133 }
122 134
123 CancelEvent(event); 135 CancelEvent(event);
124
125 ui::AXTreeIDRegistry* registry = ui::AXTreeIDRegistry::GetInstance();
126 ui::AXHostDelegate* delegate =
127 registry->GetHostDelegate(extensions::api::automation::kDesktopTreeID);
128 if (delegate) {
129 ui::AXActionData action;
130 action.action = ui::AX_ACTION_HIT_TEST;
131 action.target_point = event->root_location();
132 action.hit_test_event_to_fire = ax_event;
133 delegate->PerformAction(action);
134 }
135 } 136 }
136 137
137 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) { 138 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) {
138 DCHECK(event); 139 DCHECK(event);
139 if (event->cancelable()) { 140 if (event->cancelable()) {
140 event->SetHandled(); 141 event->SetHandled();
141 event->StopPropagation(); 142 event->StopPropagation();
142 } 143 }
143 } 144 }
144 145
145 void SelectToSpeakEventHandler::SendCancelAXEvent() {
146 AutomationManagerAura::GetInstance()->HandleEvent(
147 nullptr, nullptr, ui::AX_EVENT_MOUSE_CANCELED);
148 }
149
150 } // namespace chromeos 146 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698