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();
115 // Provides access to an object holding contextual information.
116 AcceleratorControllerContext
* context() {
120 // Provides access to the ExitWarningHandler for testing.
121 ExitWarningHandler
* GetExitWarningHandlerForTest() {
122 return &exit_warning_handler_
;
126 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
, GlobalAccelerators
);
128 // Initializes the accelerators this class handles as a target.
131 // Registers the specified accelerators.
132 void RegisterAccelerators(const AcceleratorData accelerators
[],
133 size_t accelerators_length
);
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 // Contextual information, eg. if the current accelerator is repeated.
150 AcceleratorControllerContext context_
;
152 // Handles the exit accelerator which requires a double press to exit and
153 // shows a popup with an explanation.
154 ExitWarningHandler exit_warning_handler_
;
156 // A map from accelerators to the AcceleratorAction values, which are used in
157 // the implementation.
158 std::map
<ui::Accelerator
, int> accelerators_
;
160 // Actions allowed when the user is not signed in.
161 std::set
<int> actions_allowed_at_login_screen_
;
162 // Actions allowed when the screen is locked.
163 std::set
<int> actions_allowed_at_lock_screen_
;
164 // Actions allowed when a modal window is up.
165 std::set
<int> actions_allowed_at_modal_window_
;
166 // Reserved actions. See accelerator_table.h for details.
167 std::set
<int> reserved_actions_
;
168 // Actions which will not be repeated while holding the accelerator key.
169 std::set
<int> nonrepeatable_actions_
;
170 // Actions allowed in app mode.
171 std::set
<int> actions_allowed_in_app_mode_
;
173 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
178 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_