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> |
| 8 |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
8 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
| 11 #include "base/strings/string_split.h" |
9 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/api/commands/commands.h" | 15 #include "chrome/browser/extensions/api/commands/commands.h" |
13 #include "chrome/browser/extensions/extension_commands_global_registry.h" | 16 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
14 #include "chrome/browser/extensions/extension_function_registry.h" | 17 #include "chrome/browser/extensions/extension_function_registry.h" |
15 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 18 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
18 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/accelerator_utils.h" | 22 #include "chrome/browser/ui/accelerator_utils.h" |
20 #include "chrome/common/extensions/api/commands/commands_handler.h" | 23 #include "chrome/common/extensions/api/commands/commands_handler.h" |
21 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
22 #include "components/user_prefs/pref_registry_syncable.h" | 25 #include "components/user_prefs/pref_registry_syncable.h" |
23 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
24 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
25 #include "extensions/common/feature_switch.h" | 28 #include "extensions/common/feature_switch.h" |
| 29 #include "extensions/common/manifest_constants.h" |
26 | 30 |
27 using extensions::Extension; | 31 using extensions::Extension; |
28 using extensions::ExtensionPrefs; | 32 using extensions::ExtensionPrefs; |
29 | 33 |
30 namespace { | 34 namespace { |
31 | 35 |
32 const char kExtension[] = "extension"; | 36 const char kExtension[] = "extension"; |
33 const char kCommandName[] = "command_name"; | 37 const char kCommandName[] = "command_name"; |
34 const char kGlobal[] = "global"; | 38 const char kGlobal[] = "global"; |
35 | 39 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // static | 107 // static |
104 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() { | 108 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() { |
105 return &g_factory.Get(); | 109 return &g_factory.Get(); |
106 } | 110 } |
107 | 111 |
108 // static | 112 // static |
109 CommandService* CommandService::Get(Profile* profile) { | 113 CommandService* CommandService::Get(Profile* profile) { |
110 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile); | 114 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile); |
111 } | 115 } |
112 | 116 |
| 117 // static |
| 118 bool CommandService::IsMediaKey(const ui::Accelerator& accelerator) { |
| 119 if (accelerator.modifiers() != 0) |
| 120 return false; |
| 121 |
| 122 return (accelerator.key_code() == ui::VKEY_MEDIA_NEXT_TRACK || |
| 123 accelerator.key_code() == ui::VKEY_MEDIA_PREV_TRACK || |
| 124 accelerator.key_code() == ui::VKEY_MEDIA_PLAY_PAUSE || |
| 125 accelerator.key_code() == ui::VKEY_MEDIA_STOP); |
| 126 } |
| 127 |
113 bool CommandService::GetBrowserActionCommand( | 128 bool CommandService::GetBrowserActionCommand( |
114 const std::string& extension_id, | 129 const std::string& extension_id, |
115 QueryType type, | 130 QueryType type, |
116 extensions::Command* command, | 131 extensions::Command* command, |
117 bool* active) { | 132 bool* active) { |
118 return GetExtensionActionCommand( | 133 return GetExtensionActionCommand( |
119 extension_id, type, command, active, BROWSER_ACTION); | 134 extension_id, type, command, active, BROWSER_ACTION); |
120 } | 135 } |
121 | 136 |
122 bool CommandService::GetPageActionCommand( | 137 bool CommandService::GetPageActionCommand( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 bool global) { | 202 bool global) { |
188 if (accelerator.key_code() == ui::VKEY_UNKNOWN) | 203 if (accelerator.key_code() == ui::VKEY_UNKNOWN) |
189 return false; | 204 return false; |
190 | 205 |
191 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 206 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
192 prefs::kExtensionCommands); | 207 prefs::kExtensionCommands); |
193 base::DictionaryValue* bindings = updater.Get(); | 208 base::DictionaryValue* bindings = updater.Get(); |
194 | 209 |
195 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); | 210 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); |
196 | 211 |
| 212 // Media keys have a 1-to-many relationship with targets, unlike regular |
| 213 // shortcut (1-to-1 relationship). That means two or more extensions can |
| 214 // register for the same media key so the extension ID needs to be added to |
| 215 // the key to make sure the key is unique. |
| 216 if (IsMediaKey(accelerator)) { |
| 217 // Media Keys are allowed using in named command only. |
| 218 DCHECK(command_name != manifest_values::kPageActionCommandEvent && |
| 219 command_name != manifest_values::kBrowserActionCommandEvent && |
| 220 command_name != manifest_values::kScriptBadgeCommandEvent); |
| 221 |
| 222 key += ":" + extension_id; |
| 223 } |
| 224 |
197 if (!allow_overrides && bindings->HasKey(key)) | 225 if (!allow_overrides && bindings->HasKey(key)) |
198 return false; // Already taken. | 226 return false; // Already taken. |
199 | 227 |
200 base::DictionaryValue* keybinding = new base::DictionaryValue(); | 228 base::DictionaryValue* keybinding = new base::DictionaryValue(); |
201 keybinding->SetString(kExtension, extension_id); | 229 keybinding->SetString(kExtension, extension_id); |
202 keybinding->SetString(kCommandName, command_name); | 230 keybinding->SetString(kCommandName, command_name); |
203 keybinding->SetBoolean(kGlobal, global); | 231 keybinding->SetBoolean(kGlobal, global); |
204 | 232 |
205 bindings->Set(key, keybinding); | 233 bindings->Set(key, keybinding); |
206 | 234 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 continue; | 308 continue; |
281 std::string command_name; | 309 std::string command_name; |
282 item->GetString(kCommandName, &command_name); | 310 item->GetString(kCommandName, &command_name); |
283 if (command != command_name) | 311 if (command != command_name) |
284 continue; | 312 continue; |
285 bool global = false; | 313 bool global = false; |
286 if (FeatureSwitch::global_commands()->IsEnabled()) | 314 if (FeatureSwitch::global_commands()->IsEnabled()) |
287 item->GetBoolean(kGlobal, &global); | 315 item->GetBoolean(kGlobal, &global); |
288 | 316 |
289 std::string shortcut = it.key(); | 317 std::string shortcut = it.key(); |
290 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) | 318 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) { |
291 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1); | 319 std::vector<std::string> tokens; |
| 320 base::SplitString(shortcut, ':', &tokens); |
| 321 CHECK(tokens.size() >= 2); |
| 322 shortcut = tokens[1]; // The second token is the shortcut assigned. |
| 323 } |
292 | 324 |
293 return Command(command_name, string16(), shortcut, global); | 325 return Command(command_name, string16(), shortcut, global); |
294 } | 326 } |
295 | 327 |
296 return Command(); | 328 return Command(); |
297 } | 329 } |
298 | 330 |
299 void CommandService::AssignInitialKeybindings(const Extension* extension) { | 331 void CommandService::AssignInitialKeybindings(const Extension* extension) { |
300 const extensions::CommandMap* commands = | 332 const extensions::CommandMap* commands = |
301 CommandsInfo::GetNamedCommands(extension); | 333 CommandsInfo::GetNamedCommands(extension); |
302 if (!commands) | 334 if (!commands) |
303 return; | 335 return; |
304 | 336 |
305 ExtensionService* extension_service = | 337 ExtensionService* extension_service = |
306 ExtensionSystem::Get(profile_)->extension_service(); | 338 ExtensionSystem::Get(profile_)->extension_service(); |
307 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); | 339 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); |
308 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) | 340 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) |
309 return; | 341 return; |
310 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); | 342 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
311 | 343 |
312 extensions::CommandMap::const_iterator iter = commands->begin(); | 344 extensions::CommandMap::const_iterator iter = commands->begin(); |
313 for (; iter != commands->end(); ++iter) { | 345 for (; iter != commands->end(); ++iter) { |
314 if (!chrome::IsChromeAccelerator( | 346 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && |
315 iter->second.accelerator(), profile_) && | 347 IsWhitelistedGlobalShortcut(iter->second)) || |
316 IsWhitelistedGlobalShortcut(iter->second)) { | 348 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { |
317 AddKeybindingPref(iter->second.accelerator(), | 349 AddKeybindingPref(iter->second.accelerator(), |
318 extension->id(), | 350 extension->id(), |
319 iter->second.command_name(), | 351 iter->second.command_name(), |
320 false, // Overwriting not allowed. | 352 false, // Overwriting not allowed. |
321 iter->second.global()); | 353 iter->second.global()); |
322 } | 354 } |
323 } | 355 } |
324 | 356 |
325 const extensions::Command* browser_action_command = | 357 const extensions::Command* browser_action_command = |
326 CommandsInfo::GetBrowserActionCommand(extension); | 358 CommandsInfo::GetBrowserActionCommand(extension); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 | 489 |
458 return true; | 490 return true; |
459 } | 491 } |
460 | 492 |
461 template <> | 493 template <> |
462 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 494 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
463 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 495 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
464 } | 496 } |
465 | 497 |
466 } // namespace extensions | 498 } // namespace extensions |
OLD | NEW |