Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_commands_global_registry.cc
blob453e2057452cc00e67faa4134991a7a12bc6d0e7
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 "chrome/browser/profiles/profile.h"
11 #include "extensions/common/extension.h"
13 namespace extensions {
15 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry(
16 Profile* profile)
17 : ExtensionKeybindingRegistry(
18 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL),
19 profile_(profile) {
20 Init();
23 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() {
24 for (EventTargets::const_iterator iter = event_targets_.begin();
25 iter != event_targets_.end(); ++iter) {
26 GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
27 iter->first, this);
31 static base::LazyInstance<
32 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> >
33 g_factory = LAZY_INSTANCE_INITIALIZER;
35 // static
36 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>*
37 ExtensionCommandsGlobalRegistry::GetFactoryInstance() {
38 return &g_factory.Get();
41 // static
42 ExtensionCommandsGlobalRegistry*
43 ExtensionCommandsGlobalRegistry::Get(Profile* profile) {
44 return ProfileKeyedAPIFactory<
45 ExtensionCommandsGlobalRegistry>::GetForProfile(profile);
48 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding(
49 const extensions::Extension* extension,
50 const std::string& command_name) {
51 // This object only handles named commands, not browser/page actions.
52 if (ShouldIgnoreCommand(command_name))
53 return;
55 extensions::CommandService* command_service =
56 extensions::CommandService::Get(profile_);
57 // Add all the active global keybindings, if any.
58 extensions::CommandMap commands;
59 if (!command_service->GetNamedCommands(
60 extension->id(),
61 extensions::CommandService::ACTIVE_ONLY,
62 extensions::CommandService::GLOBAL,
63 &commands))
64 return;
66 extensions::CommandMap::const_iterator iter = commands.begin();
67 for (; iter != commands.end(); ++iter) {
68 if (!command_name.empty() && (iter->second.command_name() != command_name))
69 continue;
70 const ui::Accelerator& accelerator = iter->second.accelerator();
72 VLOG(0) << "Adding global keybinding for " << extension->name().c_str()
73 << " " << command_name.c_str()
74 << " key: " << accelerator.GetShortcutText();
76 if (event_targets_.find(accelerator) == event_targets_.end()) {
77 if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(
78 accelerator, this))
79 continue;
82 event_targets_[accelerator].push_back(
83 std::make_pair(extension->id(), iter->second.command_name()));
84 // Shortcuts except media keys have only one target in the list. See comment
85 // about |event_targets_|.
86 if (!extensions::CommandService::IsMediaKey(accelerator))
87 DCHECK_EQ(1u, event_targets_[accelerator].size());
91 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl(
92 const ui::Accelerator& accelerator,
93 const std::string& command_name) {
94 VLOG(0) << "Removing keybinding for " << command_name.c_str();
96 GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
97 accelerator, this);
100 void ExtensionCommandsGlobalRegistry::OnKeyPressed(
101 const ui::Accelerator& accelerator) {
102 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
105 } // namespace extensions