OLD | NEW |
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/chromeos/accessibility/event_handler_common.h" | 9 #include "chrome/browser/speech/tts_controller.h" |
10 #include "chrome/common/extensions/extension_constants.h" | 10 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" |
| 11 #include "chrome/common/extensions/api/automation_api_constants.h" |
11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/render_view_host.h" | 13 #include "ui/accessibility/ax_tree_id_registry.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" | |
16 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
17 #include "ui/display/display.h" | 15 #include "ui/display/display.h" |
18 #include "ui/events/blink/web_input_event.h" | |
19 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
20 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
21 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
22 | 19 |
23 namespace chromeos { | 20 namespace chromeos { |
24 | 21 |
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 | |
41 SelectToSpeakEventHandler::SelectToSpeakEventHandler() { | 22 SelectToSpeakEventHandler::SelectToSpeakEventHandler() { |
42 if (ash::Shell::HasInstance()) | 23 if (ash::Shell::HasInstance()) |
43 ash::Shell::Get()->GetPrimaryRootWindow()->AddPreTargetHandler(this); | 24 ash::Shell::Get()->GetPrimaryRootWindow()->AddPreTargetHandler(this); |
44 } | 25 } |
45 | 26 |
46 SelectToSpeakEventHandler::~SelectToSpeakEventHandler() { | 27 SelectToSpeakEventHandler::~SelectToSpeakEventHandler() { |
47 if (ash::Shell::HasInstance()) | 28 if (ash::Shell::HasInstance()) |
48 ash::Shell::Get()->GetPrimaryRootWindow()->RemovePreTargetHandler(this); | 29 ash::Shell::Get()->GetPrimaryRootWindow()->RemovePreTargetHandler(this); |
49 } | 30 } |
50 | 31 |
51 void SelectToSpeakEventHandler::CaptureForwardedEventsForTesting( | |
52 SelectToSpeakForwardedEventDelegateForTesting* delegate) { | |
53 event_delegate_for_testing_ = delegate; | |
54 } | |
55 | |
56 void SelectToSpeakEventHandler::OnKeyEvent(ui::KeyEvent* event) { | 32 void SelectToSpeakEventHandler::OnKeyEvent(ui::KeyEvent* event) { |
57 DCHECK(event); | 33 DCHECK(event); |
58 | 34 |
59 // We can only call TtsController on the UI thread, make sure we | 35 // We can only call TtsController on the UI thread, make sure we |
60 // don't ever try to run this code on some other thread. | 36 // don't ever try to run this code on some other thread. |
61 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 37 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
62 | 38 |
63 ui::KeyboardCode key_code = event->key_code(); | 39 ui::KeyboardCode key_code = event->key_code(); |
64 bool cancel_event = false; | 40 |
| 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 } |
65 | 59 |
66 // Update the state when pressing and releasing the Search key (VKEY_LWIN). | 60 // Update the state when pressing and releasing the Search key (VKEY_LWIN). |
67 if (key_code == ui::VKEY_LWIN) { | 61 if (key_code == ui::VKEY_LWIN) { |
68 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { | 62 if (event->type() == ui::ET_KEY_PRESSED && state_ == INACTIVE) { |
69 state_ = SEARCH_DOWN; | 63 state_ = SEARCH_DOWN; |
70 } else if (event->type() == ui::ET_KEY_RELEASED) { | 64 } else if (event->type() == ui::ET_KEY_RELEASED) { |
71 if (state_ == CAPTURING) { | 65 if (state_ == CAPTURING) { |
72 cancel_event = true; | 66 SendCancelAXEvent(); |
| 67 CancelEvent(event); |
73 state_ = WAIT_FOR_MOUSE_RELEASE; | 68 state_ = WAIT_FOR_MOUSE_RELEASE; |
74 } else if (state_ == MOUSE_RELEASED) { | 69 } else if (state_ == MOUSE_RELEASED) { |
75 cancel_event = true; | 70 CancelEvent(event); |
76 state_ = INACTIVE; | 71 state_ = INACTIVE; |
77 } | 72 } |
78 } | 73 } |
79 } else if (state_ == SEARCH_DOWN) { | 74 } else if (state_ == SEARCH_DOWN) { |
80 state_ = INACTIVE; | 75 state_ = INACTIVE; |
81 } | 76 } |
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); | |
91 } | 77 } |
92 | 78 |
93 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) { | 79 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) { |
94 DCHECK(event); | 80 DCHECK(event); |
95 if (state_ == INACTIVE) | 81 if (state_ == INACTIVE) |
96 return; | 82 return; |
97 | 83 |
98 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) && | 84 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) && |
99 event->type() == ui::ET_MOUSE_PRESSED) { | 85 event->type() == ui::ET_MOUSE_PRESSED) { |
100 state_ = CAPTURING; | 86 state_ = CAPTURING; |
101 } | 87 } |
102 | 88 |
103 if (state_ == WAIT_FOR_MOUSE_RELEASE && | 89 if (state_ == WAIT_FOR_MOUSE_RELEASE && |
104 event->type() == ui::ET_MOUSE_RELEASED) { | 90 event->type() == ui::ET_MOUSE_RELEASED) { |
105 CancelEvent(event); | 91 CancelEvent(event); |
106 state_ = INACTIVE; | 92 state_ = INACTIVE; |
107 return; | 93 return; |
108 } | 94 } |
109 | 95 |
110 if (state_ != CAPTURING) | 96 if (state_ != CAPTURING) |
111 return; | 97 return; |
112 | 98 |
113 if (event->type() == ui::ET_MOUSE_RELEASED) | 99 // If we're in the capturing state, send accessibility events to |
114 state_ = MOUSE_RELEASED; | 100 // the Select-to-speak extension based on the mouse event. |
115 | 101 // First, figure out what event to send. |
116 // If we're in the capturing state, forward the mouse event to | 102 ui::AXEvent ax_event = ui::AX_EVENT_NONE; |
117 // select-to-speak. | 103 switch (event->type()) { |
118 if (event_delegate_for_testing_) { | 104 case ui::ET_MOUSE_PRESSED: |
119 event_delegate_for_testing_->OnForwardEventToSelectToSpeakExtension(*event); | 105 ax_event = ui::AX_EVENT_MOUSE_PRESSED; |
120 } else { | 106 break; |
121 extensions::ExtensionHost* host = GetAccessibilityExtensionHost( | 107 case ui::ET_MOUSE_DRAGGED: |
122 extension_misc::kSelectToSpeakExtensionId); | 108 ax_event = ui::AX_EVENT_MOUSE_DRAGGED; |
123 if (!host) | 109 break; |
| 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: |
124 return; | 120 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); | |
133 } | 121 } |
134 | 122 |
135 CancelEvent(event); | 123 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 } |
136 } | 135 } |
137 | 136 |
138 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) { | 137 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) { |
139 DCHECK(event); | 138 DCHECK(event); |
140 if (event->cancelable()) { | 139 if (event->cancelable()) { |
141 event->SetHandled(); | 140 event->SetHandled(); |
142 event->StopPropagation(); | 141 event->StopPropagation(); |
143 } | 142 } |
144 } | 143 } |
145 | 144 |
| 145 void SelectToSpeakEventHandler::SendCancelAXEvent() { |
| 146 AutomationManagerAura::GetInstance()->HandleEvent( |
| 147 nullptr, nullptr, ui::AX_EVENT_MOUSE_CANCELED); |
| 148 } |
| 149 |
146 } // namespace chromeos | 150 } // namespace chromeos |
OLD | NEW |