• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
6 #define ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
7 
8 #include <map>
9 #include <set>
10 
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"
18 
19 namespace ui {
20 class AcceleratorManager;
21 }
22 
23 namespace ash {
24 
25 struct AcceleratorData;
26 class BrightnessControlDelegate;
27 class ExitWarningHandler;
28 class ImeControlDelegate;
29 class KeyboardBrightnessControlDelegate;
30 class ScreenshotDelegate;
31 class VolumeControlDelegate;
32 
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 {
37  public:
38   AcceleratorController();
39   virtual ~AcceleratorController();
40 
41   // Registers a global keyboard accelerator for the specified target. If
42   // multiple targets are registered for an accelerator, a target registered
43   // later has higher priority.
44   void Register(const ui::Accelerator& accelerator,
45                 ui::AcceleratorTarget* target);
46 
47   // Unregisters the specified keyboard accelerator for the specified target.
48   void Unregister(const ui::Accelerator& accelerator,
49                   ui::AcceleratorTarget* target);
50 
51   // Unregisters all keyboard accelerators for the specified target.
52   void UnregisterAll(ui::AcceleratorTarget* target);
53 
54   // Activates the target associated with the specified accelerator.
55   // First, AcceleratorPressed handler of the most recently registered target
56   // is called, and if that handler processes the event (i.e. returns true),
57   // this method immediately returns. If not, we do the same thing on the next
58   // target, and so on.
59   // Returns true if an accelerator was activated.
60   bool Process(const ui::Accelerator& accelerator);
61 
62   // Returns true if the |accelerator| is registered.
63   bool IsRegistered(const ui::Accelerator& accelerator) const;
64 
65   // Returns true if the |accelerator| is one of the |reserved_actions_|.
66   bool IsReservedAccelerator(const ui::Accelerator& accelerator) const;
67 
68   // Performs the specified action. The |accelerator| may provide additional
69   // data the action needs. Returns whether an action was performed
70   // successfully.
71   bool PerformAction(int action,
72                      const ui::Accelerator& accelerator);
73 
74   // Overridden from ui::AcceleratorTarget:
75   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
76   virtual bool CanHandleAccelerators() const OVERRIDE;
77 
78   void SetBrightnessControlDelegate(
79       scoped_ptr<BrightnessControlDelegate> brightness_control_delegate);
80   void SetImeControlDelegate(
81       scoped_ptr<ImeControlDelegate> ime_control_delegate);
82   void SetScreenshotDelegate(
83       scoped_ptr<ScreenshotDelegate> screenshot_delegate);
brightness_control_delegate()84   BrightnessControlDelegate* brightness_control_delegate() const {
85     return brightness_control_delegate_.get();
86   }
screenshot_delegate()87   ScreenshotDelegate* screenshot_delegate() {
88     return screenshot_delegate_.get();
89   }
90 
91   // Provides access to the ExitWarningHandler for testing.
GetExitWarningHandlerForTest()92   ExitWarningHandler* GetExitWarningHandlerForTest() {
93     return &exit_warning_handler_;
94   }
95 
previous_accelerator_for_test()96   const ui::Accelerator& previous_accelerator_for_test() const {
97     return previous_accelerator_;
98   }
99 
100  private:
101   FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest, GlobalAccelerators);
102   FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest,
103                            DontRepeatToggleFullscreen);
104 
105   // Initializes the accelerators this class handles as a target.
106   void Init();
107 
108   // Registers the specified accelerators.
109   void RegisterAccelerators(const AcceleratorData accelerators[],
110                             size_t accelerators_length);
111 
112   void SetKeyboardBrightnessControlDelegate(
113       scoped_ptr<KeyboardBrightnessControlDelegate>
114       keyboard_brightness_control_delegate);
115 
116   scoped_ptr<ui::AcceleratorManager> accelerator_manager_;
117 
118   // TODO(derat): BrightnessControlDelegate is also used by the system tray;
119   // move it outside of this class.
120   scoped_ptr<BrightnessControlDelegate> brightness_control_delegate_;
121   scoped_ptr<ImeControlDelegate> ime_control_delegate_;
122   scoped_ptr<KeyboardBrightnessControlDelegate>
123       keyboard_brightness_control_delegate_;
124   scoped_ptr<ScreenshotDelegate> screenshot_delegate_;
125 
126   // Remember previous accelerator as some accelerator needs to be fired
127   // with a specific sequence.
128   ui::Accelerator previous_accelerator_;
129 
130   // Handles the exit accelerator which requires a double press to exit and
131   // shows a popup with an explanation.
132   ExitWarningHandler exit_warning_handler_;
133 
134   // A map from accelerators to the AcceleratorAction values, which are used in
135   // the implementation.
136   std::map<ui::Accelerator, int> accelerators_;
137 
138   // Actions allowed when the user is not signed in.
139   std::set<int> actions_allowed_at_login_screen_;
140   // Actions allowed when the screen is locked.
141   std::set<int> actions_allowed_at_lock_screen_;
142   // Actions allowed when a modal window is up.
143   std::set<int> actions_allowed_at_modal_window_;
144   // Reserved actions. See accelerator_table.h for details.
145   std::set<int> reserved_actions_;
146   // Actions which will not be repeated while holding the accelerator key.
147   std::set<int> nonrepeatable_actions_;
148   // Actions allowed in app mode.
149   std::set<int> actions_allowed_in_app_mode_;
150   // Actions disallowed if there are no windows.
151   std::set<int> actions_needing_window_;
152 
153   DISALLOW_COPY_AND_ASSIGN(AcceleratorController);
154 };
155 
156 }  // namespace ash
157 
158 #endif  // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
159