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 // Stores information about accelerator context, eg. previous accelerator
34 // or if the current accelerator is repeated or not.
35 class ASH_EXPORT AcceleratorControllerContext
{
37 AcceleratorControllerContext();
38 ~AcceleratorControllerContext() {}
40 // Updates context - determines if the accelerator is repeated, as well as
41 // event type of the previous accelerator.
42 void UpdateContext(const ui::Accelerator
& accelerator
);
44 const ui::Accelerator
& previous_accelerator() const {
45 return previous_accelerator_
;
47 bool repeated() const {
48 return current_accelerator_
== previous_accelerator_
&&
49 current_accelerator_
.type() != ui::ET_UNKNOWN
;
53 ui::Accelerator current_accelerator_
;
54 // Used for NEXT_IME and DISABLE_CAPS_LOCK accelerator actions.
55 ui::Accelerator previous_accelerator_
;
57 DISALLOW_COPY_AND_ASSIGN(AcceleratorControllerContext
);
60 // AcceleratorController provides functions for registering or unregistering
61 // global keyboard accelerators, which are handled earlier than any windows. It
62 // also implements several handlers as an accelerator target.
63 class ASH_EXPORT AcceleratorController
: public ui::AcceleratorTarget
{
65 AcceleratorController();
66 virtual ~AcceleratorController();
68 // Registers a global keyboard accelerator for the specified target. If
69 // multiple targets are registered for an accelerator, a target registered
70 // later has higher priority.
71 void Register(const ui::Accelerator
& accelerator
,
72 ui::AcceleratorTarget
* target
);
74 // Unregisters the specified keyboard accelerator for the specified target.
75 void Unregister(const ui::Accelerator
& accelerator
,
76 ui::AcceleratorTarget
* target
);
78 // Unregisters all keyboard accelerators for the specified target.
79 void UnregisterAll(ui::AcceleratorTarget
* target
);
81 // Activates the target associated with the specified accelerator.
82 // First, AcceleratorPressed handler of the most recently registered target
83 // is called, and if that handler processes the event (i.e. returns true),
84 // this method immediately returns. If not, we do the same thing on the next
86 // Returns true if an accelerator was activated.
87 bool Process(const ui::Accelerator
& accelerator
);
89 // Returns true if the |accelerator| is registered.
90 bool IsRegistered(const ui::Accelerator
& accelerator
) const;
92 // Returns true if the |accelerator| is one of the |reserved_actions_|.
93 bool IsReservedAccelerator(const ui::Accelerator
& accelerator
) const;
95 // Performs the specified action. The |accelerator| may provide additional
96 // data the action needs. Returns whether an action was performed
98 bool PerformAction(int action
,
99 const ui::Accelerator
& accelerator
);
101 // Overridden from ui::AcceleratorTarget:
102 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
103 virtual bool CanHandleAccelerators() const OVERRIDE
;
105 void SetBrightnessControlDelegate(
106 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
);
107 void SetImeControlDelegate(
108 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
);
109 void SetScreenshotDelegate(
110 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
);
111 BrightnessControlDelegate
* brightness_control_delegate() const {
112 return brightness_control_delegate_
.get();
114 ScreenshotDelegate
* screenshot_delegate() {
115 return screenshot_delegate_
.get();
118 // Provides access to an object holding contextual information.
119 AcceleratorControllerContext
* context() {
123 // Provides access to the ExitWarningHandler for testing.
124 ExitWarningHandler
* GetExitWarningHandlerForTest() {
125 return &exit_warning_handler_
;
129 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
, GlobalAccelerators
);
131 // Initializes the accelerators this class handles as a target.
134 // Registers the specified accelerators.
135 void RegisterAccelerators(const AcceleratorData accelerators
[],
136 size_t accelerators_length
);
138 void SetKeyboardBrightnessControlDelegate(
139 scoped_ptr
<KeyboardBrightnessControlDelegate
>
140 keyboard_brightness_control_delegate
);
142 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
144 // TODO(derat): BrightnessControlDelegate is also used by the system tray;
145 // move it outside of this class.
146 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate_
;
147 scoped_ptr
<ImeControlDelegate
> ime_control_delegate_
;
148 scoped_ptr
<KeyboardBrightnessControlDelegate
>
149 keyboard_brightness_control_delegate_
;
150 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate_
;
152 // Contextual information, eg. if the current accelerator is repeated.
153 AcceleratorControllerContext context_
;
155 // Handles the exit accelerator which requires a double press to exit and
156 // shows a popup with an explanation.
157 ExitWarningHandler exit_warning_handler_
;
159 // A map from accelerators to the AcceleratorAction values, which are used in
160 // the implementation.
161 std::map
<ui::Accelerator
, int> accelerators_
;
163 // Actions allowed when the user is not signed in.
164 std::set
<int> actions_allowed_at_login_screen_
;
165 // Actions allowed when the screen is locked.
166 std::set
<int> actions_allowed_at_lock_screen_
;
167 // Actions allowed when a modal window is up.
168 std::set
<int> actions_allowed_at_modal_window_
;
169 // Reserved actions. See accelerator_table.h for details.
170 std::set
<int> reserved_actions_
;
171 // Actions which will not be repeated while holding the accelerator key.
172 std::set
<int> nonrepeatable_actions_
;
173 // Actions allowed in app mode.
174 std::set
<int> actions_allowed_in_app_mode_
;
175 // Actions disallowed if there are no windows.
176 std::set
<int> actions_needing_window_
;
178 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
183 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_