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/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/extensions/api/commands/commands.h" | 16 #include "chrome/browser/extensions/api/commands/commands.h" |
16 #include "chrome/browser/extensions/extension_commands_global_registry.h" | 17 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
17 #include "chrome/browser/extensions/extension_function_registry.h" | 18 #include "chrome/browser/extensions/extension_function_registry.h" |
18 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 19 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/accelerator_utils.h" | 22 #include "chrome/browser/ui/accelerator_utils.h" |
22 #include "chrome/common/extensions/api/commands/commands_handler.h" | 23 #include "chrome/common/extensions/api/commands/commands_handler.h" |
| 24 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.
h" |
23 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
24 #include "components/user_prefs/pref_registry_syncable.h" | 26 #include "components/user_prefs/pref_registry_syncable.h" |
25 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
27 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
28 #include "extensions/common/feature_switch.h" | 30 #include "extensions/common/feature_switch.h" |
29 #include "extensions/common/manifest_constants.h" | 31 #include "extensions/common/manifest_constants.h" |
| 32 #include "extensions/common/permissions/permissions_data.h" |
30 | 33 |
31 using extensions::Extension; | 34 using extensions::Extension; |
32 using extensions::ExtensionPrefs; | 35 using extensions::ExtensionPrefs; |
33 | 36 |
34 namespace { | 37 namespace { |
35 | 38 |
36 const char kExtension[] = "extension"; | 39 const char kExtension[] = "extension"; |
37 const char kCommandName[] = "command_name"; | 40 const char kCommandName[] = "command_name"; |
38 const char kGlobal[] = "global"; | 41 const char kGlobal[] = "global"; |
39 | 42 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 const ExtensionPrefs* prefs, const std::string& extension_id) { | 74 const ExtensionPrefs* prefs, const std::string& extension_id) { |
72 bool assigned = false; | 75 bool assigned = false; |
73 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, | 76 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, |
74 kInitialBindingsHaveBeenAssigned, | 77 kInitialBindingsHaveBeenAssigned, |
75 &assigned)) | 78 &assigned)) |
76 return false; | 79 return false; |
77 | 80 |
78 return assigned; | 81 return assigned; |
79 } | 82 } |
80 | 83 |
81 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) { | 84 // Checks if |extension| is permitted to automatically assign the |accelerator| |
82 // Non-global shortcuts are always allowed. | 85 // key. |
83 if (!command.global()) | 86 bool CanAutoAssign(const ui::Accelerator& accelerator, |
| 87 const Extension* extension, |
| 88 Profile* profile, |
| 89 bool is_named_command, |
| 90 bool is_global) { |
| 91 // Media Keys are non-exclusive, so allow auto-assigning them. |
| 92 if (extensions::CommandService::IsMediaKey(accelerator)) |
84 return true; | 93 return true; |
85 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9]. | 94 |
| 95 if (is_global) { |
| 96 if (!is_named_command) |
| 97 return false; // Browser and page actions are not global in nature. |
| 98 |
| 99 // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9]. |
86 #if defined OS_MACOSX | 100 #if defined OS_MACOSX |
87 if (!command.accelerator().IsCmdDown()) | 101 if (!accelerator.IsCmdDown()) |
88 return false; | 102 return false; |
89 #else | 103 #else |
90 if (!command.accelerator().IsCtrlDown()) | 104 if (!accelerator.IsCtrlDown()) |
91 return false; | 105 return false; |
92 #endif | 106 #endif |
93 if (!command.accelerator().IsShiftDown()) | 107 if (!accelerator.IsShiftDown()) |
94 return false; | 108 return false; |
95 return (command.accelerator().key_code() >= ui::VKEY_0 && | 109 return (accelerator.key_code() >= ui::VKEY_0 && |
96 command.accelerator().key_code() <= ui::VKEY_9); | 110 accelerator.key_code() <= ui::VKEY_9); |
| 111 } else { |
| 112 // Not a global command, check if Chrome shortcut and whether |
| 113 // we can override it. |
| 114 if (accelerator == |
| 115 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE)) { |
| 116 using extensions::SettingsOverrides; |
| 117 using extensions::FeatureSwitch; |
| 118 const SettingsOverrides* settings_overrides = |
| 119 SettingsOverrides::Get(extension); |
| 120 if (settings_overrides && |
| 121 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides) && |
| 122 (extensions::PermissionsData::HasAPIPermission( |
| 123 extension, |
| 124 extensions::APIPermission::kBookmarkManagerPrivate) || |
| 125 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled())) { |
| 126 // If this check fails it either means we have an API to override a |
| 127 // key that isn't a ChromeAccelerator (and the API can therefore be |
| 128 // deprecated) or the IsChromeAccelerator isn't consistently |
| 129 // returning true for all accelerators. |
| 130 DCHECK(chrome::IsChromeAccelerator(accelerator, profile)); |
| 131 return true; |
| 132 } |
| 133 } |
| 134 |
| 135 return !chrome::IsChromeAccelerator(accelerator, profile); |
| 136 } |
97 } | 137 } |
98 | 138 |
99 } // namespace | 139 } // namespace |
100 | 140 |
101 namespace extensions { | 141 namespace extensions { |
102 | 142 |
103 // static | 143 // static |
104 void CommandService::RegisterProfilePrefs( | 144 void CommandService::RegisterProfilePrefs( |
105 user_prefs::PrefRegistrySyncable* registry) { | 145 user_prefs::PrefRegistrySyncable* registry) { |
106 registry->RegisterDictionaryPref( | 146 registry->RegisterDictionaryPref( |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 395 |
356 ExtensionService* extension_service = | 396 ExtensionService* extension_service = |
357 ExtensionSystem::Get(profile_)->extension_service(); | 397 ExtensionSystem::Get(profile_)->extension_service(); |
358 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); | 398 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); |
359 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) | 399 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) |
360 return; | 400 return; |
361 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); | 401 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
362 | 402 |
363 extensions::CommandMap::const_iterator iter = commands->begin(); | 403 extensions::CommandMap::const_iterator iter = commands->begin(); |
364 for (; iter != commands->end(); ++iter) { | 404 for (; iter != commands->end(); ++iter) { |
365 // Make sure registered Chrome shortcuts cannot be automatically assigned | 405 const extensions::Command command = iter->second; |
366 // (overwritten) by extension developers. Media keys are an exception here. | 406 if (CanAutoAssign(command.accelerator(), |
367 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && | 407 extension, |
368 IsWhitelistedGlobalShortcut(iter->second)) || | 408 profile_, |
369 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { | 409 true, // Is a named command. |
370 AddKeybindingPref(iter->second.accelerator(), | 410 command.global())) { |
| 411 AddKeybindingPref(command.accelerator(), |
371 extension->id(), | 412 extension->id(), |
372 iter->second.command_name(), | 413 command.command_name(), |
373 false, // Overwriting not allowed. | 414 false, // Overwriting not allowed. |
374 iter->second.global()); | 415 command.global()); |
375 } | 416 } |
376 } | 417 } |
377 | 418 |
378 const extensions::Command* browser_action_command = | 419 const extensions::Command* browser_action_command = |
379 CommandsInfo::GetBrowserActionCommand(extension); | 420 CommandsInfo::GetBrowserActionCommand(extension); |
380 if (browser_action_command) { | 421 if (browser_action_command && |
381 if (!chrome::IsChromeAccelerator( | 422 CanAutoAssign(browser_action_command->accelerator(), |
382 browser_action_command->accelerator(), profile_)) { | 423 extension, |
383 AddKeybindingPref(browser_action_command->accelerator(), | 424 profile_, |
384 extension->id(), | 425 false, // Not a named command. |
385 browser_action_command->command_name(), | 426 false)) { // Not global. |
386 false, // Overwriting not allowed. | 427 AddKeybindingPref(browser_action_command->accelerator(), |
387 false); // Browser actions can't be global. | 428 extension->id(), |
388 } | 429 browser_action_command->command_name(), |
| 430 false, // Overwriting not allowed. |
| 431 false); // Not global. |
389 } | 432 } |
390 | 433 |
391 const extensions::Command* page_action_command = | 434 const extensions::Command* page_action_command = |
392 CommandsInfo::GetPageActionCommand(extension); | 435 CommandsInfo::GetPageActionCommand(extension); |
393 if (page_action_command) { | 436 if (page_action_command && |
394 if (!chrome::IsChromeAccelerator( | 437 CanAutoAssign(page_action_command->accelerator(), |
395 page_action_command->accelerator(), profile_)) { | 438 extension, |
396 AddKeybindingPref(page_action_command->accelerator(), | 439 profile_, |
397 extension->id(), | 440 false, // Not a named command. |
398 page_action_command->command_name(), | 441 false)) { // Not global. |
399 false, // Overwriting not allowed. | 442 AddKeybindingPref(page_action_command->accelerator(), |
400 false); // Page actions can't be global. | 443 extension->id(), |
401 } | 444 page_action_command->command_name(), |
| 445 false, // Overwriting not allowed. |
| 446 false); // Not global. |
402 } | 447 } |
403 } | 448 } |
404 | 449 |
405 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, | 450 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, |
406 const std::string& command_name) { | 451 const std::string& command_name) { |
407 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 452 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
408 prefs::kExtensionCommands); | 453 prefs::kExtensionCommands); |
409 base::DictionaryValue* bindings = updater.Get(); | 454 base::DictionaryValue* bindings = updater.Get(); |
410 | 455 |
411 typedef std::vector<std::string> KeysToRemove; | 456 typedef std::vector<std::string> KeysToRemove; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 | 543 |
499 return true; | 544 return true; |
500 } | 545 } |
501 | 546 |
502 template <> | 547 template <> |
503 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 548 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
504 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 549 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
505 } | 550 } |
506 | 551 |
507 } // namespace extensions | 552 } // namespace extensions |
OLD | NEW |