Chromium Code Reviews| Index: chrome/browser/extensions/api/commands/command_service.cc |
| diff --git a/chrome/browser/extensions/api/commands/command_service.cc b/chrome/browser/extensions/api/commands/command_service.cc |
| index a6ba60ed8c6ad0b15451b53e2f5f68db967a697a..965e6d7d1ca42296c3b39d0b2c750373e600c18c 100644 |
| --- a/chrome/browser/extensions/api/commands/command_service.cc |
| +++ b/chrome/browser/extensions/api/commands/command_service.cc |
| @@ -23,7 +23,6 @@ |
| #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h" |
| #include "chrome/common/pref_names.h" |
| #include "components/pref_registry/pref_registry_syncable.h" |
| -#include "content/public/browser/notification_details.h" |
|
Devlin
2014/05/28 16:58:34
We still want this because of the content::Details
limasdf
2014/05/28 17:38:43
Done.
|
| #include "content/public/browser/notification_service.h" |
| #include "extensions/browser/extension_function_registry.h" |
| #include "extensions/browser/extension_prefs.h" |
| @@ -33,11 +32,7 @@ |
| #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 extensions { |
|
Devlin
2014/05/28 16:58:34
Thanks :)
|
| namespace { |
| const char kExtension[] = "extension"; |
| @@ -50,22 +45,21 @@ const char kInitialBindingsHaveBeenAssigned[] = "initial_keybindings_set"; |
| std::string GetPlatformKeybindingKeyForAccelerator( |
| const ui::Accelerator& accelerator, const std::string extension_id) { |
| - std::string key = extensions::Command::CommandPlatform() + ":" + |
| - extensions::Command::AcceleratorToString(accelerator); |
| + std::string key = Command::CommandPlatform() + ":" + |
| + Command::AcceleratorToString(accelerator); |
| // Media keys have a 1-to-many relationship with targets, unlike regular |
| // 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::Command::IsMediaKey(accelerator)) |
| + if (Command::IsMediaKey(accelerator)) |
| key += ":" + extension_id; |
| return key; |
| } |
| bool IsForCurrentPlatform(const std::string& key) { |
| - return StartsWithASCII( |
| - key, extensions::Command::CommandPlatform() + ":", true); |
| + return StartsWithASCII(key, Command::CommandPlatform() + ":", true); |
| } |
| void SetInitialBindingsHaveBeenAssigned( |
| @@ -93,7 +87,7 @@ bool CanAutoAssign(const ui::Accelerator& accelerator, |
| bool is_named_command, |
| bool is_global) { |
| // Media Keys are non-exclusive, so allow auto-assigning them. |
| - if (extensions::Command::IsMediaKey(accelerator)) |
| + if (Command::IsMediaKey(accelerator)) |
| return true; |
| if (is_global) { |
| @@ -115,9 +109,9 @@ bool CanAutoAssign(const ui::Accelerator& accelerator, |
| } 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 (accelerator == chrome::GetPrimaryChromeAcceleratorForCommandId( |
| + IDC_BOOKMARK_PAGE) && |
| + 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 |
| @@ -132,8 +126,6 @@ bool CanAutoAssign(const ui::Accelerator& accelerator, |
| } // namespace |
| -namespace extensions { |
| - |
| // static |
| void CommandService::RegisterProfilePrefs( |
| user_prefs::PrefRegistrySyncable* registry) { |
| @@ -143,16 +135,12 @@ void CommandService::RegisterProfilePrefs( |
| } |
| CommandService::CommandService(content::BrowserContext* context) |
| - : profile_(Profile::FromBrowserContext(context)) { |
| + : profile_(Profile::FromBrowserContext(context)), |
| + extension_registry_observer_(this) { |
| ExtensionFunctionRegistry::GetInstance()-> |
| RegisterFunction<GetAllCommandsFunction>(); |
| - registrar_.Add(this, |
| - chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, |
| - content::Source<Profile>(profile_)); |
| - registrar_.Add(this, |
| - chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| - content::Source<Profile>(profile_)); |
| + extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| } |
| CommandService::~CommandService() { |
| @@ -173,8 +161,7 @@ CommandService* CommandService::Get(content::BrowserContext* context) { |
| } |
| // static |
| -bool CommandService::RemovesBookmarkShortcut( |
| - const extensions::Extension* extension) { |
| +bool CommandService::RemovesBookmarkShortcut(const Extension* extension) { |
| const UIOverrides* ui_overrides = UIOverrides::Get(extension); |
| const SettingsOverrides* settings_overrides = |
| SettingsOverrides::Get(extension); |
| @@ -183,16 +170,14 @@ bool CommandService::RemovesBookmarkShortcut( |
| SettingsOverrides::RemovesBookmarkShortcut(*settings_overrides)) || |
| (ui_overrides && |
| UIOverrides::RemovesBookmarkShortcut(*ui_overrides))) && |
| - (extensions::PermissionsData::HasAPIPermission( |
| - extension, |
| - extensions::APIPermission::kBookmarkManagerPrivate) || |
| - extensions::FeatureSwitch::enable_override_bookmarks_ui()-> |
| - IsEnabled()); |
| + (PermissionsData::HasAPIPermission( |
| + extension, APIPermission::kBookmarkManagerPrivate) || |
| + FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()); |
| } |
| // static |
| bool CommandService::RemovesBookmarkOpenPagesShortcut( |
| - const extensions::Extension* extension) { |
| + const Extension* extension) { |
| const UIOverrides* ui_overrides = UIOverrides::Get(extension); |
| const SettingsOverrides* settings_overrides = |
| SettingsOverrides::Get(extension); |
| @@ -202,16 +187,14 @@ bool CommandService::RemovesBookmarkOpenPagesShortcut( |
| *settings_overrides)) || |
| (ui_overrides && |
| UIOverrides::RemovesBookmarkOpenPagesShortcut(*ui_overrides))) && |
| - (extensions::PermissionsData::HasAPIPermission( |
| - extension, |
| - extensions::APIPermission::kBookmarkManagerPrivate) || |
| - extensions::FeatureSwitch::enable_override_bookmarks_ui()-> |
| - IsEnabled()); |
| + (PermissionsData::HasAPIPermission( |
| + extension, APIPermission::kBookmarkManagerPrivate) || |
| + FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()); |
| } |
| bool CommandService::GetBrowserActionCommand(const std::string& extension_id, |
| QueryType type, |
| - extensions::Command* command, |
| + Command* command, |
| bool* active) const { |
| return GetExtensionActionCommand( |
| extension_id, type, command, active, BROWSER_ACTION); |
| @@ -219,39 +202,37 @@ bool CommandService::GetBrowserActionCommand(const std::string& extension_id, |
| bool CommandService::GetPageActionCommand(const std::string& extension_id, |
| QueryType type, |
| - extensions::Command* command, |
| + 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) const { |
| +bool CommandService::GetNamedCommands(const std::string& extension_id, |
| + QueryType type, |
| + CommandScope scope, |
| + CommandMap* command_map) const { |
| const ExtensionSet& extensions = |
| ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| const Extension* extension = extensions.GetByID(extension_id); |
| CHECK(extension); |
| command_map->clear(); |
| - const extensions::CommandMap* commands = |
| - CommandsInfo::GetNamedCommands(extension); |
| + const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
| if (!commands) |
| return false; |
| - extensions::CommandMap::const_iterator iter = commands->begin(); |
| + CommandMap::const_iterator iter = commands->begin(); |
| for (; iter != commands->end(); ++iter) { |
| // Look up to see if the user has overridden how the command should work. |
| - extensions::Command saved_command = |
| + Command saved_command = |
| FindCommandByName(extension_id, iter->second.command_name()); |
| ui::Accelerator shortcut_assigned = saved_command.accelerator(); |
| if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
| continue; |
| - extensions::Command command = iter->second; |
| + Command command = iter->second; |
| if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global())) |
| continue; |
| @@ -320,30 +301,24 @@ bool CommandService::AddKeybindingPref( |
| return true; |
| } |
| -void CommandService::Observe( |
| - int type, |
| - const content::NotificationSource& source, |
| - const content::NotificationDetails& details) { |
| - switch (type) { |
| - case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: |
| - AssignInitialKeybindings( |
| - content::Details<const InstalledExtensionInfo>(details)->extension); |
| - break; |
| - case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: |
| - RemoveKeybindingPrefs( |
| - content::Details<const Extension>(details)->id(), |
| - std::string()); |
| - break; |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| +void CommandService::OnExtensionWillBeInstalled( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension, |
| + bool is_update, |
| + const std::string& old_name) { |
| + AssignInitialKeybindings(extension); |
| +} |
| + |
| +void CommandService::OnExtensionUninstalled( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension) { |
| + RemoveKeybindingPrefs(extension->id(), std::string()); |
| } |
| void CommandService::UpdateKeybindingPrefs(const std::string& extension_id, |
| const std::string& command_name, |
| const std::string& keystroke) { |
| - extensions::Command command = FindCommandByName(extension_id, command_name); |
| + Command command = FindCommandByName(extension_id, command_name); |
| // The extension command might be assigned another shortcut. Remove that |
| // shortcut before proceeding. |
| @@ -358,7 +333,7 @@ void CommandService::UpdateKeybindingPrefs(const std::string& extension_id, |
| bool CommandService::SetScope(const std::string& extension_id, |
| const std::string& command_name, |
| bool global) { |
| - extensions::Command command = FindCommandByName(extension_id, command_name); |
| + Command command = FindCommandByName(extension_id, command_name); |
| if (global == command.global()) |
| return false; |
| @@ -409,42 +384,43 @@ Command CommandService::FindCommandByName(const std::string& extension_id, |
| bool CommandService::GetBoundExtensionCommand( |
| const std::string& extension_id, |
| const ui::Accelerator& accelerator, |
| - extensions::Command* command, |
| + Command* command, |
| ExtensionCommandType* command_type) const { |
| - const ExtensionSet& extensions = |
| - ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| - const Extension* extension = extensions.GetByID(extension_id); |
| + const Extension* extension = |
| + ExtensionRegistry::Get(profile_) |
| + ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| CHECK(extension); |
| - extensions::Command prospective_command; |
| - extensions::CommandMap command_map; |
| + Command prospective_command; |
| + CommandMap command_map; |
| bool active = false; |
| if (GetBrowserActionCommand(extension_id, |
| - extensions::CommandService::ACTIVE_ONLY, |
| + CommandService::ACTIVE_ONLY, |
| &prospective_command, |
| - &active) && active && |
| - accelerator == prospective_command.accelerator()) { |
| + &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, |
| + CommandService::ACTIVE_ONLY, |
| &prospective_command, |
| - &active) && active && |
| - accelerator == prospective_command.accelerator()) { |
| + &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, |
| + CommandService::ACTIVE_ONLY, |
| + CommandService::REGULAR, |
| &command_map)) { |
| - for (extensions::CommandMap::const_iterator it = command_map.begin(); |
| - it != command_map.end(); ++it) { |
| + for (CommandMap::const_iterator it = command_map.begin(); |
| + it != command_map.end(); |
| + ++it) { |
| if (accelerator == it->second.accelerator()) { |
| if (command) |
| *command = it->second; |
| @@ -458,7 +434,7 @@ bool CommandService::GetBoundExtensionCommand( |
| } |
| bool CommandService::OverridesBookmarkShortcut( |
| - const extensions::Extension* extension) const { |
| + const Extension* extension) const { |
| return RemovesBookmarkShortcut(extension) && |
| GetBoundExtensionCommand( |
| extension->id(), |
| @@ -468,8 +444,7 @@ bool CommandService::OverridesBookmarkShortcut( |
| } |
| void CommandService::AssignInitialKeybindings(const Extension* extension) { |
| - const extensions::CommandMap* commands = |
| - CommandsInfo::GetNamedCommands(extension); |
| + const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
| if (!commands) |
| return; |
| @@ -478,9 +453,9 @@ void CommandService::AssignInitialKeybindings(const Extension* extension) { |
| return; |
| SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
| - extensions::CommandMap::const_iterator iter = commands->begin(); |
| + CommandMap::const_iterator iter = commands->begin(); |
| for (; iter != commands->end(); ++iter) { |
| - const extensions::Command command = iter->second; |
| + const Command command = iter->second; |
| if (CanAutoAssign(command.accelerator(), |
| extension, |
| profile_, |
| @@ -494,7 +469,7 @@ void CommandService::AssignInitialKeybindings(const Extension* extension) { |
| } |
| } |
| - const extensions::Command* browser_action_command = |
| + const Command* browser_action_command = |
| CommandsInfo::GetBrowserActionCommand(extension); |
| if (browser_action_command && |
| CanAutoAssign(browser_action_command->accelerator(), |
| @@ -509,7 +484,7 @@ void CommandService::AssignInitialKeybindings(const Extension* extension) { |
| false); // Not global. |
| } |
| - const extensions::Command* page_action_command = |
| + const Command* page_action_command = |
| CommandsInfo::GetPageActionCommand(extension); |
| if (page_action_command && |
| CanAutoAssign(page_action_command->accelerator(), |
| @@ -577,7 +552,7 @@ void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, |
| bool CommandService::GetExtensionActionCommand( |
| const std::string& extension_id, |
| QueryType query_type, |
| - extensions::Command* command, |
| + Command* command, |
| bool* active, |
| ExtensionCommandType action_type) const { |
| const ExtensionSet& extensions = |
| @@ -588,7 +563,7 @@ bool CommandService::GetExtensionActionCommand( |
| if (active) |
| *active = false; |
| - const extensions::Command* requested_command = NULL; |
| + const Command* requested_command = NULL; |
| switch (action_type) { |
| case BROWSER_ACTION: |
| requested_command = CommandsInfo::GetBrowserActionCommand(extension); |
| @@ -604,7 +579,7 @@ bool CommandService::GetExtensionActionCommand( |
| return false; |
| // Look up to see if the user has overridden how the command should work. |
| - extensions::Command saved_command = |
| + Command saved_command = |
| FindCommandByName(extension_id, requested_command->command_name()); |
| ui::Accelerator shortcut_assigned = saved_command.accelerator(); |