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 preferred. A preferred accelerator
81 // is handled before being passed to an window/web contents, unless
82 // the window is in fullscreen state.
83 bool IsPreferred(const ui::Accelerator
& accelerator
) const;
85 // Returns true if the |accelerator| is reserved. A reserved accelerator
86 // is always handled and will never be passed to an window/web contents.
87 bool IsReserved(const ui::Accelerator
& accelerator
) const;
89 // Performs the specified action. The |accelerator| may provide additional
90 // data the action needs. Returns whether an action was performed
92 bool PerformAction(int action
,
93 const ui::Accelerator
& accelerator
);
95 // Returns the restriction for the current context.
96 AcceleratorProcessingRestriction
GetCurrentAcceleratorRestriction();
98 // Overridden from ui::AcceleratorTarget:
99 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
100 virtual bool CanHandleAccelerators() const override
;
102 void SetBrightnessControlDelegate(
103 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
);
104 void SetImeControlDelegate(
105 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
);
106 void SetScreenshotDelegate(
107 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
);
108 BrightnessControlDelegate
* brightness_control_delegate() const {
109 return brightness_control_delegate_
.get();
111 ScreenshotDelegate
* screenshot_delegate() {
112 return screenshot_delegate_
.get();
115 // Provides access to the ExitWarningHandler for testing.
116 ExitWarningHandler
* GetExitWarningHandlerForTest() {
117 return &exit_warning_handler_
;
120 const ui::Accelerator
& previous_accelerator_for_test() const {
121 return previous_accelerator_
;
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 // Get the accelerator restriction for the given action. Supply an |action|
137 // of -1 to get restrictions that apply for the current context.
138 AcceleratorProcessingRestriction
GetAcceleratorProcessingRestriction(
141 void SetKeyboardBrightnessControlDelegate(
142 scoped_ptr
<KeyboardBrightnessControlDelegate
>
143 keyboard_brightness_control_delegate
);
145 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
147 // TODO(derat): BrightnessControlDelegate is also used by the system tray;
148 // move it outside of this class.
149 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate_
;
150 scoped_ptr
<ImeControlDelegate
> ime_control_delegate_
;
151 scoped_ptr
<KeyboardBrightnessControlDelegate
>
152 keyboard_brightness_control_delegate_
;
153 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate_
;
155 // Remember previous accelerator as some accelerator needs to be fired
156 // with a specific sequence.
157 ui::Accelerator previous_accelerator_
;
159 // Handles the exit accelerator which requires a double press to exit and
160 // shows a popup with an explanation.
161 ExitWarningHandler exit_warning_handler_
;
163 // A map from accelerators to the AcceleratorAction values, which are used in
164 // the implementation.
165 std::map
<ui::Accelerator
, int> accelerators_
;
167 // Actions allowed when the user is not signed in.
168 std::set
<int> actions_allowed_at_login_screen_
;
169 // Actions allowed when the screen is locked.
170 std::set
<int> actions_allowed_at_lock_screen_
;
171 // Actions allowed when a modal window is up.
172 std::set
<int> actions_allowed_at_modal_window_
;
173 // Preferred actions. See accelerator_table.h for details.
174 std::set
<int> preferred_actions_
;
175 // Reserved actions. See accelerator_table.h for details.
176 std::set
<int> reserved_actions_
;
177 // Actions which will not be repeated while holding the accelerator key.
178 std::set
<int> nonrepeatable_actions_
;
179 // Actions allowed in app mode.
180 std::set
<int> actions_allowed_in_app_mode_
;
181 // Actions disallowed if there are no windows.
182 std::set
<int> actions_needing_window_
;
184 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
189 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_