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: 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;
89
90 using extensions::SettingsOverrides;
91 using extensions::FeatureSwitch;
92 if (SettingsOverrides::Get(extension)->RemovesBookmarkShortcut() &&
Finnur 2014/02/05 13:37:40 I mentioned this in the design doc, but worth repe
Mike Wittman 2014/02/05 19:17:07 I'll update this as necessary pending agreement in
93 (extensions::PermissionsData::HasAPIPermission(
94 extension,
95 extensions::APIPermission::kBookmarkManagerPrivate) ||
Finnur 2014/02/05 13:37:40 You mentioned you didn't want a dependency on the
Mike Wittman 2014/02/05 19:17:07 The check for kBookmarkManagerPrivate API permissi
96 FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled())) {
97 return accelerator !=
98 chrome::GetChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE);
99 }
100
101 return true;
102 }
103
81 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) { 104 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) {
82 // Non-global shortcuts are always allowed. 105 // Non-global shortcuts are always allowed.
83 if (!command.global()) 106 if (!command.global())
84 return true; 107 return true;
85 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9]. 108 // Global shortcuts must be (Ctrl|Command)-Shift-[0-9].
86 #if defined OS_MACOSX 109 #if defined OS_MACOSX
87 if (!command.accelerator().IsCmdDown()) 110 if (!command.accelerator().IsCmdDown())
88 return false; 111 return false;
89 #else 112 #else
90 if (!command.accelerator().IsCtrlDown()) 113 if (!command.accelerator().IsCtrlDown())
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 ExtensionSystem::Get(profile_)->extension_service(); 380 ExtensionSystem::Get(profile_)->extension_service();
358 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); 381 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
359 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) 382 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
360 return; 383 return;
361 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); 384 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
362 385
363 extensions::CommandMap::const_iterator iter = commands->begin(); 386 extensions::CommandMap::const_iterator iter = commands->begin();
364 for (; iter != commands->end(); ++iter) { 387 for (; iter != commands->end(); ++iter) {
365 // Make sure registered Chrome shortcuts cannot be automatically assigned 388 // Make sure registered Chrome shortcuts cannot be automatically assigned
366 // (overwritten) by extension developers. Media keys are an exception here. 389 // (overwritten) by extension developers. Media keys are an exception here.
367 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && 390 if ((!IsReservedChromeAccelerator(
368 IsWhitelistedGlobalShortcut(iter->second)) || 391 iter->second.accelerator(), extension, profile_) &&
392 IsWhitelistedGlobalShortcut(iter->second)) ||
369 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { 393 extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
370 AddKeybindingPref(iter->second.accelerator(), 394 AddKeybindingPref(iter->second.accelerator(),
371 extension->id(), 395 extension->id(),
372 iter->second.command_name(), 396 iter->second.command_name(),
373 false, // Overwriting not allowed. 397 false, // Overwriting not allowed.
Finnur 2014/02/05 13:37:40 Note: If the user has reassigned Ctrl+D to somethi
Mike Wittman 2014/02/05 19:17:07 I tend to agree that user-created shortcuts should
374 iter->second.global()); 398 iter->second.global());
Finnur 2014/02/05 13:37:40 Commands can be global/non-global in scope and a g
Mike Wittman 2014/02/05 19:17:07 SGTM. I've added the comment.
375 } 399 }
376 } 400 }
377 401
378 const extensions::Command* browser_action_command = 402 const extensions::Command* browser_action_command =
379 CommandsInfo::GetBrowserActionCommand(extension); 403 CommandsInfo::GetBrowserActionCommand(extension);
380 if (browser_action_command) { 404 if (browser_action_command) {
381 if (!chrome::IsChromeAccelerator( 405 if (!IsReservedChromeAccelerator(
382 browser_action_command->accelerator(), profile_)) { 406 browser_action_command->accelerator(), extension, profile_)) {
383 AddKeybindingPref(browser_action_command->accelerator(), 407 AddKeybindingPref(browser_action_command->accelerator(),
384 extension->id(), 408 extension->id(),
385 browser_action_command->command_name(), 409 browser_action_command->command_name(),
386 false, // Overwriting not allowed. 410 false, // Overwriting not allowed.
387 false); // Browser actions can't be global. 411 false); // Browser actions can't be global.
388 } 412 }
389 } 413 }
390 414
391 const extensions::Command* page_action_command = 415 const extensions::Command* page_action_command =
392 CommandsInfo::GetPageActionCommand(extension); 416 CommandsInfo::GetPageActionCommand(extension);
393 if (page_action_command) { 417 if (page_action_command) {
394 if (!chrome::IsChromeAccelerator( 418 if (!IsReservedChromeAccelerator(
395 page_action_command->accelerator(), profile_)) { 419 page_action_command->accelerator(), extension, profile_)) {
396 AddKeybindingPref(page_action_command->accelerator(), 420 AddKeybindingPref(page_action_command->accelerator(),
397 extension->id(), 421 extension->id(),
398 page_action_command->command_name(), 422 page_action_command->command_name(),
399 false, // Overwriting not allowed. 423 false, // Overwriting not allowed.
400 false); // Page actions can't be global. 424 false); // Page actions can't be global.
401 } 425 }
402 } 426 }
403 } 427 }
404 428
405 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 429 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 522
499 return true; 523 return true;
500 } 524 }
501 525
502 template <> 526 template <>
503 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { 527 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
504 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); 528 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
505 } 529 }
506 530
507 } // namespace extensions 531 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/accelerator_utils.h » ('j') | chrome/browser/ui/accelerator_utils.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698