OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/ui/views/extensions/extension_commands_global_registry_
views.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 10 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/extensions/extension.h" |
| 14 |
| 15 #if defined OS_WIN |
| 16 #include "chrome/browser/extensions/global_shortcut_listener_win.h" |
| 17 #else |
| 18 // TODO(?): Needs to be implemented on other platforms using Views. |
| 19 #endif |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 ExtensionCommandsGlobalRegistryViews::ExtensionCommandsGlobalRegistryViews( |
| 24 Profile* profile) |
| 25 : ExtensionKeybindingRegistry( |
| 26 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), |
| 27 profile_(profile) { |
| 28 Init(); |
| 29 } |
| 30 |
| 31 ExtensionCommandsGlobalRegistryViews::~ExtensionCommandsGlobalRegistryViews() { |
| 32 EventTargets::const_iterator iter; |
| 33 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { |
| 34 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
| 35 iter->first, this); |
| 36 } |
| 37 GlobalShortcutListener::GetInstance()->StopHooking(); |
| 38 } |
| 39 |
| 40 static base::LazyInstance< |
| 41 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistryViews> > |
| 42 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 43 |
| 44 // static |
| 45 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistryViews>* |
| 46 ExtensionCommandsGlobalRegistryViews::GetFactoryInstance() { |
| 47 return &g_factory.Get(); |
| 48 } |
| 49 |
| 50 template<> |
| 51 void ProfileKeyedAPIFactory< |
| 52 ExtensionCommandsGlobalRegistryViews>::DeclareFactoryDependencies() { |
| 53 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 54 } |
| 55 |
| 56 // static |
| 57 ExtensionCommandsGlobalRegistryViews* |
| 58 ExtensionCommandsGlobalRegistryViews::Get(Profile* profile) { |
| 59 return ProfileKeyedAPIFactory< |
| 60 ExtensionCommandsGlobalRegistryViews>::GetForProfile(profile); |
| 61 } |
| 62 |
| 63 |
| 64 void ExtensionCommandsGlobalRegistryViews::AddExtensionKeybinding( |
| 65 const extensions::Extension* extension, |
| 66 const std::string& command_name) { |
| 67 // This object only handles named commands, not browser/page actions. |
| 68 if (ShouldIgnoreCommand(command_name)) |
| 69 return; |
| 70 |
| 71 extensions::CommandService* command_service = |
| 72 extensions::CommandService::Get(profile_); |
| 73 // Add all the active keybindings (except page actions and browser actions, |
| 74 // which are handled elsewhere). |
| 75 extensions::CommandMap commands; |
| 76 if (!command_service->GetNamedCommands( |
| 77 extension->id(), |
| 78 extensions::CommandService::ACTIVE_ONLY, |
| 79 extensions::CommandService::GLOBAL, |
| 80 &commands)) |
| 81 return; |
| 82 |
| 83 size_t size_before = event_targets_.size(); |
| 84 extensions::CommandMap::const_iterator iter = commands.begin(); |
| 85 for (; iter != commands.end(); ++iter) { |
| 86 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
| 87 continue; |
| 88 |
| 89 LOG(ERROR) << "Adding global keybinding for " << extension->name().c_str() |
| 90 << " " << command_name.c_str() |
| 91 << " key: " << iter->second.accelerator().GetShortcutText(); |
| 92 |
| 93 event_targets_[iter->second.accelerator()] = |
| 94 std::make_pair(extension->id(), iter->second.command_name()); |
| 95 |
| 96 GlobalShortcutListener::GetInstance()->RegisterAccelerator( |
| 97 iter->second.accelerator(), this); |
| 98 } |
| 99 if (size_before == 0U && event_targets_.size() > 0U) |
| 100 GlobalShortcutListener::GetInstance()->StartHooking(); |
| 101 } |
| 102 |
| 103 void ExtensionCommandsGlobalRegistryViews::RemoveExtensionKeybinding( |
| 104 const extensions::Extension* extension, |
| 105 const std::string& command_name) { |
| 106 LOG(ERROR) << "Removing keybinding for " << extension->name().c_str() |
| 107 << " " << command_name.c_str(); |
| 108 |
| 109 // This object only handles named commands, not browser/page actions. |
| 110 if (ShouldIgnoreCommand(command_name)) |
| 111 return; |
| 112 |
| 113 size_t size_before = event_targets_.size(); |
| 114 EventTargets::iterator iter = event_targets_.begin(); |
| 115 while (iter != event_targets_.end()) { |
| 116 if (iter->second.first != extension->id() || |
| 117 (!command_name.empty() && (iter->second.second != command_name))) { |
| 118 ++iter; |
| 119 continue; // Not the extension or command we asked for. |
| 120 } |
| 121 |
| 122 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
| 123 iter->first, this); |
| 124 |
| 125 EventTargets::iterator old = iter++; |
| 126 event_targets_.erase(old); |
| 127 } |
| 128 |
| 129 if (size_before > 0U && event_targets_.size() == 0U) |
| 130 GlobalShortcutListener::GetInstance()->StopHooking(); |
| 131 } |
| 132 |
| 133 void ExtensionCommandsGlobalRegistryViews::OnKeyPressed( |
| 134 const ui::Accelerator& accelerator) { |
| 135 EventTargets::iterator it = event_targets_.find(accelerator); |
| 136 if (it == event_targets_.end()) { |
| 137 NOTREACHED(); // Shouldn't get this event for something not registered. |
| 138 return; |
| 139 } |
| 140 |
| 141 CommandExecuted(it->second.first, it->second.second); |
| 142 } |
| 143 |
| 144 } // namespace extensions |
OLD | NEW |