1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
ash / webui / common / backend / shortcut_input_provider.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/webui/common/backend/shortcut_input_provider.h"
#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/accelerators/shortcut_input_handler.h"
#include "ash/constants/ash_features.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "ash/shell.h"
#include "ash/webui/common/mojom/shortcut_input_provider.mojom.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/views/widget/widget.h"
namespace ash {
ShortcutInputProvider::ShortcutInputProvider() {
if (Shell::HasInstance()) {
auto* shortcut_input_handler = Shell::Get()->shortcut_input_handler();
if (shortcut_input_handler) {
shortcut_input_handler->AddObserver(this);
}
}
}
ShortcutInputProvider::~ShortcutInputProvider() {
if (Shell::HasInstance()) {
auto* shortcut_input_handler = Shell::Get()->shortcut_input_handler();
if (shortcut_input_handler) {
shortcut_input_handler->SetShouldConsumeKeyEvents(
/*should_consume_key_events=*/false);
shortcut_input_handler->RemoveObserver(this);
}
Shell::Get()->accelerator_controller()->SetPreventProcessingAccelerators(
/*prevent_processing_accelerators=*/false);
}
if (widget_) {
widget_->RemoveObserver(this);
}
}
void ShortcutInputProvider::BindInterface(
mojo::PendingReceiver<common::mojom::ShortcutInputProvider> receiver) {
CHECK(features::IsPeripheralCustomizationEnabled() ||
::features::IsAccessibilityFaceGazeEnabled());
if (shortcut_input_receiver_.is_bound()) {
shortcut_input_receiver_.reset();
}
shortcut_input_receiver_.Bind(std::move(receiver));
}
void ShortcutInputProvider::OnShortcutInputEventPressed(
const mojom::KeyEvent& key_event) {
if (observing_paused_) {
return;
}
// If there is no `prerewritten_event_` then the event must have been sent
// via a non-standard method, e.g. virtual keyboard or IME extension. These
// events are still valid as they do not go through event rewrites.
const bool is_fabricated_event_ = prerewritten_event_.is_null();
for (auto& observer : shortcut_input_observers_) {
observer->OnShortcutInputEventPressed(
is_fabricated_event_ ? key_event.Clone() : prerewritten_event_.Clone(),
key_event.Clone());
}
prerewritten_event_.reset();
}
void ShortcutInputProvider::OnShortcutInputEventReleased(
const mojom::KeyEvent& key_event) {
if (observing_paused_) {
return;
}
// If there is no `prerewritten_event_` then the event must have been sent
// via a non-standard method, e.g. virtual keyboard or IME extension. These
// events are still valid as they do not go through event rewrites.
const bool is_fabricated_event_ = prerewritten_event_.is_null();
for (auto& observer : shortcut_input_observers_) {
observer->OnShortcutInputEventReleased(
is_fabricated_event_ ? key_event.Clone() : prerewritten_event_.Clone(),
key_event.Clone());
}
prerewritten_event_.reset();
}
void ShortcutInputProvider::OnPrerewrittenShortcutInputEventPressed(
const mojom::KeyEvent& key_event) {
// We discard the event if it is a function key, so no "post rewrite" event
// will be seen.
if (key_event.vkey == ui::VKEY_FUNCTION) {
for (auto& observer : shortcut_input_observers_) {
observer->OnShortcutInputEventPressed(key_event.Clone(), nullptr);
}
return;
}
prerewritten_event_ = key_event.Clone();
}
void ShortcutInputProvider::OnPrerewrittenShortcutInputEventReleased(
// We discard the event if it is a function key, so no "post rewrite" event
// will be seen.
const mojom::KeyEvent& key_event) {
if (key_event.vkey == ui::VKEY_FUNCTION) {
for (auto& observer : shortcut_input_observers_) {
observer->OnShortcutInputEventReleased(key_event.Clone(), nullptr);
}
return;
}
prerewritten_event_ = key_event.Clone();
}
void ShortcutInputProvider::StartObservingShortcutInput(
mojo::PendingRemote<common::mojom::ShortcutInputObserver> observer) {
shortcut_input_observers_.Add(std::move(observer));
AdjustShortcutBlockingIfNeeded();
}
void ShortcutInputProvider::StopObservingShortcutInput() {
shortcut_input_observers_.Clear();
AdjustShortcutBlockingIfNeeded();
}
void ShortcutInputProvider::OnWidgetVisibilityChanged(views::Widget* widget,
bool visible) {
HandleObserving();
}
void ShortcutInputProvider::OnWidgetActivationChanged(views::Widget* widget,
bool active) {
HandleObserving();
}
void ShortcutInputProvider::OnWidgetDestroying(views::Widget* widget) {
widget_->RemoveObserver(this);
widget_ = nullptr;
observing_paused_ = true;
AdjustShortcutBlockingIfNeeded();
}
void ShortcutInputProvider::HandleObserving() {
if (!widget_) {
return;
}
const bool widget_open = !widget_->IsClosed();
const bool widget_active = widget_->IsActive();
const bool widget_visible = widget_->IsVisible();
observing_paused_ = !(widget_open && widget_visible && widget_active);
AdjustShortcutBlockingIfNeeded();
}
void ShortcutInputProvider::TieProviderToWidget(views::Widget* widget) {
if (widget_ == widget) {
return;
}
CHECK(!widget_);
CHECK(widget);
widget_ = widget;
widget_->AddObserver(this);
HandleObserving();
}
void ShortcutInputProvider::AdjustShortcutBlockingIfNeeded() {
if (!observing_paused_ && !shortcut_input_observers_.empty()) {
Shell::Get()->shortcut_input_handler()->SetShouldConsumeKeyEvents(
/*should_consume_key_events=*/true);
Shell::Get()->accelerator_controller()->SetPreventProcessingAccelerators(
/*prevent_processing_accelerators=*/true);
return;
}
Shell::Get()->shortcut_input_handler()->SetShouldConsumeKeyEvents(
/*should_consume_key_events=*/false);
Shell::Get()->accelerator_controller()->SetPreventProcessingAccelerators(
/*prevent_processing_accelerators=*/false);
}
void ShortcutInputProvider::FlushMojoForTesting() {
shortcut_input_observers_.FlushForTesting(); // IN-TEST
}
} // namespace ash