| 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/extensions/extension_keybinding_registry.h" | 5 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
| 10 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 10 #include "chrome/browser/extensions/event_router.h" | 11 #include "chrome/browser/extensions/event_router.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension_set.h" | 15 #include "chrome/common/extensions/extension_set.h" |
| 15 #include "extensions/common/manifest_constants.h" | 16 #include "extensions/common/manifest_constants.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry( | 20 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 30 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
| 31 content::Source<Profile>(profile->GetOriginalProfile())); | 32 content::Source<Profile>(profile->GetOriginalProfile())); |
| 32 } | 33 } |
| 33 | 34 |
| 34 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { | 35 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { |
| 35 } | 36 } |
| 36 | 37 |
| 37 void ExtensionKeybindingRegistry::RemoveExtensionKeybinding( | 38 void ExtensionKeybindingRegistry::RemoveExtensionKeybinding( |
| 38 const Extension* extension, | 39 const Extension* extension, |
| 39 const std::string& command_name) { | 40 const std::string& command_name) { |
| 40 EventTargets::iterator iter = event_targets_.begin(); | 41 EventTargets::iterator eit = event_targets_.begin(); |
| 41 while (iter != event_targets_.end()) { | 42 while (eit != event_targets_.end()) { |
| 42 if (iter->second.first != extension->id() || | 43 if (eit->second.first != extension->id() || |
| 43 (!command_name.empty() && (iter->second.second != command_name))) { | 44 (!command_name.empty() && (eit->second.second != command_name))) { |
| 44 ++iter; | 45 ++eit; |
| 45 continue; // Not the extension or command we asked for. | 46 continue; // Not the extension or command we asked for. |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Let each platform-specific implementation get a chance to clean up. | 49 // Let each platform-specific implementation get a chance to clean up. |
| 49 RemoveExtensionKeybindingImpl(iter->first, command_name); | 50 RemoveExtensionKeybindingImpl(eit->first, command_name); |
| 50 | 51 |
| 51 EventTargets::iterator old = iter++; | 52 EventTargets::iterator old = eit++; |
| 52 event_targets_.erase(old); | 53 event_targets_.erase(old); |
| 53 | 54 |
| 54 // If a specific command_name was requested, it has now been deleted so | 55 // If a specific command_name was requested, it has now been deleted so |
| 55 // no further work is required. | 56 // no further work is required. |
| 56 if (!command_name.empty()) | 57 if (!command_name.empty()) |
| 57 break; | 58 break; |
| 58 } | 59 } |
| 60 |
| 61 MediaKeysEventTargets::iterator mit = media_keys_event_targets_.begin(); |
| 62 while (mit != media_keys_event_targets_.end()) { |
| 63 TargetList& target_list = mit->second; |
| 64 TargetList::iterator target = target_list.begin(); |
| 65 while (target != target_list.end()) { |
| 66 if (target->first == extension->id() && |
| 67 (command_name.empty() || command_name == target->second)) { |
| 68 target = target_list.erase(target); |
| 69 } else { |
| 70 target++; |
| 71 } |
| 72 } |
| 73 |
| 74 MediaKeysEventTargets::iterator old = mit++; |
| 75 if (target_list.empty()) { |
| 76 RemoveExtensionKeybindingImpl(old->first, command_name); |
| 77 media_keys_event_targets_.erase(old); |
| 78 } |
| 79 } |
| 59 } | 80 } |
| 60 | 81 |
| 61 void ExtensionKeybindingRegistry::Init() { | 82 void ExtensionKeybindingRegistry::Init() { |
| 62 ExtensionService* service = | 83 ExtensionService* service = |
| 63 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 84 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 64 if (!service) | 85 if (!service) |
| 65 return; // ExtensionService can be null during testing. | 86 return; // ExtensionService can be null during testing. |
| 66 | 87 |
| 67 const ExtensionSet* extensions = service->extensions(); | 88 const ExtensionSet* extensions = service->extensions(); |
| 68 ExtensionSet::const_iterator iter = extensions->begin(); | 89 ExtensionSet::const_iterator iter = extensions->begin(); |
| 69 for (; iter != extensions->end(); ++iter) | 90 for (; iter != extensions->end(); ++iter) |
| 70 if (ExtensionMatchesFilter(iter->get())) | 91 if (ExtensionMatchesFilter(iter->get())) |
| 71 AddExtensionKeybinding(iter->get(), std::string()); | 92 AddExtensionKeybinding(iter->get(), std::string()); |
| 72 } | 93 } |
| 73 | 94 |
| 74 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( | 95 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( |
| 75 const std::string& command) const { | 96 const std::string& command) const { |
| 76 return command == manifest_values::kPageActionCommandEvent || | 97 return command == manifest_values::kPageActionCommandEvent || |
| 77 command == manifest_values::kBrowserActionCommandEvent || | 98 command == manifest_values::kBrowserActionCommandEvent || |
| 78 command == manifest_values::kScriptBadgeCommandEvent; | 99 command == manifest_values::kScriptBadgeCommandEvent; |
| 79 } | 100 } |
| 80 | 101 |
| 102 bool ExtensionKeybindingRegistry::NotifyEventTargetsByAccelerator( |
| 103 const ui::Accelerator& accelerator) { |
| 104 if (extensions::CommandService::IsMediaKey(accelerator)) { |
| 105 MediaKeysEventTargets::iterator targets = |
| 106 media_keys_event_targets_.find(accelerator); |
| 107 if (targets == media_keys_event_targets_.end() || targets->second.empty()) |
| 108 return false; |
| 109 |
| 110 for (TargetList::const_iterator it = targets->second.begin(); |
| 111 it != targets->second.end(); it++) |
| 112 CommandExecuted(it->first, it->second); |
| 113 } else { |
| 114 EventTargets::iterator it = event_targets_.find(accelerator); |
| 115 if (it == event_targets_.end()) |
| 116 return false; |
| 117 |
| 118 CommandExecuted(it->second.first, it->second.second); |
| 119 } |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 81 void ExtensionKeybindingRegistry::CommandExecuted( | 124 void ExtensionKeybindingRegistry::CommandExecuted( |
| 82 const std::string& extension_id, const std::string& command) { | 125 const std::string& extension_id, const std::string& command) { |
| 83 ExtensionService* service = | 126 ExtensionService* service = |
| 84 ExtensionSystem::Get(profile_)->extension_service(); | 127 ExtensionSystem::Get(profile_)->extension_service(); |
| 85 | 128 |
| 86 const Extension* extension = service->extensions()->GetByID(extension_id); | 129 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 87 if (!extension) | 130 if (!extension) |
| 88 return; | 131 return; |
| 89 | 132 |
| 90 // Grant before sending the event so that the permission is granted before | 133 // Grant before sending the event so that the permission is granted before |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 return true; | 204 return true; |
| 162 case PLATFORM_APPS_ONLY: | 205 case PLATFORM_APPS_ONLY: |
| 163 return extension->is_platform_app(); | 206 return extension->is_platform_app(); |
| 164 default: | 207 default: |
| 165 NOTREACHED(); | 208 NOTREACHED(); |
| 166 } | 209 } |
| 167 return false; | 210 return false; |
| 168 } | 211 } |
| 169 | 212 |
| 170 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |