OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/extensions/extension_keybinding_registry.h" |
| 6 |
| 7 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 8 #include "chrome/browser/extensions/extension_event_router.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "ui/views/focus/focus_manager.h" |
| 15 |
| 16 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry( |
| 17 Profile* profile, views::FocusManager* focus_manager) |
| 18 : profile_(profile), |
| 19 focus_manager_(focus_manager) { |
| 20 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 21 content::Source<Profile>(profile->GetOriginalProfile())); |
| 22 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 23 content::Source<Profile>(profile->GetOriginalProfile())); |
| 24 |
| 25 ExtensionService* service = profile->GetExtensionService(); |
| 26 if (!service) |
| 27 return; // ExtensionService can be null during testing. |
| 28 |
| 29 const ExtensionSet* extensions = service->extensions(); |
| 30 |
| 31 ExtensionSet::const_iterator iter = extensions->begin(); |
| 32 for (; iter != extensions->end(); ++iter) |
| 33 AddExtensionKeybinding(*iter); |
| 34 } |
| 35 |
| 36 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { |
| 37 EventTargets::const_iterator iter; |
| 38 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) |
| 39 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 40 } |
| 41 |
| 42 void ExtensionKeybindingRegistry::AddExtensionKeybinding( |
| 43 const Extension* extension) { |
| 44 // Add all the keybindings (except pageAction and browserAction, which are |
| 45 // handled elsewhere). |
| 46 const std::vector<Extension::ExtensionKeybinding> commands = |
| 47 extension->keybindings(); |
| 48 for (size_t i = 0; i < commands.size(); ++i) { |
| 49 if (ShouldIgnoreCommand(commands[i].command_name())) |
| 50 continue; |
| 51 |
| 52 event_targets_[commands[i].accelerator()] = |
| 53 std::make_pair(extension->id(), commands[i].command_name()); |
| 54 focus_manager_->RegisterAccelerator( |
| 55 commands[i].accelerator(), ui::AcceleratorManager::kHighPriority, this); |
| 56 } |
| 57 } |
| 58 |
| 59 void ExtensionKeybindingRegistry::RemoveExtensionKeybinding( |
| 60 const Extension* extension) { |
| 61 EventTargets::const_iterator iter; |
| 62 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { |
| 63 if (iter->second.first != extension->id()) |
| 64 continue; // Not the extension we asked for. |
| 65 |
| 66 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 67 } |
| 68 } |
| 69 |
| 70 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( |
| 71 const std::string& command) const { |
| 72 return command == extension_manifest_values::kPageActionKeybindingEvent || |
| 73 command == extension_manifest_values::kBrowserActionKeybindingEvent; |
| 74 } |
| 75 |
| 76 bool ExtensionKeybindingRegistry::AcceleratorPressed( |
| 77 const ui::Accelerator& accelerator) { |
| 78 ExtensionService* service = profile_->GetExtensionService(); |
| 79 |
| 80 EventTargets::iterator it = event_targets_.find(accelerator); |
| 81 if (it == event_targets_.end()) { |
| 82 NOTREACHED(); // Shouldn't get this event for something not registered. |
| 83 return false; |
| 84 } |
| 85 |
| 86 service->browser_event_router()->CommandExecuted( |
| 87 profile_, it->second.first, it->second.second); |
| 88 |
| 89 return true; |
| 90 } |
| 91 |
| 92 bool ExtensionKeybindingRegistry::CanHandleAccelerators() const { |
| 93 return true; |
| 94 } |
| 95 |
| 96 void ExtensionKeybindingRegistry::Observe( |
| 97 int type, |
| 98 const content::NotificationSource& source, |
| 99 const content::NotificationDetails& details) { |
| 100 switch (type) { |
| 101 case chrome::NOTIFICATION_EXTENSION_LOADED: |
| 102 AddExtensionKeybinding( |
| 103 content::Details<const Extension>(details).ptr()); |
| 104 break; |
| 105 case chrome::NOTIFICATION_EXTENSION_UNLOADED: |
| 106 RemoveExtensionKeybinding( |
| 107 content::Details<UnloadedExtensionInfo>(details)->extension); |
| 108 break; |
| 109 default: |
| 110 NOTREACHED(); |
| 111 break; |
| 112 } |
| 113 } |
OLD | NEW |