Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "chrome/common/extensions/extension.h" | |
| 12 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
| 13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 15 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
| 16 | 17 |
| 17 class Extension; | 18 class Extension; |
| 19 class PrefService; | |
| 18 class Profile; | 20 class Profile; |
| 19 | 21 |
| 22 namespace ui { | |
| 23 class Accelerator; | |
| 24 } | |
| 20 // The ExtensionKeybindingRegistry is a class that handles the cross-platform | 25 // The ExtensionKeybindingRegistry is a class that handles the cross-platform |
| 21 // logic for keyboard accelerators. See platform-specific implementations for | 26 // logic for keyboard accelerators. See platform-specific implementations for |
| 22 // implementation details for each platform. | 27 // implementation details for each platform. |
| 23 class ExtensionKeybindingRegistry : public content::NotificationObserver { | 28 class ExtensionKeybindingRegistry : public content::NotificationObserver { |
| 24 public: | 29 public: |
| 25 explicit ExtensionKeybindingRegistry(Profile* profile); | 30 explicit ExtensionKeybindingRegistry(Profile* profile); |
| 26 virtual ~ExtensionKeybindingRegistry(); | 31 virtual ~ExtensionKeybindingRegistry(); |
| 27 | 32 |
| 33 // Register prefs for keybinding. | |
| 34 static void RegisterUserPrefs(PrefService* user_prefs); | |
| 35 | |
| 36 // Gets the active keybinding (if any) for the browser action of an extension | |
| 37 // given its |extension_id|. The function consults the master list (in the | |
| 38 // |profile|) to see if the keybinding is active. Returns NULL if the | |
| 39 // extension has no browser action or no active keybinding for it. | |
| 40 static const Extension::ExtensionKeybinding* GetActiveBrowserActionCommand( | |
|
Aaron Boodman
2012/04/25 19:14:38
The names being used here are making this code mor
Finnur
2012/04/26 13:12:35
Yup. Sounds good.
On 2012/04/25 19:14:38, Aaron B
| |
| 41 Profile* profile, const std::string& extension_id); | |
| 42 | |
| 43 // Gets the active keybinding (if any) for the page action of an extension | |
| 44 // given its |extension_id|. The function consults the master list (in the | |
| 45 // |profile|) to see if the keybinding is active. Returns NULL if the | |
| 46 // extension has no page action or no active keybinding for it. | |
| 47 static const Extension::ExtensionKeybinding* GetActivePageActionCommand( | |
| 48 Profile* profile, const std::string& extension_id); | |
| 49 | |
| 50 // Gets the active keybinding (if any) for the named commands of an extension | |
| 51 // given its |extension_id|. The function consults the master list (in the | |
| 52 // |profile|) to see if the keybinding is active. Returns an empty map if the | |
| 53 // extension has no named commands or no active keybinding for the commands. | |
| 54 static Extension::CommandMap GetActiveNamedCommands( | |
| 55 Profile* profile, const std::string& extension_id); | |
| 56 | |
| 57 // Resolves any keybinding for a given |extension|'s page action, browser | |
| 58 // action and named commands. In each case, if the suggested keybinding is | |
| 59 // free, it will be taken by this extension. If not, that keybinding request | |
| 60 // is ignored. |user_pref| is the PrefService used to record the new | |
| 61 // keybinding assignment. | |
| 62 static void ResolveKeyBindings(PrefService* user_prefs, | |
|
Aaron Boodman
2012/04/25 19:14:38
You are using Keybinding (not KeyBinding) everywhe
Aaron Boodman
2012/04/25 19:14:38
AssignInitialKeybindings?
| |
| 63 const Extension* extension); | |
| 64 | |
| 65 // Checks to see if a keybinding |accelerator| for a given |command_name| in | |
| 66 // an extension with id |extension_id| is registered as active (by consulting | |
| 67 // the master list in |user_prefs|). | |
| 68 static bool KeybindingActive(PrefService* user_prefs, | |
|
Aaron Boodman
2012/04/25 19:14:38
Verbify: IsKeybindingActive
| |
| 69 const ui::Accelerator& accelerator, | |
| 70 std::string extension_id, | |
| 71 std::string command_name); | |
| 72 | |
| 73 // Records a keybinding |accelerator| as active for an extension with id | |
| 74 // |extension_id| and command with the name |command_name|. If | |
| 75 // |allow_overrides| is false, the keybinding must be free for the change to | |
| 76 // be recorded (as determined by the master list in |user_prefs|). If | |
| 77 // |allow_overwrites| is true, any previously recorded keybinding for this | |
| 78 // |accelerator| will be overwritten. Returns true if the change was | |
| 79 // successfully recorded. | |
| 80 static bool AddKeybindingPref(PrefService* user_prefs, | |
| 81 const ui::Accelerator& accelerator, | |
| 82 std::string extension_id, | |
| 83 std::string command_name, | |
| 84 bool allow_overrides); | |
| 85 | |
| 86 // Removes all keybindings for a given extension by its |extension_id|. | |
| 87 // |user_pref| is the PrefService used to record the keybinding deletion. | |
| 88 static void RemoveKeybindingPref(PrefService* user_prefs, | |
| 89 std::string extension_id); | |
| 90 | |
| 28 // Overridden from content::NotificationObserver: | 91 // Overridden from content::NotificationObserver: |
| 29 virtual void Observe(int type, | 92 virtual void Observe(int type, |
| 30 const content::NotificationSource& source, | 93 const content::NotificationSource& source, |
| 31 const content::NotificationDetails& details) OVERRIDE; | 94 const content::NotificationDetails& details) OVERRIDE; |
| 32 | 95 |
| 33 protected: | 96 protected: |
| 34 // Add extension keybinding for the events defined by the |extension|. | 97 // Add extension keybinding for the events defined by the |extension|. |
| 35 virtual void AddExtensionKeybinding(const Extension* extension) = 0; | 98 virtual void AddExtensionKeybinding(const Extension* extension) = 0; |
| 36 // Remove extension bindings for |extension|. | 99 // Remove extension bindings for |extension|. |
| 37 virtual void RemoveExtensionKeybinding(const Extension* extension) = 0; | 100 virtual void RemoveExtensionKeybinding(const Extension* extension) = 0; |
| 38 | 101 |
| 39 // Make sure all extensions registered have keybindings added. | 102 // Make sure all extensions registered have keybindings added. |
| 40 void Init(); | 103 void Init(); |
| 41 | 104 |
| 42 // Whether to ignore this command. Only browserAction commands and pageAction | 105 // Whether to ignore this command. Only browserAction commands and pageAction |
| 43 // commands are currently ignored, since they are handled elsewhere. | 106 // commands are currently ignored, since they are handled elsewhere. |
| 44 bool ShouldIgnoreCommand(const std::string& command) const; | 107 bool ShouldIgnoreCommand(const std::string& command) const; |
| 45 | 108 |
| 46 private: | 109 private: |
| 47 // The content notification registrar for listening to extension events. | 110 // The content notification registrar for listening to extension events. |
| 48 content::NotificationRegistrar registrar_; | 111 content::NotificationRegistrar registrar_; |
| 49 | 112 |
| 50 // Weak pointer to the our profile. Not owned by us. | 113 // Weak pointer to the our profile. Not owned by us. |
| 51 Profile* profile_; | 114 Profile* profile_; |
| 52 | 115 |
| 53 DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistry); | 116 DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistry); |
| 54 }; | 117 }; |
| 55 | 118 |
| 56 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ | 119 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ |
| OLD | NEW |