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 (IgnoreCommand(commands[i].event_name())) |
| 50 continue; |
| 51 |
| 52 event_targets_[commands[i].accelerator()] = |
| 53 std::make_pair(extension->id(), commands[i].event_name()); |
| 54 focus_manager_->RegisterAccelerator(commands[i].accelerator(), true, this); |
| 55 } |
| 56 } |
| 57 |
| 58 void ExtensionKeybindingRegistry::RemoveExtensionKeybinding( |
| 59 const Extension* extension) { |
| 60 EventTargets::const_iterator iter; |
| 61 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { |
| 62 if (iter->second.first != extension->id()) |
| 63 continue; // Not the extension we asked for. |
| 64 |
| 65 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 66 } |
| 67 } |
| 68 |
| 69 bool ExtensionKeybindingRegistry::IgnoreCommand( |
| 70 const std::string& command) const { |
| 71 return command == extension_manifest_values::kPageActionKeybindingEvent || |
| 72 command == extension_manifest_values::kBrowserActionKeybindingEvent; |
| 73 } |
| 74 |
| 75 bool ExtensionKeybindingRegistry::AcceleratorPressed( |
| 76 const ui::Accelerator& accelerator) { |
| 77 ExtensionService* service = profile_->GetExtensionService(); |
| 78 |
| 79 EventTargets::iterator it = event_targets_.find(accelerator); |
| 80 if (it == event_targets_.end()) { |
| 81 NOTREACHED(); // Shouldn't get this event for something not registered. |
| 82 return false; |
| 83 } |
| 84 |
| 85 service->browser_event_router()->CommandExecuted( |
| 86 profile_, it->second.first, std::string("toggleToggle")); |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool ExtensionKeybindingRegistry::CanHandleAccelerators() const { |
| 92 return true; |
| 93 } |
| 94 |
| 95 void ExtensionKeybindingRegistry::Observe( |
| 96 int type, |
| 97 const content::NotificationSource& source, |
| 98 const content::NotificationDetails& details) { |
| 99 switch (type) { |
| 100 case chrome::NOTIFICATION_EXTENSION_LOADED: |
| 101 AddExtensionKeybinding( |
| 102 content::Details<const Extension>(details).ptr()); |
| 103 break; |
| 104 case chrome::NOTIFICATION_EXTENSION_UNLOADED: |
| 105 RemoveExtensionKeybinding( |
| 106 content::Details<UnloadedExtensionInfo>(details)->extension); |
| 107 break; |
| 108 default: |
| 109 NOTREACHED(); |
| 110 break; |
| 111 } |
| 112 } |
OLD | NEW |