#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
+#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/commands/commands.h"
#include "chrome/browser/extensions/extension_commands_global_registry.h"
-#include "chrome/browser/extensions/extension_function_registry.h"
#include "chrome/browser/extensions/extension_keybinding_registry.h"
-#include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/common/extensions/api/commands/commands_handler.h"
+#include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
+#include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
+#include "extensions/browser/extension_function_registry.h"
+#include "extensions/browser/extension_prefs.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
+#include "extensions/common/permissions/permissions_data.h"
using extensions::Extension;
using extensions::ExtensionPrefs;
+using extensions::SettingsOverrides;
+using extensions::UIOverrides;
namespace {
// shortcut (1-to-1 relationship). That means two or more extensions can
// register for the same media key so the extension ID needs to be added to
// the key to make sure the key is unique.
- if (extensions::CommandService::IsMediaKey(accelerator))
+ if (extensions::Command::IsMediaKey(accelerator))
key += ":" + extension_id;
return key;
return assigned;
}
-bool IsWhitelistedGlobalShortcut(const extensions::Command& command) {
- // Non-global shortcuts are always allowed.
- if (!command.global())
+// Checks if |extension| is permitted to automatically assign the |accelerator|
+// key.
+bool CanAutoAssign(const ui::Accelerator& accelerator,
+ const Extension* extension,
+ Profile* profile,
+ bool is_named_command,
+ bool is_global) {
+ // Media Keys are non-exclusive, so allow auto-assigning them.
+ if (extensions::Command::IsMediaKey(accelerator))
return true;
- // Global shortcuts must be (Ctrl|Command)-Shift-[0-9].
+
+ if (is_global) {
+ if (!is_named_command)
+ return false; // Browser and page actions are not global in nature.
+
+ // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9].
#if defined OS_MACOSX
- if (!command.accelerator().IsCmdDown())
- return false;
+ if (!accelerator.IsCmdDown())
+ return false;
#else
- if (!command.accelerator().IsCtrlDown())
- return false;
+ if (!accelerator.IsCtrlDown())
+ return false;
#endif
- if (!command.accelerator().IsShiftDown())
- return false;
- return (command.accelerator().key_code() >= ui::VKEY_0 &&
- command.accelerator().key_code() <= ui::VKEY_9);
+ if (!accelerator.IsShiftDown())
+ return false;
+ return (accelerator.key_code() >= ui::VKEY_0 &&
+ accelerator.key_code() <= ui::VKEY_9);
+ } else {
+ // Not a global command, check if Chrome shortcut and whether
+ // we can override it.
+ if (accelerator ==
+ chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE) &&
+ extensions::CommandService::RemovesBookmarkShortcut(extension)) {
+ // If this check fails it either means we have an API to override a
+ // key that isn't a ChromeAccelerator (and the API can therefore be
+ // deprecated) or the IsChromeAccelerator isn't consistently
+ // returning true for all accelerators.
+ DCHECK(chrome::IsChromeAccelerator(accelerator, profile));
+ return true;
+ }
+
+ return !chrome::IsChromeAccelerator(accelerator, profile);
+ }
}
} // namespace
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
}
-CommandService::CommandService(Profile* profile)
- : profile_(profile) {
+CommandService::CommandService(content::BrowserContext* context)
+ : profile_(Profile::FromBrowserContext(context)) {
ExtensionFunctionRegistry::GetInstance()->
RegisterFunction<GetAllCommandsFunction>();
- registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
- content::Source<Profile>(profile));
- registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
- content::Source<Profile>(profile));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_INSTALLED,
+ content::Source<Profile>(profile_));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
+ content::Source<Profile>(profile_));
}
CommandService::~CommandService() {
}
-static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> >
-g_factory = LAZY_INSTANCE_INITIALIZER;
+static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService> >
+ g_factory = LAZY_INSTANCE_INITIALIZER;
// static
-ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() {
+BrowserContextKeyedAPIFactory<CommandService>*
+CommandService::GetFactoryInstance() {
return g_factory.Pointer();
}
// static
-CommandService* CommandService::Get(Profile* profile) {
- return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile);
+CommandService* CommandService::Get(content::BrowserContext* context) {
+ return BrowserContextKeyedAPIFactory<CommandService>::Get(context);
}
// static
-bool CommandService::IsMediaKey(const ui::Accelerator& accelerator) {
- if (accelerator.modifiers() != 0)
- return false;
+bool CommandService::RemovesBookmarkShortcut(
+ const extensions::Extension* extension) {
+ const UIOverrides* ui_overrides = UIOverrides::Get(extension);
+ const SettingsOverrides* settings_overrides =
+ SettingsOverrides::Get(extension);
+
+ return ((settings_overrides &&
+ SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides)) ||
+ (ui_overrides &&
+ UIOverrides::RemovesBookmarkShortcut(*ui_overrides))) &&
+ (extensions::PermissionsData::HasAPIPermission(
+ extension,
+ extensions::APIPermission::kBookmarkManagerPrivate) ||
+ extensions::FeatureSwitch::enable_override_bookmarks_ui()->
+ IsEnabled());
+}
- return (accelerator.key_code() == ui::VKEY_MEDIA_NEXT_TRACK ||
- accelerator.key_code() == ui::VKEY_MEDIA_PREV_TRACK ||
- accelerator.key_code() == ui::VKEY_MEDIA_PLAY_PAUSE ||
- accelerator.key_code() == ui::VKEY_MEDIA_STOP);
+// static
+bool CommandService::RemovesBookmarkOpenPagesShortcut(
+ const extensions::Extension* extension) {
+ const UIOverrides* ui_overrides = UIOverrides::Get(extension);
+ const SettingsOverrides* settings_overrides =
+ SettingsOverrides::Get(extension);
+
+ return ((settings_overrides &&
+ SettingsOverrides::RemovesBookmarkOpenPagesShortcut(
+ *settings_overrides)) ||
+ (ui_overrides &&
+ UIOverrides::RemovesBookmarkOpenPagesShortcut(*ui_overrides))) &&
+ (extensions::PermissionsData::HasAPIPermission(
+ extension,
+ extensions::APIPermission::kBookmarkManagerPrivate) ||
+ extensions::FeatureSwitch::enable_override_bookmarks_ui()->
+ IsEnabled());
}
-bool CommandService::GetBrowserActionCommand(
- const std::string& extension_id,
- QueryType type,
- extensions::Command* command,
- bool* active) {
+bool CommandService::GetBrowserActionCommand(const std::string& extension_id,
+ QueryType type,
+ extensions::Command* command,
+ bool* active) const {
return GetExtensionActionCommand(
extension_id, type, command, active, BROWSER_ACTION);
}
-bool CommandService::GetPageActionCommand(
- const std::string& extension_id,
- QueryType type,
- extensions::Command* command,
- bool* active) {
+bool CommandService::GetPageActionCommand(const std::string& extension_id,
+ QueryType type,
+ extensions::Command* command,
+ bool* active) const {
return GetExtensionActionCommand(
extension_id, type, command, active, PAGE_ACTION);
}
-bool CommandService::GetNamedCommands(const std::string& extension_id,
- QueryType type,
- CommandScope scope,
- extensions::CommandMap* command_map) {
- ExtensionService* extension_service =
- ExtensionSystem::Get(profile_)->extension_service();
- if (!extension_service)
- return false; // Can occur during testing.
- const ExtensionSet* extensions = extension_service->extensions();
- const Extension* extension = extensions->GetByID(extension_id);
+bool CommandService::GetNamedCommands(
+ const std::string& extension_id,
+ QueryType type,
+ CommandScope scope,
+ extensions::CommandMap* command_map) const {
+ const ExtensionSet& extensions =
+ ExtensionRegistry::Get(profile_)->enabled_extensions();
+ const Extension* extension = extensions.GetByID(extension_id);
CHECK(extension);
command_map->clear();
return false;
// Media Keys are allowed to be used by named command only.
- DCHECK(!IsMediaKey(accelerator) ||
+ DCHECK(!Command::IsMediaKey(accelerator) ||
(command_name != manifest_values::kPageActionCommandEvent &&
command_name != manifest_values::kBrowserActionCommandEvent));
return true;
}
-Command CommandService::FindCommandByName(
- const std::string& extension_id, const std::string& command) {
+Command CommandService::FindCommandByName(const std::string& extension_id,
+ const std::string& command) const {
const base::DictionaryValue* bindings =
profile_->GetPrefs()->GetDictionary(prefs::kExtensionCommands);
for (base::DictionaryValue::Iterator it(*bindings); !it.IsAtEnd();
return Command();
}
+bool CommandService::GetBoundExtensionCommand(
+ const std::string& extension_id,
+ const ui::Accelerator& accelerator,
+ extensions::Command* command,
+ ExtensionCommandType* command_type) const {
+ const ExtensionSet& extensions =
+ ExtensionRegistry::Get(profile_)->enabled_extensions();
+ const Extension* extension = extensions.GetByID(extension_id);
+ CHECK(extension);
+
+ extensions::Command prospective_command;
+ extensions::CommandMap command_map;
+ bool active = false;
+ if (GetBrowserActionCommand(extension_id,
+ extensions::CommandService::ACTIVE_ONLY,
+ &prospective_command,
+ &active) && active &&
+ accelerator == prospective_command.accelerator()) {
+ if (command)
+ *command = prospective_command;
+ if (command_type)
+ *command_type = BROWSER_ACTION;
+ return true;
+ } else if (GetPageActionCommand(extension_id,
+ extensions::CommandService::ACTIVE_ONLY,
+ &prospective_command,
+ &active) && active &&
+ accelerator == prospective_command.accelerator()) {
+ if (command)
+ *command = prospective_command;
+ if (command_type)
+ *command_type = PAGE_ACTION;
+ return true;
+ } else if (GetNamedCommands(extension_id,
+ extensions::CommandService::ACTIVE_ONLY,
+ extensions::CommandService::REGULAR,
+ &command_map)) {
+ for (extensions::CommandMap::const_iterator it = command_map.begin();
+ it != command_map.end(); ++it) {
+ if (accelerator == it->second.accelerator()) {
+ if (command)
+ *command = it->second;
+ if (command_type)
+ *command_type = NAMED;
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+bool CommandService::OverridesBookmarkShortcut(
+ const extensions::Extension* extension) const {
+ return RemovesBookmarkShortcut(extension) &&
+ GetBoundExtensionCommand(
+ extension->id(),
+ chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE),
+ NULL,
+ NULL);
+}
+
void CommandService::AssignInitialKeybindings(const Extension* extension) {
const extensions::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension);
if (!commands)
return;
- ExtensionService* extension_service =
- ExtensionSystem::Get(profile_)->extension_service();
- ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
+ ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
return;
SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
extensions::CommandMap::const_iterator iter = commands->begin();
for (; iter != commands->end(); ++iter) {
- // Make sure registered Chrome shortcuts cannot be automatically assigned
- // (overwritten) by extension developers. Media keys are an exception here.
- if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) &&
- IsWhitelistedGlobalShortcut(iter->second)) ||
- extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
- AddKeybindingPref(iter->second.accelerator(),
+ const extensions::Command command = iter->second;
+ if (CanAutoAssign(command.accelerator(),
+ extension,
+ profile_,
+ true, // Is a named command.
+ command.global())) {
+ AddKeybindingPref(command.accelerator(),
extension->id(),
- iter->second.command_name(),
+ command.command_name(),
false, // Overwriting not allowed.
- iter->second.global());
+ command.global());
}
}
const extensions::Command* browser_action_command =
CommandsInfo::GetBrowserActionCommand(extension);
- if (browser_action_command) {
- if (!chrome::IsChromeAccelerator(
- browser_action_command->accelerator(), profile_)) {
- AddKeybindingPref(browser_action_command->accelerator(),
- extension->id(),
- browser_action_command->command_name(),
- false, // Overwriting not allowed.
- false); // Browser actions can't be global.
- }
+ if (browser_action_command &&
+ CanAutoAssign(browser_action_command->accelerator(),
+ extension,
+ profile_,
+ false, // Not a named command.
+ false)) { // Not global.
+ AddKeybindingPref(browser_action_command->accelerator(),
+ extension->id(),
+ browser_action_command->command_name(),
+ false, // Overwriting not allowed.
+ false); // Not global.
}
const extensions::Command* page_action_command =
CommandsInfo::GetPageActionCommand(extension);
- if (page_action_command) {
- if (!chrome::IsChromeAccelerator(
- page_action_command->accelerator(), profile_)) {
- AddKeybindingPref(page_action_command->accelerator(),
- extension->id(),
- page_action_command->command_name(),
- false, // Overwriting not allowed.
- false); // Page actions can't be global.
- }
+ if (page_action_command &&
+ CanAutoAssign(page_action_command->accelerator(),
+ extension,
+ profile_,
+ false, // Not a named command.
+ false)) { // Not global.
+ AddKeybindingPref(page_action_command->accelerator(),
+ extension->id(),
+ page_action_command->command_name(),
+ false, // Overwriting not allowed.
+ false); // Not global.
}
}
QueryType query_type,
extensions::Command* command,
bool* active,
- ExtensionActionType action_type) {
- ExtensionService* service =
- ExtensionSystem::Get(profile_)->extension_service();
- if (!service)
- return false; // Can happen in tests.
- const ExtensionSet* extensions = service->extensions();
- const Extension* extension = extensions->GetByID(extension_id);
+ ExtensionCommandType action_type) const {
+ const ExtensionSet& extensions =
+ ExtensionRegistry::Get(profile_)->enabled_extensions();
+ const Extension* extension = extensions.GetByID(extension_id);
CHECK(extension);
if (active)
case PAGE_ACTION:
requested_command = CommandsInfo::GetPageActionCommand(extension);
break;
+ case NAMED:
+ NOTREACHED();
+ return false;
}
if (!requested_command)
return false;
}
template <>
-void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
+void
+BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
}