Chromium Code Reviews| Index: chrome/browser/extensions/extension_context_menu_model.cc |
| diff --git a/chrome/browser/extensions/extension_context_menu_model.cc b/chrome/browser/extensions/extension_context_menu_model.cc |
| index e2774b619dbb4f86c402c70754e4ea68cbfc8ff5..c53342abe503ea241a1e7e1e92ac658aea7c3895 100644 |
| --- a/chrome/browser/extensions/extension_context_menu_model.cc |
| +++ b/chrome/browser/extensions/extension_context_menu_model.cc |
| @@ -4,13 +4,19 @@ |
| #include "chrome/browser/extensions/extension_context_menu_model.h" |
| +#include "base/metrics/histogram.h" |
| #include "base/prefs/pref_service.h" |
| +#include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/browser_process.h" |
| #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| +#include "chrome/browser/extensions/context_menu_matcher.h" |
| #include "chrome/browser/extensions/extension_action.h" |
| #include "chrome/browser/extensions/extension_action_manager.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| #include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/extensions/menu_manager.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/chrome_pages.h" |
| @@ -21,6 +27,7 @@ |
| #include "chrome/common/url_constants.h" |
| #include "content/public/browser/web_contents.h" |
| #include "extensions/browser/extension_prefs.h" |
| +#include "extensions/browser/extension_registry.h" |
| #include "extensions/browser/extension_system.h" |
| #include "extensions/browser/management_policy.h" |
| #include "extensions/common/extension.h" |
| @@ -32,6 +39,35 @@ using content::OpenURLParams; |
| using content::Referrer; |
| using content::WebContents; |
| using extensions::Extension; |
| +using extensions::MenuItem; |
| +using extensions::MenuManager; |
| + |
| +namespace { |
| + |
| +// Matches menu items with the context menu's extension. |
|
Devlin
2014/07/07 20:20:57
What does "matches" mean? How about,
"Returns tru
gpdavis
2014/07/09 02:13:53
Done.
|
| +bool MenuItemMatchesExtension( |
| + ExtensionContextMenuModel::ActionType type, |
| + const Extension* extension, |
| + const MenuItem* item) { |
| + if (type == ExtensionContextMenuModel::NO_ACTION || |
| + item->extension_id() != extension->id()) |
| + return false; |
| + |
| + MenuItem::ContextList contexts = item->contexts(); |
|
Devlin
2014/07/07 20:20:58
While you're in the area, would you mind updating
gpdavis
2014/07/09 02:13:53
Done.
|
| + |
| + if (contexts.Contains(MenuItem::ALL)) |
| + return true; |
| + if (contexts.Contains(MenuItem::PAGE_ACTION) && |
| + (type == ExtensionContextMenuModel::PAGE_ACTION)) |
| + return true; |
| + if (contexts.Contains(MenuItem::BROWSER_ACTION) && |
| + (type == ExtensionContextMenuModel::BROWSER_ACTION)) |
| + return true; |
| + |
| + return false; |
| +} |
| + |
| +} // namespace |
| ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| Browser* browser, |
| @@ -40,7 +76,8 @@ ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| extension_id_(extension->id()), |
| browser_(browser), |
| profile_(browser->profile()), |
| - delegate_(delegate) { |
| + delegate_(delegate), |
| + action_type_(NO_ACTION) { |
| InitMenu(extension); |
| if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
| @@ -56,11 +93,15 @@ ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| extension_id_(extension->id()), |
| browser_(browser), |
| profile_(browser->profile()), |
| - delegate_(NULL) { |
| + delegate_(NULL), |
| + action_type_(NO_ACTION) { |
| InitMenu(extension); |
| } |
| bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
| + if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
| + command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) |
| + return extension_items_->IsCommandIdChecked(command_id); |
| return false; |
| } |
| @@ -103,6 +144,17 @@ void ExtensionContextMenuModel::ExecuteCommand(int command_id, |
| if (!extension) |
| return; |
| + if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
| + command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { |
| + WebContents* web_contents = |
| + browser_->tab_strip_model()->GetActiveWebContents(); |
| + DCHECK(extension_items_); |
| + extension_items_->ExecuteCommand(command_id, |
| + web_contents, |
| + content::ContextMenuParams()); |
| + return; |
| + } |
| + |
| switch (command_id) { |
| case NAME: { |
| OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), |
| @@ -162,14 +214,28 @@ void ExtensionContextMenuModel::InitMenu(const Extension* extension) { |
| extensions::ExtensionActionManager* extension_action_manager = |
| extensions::ExtensionActionManager::Get(profile_); |
| extension_action_ = extension_action_manager->GetBrowserAction(*extension); |
| - if (!extension_action_) |
| + if (!extension_action_) { |
| extension_action_ = extension_action_manager->GetPageAction(*extension); |
| + if (extension_action_) |
| + action_type_ = PAGE_ACTION; |
| + } else { |
| + action_type_ = BROWSER_ACTION; |
| + } |
| + |
| + extension_items_.reset( |
| + new extensions::ContextMenuMatcher(profile_, |
| + this, |
| + this, |
| + base::Bind(MenuItemMatchesExtension, |
| + action_type_, |
| + extension))); |
| std::string extension_name = extension->name(); |
| // Ampersands need to be escaped to avoid being treated like |
| // mnemonics in the menu. |
| base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
| AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
| + AppendExtensionItems(); |
| AddSeparator(ui::NORMAL_SEPARATOR); |
| AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
| AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
| @@ -184,3 +250,53 @@ const Extension* ExtensionContextMenuModel::GetExtension() const { |
| extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| return extension_service->GetExtensionById(extension_id_, false); |
| } |
| + |
| +void ExtensionContextMenuModel::AppendExtensionItems() { |
| + extension_items_->Clear(); |
| + ExtensionService* service = |
|
Devlin
2014/07/07 20:20:57
We don't use this anymore, right?
gpdavis
2014/07/09 02:13:54
Negative. I just put the method call into the con
|
| + extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| + if (!service) |
| + return; // In unit-tests, we may not have an ExtensionService. |
| + |
| + MenuManager* menu_manager = MenuManager::Get(profile_); |
| + if (!menu_manager) |
| + return; |
| + |
| + // Get a list of extension ids that have context menu items, and sort by the |
| + // top level context menu title of the extension. |
| + std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds(); |
| + std::vector<base::string16> sorted_menu_titles; |
| + std::map<base::string16, std::string> map_ids; |
| + for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin(); |
| + iter != ids.end(); |
| + ++iter) { |
| + const Extension* extension = extensions::ExtensionRegistry::Get( |
|
Devlin
2014/07/07 20:20:57
cache ExtensionRegistry, or better yet, enabled_ex
gpdavis
2014/07/09 02:13:54
Done.
|
| + profile_)->enabled_extensions().GetByID(iter->extension_id); |
| + if (extension) { |
| + base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle( |
| + *iter, base::EmptyString16()); |
| + map_ids[menu_title] = iter->extension_id; |
| + sorted_menu_titles.push_back(menu_title); |
| + } |
| + } |
| + if (sorted_menu_titles.empty()) |
| + return; |
| + |
| + AddSeparator(ui::NORMAL_SEPARATOR); |
| + |
| + const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
| + l10n_util::SortStrings16(app_locale, &sorted_menu_titles); |
| + |
| + int index = 0; |
| + base::TimeTicks begin = base::TimeTicks::Now(); |
| + for (size_t i = 0; i < sorted_menu_titles.size(); ++i) { |
| + const std::string& id = map_ids[sorted_menu_titles[i]]; |
| + const MenuItem::ExtensionKey extension_key(id); |
| + extension_items_->AppendExtensionItems( |
| + extension_key, base::string16(), &index); |
| + } |
| + |
| + UMA_HISTOGRAM_TIMES("Extensions.ActionContextMenus_BuildTime", |
| + base::TimeTicks::Now() - begin); |
| + UMA_HISTOGRAM_COUNTS("Extensions.ActionContextMenus_ItemCount", index); |
| +} |