| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views
.h" | 5 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views
.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/commands/command_service.h" | 7 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 8 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 8 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
| 9 #include "chrome/browser/extensions/extension_browser_event_router.h" | 9 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 10 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 10 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 Init(); | 27 Init(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() { | 30 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() { |
| 31 EventTargets::const_iterator iter; | 31 EventTargets::const_iterator iter; |
| 32 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) | 32 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) |
| 33 focus_manager_->UnregisterAccelerator(iter->first, this); | 33 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding( | 36 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding( |
| 37 const extensions::Extension* extension) { | 37 const extensions::Extension* extension, |
| 38 const std::string& command_name) { |
| 39 // This object only handles named commands, not browser/page actions. |
| 40 if (ShouldIgnoreCommand(command_name)) |
| 41 return; |
| 42 |
| 38 extensions::CommandService* command_service = | 43 extensions::CommandService* command_service = |
| 39 extensions::CommandServiceFactory::GetForProfile(profile_); | 44 extensions::CommandServiceFactory::GetForProfile(profile_); |
| 40 // Add all the active keybindings (except page actions and browser actions, | 45 // Add all the active keybindings (except page actions and browser actions, |
| 41 // which are handled elsewhere). | 46 // which are handled elsewhere). |
| 42 extensions::CommandMap commands; | 47 extensions::CommandMap commands; |
| 43 if (!command_service->GetNamedCommands( | 48 if (!command_service->GetNamedCommands( |
| 44 extension->id(), extensions::CommandService::ACTIVE_ONLY, &commands)) | 49 extension->id(), extensions::CommandService::ACTIVE_ONLY, &commands)) |
| 45 return; | 50 return; |
| 46 extensions::CommandMap::const_iterator iter = commands.begin(); | 51 extensions::CommandMap::const_iterator iter = commands.begin(); |
| 47 for (; iter != commands.end(); ++iter) { | 52 for (; iter != commands.end(); ++iter) { |
| 53 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
| 54 continue; |
| 55 |
| 48 event_targets_[iter->second.accelerator()] = | 56 event_targets_[iter->second.accelerator()] = |
| 49 std::make_pair(extension->id(), iter->second.command_name()); | 57 std::make_pair(extension->id(), iter->second.command_name()); |
| 50 focus_manager_->RegisterAccelerator( | 58 focus_manager_->RegisterAccelerator( |
| 51 iter->second.accelerator(), | 59 iter->second.accelerator(), |
| 52 ui::AcceleratorManager::kHighPriority, this); | 60 ui::AcceleratorManager::kHighPriority, this); |
| 53 } | 61 } |
| 54 } | 62 } |
| 55 | 63 |
| 56 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybinding( | 64 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybinding( |
| 57 const extensions::Extension* extension) { | 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 |
| 58 EventTargets::iterator iter = event_targets_.begin(); | 71 EventTargets::iterator iter = event_targets_.begin(); |
| 59 while (iter != event_targets_.end()) { | 72 while (iter != event_targets_.end()) { |
| 60 if (iter->second.first != extension->id()) { | 73 if (iter->second.first != extension->id() || |
| 74 (!command_name.empty() && (iter->second.second != command_name))) { |
| 61 ++iter; | 75 ++iter; |
| 62 continue; // Not the extension we asked for. | 76 continue; // Not the extension or command we asked for. |
| 63 } | 77 } |
| 64 | 78 |
| 65 focus_manager_->UnregisterAccelerator(iter->first, this); | 79 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 66 | 80 |
| 67 EventTargets::iterator old = iter++; | 81 EventTargets::iterator old = iter++; |
| 68 event_targets_.erase(old); | 82 event_targets_.erase(old); |
| 69 } | 83 } |
| 70 } | 84 } |
| 71 | 85 |
| 72 bool ExtensionKeybindingRegistryViews::AcceleratorPressed( | 86 bool ExtensionKeybindingRegistryViews::AcceleratorPressed( |
| 73 const ui::Accelerator& accelerator) { | 87 const ui::Accelerator& accelerator) { |
| 74 ExtensionService* service = profile_->GetExtensionService(); | 88 ExtensionService* service = profile_->GetExtensionService(); |
| 75 | 89 |
| 76 EventTargets::iterator it = event_targets_.find(accelerator); | 90 EventTargets::iterator it = event_targets_.find(accelerator); |
| 77 if (it == event_targets_.end()) { | 91 if (it == event_targets_.end()) { |
| 78 NOTREACHED(); // Shouldn't get this event for something not registered. | 92 NOTREACHED(); // Shouldn't get this event for something not registered. |
| 79 return false; | 93 return false; |
| 80 } | 94 } |
| 81 | 95 |
| 82 service->browser_event_router()->CommandExecuted( | 96 service->browser_event_router()->CommandExecuted( |
| 83 profile_, it->second.first, it->second.second); | 97 profile_, it->second.first, it->second.second); |
| 84 | 98 |
| 85 return true; | 99 return true; |
| 86 } | 100 } |
| 87 | 101 |
| 88 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const { | 102 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const { |
| 89 return true; | 103 return true; |
| 90 } | 104 } |
| OLD | NEW |