| 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/api/commands/extension_command_service.h" | 5 #include "chrome/browser/extensions/api/commands/extension_command_service.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 8 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 11 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const char kExtension[] = "extension"; | 19 const char kExtension[] = "extension"; |
| 20 const char kCommandName[] = "command_name"; | 20 const char kCommandName[] = "command_name"; |
| 21 | 21 |
| 22 std::string GetPlatformKeybindingKeyForAccelerator( | 22 std::string GetPlatformKeybindingKeyForAccelerator( |
| 23 const ui::Accelerator& accelerator) { | 23 const ui::Accelerator& accelerator) { |
| 24 return Extension::Command::CommandPlatform() + ":" + | 24 return extensions::Command::CommandPlatform() + ":" + |
| 25 UTF16ToUTF8(accelerator.GetShortcutText()); | 25 UTF16ToUTF8(accelerator.GetShortcutText()); |
| 26 } | 26 } |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 // static | 30 // static |
| 31 void ExtensionCommandService::RegisterUserPrefs( | 31 void ExtensionCommandService::RegisterUserPrefs( |
| 32 PrefService* user_prefs) { | 32 PrefService* user_prefs) { |
| 33 user_prefs->RegisterDictionaryPref(prefs::kExtensionKeybindings, | 33 user_prefs->RegisterDictionaryPref(prefs::kExtensionKeybindings, |
| 34 PrefService::SYNCABLE_PREF); | 34 PrefService::SYNCABLE_PREF); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ExtensionCommandService::ExtensionCommandService( | 37 ExtensionCommandService::ExtensionCommandService( |
| 38 Profile* profile) | 38 Profile* profile) |
| 39 : profile_(profile) { | 39 : profile_(profile) { |
| 40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 41 content::Source<Profile>(profile)); | 41 content::Source<Profile>(profile)); |
| 42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 43 content::Source<Profile>(profile)); | 43 content::Source<Profile>(profile)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 ExtensionCommandService::~ExtensionCommandService() { | 46 ExtensionCommandService::~ExtensionCommandService() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 const Extension::Command* | 49 const extensions::Command* |
| 50 ExtensionCommandService::GetActiveBrowserActionCommand( | 50 ExtensionCommandService::GetActiveBrowserActionCommand( |
| 51 const std::string& extension_id) { | 51 const std::string& extension_id) { |
| 52 const ExtensionSet* extensions = | 52 const ExtensionSet* extensions = |
| 53 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 53 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 54 const Extension* extension = extensions->GetByID(extension_id); | 54 const Extension* extension = extensions->GetByID(extension_id); |
| 55 CHECK(extension); | 55 CHECK(extension); |
| 56 | 56 |
| 57 const Extension::Command* command = extension->browser_action_command(); | 57 const extensions::Command* command = extension->browser_action_command(); |
| 58 if (!command) | 58 if (!command) |
| 59 return NULL; | 59 return NULL; |
| 60 if (!IsKeybindingActive(command->accelerator(), | 60 if (!IsKeybindingActive(command->accelerator(), |
| 61 extension_id, | 61 extension_id, |
| 62 command->command_name())) { | 62 command->command_name())) { |
| 63 return NULL; | 63 return NULL; |
| 64 } | 64 } |
| 65 | 65 |
| 66 return command; | 66 return command; |
| 67 } | 67 } |
| 68 | 68 |
| 69 const Extension::Command* ExtensionCommandService::GetActivePageActionCommand( | 69 const extensions::Command* ExtensionCommandService::GetActivePageActionCommand( |
| 70 const std::string& extension_id) { | 70 const std::string& extension_id) { |
| 71 const ExtensionSet* extensions = | 71 const ExtensionSet* extensions = |
| 72 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 72 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 73 const Extension* extension = extensions->GetByID(extension_id); | 73 const Extension* extension = extensions->GetByID(extension_id); |
| 74 CHECK(extension); | 74 CHECK(extension); |
| 75 | 75 |
| 76 const Extension::Command* command = extension->page_action_command(); | 76 const extensions::Command* command = extension->page_action_command(); |
| 77 if (!command) | 77 if (!command) |
| 78 return NULL; | 78 return NULL; |
| 79 if (!IsKeybindingActive(command->accelerator(), | 79 if (!IsKeybindingActive(command->accelerator(), |
| 80 extension_id, | 80 extension_id, |
| 81 command->command_name())) { | 81 command->command_name())) { |
| 82 return NULL; | 82 return NULL; |
| 83 } | 83 } |
| 84 | 84 |
| 85 return command; | 85 return command; |
| 86 } | 86 } |
| 87 | 87 |
| 88 Extension::CommandMap ExtensionCommandService::GetActiveNamedCommands( | 88 extensions::CommandMap ExtensionCommandService::GetActiveNamedCommands( |
| 89 const std::string& extension_id) { | 89 const std::string& extension_id) { |
| 90 const ExtensionSet* extensions = | 90 const ExtensionSet* extensions = |
| 91 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 91 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 92 const Extension* extension = extensions->GetByID(extension_id); | 92 const Extension* extension = extensions->GetByID(extension_id); |
| 93 CHECK(extension); | 93 CHECK(extension); |
| 94 | 94 |
| 95 Extension::CommandMap result; | 95 extensions::CommandMap result; |
| 96 const Extension::CommandMap& commands = extension->named_commands(); | 96 const extensions::CommandMap& commands = extension->named_commands(); |
| 97 if (commands.empty()) | 97 if (commands.empty()) |
| 98 return result; | 98 return result; |
| 99 | 99 |
| 100 Extension::CommandMap::const_iterator iter = commands.begin(); | 100 extensions::CommandMap::const_iterator iter = commands.begin(); |
| 101 for (; iter != commands.end(); ++iter) { | 101 for (; iter != commands.end(); ++iter) { |
| 102 if (!IsKeybindingActive(iter->second.accelerator(), | 102 if (!IsKeybindingActive(iter->second.accelerator(), |
| 103 extension_id, | 103 extension_id, |
| 104 iter->second.command_name())) { | 104 iter->second.command_name())) { |
| 105 continue; | 105 continue; |
| 106 } | 106 } |
| 107 | 107 |
| 108 result[iter->second.command_name()] = iter->second; | 108 result[iter->second.command_name()] = iter->second; |
| 109 } | 109 } |
| 110 | 110 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 RemoveKeybindingPrefs(*content::Details<std::string>(details).ptr()); | 173 RemoveKeybindingPrefs(*content::Details<std::string>(details).ptr()); |
| 174 break; | 174 break; |
| 175 default: | 175 default: |
| 176 NOTREACHED(); | 176 NOTREACHED(); |
| 177 break; | 177 break; |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 void ExtensionCommandService::AssignInitialKeybindings( | 181 void ExtensionCommandService::AssignInitialKeybindings( |
| 182 const Extension* extension) { | 182 const Extension* extension) { |
| 183 const Extension::CommandMap& commands = extension->named_commands(); | 183 const extensions::CommandMap& commands = extension->named_commands(); |
| 184 Extension::CommandMap::const_iterator iter = commands.begin(); | 184 extensions::CommandMap::const_iterator iter = commands.begin(); |
| 185 for (; iter != commands.end(); ++iter) { | 185 for (; iter != commands.end(); ++iter) { |
| 186 AddKeybindingPref(iter->second.accelerator(), | 186 AddKeybindingPref(iter->second.accelerator(), |
| 187 extension->id(), | 187 extension->id(), |
| 188 iter->second.command_name(), | 188 iter->second.command_name(), |
| 189 false); // Overwriting not allowed. | 189 false); // Overwriting not allowed. |
| 190 } | 190 } |
| 191 | 191 |
| 192 const Extension::Command* browser_action_command = | 192 const extensions::Command* browser_action_command = |
| 193 extension->browser_action_command(); | 193 extension->browser_action_command(); |
| 194 if (browser_action_command) { | 194 if (browser_action_command) { |
| 195 AddKeybindingPref(browser_action_command->accelerator(), | 195 AddKeybindingPref(browser_action_command->accelerator(), |
| 196 extension->id(), | 196 extension->id(), |
| 197 browser_action_command->command_name(), | 197 browser_action_command->command_name(), |
| 198 false); // Overwriting not allowed. | 198 false); // Overwriting not allowed. |
| 199 } | 199 } |
| 200 | 200 |
| 201 const Extension::Command* page_action_command = | 201 const extensions::Command* page_action_command = |
| 202 extension->page_action_command(); | 202 extension->page_action_command(); |
| 203 if (page_action_command) { | 203 if (page_action_command) { |
| 204 AddKeybindingPref(page_action_command->accelerator(), | 204 AddKeybindingPref(page_action_command->accelerator(), |
| 205 extension->id(), | 205 extension->id(), |
| 206 page_action_command->command_name(), | 206 page_action_command->command_name(), |
| 207 false); // Overwriting not allowed. | 207 false); // Overwriting not allowed. |
| 208 } | 208 } |
| 209 } | 209 } |
| 210 | 210 |
| 211 void ExtensionCommandService::RemoveKeybindingPrefs(std::string extension_id) { | 211 void ExtensionCommandService::RemoveKeybindingPrefs(std::string extension_id) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 225 item->GetString(kExtension, &extension); | 225 item->GetString(kExtension, &extension); |
| 226 if (extension == extension_id) | 226 if (extension == extension_id) |
| 227 keys_to_remove.push_back(key); | 227 keys_to_remove.push_back(key); |
| 228 } | 228 } |
| 229 | 229 |
| 230 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); | 230 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); |
| 231 it != keys_to_remove.end(); ++it) { | 231 it != keys_to_remove.end(); ++it) { |
| 232 bindings->Remove(*it, NULL); | 232 bindings->Remove(*it, NULL); |
| 233 } | 233 } |
| 234 } | 234 } |
| OLD | NEW |