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

Unified Diff: chrome/browser/extensions/extension_context_menu_model.cc

Issue 359493005: Extend contextMenus API to support browser/page actions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed AppendExtensionItemsImpl, minor cleanup, comments Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
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 e06d3a73e5e092b3d8f38f4ca3f33468cee7d184..30f0c1d692990a1e7e7ba61cf0848976f30b3716 100644
--- a/chrome/browser/extensions/extension_context_menu_model.cc
+++ b/chrome/browser/extensions/extension_context_menu_model.cc
@@ -6,11 +6,15 @@
#include "base/prefs/pref_service.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"
@@ -20,7 +24,9 @@
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/web_contents.h"
+#include "content/public/common/context_menu_params.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 +38,35 @@ using content::OpenURLParams;
using content::Referrer;
using content::WebContents;
using extensions::Extension;
+using extensions::MenuItem;
+using extensions::MenuManager;
+
+namespace {
+
+// Returns true if the given |item| is for the given |extension| and |type|.
Yoyo Zhou 2014/07/15 23:35:00 nit: for -> of
gpdavis 2014/07/16 01:11:10 Done.
+bool MenuItemMatchesExtension(
+ ExtensionContextMenuModel::ActionType type,
+ const Extension* extension,
+ const MenuItem* item) {
+ if (type == ExtensionContextMenuModel::NO_ACTION ||
+ item->extension_id() != extension->id())
+ return false;
+
+ const MenuItem::ContextList& contexts = item->contexts();
+
+ 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 +75,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,16 +92,20 @@ 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;
}
bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
- const Extension* extension = this->GetExtension();
+ const Extension* extension = GetExtension();
if (!extension)
return false;
@@ -103,6 +143,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),
@@ -165,14 +216,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));
@@ -183,7 +248,52 @@ void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
}
const Extension* ExtensionContextMenuModel::GetExtension() const {
- ExtensionService* extension_service =
- extensions::ExtensionSystem::Get(profile_)->extension_service();
- return extension_service->GetExtensionById(extension_id_, false);
+ return extensions::ExtensionRegistry::Get(profile_)
+ ->enabled_extensions().GetByID(extension_id_);
+}
+
+void ExtensionContextMenuModel::AppendExtensionItems() {
+ extension_items_->Clear();
+
+ 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;
+ const extensions::ExtensionSet& enabled_extensions =
+ extensions::ExtensionRegistry::Get(profile_)->enabled_extensions();
+ for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin();
Yoyo Zhou 2014/07/15 23:35:00 I still don't understand why we're iterating over
gpdavis 2014/07/16 01:11:10 This is a really good point; I don't think I fully
+ iter != ids.end();
+ ++iter) {
+ if (!enabled_extensions.GetByID(iter->extension_id))
+ continue;
+ base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle(
+ *iter, base::string16());
+ 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;
+ for (std::vector<base::string16>::iterator iter = sorted_menu_titles.begin();
+ iter != sorted_menu_titles.end();
+ ++iter) {
+ const std::string& id = map_ids[*iter];
+ // To avoid clutter, do not include icons for extension menu items since
+ // only items for the attributed extension are added to the context menu.
+ extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(id),
+ base::string16(),
+ &index,
+ false); // include_icons
+ }
}

Powered by Google App Engine
This is Rietveld 408576698