1 // Copyright (c) 2012 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.
5 #ifndef ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
6 #define ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
11 #include "ash/accelerators/accelerator_table.h"
12 #include "ash/accelerators/exit_warning_handler.h"
13 #include "ash/ash_export.h"
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "ui/base/accelerators/accelerator.h"
19 #include "ui/base/accelerators/accelerator_history.h"
22 class AcceleratorManager
;
27 struct AcceleratorData
;
28 class BrightnessControlDelegate
;
29 class ExitWarningHandler
;
30 class ImeControlDelegate
;
31 class KeyboardBrightnessControlDelegate
;
32 class ScreenshotDelegate
;
33 class VolumeControlDelegate
;
35 // AcceleratorController provides functions for registering or unregistering
36 // global keyboard accelerators, which are handled earlier than any windows. It
37 // also implements several handlers as an accelerator target.
38 class ASH_EXPORT AcceleratorController
: public ui::AcceleratorTarget
{
40 AcceleratorController();
41 ~AcceleratorController() override
;
43 // A list of possible ways in which an accelerator should be restricted before
44 // processing. Any target registered with this controller should respect
45 // restrictions by calling |GetCurrentAcceleratorRestriction| during
47 enum AcceleratorProcessingRestriction
{
48 // Process the accelerator normally.
51 // Don't process the accelerator.
52 RESTRICTION_PREVENT_PROCESSING
,
54 // Don't process the accelerator and prevent propagation to other targets.
55 RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION
58 // Registers a global keyboard accelerator for the specified target. If
59 // multiple targets are registered for an accelerator, a target registered
60 // later has higher priority.
61 void Register(const ui::Accelerator
& accelerator
,
62 ui::AcceleratorTarget
* target
);
64 // Unregisters the specified keyboard accelerator for the specified target.
65 void Unregister(const ui::Accelerator
& accelerator
,
66 ui::AcceleratorTarget
* target
);
68 // Unregisters all keyboard accelerators for the specified target.
69 void UnregisterAll(ui::AcceleratorTarget
* target
);
71 // Activates the target associated with the specified accelerator.
72 // First, AcceleratorPressed handler of the most recently registered target
73 // is called, and if that handler processes the event (i.e. returns true),
74 // this method immediately returns. If not, we do the same thing on the next
76 // Returns true if an accelerator was activated.
77 bool Process(const ui::Accelerator
& accelerator
);
79 // Returns true if the |accelerator| is registered.
80 bool IsRegistered(const ui::Accelerator
& accelerator
) const;
82 // Returns true if the |accelerator| is preferred. A preferred accelerator
83 // is handled before being passed to an window/web contents, unless
84 // the window is in fullscreen state.
85 bool IsPreferred(const ui::Accelerator
& accelerator
) const;
87 // Returns true if the |accelerator| is reserved. A reserved accelerator
88 // is always handled and will never be passed to an window/web contents.
89 bool IsReserved(const ui::Accelerator
& accelerator
) const;
91 // Performs the specified action if it is enabled. Returns whether the action
92 // was performed successfully.
93 bool PerformActionIfEnabled(AcceleratorAction action
);
95 // Returns the restriction for the current context.
96 AcceleratorProcessingRestriction
GetCurrentAcceleratorRestriction();
98 void SetBrightnessControlDelegate(
99 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
);
100 void SetImeControlDelegate(
101 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
);
102 void SetScreenshotDelegate(
103 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
);
104 BrightnessControlDelegate
* brightness_control_delegate() const {
105 return brightness_control_delegate_
.get();
107 ScreenshotDelegate
* screenshot_delegate() {
108 return screenshot_delegate_
.get();
111 // Provides access to the ExitWarningHandler for testing.
112 ExitWarningHandler
* GetExitWarningHandlerForTest() {
113 return &exit_warning_handler_
;
116 ui::AcceleratorHistory
* accelerator_history() {
117 return accelerator_history_
.get();
120 // Overridden from ui::AcceleratorTarget:
121 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
122 bool CanHandleAccelerators() const override
;
125 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
, GlobalAccelerators
);
126 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
,
127 DontRepeatToggleFullscreen
);
129 // Initializes the accelerators this class handles as a target.
132 // Registers the specified accelerators.
133 void RegisterAccelerators(const AcceleratorData accelerators
[],
134 size_t accelerators_length
);
136 // Returns whether |action| can be performed. The |accelerator| may provide
137 // additional data the action needs.
138 bool CanPerformAction(AcceleratorAction action
,
139 const ui::Accelerator
& accelerator
);
141 // Performs the specified action. The |accelerator| may provide additional
142 // data the action needs.
143 void PerformAction(AcceleratorAction action
,
144 const ui::Accelerator
& accelerator
);
146 // Returns whether performing |action| should consume the key event.
147 bool ShouldActionConsumeKeyEvent(AcceleratorAction action
);
149 // Get the accelerator restriction for the given action. Supply an |action|
150 // of -1 to get restrictions that apply for the current context.
151 AcceleratorProcessingRestriction
GetAcceleratorProcessingRestriction(
154 void SetKeyboardBrightnessControlDelegate(
155 scoped_ptr
<KeyboardBrightnessControlDelegate
>
156 keyboard_brightness_control_delegate
);
158 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
160 // A tracker for the current and previous accelerators.
161 scoped_ptr
<ui::AcceleratorHistory
> accelerator_history_
;
163 // TODO(derat): BrightnessControlDelegate is also used by the system tray;
164 // move it outside of this class.
165 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate_
;
166 scoped_ptr
<ImeControlDelegate
> ime_control_delegate_
;
167 scoped_ptr
<KeyboardBrightnessControlDelegate
>
168 keyboard_brightness_control_delegate_
;
169 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate_
;
171 // Handles the exit accelerator which requires a double press to exit and
172 // shows a popup with an explanation.
173 ExitWarningHandler exit_warning_handler_
;
175 // A map from accelerators to the AcceleratorAction values, which are used in
176 // the implementation.
177 std::map
<ui::Accelerator
, AcceleratorAction
> accelerators_
;
179 // Actions allowed when the user is not signed in.
180 std::set
<int> actions_allowed_at_login_screen_
;
181 // Actions allowed when the screen is locked.
182 std::set
<int> actions_allowed_at_lock_screen_
;
183 // Actions allowed when a modal window is up.
184 std::set
<int> actions_allowed_at_modal_window_
;
185 // Preferred actions. See accelerator_table.h for details.
186 std::set
<int> preferred_actions_
;
187 // Reserved actions. See accelerator_table.h for details.
188 std::set
<int> reserved_actions_
;
189 // Actions which will not be repeated while holding the accelerator key.
190 std::set
<int> nonrepeatable_actions_
;
191 // Actions allowed in app mode.
192 std::set
<int> actions_allowed_in_app_mode_
;
193 // Actions disallowed if there are no windows.
194 std::set
<int> actions_needing_window_
;
196 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
201 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_