extension_commands_global_registry.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COMMANDS_GLOBAL_REGISTRY_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_COMMANDS_GLOBAL_REGISTRY_H_
7
8#include <map>
9#include <string>
10
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"
16
17namespace content {
18class BrowserContext;
19}
20
21namespace extensions {
22class Extension;
23
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.
31class ExtensionCommandsGlobalRegistry
32    : public BrowserContextKeyedAPI,
33      public ExtensionKeybindingRegistry,
34      public GlobalShortcutListener::Observer {
35 public:
36  // BrowserContextKeyedAPI implementation.
37  static BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>*
38      GetFactoryInstance();
39
40  // Convenience method to get the ExtensionCommandsGlobalRegistry for a
41  // profile.
42  static ExtensionCommandsGlobalRegistry* Get(content::BrowserContext* context);
43
44  explicit ExtensionCommandsGlobalRegistry(content::BrowserContext* context);
45  virtual ~ExtensionCommandsGlobalRegistry();
46
47 private:
48  friend class BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>;
49
50  // BrowserContextKeyedAPI implementation.
51  static const char* service_name() {
52    return "ExtensionCommandsGlobalRegistry";
53  }
54
55  // Overridden from ExtensionKeybindingRegistry:
56  virtual void AddExtensionKeybinding(
57      const Extension* extension,
58      const std::string& command_name) OVERRIDE;
59  virtual void RemoveExtensionKeybindingImpl(
60      const ui::Accelerator& accelerator,
61      const std::string& command_name) OVERRIDE;
62
63  // Called by the GlobalShortcutListener object when a shortcut this class has
64  // registered for has been pressed.
65  virtual void OnKeyPressed(const ui::Accelerator& accelerator) OVERRIDE;
66
67  // Weak pointer to our browser context. Not owned by us.
68  content::BrowserContext* browser_context_;
69
70  DISALLOW_COPY_AND_ASSIGN(ExtensionCommandsGlobalRegistry);
71};
72
73}  // namespace extensions
74
75#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_COMMANDS_GLOBAL_REGISTRY_H_
76