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 // Stores information about accelerator context, eg. previous accelerator 34 // or if the current accelerator is repeated or not. 35 class ASH_EXPORT AcceleratorControllerContext { 36 public: 37 AcceleratorControllerContext(); 38 ~AcceleratorControllerContext() {} 39 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); 43 44 const ui::Accelerator& previous_accelerator() const { 45 return previous_accelerator_; 46 } 47 bool repeated() const { 48 return current_accelerator_ == previous_accelerator_ && 49 current_accelerator_.type() != ui::ET_UNKNOWN; 50 } 51 52 private: 53 ui::Accelerator current_accelerator_; 54 // Used for NEXT_IME and DISABLE_CAPS_LOCK accelerator actions. 55 ui::Accelerator previous_accelerator_; 56 57 DISALLOW_COPY_AND_ASSIGN(AcceleratorControllerContext); 58 }; 59 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 { 64 public: 65 AcceleratorController(); 66 virtual ~AcceleratorController(); 67 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); 73 74 // Unregisters the specified keyboard accelerator for the specified target. 75 void Unregister(const ui::Accelerator& accelerator, 76 ui::AcceleratorTarget* target); 77 78 // Unregisters all keyboard accelerators for the specified target. 79 void UnregisterAll(ui::AcceleratorTarget* target); 80 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 85 // target, and so on. 86 // Returns true if an accelerator was activated. 87 bool Process(const ui::Accelerator& accelerator); 88 89 // Returns true if the |accelerator| is registered. 90 bool IsRegistered(const ui::Accelerator& accelerator) const; 91 92 // Returns true if the |accelerator| is one of the |reserved_actions_|. 93 bool IsReservedAccelerator(const ui::Accelerator& accelerator) const; 94 95 // Performs the specified action. The |accelerator| may provide additional 96 // data the action needs. Returns whether an action was performed 97 // successfully. 98 bool PerformAction(int action, 99 const ui::Accelerator& accelerator); 100 101 // Overridden from ui::AcceleratorTarget: 102 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 103 virtual bool CanHandleAccelerators() const OVERRIDE; 104 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(); 113 } 114 115 // Provides access to an object holding contextual information. 116 AcceleratorControllerContext* context() { 117 return &context_; 118 } 119 120 // Provides access to the ExitWarningHandler for testing. 121 ExitWarningHandler* GetExitWarningHandlerForTest() { 122 return &exit_warning_handler_; 123 } 124 125 private: 126 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest, GlobalAccelerators); 127 128 // Initializes the accelerators this class handles as a target. 129 void Init(); 130 131 // Registers the specified accelerators. 132 void RegisterAccelerators(const AcceleratorData accelerators[], 133 size_t accelerators_length); 134 135 void SetKeyboardBrightnessControlDelegate( 136 scoped_ptr<KeyboardBrightnessControlDelegate> 137 keyboard_brightness_control_delegate); 138 139 scoped_ptr<ui::AcceleratorManager> accelerator_manager_; 140 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_; 148 149 // Contextual information, eg. if the current accelerator is repeated. 150 AcceleratorControllerContext context_; 151 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_; 155 156 // A map from accelerators to the AcceleratorAction values, which are used in 157 // the implementation. 158 std::map<ui::Accelerator, int> accelerators_; 159 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_; 172 // Actions disallowed if there are no windows. 173 std::set<int> actions_needing_window_; 174 175 DISALLOW_COPY_AND_ASSIGN(AcceleratorController); 176 }; 177 178 } // namespace ash 179 180 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_ 181