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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_
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"
24 namespace extensions
{
26 class ActiveTabPermissionGranter
;
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
{
34 enum ExtensionFilter
{
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;
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
,
52 virtual ~ExtensionKeybindingRegistry();
54 // Enables/Disables general shortcut handing in Chrome. Implemented in
55 // platform-specific ExtensionKeybindingsRegistry* files.
56 static void SetShortcutHandlingSuspended(bool suspended
);
58 // Overridden from content::NotificationObserver:
59 virtual void Observe(int type
,
60 const content::NotificationSource
& source
,
61 const content::NotificationDetails
& details
) OVERRIDE
;
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;
81 // Make sure all extensions registered have keybindings added.
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;
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
);
92 // Notifies appropriate parties that a command has been executed.
93 void CommandExecuted(const std::string
& extension_id
,
94 const std::string
& command
);
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_
;
108 // Returns true if the |extension| matches our extension filter.
109 bool ExtensionMatchesFilter(const extensions::Extension
* extension
);
111 // The content notification registrar for listening to extension events.
112 content::NotificationRegistrar registrar_
;
114 // Weak pointer to our profile. Not owned by us.
117 // What extensions to register keybindings for.
118 ExtensionFilter extension_filter_
;
120 // Weak pointer to our delegate. Not owned by us. Must outlive this class.
123 DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistry
);
126 } // namespace extensions
128 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_H_