Home | History | Annotate | Download | only in extensions
      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 <list>
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "content/public/browser/notification_details.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/notification_source.h"
     17 
     18 class Profile;
     19 
     20 namespace ui {
     21 class Accelerator;
     22 }
     23 
     24 namespace extensions {
     25 
     26 class ActiveTabPermissionGranter;
     27 class Extension;
     28 
     29 // The ExtensionKeybindingRegistry is a class that handles the cross-platform
     30 // logic for keyboard accelerators. See platform-specific implementations for
     31 // implementation details for each platform.
     32 class ExtensionKeybindingRegistry : public content::NotificationObserver {
     33  public:
     34   enum ExtensionFilter {
     35     ALL_EXTENSIONS,
     36     PLATFORM_APPS_ONLY
     37   };
     38 
     39   class Delegate {
     40    public:
     41     // Gets the ActiveTabPermissionGranter for the active tab, if any.
     42     // If there is no active tab then returns NULL.
     43     virtual ActiveTabPermissionGranter* GetActiveTabPermissionGranter() = 0;
     44   };
     45 
     46   // If |extension_filter| is not ALL_EXTENSIONS, only keybindings by
     47   // by extensions that match the filter will be registered.
     48   ExtensionKeybindingRegistry(Profile* profile,
     49                               ExtensionFilter extension_filter,
     50                               Delegate* delegate);
     51 
     52   virtual ~ExtensionKeybindingRegistry();
     53 
     54   // Enables/Disables general shortcut handing in Chrome. Implemented in
     55   // platform-specific ExtensionKeybindingsRegistry* files.
     56   static void SetShortcutHandlingSuspended(bool suspended);
     57 
     58   // Overridden from content::NotificationObserver:
     59   virtual void Observe(int type,
     60                        const content::NotificationSource& source,
     61                        const content::NotificationDetails& details) OVERRIDE;
     62 
     63  protected:
     64   // Add extension keybinding for the events defined by the |extension|.
     65   // |command_name| is optional, but if not blank then only the command
     66   // specified will be added.
     67   virtual void AddExtensionKeybinding(
     68       const Extension* extension,
     69       const std::string& command_name) = 0;
     70   // Remove extension bindings for |extension|. |command_name| is optional,
     71   // but if not blank then only the command specified will be removed.
     72   void RemoveExtensionKeybinding(
     73       const Extension* extension,
     74       const std::string& command_name);
     75   // Overridden by platform specific implementations to provide additional
     76   // unregistration (which varies between platforms).
     77   virtual void RemoveExtensionKeybindingImpl(
     78       const ui::Accelerator& accelerator,
     79       const std::string& command_name) = 0;
     80 
     81   // Make sure all extensions registered have keybindings added.
     82   void Init();
     83 
     84   // Whether to ignore this command. Only browserAction commands and pageAction
     85   // commands are currently ignored, since they are handled elsewhere.
     86   bool ShouldIgnoreCommand(const std::string& command) const;
     87 
     88   // Fire event targets which the specified |accelerator| is binding with.
     89   // Returns true if we can find the appropriate event targets.
     90   bool NotifyEventTargets(const ui::Accelerator& accelerator);
     91 
     92   // Notifies appropriate parties that a command has been executed.
     93   void CommandExecuted(const std::string& extension_id,
     94                        const std::string& command);
     95 
     96   // Maps an accelerator to a list of string pairs (extension id, command name)
     97   // for commands that have been registered. This keeps track of the targets for
     98   // the keybinding event (which named command to call in which extension). On
     99   // GTK this map contains registration for pageAction and browserAction
    100   // commands, whereas on other platforms it does not. Note that normal
    101   // accelerator (which isn't media keys) has only one target, while the media
    102   // keys can have more than one.
    103   typedef std::list<std::pair<std::string, std::string> > TargetList;
    104   typedef std::map<ui::Accelerator, TargetList> EventTargets;
    105   EventTargets event_targets_;
    106 
    107  private:
    108   // Returns true if the |extension| matches our extension filter.
    109   bool ExtensionMatchesFilter(const extensions::Extension* extension);
    110 
    111   // The content notification registrar for listening to extension events.
    112   content::NotificationRegistrar registrar_;
    113 
    114   // Weak pointer to our profile. Not owned by us.
    115   Profile* profile_;
    116 
    117   // What extensions to register keybindings for.
    118   ExtensionFilter extension_filter_;
    119 
    120   // Weak pointer to our delegate. Not owned by us. Must outlive this class.
    121   Delegate* delegate_;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistry);
    124 };
    125 
    126 }  // namespace extensions
    127 
    128 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_
    129