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

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: remove use of GetActiveDesktop() 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
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 (!chrome::IsChromeAccelerator(accelerator, profile))
88 return false;
Finnur 2014/02/11 09:35:01 Ooops! Not LGTM. This is incorrect for Mac at the
Mike Wittman 2014/02/11 23:09:45 OK. I will ping this review when the Mac changes a
Finnur 2014/02/12 12:51:14 Thank you.
89
90 using extensions::SettingsOverrides;
91 using extensions::FeatureSwitch;
92 const SettingsOverrides* settings_overrides =
93 SettingsOverrides::Get(extension);
94 if (settings_overrides &&
95 SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides) &&
96 (extensions::PermissionsData::HasAPIPermission(
97 extension,
98 extensions::APIPermission::kBookmarkManagerPrivate) ||
99 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled())) {
100 return accelerator !=
101 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE);
102 }
103
104 return true;
105 }
Finnur 2014/02/11 09:50:05 We need to flip this logic. Instead of current imp
Mike Wittman 2014/02/11 23:09:45 Done.
106
81 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) { 107 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) {
108 // NOTE(wittman): If the restrictions on auto-assigning global shortcuts are
109 // ever relaxed in the future, then keys such as the bookmarking shortcut
110 // (IDC_BOOKMARK_PAGE) need to be evaluated on a case-by-case basis. Such keys
111 // can be auto-assigned by extensions (with the right permission), even though
112 // the shortcut is already in use by Chrome.
113
82 // Non-global shortcuts are always allowed. 114 // Non-global shortcuts are always allowed.
83 if (!command.global()) 115 if (!command.global())
84 return true; 116 return true;
85 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9]. 117 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9].
86 #if defined OS_MACOSX 118 #if defined OS_MACOSX
87 if (!command.accelerator().IsCmdDown()) 119 if (!command.accelerator().IsCmdDown())
88 return false; 120 return false;
89 #else 121 #else
90 if (!command.accelerator().IsCtrlDown()) 122 if (!command.accelerator().IsCtrlDown())
91 return false; 123 return false;
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 ExtensionSystem::Get(profile_)->extension_service(); 389 ExtensionSystem::Get(profile_)->extension_service();
358 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); 390 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
359 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) 391 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
360 return; 392 return;
361 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); 393 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
362 394
363 extensions::CommandMap::const_iterator iter = commands->begin(); 395 extensions::CommandMap::const_iterator iter = commands->begin();
364 for (; iter != commands->end(); ++iter) { 396 for (; iter != commands->end(); ++iter) {
365 // Make sure registered Chrome shortcuts cannot be automatically assigned 397 // Make sure registered Chrome shortcuts cannot be automatically assigned
366 // (overwritten) by extension developers. Media keys are an exception here. 398 // (overwritten) by extension developers. Media keys are an exception here.
367 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && 399 if ((!IsReservedChromeAccelerator(
368 IsWhitelistedGlobalShortcut(iter->second)) || 400 iter->second.accelerator(), extension, profile_) &&
401 IsWhitelistedGlobalShortcut(iter->second)) ||
369 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { 402 extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
370 AddKeybindingPref(iter->second.accelerator(), 403 AddKeybindingPref(iter->second.accelerator(),
371 extension->id(), 404 extension->id(),
372 iter->second.command_name(), 405 iter->second.command_name(),
373 false, // Overwriting not allowed. 406 false, // Overwriting not allowed.
374 iter->second.global()); 407 iter->second.global());
375 } 408 }
376 } 409 }
377 410
378 const extensions::Command* browser_action_command = 411 const extensions::Command* browser_action_command =
379 CommandsInfo::GetBrowserActionCommand(extension); 412 CommandsInfo::GetBrowserActionCommand(extension);
380 if (browser_action_command) { 413 if (browser_action_command) {
381 if (!chrome::IsChromeAccelerator( 414 if (!IsReservedChromeAccelerator(
382 browser_action_command->accelerator(), profile_)) { 415 browser_action_command->accelerator(), extension, profile_)) {
383 AddKeybindingPref(browser_action_command->accelerator(), 416 AddKeybindingPref(browser_action_command->accelerator(),
384 extension->id(), 417 extension->id(),
385 browser_action_command->command_name(), 418 browser_action_command->command_name(),
386 false, // Overwriting not allowed. 419 false, // Overwriting not allowed.
387 false); // Browser actions can't be global. 420 false); // Browser actions can't be global.
388 } 421 }
389 } 422 }
390 423
391 const extensions::Command* page_action_command = 424 const extensions::Command* page_action_command =
392 CommandsInfo::GetPageActionCommand(extension); 425 CommandsInfo::GetPageActionCommand(extension);
393 if (page_action_command) { 426 if (page_action_command) {
394 if (!chrome::IsChromeAccelerator( 427 if (!IsReservedChromeAccelerator(
395 page_action_command->accelerator(), profile_)) { 428 page_action_command->accelerator(), extension, profile_)) {
396 AddKeybindingPref(page_action_command->accelerator(), 429 AddKeybindingPref(page_action_command->accelerator(),
397 extension->id(), 430 extension->id(),
398 page_action_command->command_name(), 431 page_action_command->command_name(),
399 false, // Overwriting not allowed. 432 false, // Overwriting not allowed.
400 false); // Page actions can't be global. 433 false); // Page actions can't be global.
401 } 434 }
402 } 435 }
403 } 436 }
404 437
405 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 438 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 531
499 return true; 532 return true;
500 } 533 }
501 534
502 template <> 535 template <>
503 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { 536 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
504 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); 537 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
505 } 538 }
506 539
507 } // namespace extensions 540 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698