forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerator_controller.h
254 lines (202 loc) · 10.6 KB
/
accelerator_controller.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
#define ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
#include <stddef.h>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include "ash/accelerators/accelerator_confirmation_dialog.h"
#include "ash/accelerators/accelerator_table.h"
#include "ash/accelerators/exit_warning_handler.h"
#include "ash/ash_export.h"
#include "ash/public/interfaces/accelerator_controller.mojom.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator_history.h"
namespace ui {
class AcceleratorManager;
class AcceleratorManagerDelegate;
}
namespace ash {
struct AcceleratorData;
class ExitWarningHandler;
// Identifiers for toggling accelerator notifications.
ASH_EXPORT extern const char kHighContrastToggleAccelNotificationId[];
ASH_EXPORT extern const char kDockedMagnifierToggleAccelNotificationId[];
ASH_EXPORT extern const char kFullscreenMagnifierToggleAccelNotificationId[];
// AcceleratorController provides functions for registering or unregistering
// global keyboard accelerators, which are handled earlier than any windows. It
// also implements several handlers as an accelerator target.
class ASH_EXPORT AcceleratorController : public ui::AcceleratorTarget,
public mojom::AcceleratorController {
public:
explicit AcceleratorController(
ui::AcceleratorManagerDelegate* manager_delegate);
~AcceleratorController() override;
// A list of possible ways in which an accelerator should be restricted before
// processing. Any target registered with this controller should respect
// restrictions by calling |GetCurrentAcceleratorRestriction| during
// processing.
enum AcceleratorProcessingRestriction {
// Process the accelerator normally.
RESTRICTION_NONE,
// Don't process the accelerator.
RESTRICTION_PREVENT_PROCESSING,
// Don't process the accelerator and prevent propagation to other targets.
RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION
};
// Registers global keyboard accelerators for the specified target. If
// multiple targets are registered for any given accelerator, a target
// registered later has higher priority.
void Register(const std::vector<ui::Accelerator>& accelerators,
ui::AcceleratorTarget* target);
// Unregisters the specified keyboard accelerator for the specified target.
void Unregister(const ui::Accelerator& accelerator,
ui::AcceleratorTarget* target);
// Unregisters all keyboard accelerators for the specified target.
void UnregisterAll(ui::AcceleratorTarget* target);
// Returns true if there is an action for |accelerator| and it is enabled.
bool IsActionForAcceleratorEnabled(const ui::Accelerator& accelerator) const;
// Activates the target associated with the specified accelerator.
// First, AcceleratorPressed handler of the most recently registered target
// is called, and if that handler processes the event (i.e. returns true),
// this method immediately returns. If not, we do the same thing on the next
// target, and so on.
// Returns true if an accelerator was activated.
bool Process(const ui::Accelerator& accelerator);
// Returns true if the |accelerator| is registered.
bool IsRegistered(const ui::Accelerator& accelerator) const;
// Returns true if the |accelerator| is preferred. A preferred accelerator
// is handled before being passed to an window/web contents, unless
// the window is in fullscreen state.
bool IsPreferred(const ui::Accelerator& accelerator) const;
// Returns true if the |accelerator| is reserved. A reserved accelerator
// is always handled and will never be passed to an window/web contents.
bool IsReserved(const ui::Accelerator& accelerator) const;
// Returns true if the |accelerator| is deprecated. Deprecated accelerators
// can be consumed by web contents if needed.
bool IsDeprecated(const ui::Accelerator& accelerator) const;
// Performs the specified action if it is enabled. Returns whether the action
// was performed successfully.
bool PerformActionIfEnabled(AcceleratorAction action);
// Returns the restriction for the current context.
AcceleratorProcessingRestriction GetCurrentAcceleratorRestriction();
// Provides access to the ExitWarningHandler for testing.
ExitWarningHandler* GetExitWarningHandlerForTest() {
return &exit_warning_handler_;
}
// Returns true if the menu should close in order to perform the accelerator.
bool ShouldCloseMenuAndRepostAccelerator(
const ui::Accelerator& accelerator) const;
ui::AcceleratorHistory* accelerator_history() {
return accelerator_history_.get();
}
// Overridden from ui::AcceleratorTarget:
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
bool CanHandleAccelerators() const override;
// Binds the mojom::AcceleratorController interface to this object.
void BindRequest(mojom::AcceleratorControllerRequest request);
// mojom::AcceleratorController:
void SetVolumeController(mojom::VolumeControllerPtr controller) override;
// A confirmation dialog will be shown the first time an accessibility feature
// is enabled using the specified accelerator key sequence. Only one dialog
// will be shown at a time, and will not be shown again if the user has
// selected "accept" on a given dialog. The dialog was added to ensure that
// users would be aware of the shortcut they have just enabled, and to prevent
// users from accidentally triggering the feature. The dialog is currently
// shown when enabling the following features: high contrast, full screen
// magnifier and docked magnifier. The shown dialog is stored as a weak
// pointer in the variable |confirmation_dialog_| below.
void MaybeShowConfirmationDialog(int window_title_text_id,
int dialog_text_id,
base::OnceClosure on_accept_callback);
// Accessor to accelerator confirmation dialog.
AcceleratorConfirmationDialog* confirmation_dialog_for_testing();
private:
FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest, GlobalAccelerators);
FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest,
DontRepeatToggleFullscreen);
FRIEND_TEST_ALL_PREFIXES(DeprecatedAcceleratorTester,
TestDeprecatedAcceleratorsBehavior);
// Initializes the accelerators this class handles as a target.
void Init();
// Registers the specified accelerators.
void RegisterAccelerators(const AcceleratorData accelerators[],
size_t accelerators_length);
// Registers the deprecated accelerators and their replacing new ones.
void RegisterDeprecatedAccelerators();
// Returns whether |action| can be performed. The |accelerator| may provide
// additional data the action needs.
bool CanPerformAction(AcceleratorAction action,
const ui::Accelerator& accelerator) const;
// Performs the specified action. The |accelerator| may provide additional
// data the action needs.
void PerformAction(AcceleratorAction action,
const ui::Accelerator& accelerator);
// Returns whether performing |action| should consume the key event.
bool ShouldActionConsumeKeyEvent(AcceleratorAction action);
// Get the accelerator restriction for the given action. Supply an |action|
// of -1 to get restrictions that apply for the current context.
AcceleratorProcessingRestriction GetAcceleratorProcessingRestriction(
int action) const;
// If |accelerator| is a deprecated accelerator, it performs the appropriate
// deprecated accelerator pre-handling.
// Returns PROCEED if the accelerator's action should be performed (i.e. if
// |accelerator| is not a deprecated accelerator, or it's an enabled
// deprecated accelerator), and STOP otherwise (if the accelerator is a
// disabled deprecated accelerator).
enum class AcceleratorProcessingStatus { PROCEED, STOP };
AcceleratorProcessingStatus MaybeDeprecatedAcceleratorPressed(
AcceleratorAction action,
const ui::Accelerator& accelerator) const;
std::unique_ptr<ui::AcceleratorManager> accelerator_manager_;
// A tracker for the current and previous accelerators.
std::unique_ptr<ui::AcceleratorHistory> accelerator_history_;
// Handles the exit accelerator which requires a double press to exit and
// shows a popup with an explanation.
ExitWarningHandler exit_warning_handler_;
// A map from accelerators to the AcceleratorAction values, which are used in
// the implementation.
std::map<ui::Accelerator, AcceleratorAction> accelerators_;
std::map<AcceleratorAction, const DeprecatedAcceleratorData*>
actions_with_deprecations_;
std::set<ui::Accelerator> deprecated_accelerators_;
// Bindings for the mojom::AcceleratorController interface.
mojo::BindingSet<mojom::AcceleratorController> bindings_;
// Volume controller interface in chrome browser. May be null in tests. Exists
// because chrome owns the CrasAudioHandler dbus communication.
mojom::VolumeControllerPtr volume_controller_;
// Actions allowed when the user is not signed in.
std::set<int> actions_allowed_at_login_screen_;
// Actions allowed when the screen is locked.
std::set<int> actions_allowed_at_lock_screen_;
// Actions allowed when the power menu is opened.
std::set<int> actions_allowed_at_power_menu_;
// Actions allowed when a modal window is up.
std::set<int> actions_allowed_at_modal_window_;
// Preferred actions. See accelerator_table.h for details.
std::set<int> preferred_actions_;
// Reserved actions. See accelerator_table.h for details.
std::set<int> reserved_actions_;
// Actions which will be repeated while holding the accelerator key.
std::set<int> repeatable_actions_;
// Actions allowed in app mode.
std::set<int> actions_allowed_in_app_mode_;
// Actions allowed in pinned mode.
std::set<int> actions_allowed_in_pinned_mode_;
// Actions disallowed if there are no windows.
std::set<int> actions_needing_window_;
// Actions that can be performed without closing the menu (if one is present).
std::set<int> actions_keeping_menu_open_;
// Holds a weak pointer to the accelerator confirmation dialog.
base::WeakPtr<AcceleratorConfirmationDialog> confirmation_dialog_;
DISALLOW_COPY_AND_ASSIGN(AcceleratorController);
};
} // namespace ash
#endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_