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 CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ 7 8 #include <string> 9 10 #include "base/compiler_specific.h" 11 #include "content/public/browser/notification_details.h" 12 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_registrar.h" 14 #include "content/public/browser/notification_source.h" 15 16 class Profile; 17 18 namespace extensions { 19 20 class ActiveTabPermissionGranter; 21 class Extension; 22 23 // The ExtensionKeybindingRegistry is a class that handles the cross-platform 24 // logic for keyboard accelerators. See platform-specific implementations for 25 // implementation details for each platform. 26 class ExtensionKeybindingRegistry : public content::NotificationObserver { 27 public: 28 enum ExtensionFilter { 29 ALL_EXTENSIONS, 30 PLATFORM_APPS_ONLY 31 }; 32 33 class Delegate { 34 public: 35 // Gets the ActiveTabPermissionGranter for the active tab, if any. 36 // If there is no active tab then returns NULL. 37 virtual ActiveTabPermissionGranter* GetActiveTabPermissionGranter() = 0; 38 }; 39 40 // If |extension_filter| is not ALL_EXTENSIONS, only keybindings by 41 // by extensions that match the filter will be registered. 42 ExtensionKeybindingRegistry(Profile* profile, 43 ExtensionFilter extension_filter, 44 Delegate* delegate); 45 46 virtual ~ExtensionKeybindingRegistry(); 47 48 // Enables/Disables general shortcut handing in Chrome. Implemented in 49 // platform-specific ExtensionKeybindingsRegistry* files. 50 static void SetShortcutHandlingSuspended(bool suspended); 51 52 // Overridden from content::NotificationObserver: 53 virtual void Observe(int type, 54 const content::NotificationSource& source, 55 const content::NotificationDetails& details) OVERRIDE; 56 57 protected: 58 // Add extension keybinding for the events defined by the |extension|. 59 // |command_name| is optional, but if not blank then only the command 60 // specified will be added. 61 virtual void AddExtensionKeybinding( 62 const Extension* extension, 63 const std::string& command_name) = 0; 64 // Remove extension bindings for |extension|. |command_name| is optional, 65 // but if not blank then only the command specified will be removed. 66 virtual void RemoveExtensionKeybinding( 67 const Extension* extension, 68 const std::string& command_name) = 0; 69 70 // Make sure all extensions registered have keybindings added. 71 void Init(); 72 73 // Whether to ignore this command. Only browserAction commands and pageAction 74 // commands are currently ignored, since they are handled elsewhere. 75 bool ShouldIgnoreCommand(const std::string& command) const; 76 77 // Notifies appropriate parties that a command has been executed. 78 void CommandExecuted(const std::string& extension_id, 79 const std::string& command); 80 81 private: 82 // Returns true if the |extension| matches our extension filter. 83 bool ExtensionMatchesFilter(const extensions::Extension* extension); 84 85 // The content notification registrar for listening to extension events. 86 content::NotificationRegistrar registrar_; 87 88 // Weak pointer to our profile. Not owned by us. 89 Profile* profile_; 90 91 // What extensions to register keybindings for. 92 ExtensionFilter extension_filter_; 93 94 // Weak pointer to our delegate. Not owned by us. Must outlive this class. 95 Delegate* delegate_; 96 97 DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistry); 98 }; 99 100 } // namespace extensions 101 102 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_ 103