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

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: Minor cleanup 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..971bdbcf4b56df5d2b5781762b456a0097e30052 100644
--- a/chrome/browser/extensions/extension_context_menu_model.cc
+++ b/chrome/browser/extensions/extension_context_menu_model.cc
@@ -5,12 +5,17 @@
#include "chrome/browser/extensions/extension_context_menu_model.h"
#include "base/prefs/pref_service.h"
+#include "base/strings/string_util.h"
Devlin 2014/07/09 22:42:59 Pretty sure you don't need this.
gpdavis 2014/07/10 00:22:25 Done.
#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 +25,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 +39,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|.
+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 +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),
@@ -165,14 +217,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));
@@ -187,3 +253,46 @@ const Extension* ExtensionContextMenuModel::GetExtension() const {
extensions::ExtensionSystem::Get(profile_)->extension_service();
Devlin 2014/07/09 22:42:59 Not your code, but since you're here, can you upda
gpdavis 2014/07/10 00:22:25 Done.
return extension_service->GetExtensionById(extension_id_, false);
}
+
+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();
+ iter != ids.end();
+ ++iter) {
+ const Extension* extension = enabled_extensions.GetByID(iter->extension_id);
+ if (extension) {
+ 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;
+ std::vector<base::string16>::iterator it;
Devlin 2014/07/09 22:42:59 scope the iterator with the for-loop.
gpdavis 2014/07/10 00:22:25 Done.
+ for (it = sorted_menu_titles.begin(); it != sorted_menu_titles.end(); ++it) {
+ const std::string& id = map_ids[*it];
+ const MenuItem::ExtensionKey extension_key(id);
Devlin 2014/07/09 22:42:59 You can inline extension_key.
gpdavis 2014/07/10 00:22:25 Done.
+ extension_items_->AppendExtensionItems(
+ extension_key, base::string16(), &index);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698