Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / extension_commands_global_registry.cc
blobc98051c45e07ed38b3c0e47203582271a63bc6e5
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 #include "chrome/browser/extensions/extension_commands_global_registry.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/extensions/api/commands/command_service.h"
9 #include "chrome/browser/extensions/global_shortcut_listener.h"
10 #include "extensions/common/extension.h"
12 namespace extensions {
14 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry(
15 content::BrowserContext* context)
16 : ExtensionKeybindingRegistry(context,
17 ExtensionKeybindingRegistry::ALL_EXTENSIONS,
18 NULL),
19 browser_context_(context),
20 registry_for_active_window_(NULL) {
21 Init();
24 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() {
25 if (!IsEventTargetsEmpty()) {
26 GlobalShortcutListener* global_shortcut_listener =
27 GlobalShortcutListener::GetInstance();
29 // Resume GlobalShortcutListener before we clean up if the shortcut handling
30 // is currently suspended.
31 if (global_shortcut_listener->IsShortcutHandlingSuspended())
32 global_shortcut_listener->SetShortcutHandlingSuspended(false);
34 global_shortcut_listener->UnregisterAccelerators(this);
38 static base::LazyInstance<
39 BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > g_factory =
40 LAZY_INSTANCE_INITIALIZER;
42 // static
43 BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>*
44 ExtensionCommandsGlobalRegistry::GetFactoryInstance() {
45 return g_factory.Pointer();
48 // static
49 ExtensionCommandsGlobalRegistry* ExtensionCommandsGlobalRegistry::Get(
50 content::BrowserContext* context) {
51 return BrowserContextKeyedAPIFactory<ExtensionCommandsGlobalRegistry>::Get(
52 context);
55 bool ExtensionCommandsGlobalRegistry::IsRegistered(
56 const ui::Accelerator& accelerator) {
57 return (registry_for_active_window() &&
58 registry_for_active_window()->IsAcceleratorRegistered(accelerator)) ||
59 IsAcceleratorRegistered(accelerator);
62 void ExtensionCommandsGlobalRegistry::AddExtensionKeybindings(
63 const extensions::Extension* extension,
64 const std::string& command_name) {
65 // This object only handles named commands, not browser/page actions.
66 if (ShouldIgnoreCommand(command_name))
67 return;
69 extensions::CommandService* command_service =
70 extensions::CommandService::Get(browser_context_);
71 // Add all the active global keybindings, if any.
72 extensions::CommandMap commands;
73 if (!command_service->GetNamedCommands(
74 extension->id(),
75 extensions::CommandService::ACTIVE,
76 extensions::CommandService::GLOBAL,
77 &commands))
78 return;
80 extensions::CommandMap::const_iterator iter = commands.begin();
81 for (; iter != commands.end(); ++iter) {
82 if (!command_name.empty() && (iter->second.command_name() != command_name))
83 continue;
84 const ui::Accelerator& accelerator = iter->second.accelerator();
86 VLOG(0) << "Adding global keybinding for " << extension->name().c_str()
87 << " " << command_name.c_str()
88 << " key: " << accelerator.GetShortcutText();
90 if (!IsAcceleratorRegistered(accelerator)) {
91 if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(
92 accelerator, this))
93 continue;
96 AddEventTarget(accelerator, extension->id(), iter->second.command_name());
100 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl(
101 const ui::Accelerator& accelerator,
102 const std::string& command_name) {
103 VLOG(0) << "Removing keybinding for " << command_name.c_str();
105 GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
106 accelerator, this);
109 void ExtensionCommandsGlobalRegistry::OnShortcutHandlingSuspended(
110 bool suspended) {
111 GlobalShortcutListener::GetInstance()->SetShortcutHandlingSuspended(
112 suspended);
113 if (registry_for_active_window())
114 registry_for_active_window()->SetShortcutHandlingSuspended(suspended);
117 void ExtensionCommandsGlobalRegistry::OnKeyPressed(
118 const ui::Accelerator& accelerator) {
119 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
122 } // namespace extensions