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/exit_warning_handler.h"
12 #include "ash/ash_export.h"
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "ui/base/accelerators/accelerator.h"
20 class AcceleratorManager
;
25 struct AcceleratorData
;
26 class BrightnessControlDelegate
;
27 class ExitWarningHandler
;
28 class ImeControlDelegate
;
29 class KeyboardBrightnessControlDelegate
;
30 class ScreenshotDelegate
;
31 class VolumeControlDelegate
;
33 // AcceleratorController provides functions for registering or unregistering
34 // global keyboard accelerators, which are handled earlier than any windows. It
35 // also implements several handlers as an accelerator target.
36 class ASH_EXPORT AcceleratorController
: public ui::AcceleratorTarget
{
38 AcceleratorController();
39 virtual ~AcceleratorController();
41 // A list of possible ways in which an accelerator should be restricted before
42 // processing. Any target registered with this controller should respect
43 // restrictions by calling |GetCurrentAcceleratorRestriction| during
45 enum AcceleratorProcessingRestriction
{
46 // Process the accelerator normally.
49 // Don't process the accelerator.
50 RESTRICTION_PREVENT_PROCESSING
,
52 // Don't process the accelerator and prevent propagation to other targets.
53 RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION
56 // Registers a global keyboard accelerator for the specified target. If
57 // multiple targets are registered for an accelerator, a target registered
58 // later has higher priority.
59 void Register(const ui::Accelerator
& accelerator
,
60 ui::AcceleratorTarget
* target
);
62 // Unregisters the specified keyboard accelerator for the specified target.
63 void Unregister(const ui::Accelerator
& accelerator
,
64 ui::AcceleratorTarget
* target
);
66 // Unregisters all keyboard accelerators for the specified target.
67 void UnregisterAll(ui::AcceleratorTarget
* target
);
69 // Activates the target associated with the specified accelerator.
70 // First, AcceleratorPressed handler of the most recently registered target
71 // is called, and if that handler processes the event (i.e. returns true),
72 // this method immediately returns. If not, we do the same thing on the next
74 // Returns true if an accelerator was activated.
75 bool Process(const ui::Accelerator
& accelerator
);
77 // Returns true if the |accelerator| is registered.
78 bool IsRegistered(const ui::Accelerator
& accelerator
) const;
80 // Returns true if the |accelerator| is one of the |reserved_actions_|.
81 bool IsReservedAccelerator(const ui::Accelerator
& accelerator
) const;
83 // Performs the specified action. The |accelerator| may provide additional
84 // data the action needs. Returns whether an action was performed
86 bool PerformAction(int action
,
87 const ui::Accelerator
& accelerator
);
89 // Returns the restriction for the current context.
90 AcceleratorProcessingRestriction
GetCurrentAcceleratorRestriction();
92 // Overridden from ui::AcceleratorTarget:
93 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
94 virtual bool CanHandleAccelerators() const OVERRIDE
;
96 void SetBrightnessControlDelegate(
97 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
);
98 void SetImeControlDelegate(
99 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
);
100 void SetScreenshotDelegate(
101 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
);
102 BrightnessControlDelegate
* brightness_control_delegate() const {
103 return brightness_control_delegate_
.get();
105 ScreenshotDelegate
* screenshot_delegate() {
106 return screenshot_delegate_
.get();
109 // Provides access to the ExitWarningHandler for testing.
110 ExitWarningHandler
* GetExitWarningHandlerForTest() {
111 return &exit_warning_handler_
;
114 const ui::Accelerator
& previous_accelerator_for_test() const {
115 return previous_accelerator_
;
119 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
, GlobalAccelerators
);
120 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
,
121 DontRepeatToggleFullscreen
);
123 // Initializes the accelerators this class handles as a target.
126 // Registers the specified accelerators.
127 void RegisterAccelerators(const AcceleratorData accelerators
[],
128 size_t accelerators_length
);
130 // Get the accelerator restriction for the given action. Supply an |action|
131 // of -1 to get restrictions that apply for the current context.
132 AcceleratorProcessingRestriction
GetAcceleratorProcessingRestriction(
135 void SetKeyboardBrightnessControlDelegate(
136 scoped_ptr
<KeyboardBrightnessControlDelegate
>
137 keyboard_brightness_control_delegate
);
139 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
141 // TODO(derat): BrightnessControlDelegate is also used by the system tray;
142 // move it outside of this class.
143 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate_
;
144 scoped_ptr
<ImeControlDelegate
> ime_control_delegate_
;
145 scoped_ptr
<KeyboardBrightnessControlDelegate
>
146 keyboard_brightness_control_delegate_
;
147 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate_
;
149 // Remember previous accelerator as some accelerator needs to be fired
150 // with a specific sequence.
151 ui::Accelerator previous_accelerator_
;
153 // Handles the exit accelerator which requires a double press to exit and
154 // shows a popup with an explanation.
155 ExitWarningHandler exit_warning_handler_
;
157 // A map from accelerators to the AcceleratorAction values, which are used in
158 // the implementation.
159 std::map
<ui::Accelerator
, int> accelerators_
;
161 // Actions allowed when the user is not signed in.
162 std::set
<int> actions_allowed_at_login_screen_
;
163 // Actions allowed when the screen is locked.
164 std::set
<int> actions_allowed_at_lock_screen_
;
165 // Actions allowed when a modal window is up.
166 std::set
<int> actions_allowed_at_modal_window_
;
167 // Reserved actions. See accelerator_table.h for details.
168 std::set
<int> reserved_actions_
;
169 // Actions which will not be repeated while holding the accelerator key.
170 std::set
<int> nonrepeatable_actions_
;
171 // Actions allowed in app mode.
172 std::set
<int> actions_allowed_in_app_mode_
;
173 // Actions disallowed if there are no windows.
174 std::set
<int> actions_needing_window_
;
176 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
181 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_