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