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

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: address comments 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
84 bool IsReservedChromeAccelerator(const ui::Accelerator& accelerator,
85 const Extension* extension,
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()));
Finnur 2014/02/12 12:51:14 It is hard to wrap one's head around verifying the
Mike Wittman 2014/02/12 19:55:58 Agreed.
99 }
100
101 return chrome::IsChromeAccelerator(accelerator, profile);
102 }
103
81 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) { 104 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) {
105 // NOTE(wittman): If the restrictions on auto-assigning global shortcuts are
106 // ever relaxed in the future, then keys such as the bookmarking shortcut
107 // (IDC_BOOKMARK_PAGE) need to be evaluated on a case-by-case basis. Such keys
108 // can be auto-assigned by extensions (with the right permission), even though
109 // the shortcut is already in use by Chrome.
110
82 // Non-global shortcuts are always allowed. 111 // Non-global shortcuts are always allowed.
83 if (!command.global()) 112 if (!command.global())
84 return true; 113 return true;
85 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9]. 114 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9].
86 #if defined OS_MACOSX 115 #if defined OS_MACOSX
87 if (!command.accelerator().IsCmdDown()) 116 if (!command.accelerator().IsCmdDown())
88 return false; 117 return false;
89 #else 118 #else
90 if (!command.accelerator().IsCtrlDown()) 119 if (!command.accelerator().IsCtrlDown())
91 return false; 120 return false;
92 #endif 121 #endif
93 if (!command.accelerator().IsShiftDown()) 122 if (!command.accelerator().IsShiftDown())
94 return false; 123 return false;
95 return (command.accelerator().key_code() >= ui::VKEY_0 && 124 return (command.accelerator().key_code() >= ui::VKEY_0 &&
96 command.accelerator().key_code() <= ui::VKEY_9); 125 command.accelerator().key_code() <= ui::VKEY_9);
97 } 126 }
Finnur 2014/02/12 12:51:14 I just realized (coming in this morning with a cle
Mike Wittman 2014/02/12 19:55:58 SGTM. This is clearer, and I believe implements th
98 127
99 } // namespace 128 } // namespace
100 129
101 namespace extensions { 130 namespace extensions {
102 131
103 // static 132 // static
104 void CommandService::RegisterProfilePrefs( 133 void CommandService::RegisterProfilePrefs(
105 user_prefs::PrefRegistrySyncable* registry) { 134 user_prefs::PrefRegistrySyncable* registry) {
106 registry->RegisterDictionaryPref( 135 registry->RegisterDictionaryPref(
107 prefs::kExtensionCommands, 136 prefs::kExtensionCommands,
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 ExtensionSystem::Get(profile_)->extension_service(); 386 ExtensionSystem::Get(profile_)->extension_service();
358 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); 387 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
359 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) 388 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
360 return; 389 return;
361 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); 390 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
362 391
363 extensions::CommandMap::const_iterator iter = commands->begin(); 392 extensions::CommandMap::const_iterator iter = commands->begin();
364 for (; iter != commands->end(); ++iter) { 393 for (; iter != commands->end(); ++iter) {
365 // Make sure registered Chrome shortcuts cannot be automatically assigned 394 // Make sure registered Chrome shortcuts cannot be automatically assigned
366 // (overwritten) by extension developers. Media keys are an exception here. 395 // (overwritten) by extension developers. Media keys are an exception here.
367 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && 396 if ((!IsReservedChromeAccelerator(
368 IsWhitelistedGlobalShortcut(iter->second)) || 397 iter->second.accelerator(), extension, profile_) &&
398 IsWhitelistedGlobalShortcut(iter->second)) ||
Finnur 2014/02/12 12:51:14 This would turn into one simple if statement: if
Mike Wittman 2014/02/12 19:55:58 Done.
369 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { 399 extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
370 AddKeybindingPref(iter->second.accelerator(), 400 AddKeybindingPref(iter->second.accelerator(),
371 extension->id(), 401 extension->id(),
372 iter->second.command_name(), 402 iter->second.command_name(),
373 false, // Overwriting not allowed. 403 false, // Overwriting not allowed.
374 iter->second.global()); 404 iter->second.global());
375 } 405 }
376 } 406 }
377 407
378 const extensions::Command* browser_action_command = 408 const extensions::Command* browser_action_command =
379 CommandsInfo::GetBrowserActionCommand(extension); 409 CommandsInfo::GetBrowserActionCommand(extension);
380 if (browser_action_command) { 410 if (browser_action_command) {
381 if (!chrome::IsChromeAccelerator( 411 if (!IsReservedChromeAccelerator(
382 browser_action_command->accelerator(), profile_)) { 412 browser_action_command->accelerator(), extension, profile_)) {
383 AddKeybindingPref(browser_action_command->accelerator(), 413 AddKeybindingPref(browser_action_command->accelerator(),
384 extension->id(), 414 extension->id(),
385 browser_action_command->command_name(), 415 browser_action_command->command_name(),
386 false, // Overwriting not allowed. 416 false, // Overwriting not allowed.
387 false); // Browser actions can't be global. 417 false); // Browser actions can't be global.
388 } 418 }
389 } 419 }
390 420
391 const extensions::Command* page_action_command = 421 const extensions::Command* page_action_command =
392 CommandsInfo::GetPageActionCommand(extension); 422 CommandsInfo::GetPageActionCommand(extension);
393 if (page_action_command) { 423 if (page_action_command) {
394 if (!chrome::IsChromeAccelerator( 424 if (!IsReservedChromeAccelerator(
395 page_action_command->accelerator(), profile_)) { 425 page_action_command->accelerator(), extension, profile_)) {
396 AddKeybindingPref(page_action_command->accelerator(), 426 AddKeybindingPref(page_action_command->accelerator(),
397 extension->id(), 427 extension->id(),
398 page_action_command->command_name(), 428 page_action_command->command_name(),
399 false, // Overwriting not allowed. 429 false, // Overwriting not allowed.
400 false); // Page actions can't be global. 430 false); // Page actions can't be global.
401 } 431 }
402 } 432 }
403 } 433 }
404 434
405 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 435 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 528
499 return true; 529 return true;
500 } 530 }
501 531
502 template <> 532 template <>
503 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { 533 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
504 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); 534 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
505 } 535 }
506 536
507 } // namespace extensions 537 } // 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