| 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/command_service.h" | 5 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
| 27 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 28 #include "extensions/browser/extension_function_registry.h" | 28 #include "extensions/browser/extension_function_registry.h" |
| 29 #include "extensions/browser/extension_prefs.h" | 29 #include "extensions/browser/extension_prefs.h" |
| 30 #include "extensions/browser/extension_registry.h" | 30 #include "extensions/browser/extension_registry.h" |
| 31 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
| 32 #include "extensions/common/feature_switch.h" | 32 #include "extensions/common/feature_switch.h" |
| 33 #include "extensions/common/manifest_constants.h" | 33 #include "extensions/common/manifest_constants.h" |
| 34 #include "extensions/common/permissions/permissions_data.h" | 34 #include "extensions/common/permissions/permissions_data.h" |
| 35 | 35 |
| 36 using extensions::Extension; | 36 namespace extensions { |
| 37 using extensions::ExtensionPrefs; | |
| 38 using extensions::SettingsOverrides; | |
| 39 using extensions::UIOverrides; | |
| 40 | |
| 41 namespace { | 37 namespace { |
| 42 | 38 |
| 43 const char kExtension[] = "extension"; | 39 const char kExtension[] = "extension"; |
| 44 const char kCommandName[] = "command_name"; | 40 const char kCommandName[] = "command_name"; |
| 45 const char kGlobal[] = "global"; | 41 const char kGlobal[] = "global"; |
| 46 | 42 |
| 47 // A preference that indicates that the initial keybindings for the given | 43 // A preference that indicates that the initial keybindings for the given |
| 48 // extension have been set. | 44 // extension have been set. |
| 49 const char kInitialBindingsHaveBeenAssigned[] = "initial_keybindings_set"; | 45 const char kInitialBindingsHaveBeenAssigned[] = "initial_keybindings_set"; |
| 50 | 46 |
| 51 std::string GetPlatformKeybindingKeyForAccelerator( | 47 std::string GetPlatformKeybindingKeyForAccelerator( |
| 52 const ui::Accelerator& accelerator, const std::string extension_id) { | 48 const ui::Accelerator& accelerator, const std::string extension_id) { |
| 53 std::string key = extensions::Command::CommandPlatform() + ":" + | 49 std::string key = Command::CommandPlatform() + ":" + |
| 54 extensions::Command::AcceleratorToString(accelerator); | 50 Command::AcceleratorToString(accelerator); |
| 55 | 51 |
| 56 // Media keys have a 1-to-many relationship with targets, unlike regular | 52 // Media keys have a 1-to-many relationship with targets, unlike regular |
| 57 // shortcut (1-to-1 relationship). That means two or more extensions can | 53 // shortcut (1-to-1 relationship). That means two or more extensions can |
| 58 // register for the same media key so the extension ID needs to be added to | 54 // register for the same media key so the extension ID needs to be added to |
| 59 // the key to make sure the key is unique. | 55 // the key to make sure the key is unique. |
| 60 if (extensions::Command::IsMediaKey(accelerator)) | 56 if (Command::IsMediaKey(accelerator)) |
| 61 key += ":" + extension_id; | 57 key += ":" + extension_id; |
| 62 | 58 |
| 63 return key; | 59 return key; |
| 64 } | 60 } |
| 65 | 61 |
| 66 bool IsForCurrentPlatform(const std::string& key) { | 62 bool IsForCurrentPlatform(const std::string& key) { |
| 67 return StartsWithASCII( | 63 return StartsWithASCII(key, Command::CommandPlatform() + ":", true); |
| 68 key, extensions::Command::CommandPlatform() + ":", true); | |
| 69 } | 64 } |
| 70 | 65 |
| 71 void SetInitialBindingsHaveBeenAssigned( | 66 void SetInitialBindingsHaveBeenAssigned( |
| 72 ExtensionPrefs* prefs, const std::string& extension_id) { | 67 ExtensionPrefs* prefs, const std::string& extension_id) { |
| 73 prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned, | 68 prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned, |
| 74 new base::FundamentalValue(true)); | 69 new base::FundamentalValue(true)); |
| 75 } | 70 } |
| 76 | 71 |
| 77 bool InitialBindingsHaveBeenAssigned( | 72 bool InitialBindingsHaveBeenAssigned( |
| 78 const ExtensionPrefs* prefs, const std::string& extension_id) { | 73 const ExtensionPrefs* prefs, const std::string& extension_id) { |
| 79 bool assigned = false; | 74 bool assigned = false; |
| 80 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, | 75 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, |
| 81 kInitialBindingsHaveBeenAssigned, | 76 kInitialBindingsHaveBeenAssigned, |
| 82 &assigned)) | 77 &assigned)) |
| 83 return false; | 78 return false; |
| 84 | 79 |
| 85 return assigned; | 80 return assigned; |
| 86 } | 81 } |
| 87 | 82 |
| 88 // Checks if |extension| is permitted to automatically assign the |accelerator| | 83 // Checks if |extension| is permitted to automatically assign the |accelerator| |
| 89 // key. | 84 // key. |
| 90 bool CanAutoAssign(const ui::Accelerator& accelerator, | 85 bool CanAutoAssign(const ui::Accelerator& accelerator, |
| 91 const Extension* extension, | 86 const Extension* extension, |
| 92 Profile* profile, | 87 Profile* profile, |
| 93 bool is_named_command, | 88 bool is_named_command, |
| 94 bool is_global) { | 89 bool is_global) { |
| 95 // Media Keys are non-exclusive, so allow auto-assigning them. | 90 // Media Keys are non-exclusive, so allow auto-assigning them. |
| 96 if (extensions::Command::IsMediaKey(accelerator)) | 91 if (Command::IsMediaKey(accelerator)) |
| 97 return true; | 92 return true; |
| 98 | 93 |
| 99 if (is_global) { | 94 if (is_global) { |
| 100 if (!is_named_command) | 95 if (!is_named_command) |
| 101 return false; // Browser and page actions are not global in nature. | 96 return false; // Browser and page actions are not global in nature. |
| 102 | 97 |
| 103 // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9]. | 98 // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9]. |
| 104 #if defined OS_MACOSX | 99 #if defined OS_MACOSX |
| 105 if (!accelerator.IsCmdDown()) | 100 if (!accelerator.IsCmdDown()) |
| 106 return false; | 101 return false; |
| 107 #else | 102 #else |
| 108 if (!accelerator.IsCtrlDown()) | 103 if (!accelerator.IsCtrlDown()) |
| 109 return false; | 104 return false; |
| 110 #endif | 105 #endif |
| 111 if (!accelerator.IsShiftDown()) | 106 if (!accelerator.IsShiftDown()) |
| 112 return false; | 107 return false; |
| 113 return (accelerator.key_code() >= ui::VKEY_0 && | 108 return (accelerator.key_code() >= ui::VKEY_0 && |
| 114 accelerator.key_code() <= ui::VKEY_9); | 109 accelerator.key_code() <= ui::VKEY_9); |
| 115 } else { | 110 } else { |
| 116 // Not a global command, check if Chrome shortcut and whether | 111 // Not a global command, check if Chrome shortcut and whether |
| 117 // we can override it. | 112 // we can override it. |
| 118 if (accelerator == | 113 if (accelerator == chrome::GetPrimaryChromeAcceleratorForCommandId( |
| 119 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE) && | 114 IDC_BOOKMARK_PAGE) && |
| 120 extensions::CommandService::RemovesBookmarkShortcut(extension)) { | 115 CommandService::RemovesBookmarkShortcut(extension)) { |
| 121 // If this check fails it either means we have an API to override a | 116 // If this check fails it either means we have an API to override a |
| 122 // key that isn't a ChromeAccelerator (and the API can therefore be | 117 // key that isn't a ChromeAccelerator (and the API can therefore be |
| 123 // deprecated) or the IsChromeAccelerator isn't consistently | 118 // deprecated) or the IsChromeAccelerator isn't consistently |
| 124 // returning true for all accelerators. | 119 // returning true for all accelerators. |
| 125 DCHECK(chrome::IsChromeAccelerator(accelerator, profile)); | 120 DCHECK(chrome::IsChromeAccelerator(accelerator, profile)); |
| 126 return true; | 121 return true; |
| 127 } | 122 } |
| 128 | 123 |
| 129 return !chrome::IsChromeAccelerator(accelerator, profile); | 124 return !chrome::IsChromeAccelerator(accelerator, profile); |
| 130 } | 125 } |
| 131 } | 126 } |
| 132 | 127 |
| 133 } // namespace | 128 } // namespace |
| 134 | 129 |
| 135 namespace extensions { | |
| 136 | |
| 137 // static | 130 // static |
| 138 void CommandService::RegisterProfilePrefs( | 131 void CommandService::RegisterProfilePrefs( |
| 139 user_prefs::PrefRegistrySyncable* registry) { | 132 user_prefs::PrefRegistrySyncable* registry) { |
| 140 registry->RegisterDictionaryPref( | 133 registry->RegisterDictionaryPref( |
| 141 prefs::kExtensionCommands, | 134 prefs::kExtensionCommands, |
| 142 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 135 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 143 } | 136 } |
| 144 | 137 |
| 145 CommandService::CommandService(content::BrowserContext* context) | 138 CommandService::CommandService(content::BrowserContext* context) |
| 146 : profile_(Profile::FromBrowserContext(context)) { | 139 : profile_(Profile::FromBrowserContext(context)), |
| 140 extension_registry_observer_(this) { |
| 147 ExtensionFunctionRegistry::GetInstance()-> | 141 ExtensionFunctionRegistry::GetInstance()-> |
| 148 RegisterFunction<GetAllCommandsFunction>(); | 142 RegisterFunction<GetAllCommandsFunction>(); |
| 149 | 143 |
| 150 registrar_.Add(this, | 144 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 151 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | |
| 152 content::Source<Profile>(profile_)); | |
| 153 registrar_.Add(this, | |
| 154 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | |
| 155 content::Source<Profile>(profile_)); | |
| 156 } | 145 } |
| 157 | 146 |
| 158 CommandService::~CommandService() { | 147 CommandService::~CommandService() { |
| 159 } | 148 } |
| 160 | 149 |
| 161 static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService> > | 150 static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService> > |
| 162 g_factory = LAZY_INSTANCE_INITIALIZER; | 151 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 163 | 152 |
| 164 // static | 153 // static |
| 165 BrowserContextKeyedAPIFactory<CommandService>* | 154 BrowserContextKeyedAPIFactory<CommandService>* |
| 166 CommandService::GetFactoryInstance() { | 155 CommandService::GetFactoryInstance() { |
| 167 return g_factory.Pointer(); | 156 return g_factory.Pointer(); |
| 168 } | 157 } |
| 169 | 158 |
| 170 // static | 159 // static |
| 171 CommandService* CommandService::Get(content::BrowserContext* context) { | 160 CommandService* CommandService::Get(content::BrowserContext* context) { |
| 172 return BrowserContextKeyedAPIFactory<CommandService>::Get(context); | 161 return BrowserContextKeyedAPIFactory<CommandService>::Get(context); |
| 173 } | 162 } |
| 174 | 163 |
| 175 // static | 164 // static |
| 176 bool CommandService::RemovesBookmarkShortcut( | 165 bool CommandService::RemovesBookmarkShortcut(const Extension* extension) { |
| 177 const extensions::Extension* extension) { | |
| 178 const UIOverrides* ui_overrides = UIOverrides::Get(extension); | 166 const UIOverrides* ui_overrides = UIOverrides::Get(extension); |
| 179 const SettingsOverrides* settings_overrides = | 167 const SettingsOverrides* settings_overrides = |
| 180 SettingsOverrides::Get(extension); | 168 SettingsOverrides::Get(extension); |
| 181 | 169 |
| 182 return ((settings_overrides && | 170 return ((settings_overrides && |
| 183 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides)) || | 171 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides)) || |
| 184 (ui_overrides && | 172 (ui_overrides && |
| 185 UIOverrides::RemovesBookmarkShortcut(*ui_overrides))) && | 173 UIOverrides::RemovesBookmarkShortcut(*ui_overrides))) && |
| 186 (extensions::PermissionsData::HasAPIPermission( | 174 (PermissionsData::HasAPIPermission( |
| 187 extension, | 175 extension, APIPermission::kBookmarkManagerPrivate) || |
| 188 extensions::APIPermission::kBookmarkManagerPrivate) || | 176 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()); |
| 189 extensions::FeatureSwitch::enable_override_bookmarks_ui()-> | |
| 190 IsEnabled()); | |
| 191 } | 177 } |
| 192 | 178 |
| 193 // static | 179 // static |
| 194 bool CommandService::RemovesBookmarkOpenPagesShortcut( | 180 bool CommandService::RemovesBookmarkOpenPagesShortcut( |
| 195 const extensions::Extension* extension) { | 181 const Extension* extension) { |
| 196 const UIOverrides* ui_overrides = UIOverrides::Get(extension); | 182 const UIOverrides* ui_overrides = UIOverrides::Get(extension); |
| 197 const SettingsOverrides* settings_overrides = | 183 const SettingsOverrides* settings_overrides = |
| 198 SettingsOverrides::Get(extension); | 184 SettingsOverrides::Get(extension); |
| 199 | 185 |
| 200 return ((settings_overrides && | 186 return ((settings_overrides && |
| 201 SettingsOverrides::RemovesBookmarkOpenPagesShortcut( | 187 SettingsOverrides::RemovesBookmarkOpenPagesShortcut( |
| 202 *settings_overrides)) || | 188 *settings_overrides)) || |
| 203 (ui_overrides && | 189 (ui_overrides && |
| 204 UIOverrides::RemovesBookmarkOpenPagesShortcut(*ui_overrides))) && | 190 UIOverrides::RemovesBookmarkOpenPagesShortcut(*ui_overrides))) && |
| 205 (extensions::PermissionsData::HasAPIPermission( | 191 (PermissionsData::HasAPIPermission( |
| 206 extension, | 192 extension, APIPermission::kBookmarkManagerPrivate) || |
| 207 extensions::APIPermission::kBookmarkManagerPrivate) || | 193 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()); |
| 208 extensions::FeatureSwitch::enable_override_bookmarks_ui()-> | |
| 209 IsEnabled()); | |
| 210 } | 194 } |
| 211 | 195 |
| 212 bool CommandService::GetBrowserActionCommand(const std::string& extension_id, | 196 bool CommandService::GetBrowserActionCommand(const std::string& extension_id, |
| 213 QueryType type, | 197 QueryType type, |
| 214 extensions::Command* command, | 198 Command* command, |
| 215 bool* active) const { | 199 bool* active) const { |
| 216 return GetExtensionActionCommand( | 200 return GetExtensionActionCommand( |
| 217 extension_id, type, command, active, BROWSER_ACTION); | 201 extension_id, type, command, active, BROWSER_ACTION); |
| 218 } | 202 } |
| 219 | 203 |
| 220 bool CommandService::GetPageActionCommand(const std::string& extension_id, | 204 bool CommandService::GetPageActionCommand(const std::string& extension_id, |
| 221 QueryType type, | 205 QueryType type, |
| 222 extensions::Command* command, | 206 Command* command, |
| 223 bool* active) const { | 207 bool* active) const { |
| 224 return GetExtensionActionCommand( | 208 return GetExtensionActionCommand( |
| 225 extension_id, type, command, active, PAGE_ACTION); | 209 extension_id, type, command, active, PAGE_ACTION); |
| 226 } | 210 } |
| 227 | 211 |
| 228 bool CommandService::GetNamedCommands( | 212 bool CommandService::GetNamedCommands(const std::string& extension_id, |
| 229 const std::string& extension_id, | 213 QueryType type, |
| 230 QueryType type, | 214 CommandScope scope, |
| 231 CommandScope scope, | 215 CommandMap* command_map) const { |
| 232 extensions::CommandMap* command_map) const { | |
| 233 const ExtensionSet& extensions = | 216 const ExtensionSet& extensions = |
| 234 ExtensionRegistry::Get(profile_)->enabled_extensions(); | 217 ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| 235 const Extension* extension = extensions.GetByID(extension_id); | 218 const Extension* extension = extensions.GetByID(extension_id); |
| 236 CHECK(extension); | 219 CHECK(extension); |
| 237 | 220 |
| 238 command_map->clear(); | 221 command_map->clear(); |
| 239 const extensions::CommandMap* commands = | 222 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
| 240 CommandsInfo::GetNamedCommands(extension); | |
| 241 if (!commands) | 223 if (!commands) |
| 242 return false; | 224 return false; |
| 243 | 225 |
| 244 extensions::CommandMap::const_iterator iter = commands->begin(); | 226 CommandMap::const_iterator iter = commands->begin(); |
| 245 for (; iter != commands->end(); ++iter) { | 227 for (; iter != commands->end(); ++iter) { |
| 246 // Look up to see if the user has overridden how the command should work. | 228 // Look up to see if the user has overridden how the command should work. |
| 247 extensions::Command saved_command = | 229 Command saved_command = |
| 248 FindCommandByName(extension_id, iter->second.command_name()); | 230 FindCommandByName(extension_id, iter->second.command_name()); |
| 249 ui::Accelerator shortcut_assigned = saved_command.accelerator(); | 231 ui::Accelerator shortcut_assigned = saved_command.accelerator(); |
| 250 | 232 |
| 251 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) | 233 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
| 252 continue; | 234 continue; |
| 253 | 235 |
| 254 extensions::Command command = iter->second; | 236 Command command = iter->second; |
| 255 if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global())) | 237 if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global())) |
| 256 continue; | 238 continue; |
| 257 | 239 |
| 258 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 240 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
| 259 command.set_accelerator(shortcut_assigned); | 241 command.set_accelerator(shortcut_assigned); |
| 260 command.set_global(saved_command.global()); | 242 command.set_global(saved_command.global()); |
| 261 | 243 |
| 262 (*command_map)[iter->second.command_name()] = command; | 244 (*command_map)[iter->second.command_name()] = command; |
| 263 } | 245 } |
| 264 | 246 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 std::make_pair(extension_id, command_name); | 295 std::make_pair(extension_id, command_name); |
| 314 content::NotificationService::current()->Notify( | 296 content::NotificationService::current()->Notify( |
| 315 chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 297 chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
| 316 content::Source<Profile>(profile_), | 298 content::Source<Profile>(profile_), |
| 317 content::Details< | 299 content::Details< |
| 318 std::pair<const std::string, const std::string> >(&details)); | 300 std::pair<const std::string, const std::string> >(&details)); |
| 319 | 301 |
| 320 return true; | 302 return true; |
| 321 } | 303 } |
| 322 | 304 |
| 323 void CommandService::Observe( | 305 void CommandService::OnExtensionWillBeInstalled( |
| 324 int type, | 306 content::BrowserContext* browser_context, |
| 325 const content::NotificationSource& source, | 307 const Extension* extension, |
| 326 const content::NotificationDetails& details) { | 308 bool is_update, |
| 327 switch (type) { | 309 bool from_ephemeral, |
| 328 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: | 310 const std::string& old_name) { |
| 329 AssignInitialKeybindings( | 311 AssignInitialKeybindings(extension); |
| 330 content::Details<const InstalledExtensionInfo>(details)->extension); | 312 } |
| 331 break; | 313 |
| 332 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: | 314 void CommandService::OnExtensionUninstalled( |
| 333 RemoveKeybindingPrefs( | 315 content::BrowserContext* browser_context, |
| 334 content::Details<const Extension>(details)->id(), | 316 const Extension* extension) { |
| 335 std::string()); | 317 RemoveKeybindingPrefs(extension->id(), std::string()); |
| 336 break; | |
| 337 default: | |
| 338 NOTREACHED(); | |
| 339 break; | |
| 340 } | |
| 341 } | 318 } |
| 342 | 319 |
| 343 void CommandService::UpdateKeybindingPrefs(const std::string& extension_id, | 320 void CommandService::UpdateKeybindingPrefs(const std::string& extension_id, |
| 344 const std::string& command_name, | 321 const std::string& command_name, |
| 345 const std::string& keystroke) { | 322 const std::string& keystroke) { |
| 346 extensions::Command command = FindCommandByName(extension_id, command_name); | 323 Command command = FindCommandByName(extension_id, command_name); |
| 347 | 324 |
| 348 // The extension command might be assigned another shortcut. Remove that | 325 // The extension command might be assigned another shortcut. Remove that |
| 349 // shortcut before proceeding. | 326 // shortcut before proceeding. |
| 350 RemoveKeybindingPrefs(extension_id, command_name); | 327 RemoveKeybindingPrefs(extension_id, command_name); |
| 351 | 328 |
| 352 ui::Accelerator accelerator = | 329 ui::Accelerator accelerator = |
| 353 Command::StringToAccelerator(keystroke, command_name); | 330 Command::StringToAccelerator(keystroke, command_name); |
| 354 AddKeybindingPref(accelerator, extension_id, command_name, | 331 AddKeybindingPref(accelerator, extension_id, command_name, |
| 355 true, command.global()); | 332 true, command.global()); |
| 356 } | 333 } |
| 357 | 334 |
| 358 bool CommandService::SetScope(const std::string& extension_id, | 335 bool CommandService::SetScope(const std::string& extension_id, |
| 359 const std::string& command_name, | 336 const std::string& command_name, |
| 360 bool global) { | 337 bool global) { |
| 361 extensions::Command command = FindCommandByName(extension_id, command_name); | 338 Command command = FindCommandByName(extension_id, command_name); |
| 362 if (global == command.global()) | 339 if (global == command.global()) |
| 363 return false; | 340 return false; |
| 364 | 341 |
| 365 // Pre-existing shortcuts must be removed before proceeding because the | 342 // Pre-existing shortcuts must be removed before proceeding because the |
| 366 // handlers for global and non-global extensions are not one and the same. | 343 // handlers for global and non-global extensions are not one and the same. |
| 367 RemoveKeybindingPrefs(extension_id, command_name); | 344 RemoveKeybindingPrefs(extension_id, command_name); |
| 368 AddKeybindingPref(command.accelerator(), extension_id, | 345 AddKeybindingPref(command.accelerator(), extension_id, |
| 369 command_name, true, global); | 346 command_name, true, global); |
| 370 return true; | 347 return true; |
| 371 } | 348 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 402 | 379 |
| 403 return Command(command_name, base::string16(), shortcut, global); | 380 return Command(command_name, base::string16(), shortcut, global); |
| 404 } | 381 } |
| 405 | 382 |
| 406 return Command(); | 383 return Command(); |
| 407 } | 384 } |
| 408 | 385 |
| 409 bool CommandService::GetBoundExtensionCommand( | 386 bool CommandService::GetBoundExtensionCommand( |
| 410 const std::string& extension_id, | 387 const std::string& extension_id, |
| 411 const ui::Accelerator& accelerator, | 388 const ui::Accelerator& accelerator, |
| 412 extensions::Command* command, | 389 Command* command, |
| 413 ExtensionCommandType* command_type) const { | 390 ExtensionCommandType* command_type) const { |
| 414 const ExtensionSet& extensions = | 391 const Extension* extension = |
| 415 ExtensionRegistry::Get(profile_)->enabled_extensions(); | 392 ExtensionRegistry::Get(profile_) |
| 416 const Extension* extension = extensions.GetByID(extension_id); | 393 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 417 CHECK(extension); | 394 CHECK(extension); |
| 418 | 395 |
| 419 extensions::Command prospective_command; | 396 Command prospective_command; |
| 420 extensions::CommandMap command_map; | 397 CommandMap command_map; |
| 421 bool active = false; | 398 bool active = false; |
| 422 if (GetBrowserActionCommand(extension_id, | 399 if (GetBrowserActionCommand(extension_id, |
| 423 extensions::CommandService::ACTIVE_ONLY, | 400 CommandService::ACTIVE_ONLY, |
| 424 &prospective_command, | 401 &prospective_command, |
| 425 &active) && active && | 402 &active) && |
| 426 accelerator == prospective_command.accelerator()) { | 403 active && accelerator == prospective_command.accelerator()) { |
| 427 if (command) | 404 if (command) |
| 428 *command = prospective_command; | 405 *command = prospective_command; |
| 429 if (command_type) | 406 if (command_type) |
| 430 *command_type = BROWSER_ACTION; | 407 *command_type = BROWSER_ACTION; |
| 431 return true; | 408 return true; |
| 432 } else if (GetPageActionCommand(extension_id, | 409 } else if (GetPageActionCommand(extension_id, |
| 433 extensions::CommandService::ACTIVE_ONLY, | 410 CommandService::ACTIVE_ONLY, |
| 434 &prospective_command, | 411 &prospective_command, |
| 435 &active) && active && | 412 &active) && |
| 436 accelerator == prospective_command.accelerator()) { | 413 active && accelerator == prospective_command.accelerator()) { |
| 437 if (command) | 414 if (command) |
| 438 *command = prospective_command; | 415 *command = prospective_command; |
| 439 if (command_type) | 416 if (command_type) |
| 440 *command_type = PAGE_ACTION; | 417 *command_type = PAGE_ACTION; |
| 441 return true; | 418 return true; |
| 442 } else if (GetNamedCommands(extension_id, | 419 } else if (GetNamedCommands(extension_id, |
| 443 extensions::CommandService::ACTIVE_ONLY, | 420 CommandService::ACTIVE_ONLY, |
| 444 extensions::CommandService::REGULAR, | 421 CommandService::REGULAR, |
| 445 &command_map)) { | 422 &command_map)) { |
| 446 for (extensions::CommandMap::const_iterator it = command_map.begin(); | 423 for (CommandMap::const_iterator it = command_map.begin(); |
| 447 it != command_map.end(); ++it) { | 424 it != command_map.end(); |
| 425 ++it) { |
| 448 if (accelerator == it->second.accelerator()) { | 426 if (accelerator == it->second.accelerator()) { |
| 449 if (command) | 427 if (command) |
| 450 *command = it->second; | 428 *command = it->second; |
| 451 if (command_type) | 429 if (command_type) |
| 452 *command_type = NAMED; | 430 *command_type = NAMED; |
| 453 return true; | 431 return true; |
| 454 } | 432 } |
| 455 } | 433 } |
| 456 } | 434 } |
| 457 return false; | 435 return false; |
| 458 } | 436 } |
| 459 | 437 |
| 460 bool CommandService::OverridesBookmarkShortcut( | 438 bool CommandService::OverridesBookmarkShortcut( |
| 461 const extensions::Extension* extension) const { | 439 const Extension* extension) const { |
| 462 return RemovesBookmarkShortcut(extension) && | 440 return RemovesBookmarkShortcut(extension) && |
| 463 GetBoundExtensionCommand( | 441 GetBoundExtensionCommand( |
| 464 extension->id(), | 442 extension->id(), |
| 465 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE), | 443 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE), |
| 466 NULL, | 444 NULL, |
| 467 NULL); | 445 NULL); |
| 468 } | 446 } |
| 469 | 447 |
| 470 void CommandService::AssignInitialKeybindings(const Extension* extension) { | 448 void CommandService::AssignInitialKeybindings(const Extension* extension) { |
| 471 const extensions::CommandMap* commands = | 449 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
| 472 CommandsInfo::GetNamedCommands(extension); | |
| 473 if (!commands) | 450 if (!commands) |
| 474 return; | 451 return; |
| 475 | 452 |
| 476 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 453 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 477 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) | 454 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) |
| 478 return; | 455 return; |
| 479 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); | 456 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
| 480 | 457 |
| 481 extensions::CommandMap::const_iterator iter = commands->begin(); | 458 CommandMap::const_iterator iter = commands->begin(); |
| 482 for (; iter != commands->end(); ++iter) { | 459 for (; iter != commands->end(); ++iter) { |
| 483 const extensions::Command command = iter->second; | 460 const Command command = iter->second; |
| 484 if (CanAutoAssign(command.accelerator(), | 461 if (CanAutoAssign(command.accelerator(), |
| 485 extension, | 462 extension, |
| 486 profile_, | 463 profile_, |
| 487 true, // Is a named command. | 464 true, // Is a named command. |
| 488 command.global())) { | 465 command.global())) { |
| 489 AddKeybindingPref(command.accelerator(), | 466 AddKeybindingPref(command.accelerator(), |
| 490 extension->id(), | 467 extension->id(), |
| 491 command.command_name(), | 468 command.command_name(), |
| 492 false, // Overwriting not allowed. | 469 false, // Overwriting not allowed. |
| 493 command.global()); | 470 command.global()); |
| 494 } | 471 } |
| 495 } | 472 } |
| 496 | 473 |
| 497 const extensions::Command* browser_action_command = | 474 const Command* browser_action_command = |
| 498 CommandsInfo::GetBrowserActionCommand(extension); | 475 CommandsInfo::GetBrowserActionCommand(extension); |
| 499 if (browser_action_command && | 476 if (browser_action_command && |
| 500 CanAutoAssign(browser_action_command->accelerator(), | 477 CanAutoAssign(browser_action_command->accelerator(), |
| 501 extension, | 478 extension, |
| 502 profile_, | 479 profile_, |
| 503 false, // Not a named command. | 480 false, // Not a named command. |
| 504 false)) { // Not global. | 481 false)) { // Not global. |
| 505 AddKeybindingPref(browser_action_command->accelerator(), | 482 AddKeybindingPref(browser_action_command->accelerator(), |
| 506 extension->id(), | 483 extension->id(), |
| 507 browser_action_command->command_name(), | 484 browser_action_command->command_name(), |
| 508 false, // Overwriting not allowed. | 485 false, // Overwriting not allowed. |
| 509 false); // Not global. | 486 false); // Not global. |
| 510 } | 487 } |
| 511 | 488 |
| 512 const extensions::Command* page_action_command = | 489 const Command* page_action_command = |
| 513 CommandsInfo::GetPageActionCommand(extension); | 490 CommandsInfo::GetPageActionCommand(extension); |
| 514 if (page_action_command && | 491 if (page_action_command && |
| 515 CanAutoAssign(page_action_command->accelerator(), | 492 CanAutoAssign(page_action_command->accelerator(), |
| 516 extension, | 493 extension, |
| 517 profile_, | 494 profile_, |
| 518 false, // Not a named command. | 495 false, // Not a named command. |
| 519 false)) { // Not global. | 496 false)) { // Not global. |
| 520 AddKeybindingPref(page_action_command->accelerator(), | 497 AddKeybindingPref(page_action_command->accelerator(), |
| 521 extension->id(), | 498 extension->id(), |
| 522 page_action_command->command_name(), | 499 page_action_command->command_name(), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 547 chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
| 571 content::Source<Profile>(profile_), | 548 content::Source<Profile>(profile_), |
| 572 content::Details< | 549 content::Details< |
| 573 std::pair<const std::string, const std::string> >(&details)); | 550 std::pair<const std::string, const std::string> >(&details)); |
| 574 } | 551 } |
| 575 } | 552 } |
| 576 | 553 |
| 577 bool CommandService::GetExtensionActionCommand( | 554 bool CommandService::GetExtensionActionCommand( |
| 578 const std::string& extension_id, | 555 const std::string& extension_id, |
| 579 QueryType query_type, | 556 QueryType query_type, |
| 580 extensions::Command* command, | 557 Command* command, |
| 581 bool* active, | 558 bool* active, |
| 582 ExtensionCommandType action_type) const { | 559 ExtensionCommandType action_type) const { |
| 583 const ExtensionSet& extensions = | 560 const ExtensionSet& extensions = |
| 584 ExtensionRegistry::Get(profile_)->enabled_extensions(); | 561 ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| 585 const Extension* extension = extensions.GetByID(extension_id); | 562 const Extension* extension = extensions.GetByID(extension_id); |
| 586 CHECK(extension); | 563 CHECK(extension); |
| 587 | 564 |
| 588 if (active) | 565 if (active) |
| 589 *active = false; | 566 *active = false; |
| 590 | 567 |
| 591 const extensions::Command* requested_command = NULL; | 568 const Command* requested_command = NULL; |
| 592 switch (action_type) { | 569 switch (action_type) { |
| 593 case BROWSER_ACTION: | 570 case BROWSER_ACTION: |
| 594 requested_command = CommandsInfo::GetBrowserActionCommand(extension); | 571 requested_command = CommandsInfo::GetBrowserActionCommand(extension); |
| 595 break; | 572 break; |
| 596 case PAGE_ACTION: | 573 case PAGE_ACTION: |
| 597 requested_command = CommandsInfo::GetPageActionCommand(extension); | 574 requested_command = CommandsInfo::GetPageActionCommand(extension); |
| 598 break; | 575 break; |
| 599 case NAMED: | 576 case NAMED: |
| 600 NOTREACHED(); | 577 NOTREACHED(); |
| 601 return false; | 578 return false; |
| 602 } | 579 } |
| 603 if (!requested_command) | 580 if (!requested_command) |
| 604 return false; | 581 return false; |
| 605 | 582 |
| 606 // Look up to see if the user has overridden how the command should work. | 583 // Look up to see if the user has overridden how the command should work. |
| 607 extensions::Command saved_command = | 584 Command saved_command = |
| 608 FindCommandByName(extension_id, requested_command->command_name()); | 585 FindCommandByName(extension_id, requested_command->command_name()); |
| 609 ui::Accelerator shortcut_assigned = saved_command.accelerator(); | 586 ui::Accelerator shortcut_assigned = saved_command.accelerator(); |
| 610 | 587 |
| 611 if (active) | 588 if (active) |
| 612 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); | 589 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); |
| 613 | 590 |
| 614 if (query_type == ACTIVE_ONLY && | 591 if (query_type == ACTIVE_ONLY && |
| 615 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) | 592 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
| 616 return false; | 593 return false; |
| 617 | 594 |
| 618 *command = *requested_command; | 595 *command = *requested_command; |
| 619 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 596 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
| 620 command->set_accelerator(shortcut_assigned); | 597 command->set_accelerator(shortcut_assigned); |
| 621 | 598 |
| 622 return true; | 599 return true; |
| 623 } | 600 } |
| 624 | 601 |
| 625 template <> | 602 template <> |
| 626 void | 603 void |
| 627 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 604 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
| 628 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 605 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
| 629 } | 606 } |
| 630 | 607 |
| 631 } // namespace extensions | 608 } // namespace extensions |
| OLD | NEW |