| 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/extension_browser_event_router.h" | 7 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "ui/views/focus/focus_manager.h" | 11 #include "ui/views/focus/focus_manager.h" |
| 12 | 12 |
| 13 ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews( | 13 ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews( |
| 14 Profile* profile, views::FocusManager* focus_manager) | 14 Profile* profile, views::FocusManager* focus_manager) |
| 15 : ExtensionKeybindingRegistry(profile), | 15 : ExtensionKeybindingRegistry(profile), |
| 16 profile_(profile), | 16 profile_(profile), |
| 17 focus_manager_(focus_manager) { | 17 focus_manager_(focus_manager) { |
| 18 Init(); | 18 Init(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() { | 21 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() { |
| 22 EventTargets::const_iterator iter; | 22 EventTargets::const_iterator iter; |
| 23 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) | 23 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) |
| 24 focus_manager_->UnregisterAccelerator(iter->first, this); | 24 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding( | 27 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding( |
| 28 const Extension* extension) { | 28 const Extension* extension) { |
| 29 // Add all the keybindings (except pageAction and browserAction, which are | 29 // Add all the active keybindings (except page actions and browser actions, |
| 30 // handled elsewhere). | 30 // which are handled elsewhere). |
| 31 const Extension::CommandMap& commands = extension->named_commands(); | 31 const Extension::CommandMap& commands = |
| 32 GetActiveNamedCommands(profile_, extension->id()); |
| 32 Extension::CommandMap::const_iterator iter = commands.begin(); | 33 Extension::CommandMap::const_iterator iter = commands.begin(); |
| 33 for (; iter != commands.end(); ++iter) { | 34 for (; iter != commands.end(); ++iter) { |
| 34 event_targets_[iter->second.accelerator()] = | 35 event_targets_[iter->second.accelerator()] = |
| 35 std::make_pair(extension->id(), iter->second.command_name()); | 36 std::make_pair(extension->id(), iter->second.command_name()); |
| 36 focus_manager_->RegisterAccelerator( | 37 focus_manager_->RegisterAccelerator( |
| 37 iter->second.accelerator(), | 38 iter->second.accelerator(), |
| 38 ui::AcceleratorManager::kHighPriority, this); | 39 ui::AcceleratorManager::kHighPriority, this); |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 | 42 |
| 42 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybinding( | 43 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybinding( |
| 43 const Extension* extension) { | 44 const Extension* extension) { |
| 44 EventTargets::const_iterator iter; | 45 EventTargets::iterator iter = event_targets_.begin(); |
| 45 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { | 46 while (iter != event_targets_.end()) { |
| 46 if (iter->second.first != extension->id()) | 47 if (iter->second.first != extension->id()) { |
| 48 ++iter; |
| 47 continue; // Not the extension we asked for. | 49 continue; // Not the extension we asked for. |
| 50 } |
| 48 | 51 |
| 49 focus_manager_->UnregisterAccelerator(iter->first, this); | 52 focus_manager_->UnregisterAccelerator(iter->first, this); |
| 53 |
| 54 EventTargets::iterator old = iter++; |
| 55 event_targets_.erase(old); |
| 50 } | 56 } |
| 51 } | 57 } |
| 52 | 58 |
| 53 bool ExtensionKeybindingRegistryViews::AcceleratorPressed( | 59 bool ExtensionKeybindingRegistryViews::AcceleratorPressed( |
| 54 const ui::Accelerator& accelerator) { | 60 const ui::Accelerator& accelerator) { |
| 55 ExtensionService* service = profile_->GetExtensionService(); | 61 ExtensionService* service = profile_->GetExtensionService(); |
| 56 | 62 |
| 57 EventTargets::iterator it = event_targets_.find(accelerator); | 63 EventTargets::iterator it = event_targets_.find(accelerator); |
| 58 if (it == event_targets_.end()) { | 64 if (it == event_targets_.end()) { |
| 59 NOTREACHED(); // Shouldn't get this event for something not registered. | 65 NOTREACHED(); // Shouldn't get this event for something not registered. |
| 60 return false; | 66 return false; |
| 61 } | 67 } |
| 62 | 68 |
| 63 service->browser_event_router()->CommandExecuted( | 69 service->browser_event_router()->CommandExecuted( |
| 64 profile_, it->second.first, it->second.second); | 70 profile_, it->second.first, it->second.second); |
| 65 | 71 |
| 66 return true; | 72 return true; |
| 67 } | 73 } |
| 68 | 74 |
| 69 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const { | 75 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const { |
| 70 return true; | 76 return true; |
| 71 } | 77 } |
| OLD | NEW |