Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / extension_commands_global_registry.h
blobde6664190c1f1d602b9c858db4a188fa8112b11f
1 // Copyright (c) 2013 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_COMMANDS_GLOBAL_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_COMMANDS_GLOBAL_REGISTRY_H_
8 #include <map>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "chrome/browser/extensions/extension_keybinding_registry.h"
13 #include "chrome/browser/extensions/global_shortcut_listener.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "ui/base/accelerators/accelerator.h"
17 namespace content {
18 class BrowserContext;
21 namespace extensions {
22 class Extension;
24 // ExtensionCommandsGlobalRegistry is a class that handles the cross-platform
25 // implementation of the global shortcut registration for the Extension Commands
26 // API).
27 // Note: It handles regular extension commands (not browserAction and pageAction
28 // popups, which are not bindable to global shortcuts). This class registers the
29 // accelerators on behalf of the extensions and routes the commands to them via
30 // the BrowserEventRouter.
31 class ExtensionCommandsGlobalRegistry
32 : public BrowserContextKeyedAPI,
33 public ExtensionKeybindingRegistry,
34 public GlobalShortcutListener::Observer {
35 public:
36 // BrowserContextKeyedAPI implementation.
37 static BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>*
38 GetFactoryInstance();
40 // Convenience method to get the ExtensionCommandsGlobalRegistry for a
41 // profile.
42 static ExtensionCommandsGlobalRegistry* Get(content::BrowserContext* context);
44 explicit ExtensionCommandsGlobalRegistry(content::BrowserContext* context);
45 ~ExtensionCommandsGlobalRegistry() override;
47 // Returns which non-global command registry is active (belonging to the
48 // currently active window).
49 ExtensionKeybindingRegistry* registry_for_active_window() {
50 return registry_for_active_window_;
53 void set_registry_for_active_window(ExtensionKeybindingRegistry* registry) {
54 registry_for_active_window_ = registry;
57 // Returns whether |accelerator| is registered on the registry for the active
58 // window or on the global registry.
59 bool IsRegistered(const ui::Accelerator& accelerator);
61 private:
62 friend class BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>;
64 // BrowserContextKeyedAPI implementation.
65 static const char* service_name() {
66 return "ExtensionCommandsGlobalRegistry";
68 static const bool kServiceRedirectedInIncognito = true;
70 // Overridden from ExtensionKeybindingRegistry:
71 void AddExtensionKeybindings(const Extension* extension,
72 const std::string& command_name) override;
73 void RemoveExtensionKeybindingImpl(const ui::Accelerator& accelerator,
74 const std::string& command_name) override;
75 void OnShortcutHandlingSuspended(bool suspended) override;
77 // Called by the GlobalShortcutListener object when a shortcut this class has
78 // registered for has been pressed.
79 void OnKeyPressed(const ui::Accelerator& accelerator) override;
81 // Weak pointer to our browser context. Not owned by us.
82 content::BrowserContext* browser_context_;
84 // The global commands registry not only keeps track of global commands
85 // registered, but also of which non-global command registry is active
86 // (belonging to the currently active window). Only valid for TOOLKIT_VIEWS
87 // and
88 // NULL otherwise.
89 ExtensionKeybindingRegistry* registry_for_active_window_;
91 DISALLOW_COPY_AND_ASSIGN(ExtensionCommandsGlobalRegistry);
94 } // namespace extensions
96 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_COMMANDS_GLOBAL_REGISTRY_H_