Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: chrome/browser/extensions/api/commands/command_service.cc

Issue 143493005: Allow extensions to remove and override the bookmark shortcut key (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unify override checking logic Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_keybinding_apitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 bool IsReservedChromeAccelerator(const ui::Accelerator& accelerator,
82 // Non-global shortcuts are always allowed. 85 const Extension* extension,
83 if (!command.global()) 86 Profile* profile) {
87 if (accelerator ==
88 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE)) {
89 using extensions::SettingsOverrides;
90 using extensions::FeatureSwitch;
91 const SettingsOverrides* settings_overrides =
92 SettingsOverrides::Get(extension);
93 return !(settings_overrides &&
94 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides) &&
95 (extensions::PermissionsData::HasAPIPermission(
96 extension,
97 extensions::APIPermission::kBookmarkManagerPrivate) ||
98 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()));
99 }
100
101 return chrome::IsChromeAccelerator(accelerator, profile);
102 }
103
104 // Checks if |extension| is permitted to override |accelerator|.
105 bool CanOverride(const Extension* extension,
106 const ui::Accelerator& accelerator,
Finnur 2014/02/12 22:54:48 nit: I started with CanOverride but changed it bec
Mike Wittman 2014/02/12 23:57:07 CanAutoAssign is fine with me, and I think your or
107 Profile* profile,
108 bool is_named_command,
109 bool is_global) {
110 // Media Keys are non-exclusive, so allow auto-assigning them.
111 if (extensions::CommandService::IsMediaKey(accelerator))
84 return true; 112 return true;
85 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9]. 113
114 if (is_global) {
115 if (!is_named_command)
116 return false; // Browser and page actions are not global in nature.
117
118 // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9].
86 #if defined OS_MACOSX 119 #if defined OS_MACOSX
87 if (!command.accelerator().IsCmdDown()) 120 if (!accelerator.IsCmdDown())
88 return false; 121 return false;
89 #else 122 #else
90 if (!command.accelerator().IsCtrlDown()) 123 if (!accelerator.IsCtrlDown())
91 return false; 124 return false;
92 #endif 125 #endif
93 if (!command.accelerator().IsShiftDown()) 126 if (!accelerator.IsShiftDown())
94 return false; 127 return false;
95 return (command.accelerator().key_code() >= ui::VKEY_0 && 128 return (accelerator.key_code() >= ui::VKEY_0 &&
96 command.accelerator().key_code() <= ui::VKEY_9); 129 accelerator.key_code() <= ui::VKEY_9);
130 } else {
131 // Not a global command, check if Chrome shortcut and whether
132 // we can override it.
133 if (accelerator ==
134 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE)) {
135 using extensions::SettingsOverrides;
136 using extensions::FeatureSwitch;
137 const SettingsOverrides* settings_overrides =
138 SettingsOverrides::Get(extension);
139 if (settings_overrides &&
140 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides) &&
141 (extensions::PermissionsData::HasAPIPermission(
142 extension,
143 extensions::APIPermission::kBookmarkManagerPrivate) ||
144 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled())) {
145 // If this check fails it either means we have an API to override a
146 // key that isn't a ChromeAccelerator (and the API can therefore be
147 // deprecated) or the IsChromeAccelerator isn't consistently
148 // returning true for all accelerators.
149 DCHECK(chrome::IsChromeAccelerator(accelerator, profile));
150 return true;
151 }
152 }
153
154 return !chrome::IsChromeAccelerator(accelerator, profile);
155 }
97 } 156 }
98 157
99 } // namespace 158 } // namespace
100 159
101 namespace extensions { 160 namespace extensions {
102 161
103 // static 162 // static
104 void CommandService::RegisterProfilePrefs( 163 void CommandService::RegisterProfilePrefs(
105 user_prefs::PrefRegistrySyncable* registry) { 164 user_prefs::PrefRegistrySyncable* registry) {
106 registry->RegisterDictionaryPref( 165 registry->RegisterDictionaryPref(
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 414
356 ExtensionService* extension_service = 415 ExtensionService* extension_service =
357 ExtensionSystem::Get(profile_)->extension_service(); 416 ExtensionSystem::Get(profile_)->extension_service();
358 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); 417 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
359 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) 418 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
360 return; 419 return;
361 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); 420 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
362 421
363 extensions::CommandMap::const_iterator iter = commands->begin(); 422 extensions::CommandMap::const_iterator iter = commands->begin();
364 for (; iter != commands->end(); ++iter) { 423 for (; iter != commands->end(); ++iter) {
365 // Make sure registered Chrome shortcuts cannot be automatically assigned 424 const extensions::Command command = iter->second;
Finnur 2014/02/12 22:54:48 Like this.
366 // (overwritten) by extension developers. Media keys are an exception here. 425 if (CanOverride(extension,
367 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && 426 command.accelerator(),
368 IsWhitelistedGlobalShortcut(iter->second)) || 427 profile_,
369 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { 428 true, // Is a named command.
370 AddKeybindingPref(iter->second.accelerator(), 429 command.global())) {
430 AddKeybindingPref(command.accelerator(),
371 extension->id(), 431 extension->id(),
372 iter->second.command_name(), 432 command.command_name(),
373 false, // Overwriting not allowed. 433 false, // Overwriting not allowed.
374 iter->second.global()); 434 command.global());
375 } 435 }
376 } 436 }
377 437
378 const extensions::Command* browser_action_command = 438 const extensions::Command* browser_action_command =
379 CommandsInfo::GetBrowserActionCommand(extension); 439 CommandsInfo::GetBrowserActionCommand(extension);
380 if (browser_action_command) { 440 if (browser_action_command &&
381 if (!chrome::IsChromeAccelerator( 441 CanOverride(extension,
382 browser_action_command->accelerator(), profile_)) { 442 browser_action_command->accelerator(),
383 AddKeybindingPref(browser_action_command->accelerator(), 443 profile_,
384 extension->id(), 444 false, // Not a named command.
385 browser_action_command->command_name(), 445 false)) { // Not global.
386 false, // Overwriting not allowed. 446 AddKeybindingPref(browser_action_command->accelerator(),
387 false); // Browser actions can't be global. 447 extension->id(),
388 } 448 browser_action_command->command_name(),
449 false, // Overwriting not allowed.
450 false); // Not global.
389 } 451 }
390 452
391 const extensions::Command* page_action_command = 453 const extensions::Command* page_action_command =
392 CommandsInfo::GetPageActionCommand(extension); 454 CommandsInfo::GetPageActionCommand(extension);
393 if (page_action_command) { 455 if (page_action_command &&
394 if (!chrome::IsChromeAccelerator( 456 CanOverride(extension,
395 page_action_command->accelerator(), profile_)) { 457 page_action_command->accelerator(),
396 AddKeybindingPref(page_action_command->accelerator(), 458 profile_,
397 extension->id(), 459 false, // Not a named command.
398 page_action_command->command_name(), 460 false)) { // Not global.
399 false, // Overwriting not allowed. 461 AddKeybindingPref(page_action_command->accelerator(),
400 false); // Page actions can't be global. 462 extension->id(),
401 } 463 page_action_command->command_name(),
464 false, // Overwriting not allowed.
465 false); // Not global.
402 } 466 }
403 } 467 }
404 468
405 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 469 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
406 const std::string& command_name) { 470 const std::string& command_name) {
407 DictionaryPrefUpdate updater(profile_->GetPrefs(), 471 DictionaryPrefUpdate updater(profile_->GetPrefs(),
408 prefs::kExtensionCommands); 472 prefs::kExtensionCommands);
409 base::DictionaryValue* bindings = updater.Get(); 473 base::DictionaryValue* bindings = updater.Get();
410 474
411 typedef std::vector<std::string> KeysToRemove; 475 typedef std::vector<std::string> KeysToRemove;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 562
499 return true; 563 return true;
500 } 564 }
501 565
502 template <> 566 template <>
503 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { 567 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
504 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); 568 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
505 } 569 }
506 570
507 } // namespace extensions 571 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_keybinding_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698