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

Unified Diff: chrome/browser/tab_contents/render_view_context_menu.cc

Issue 169093009: Separate out logic to handle different category/group of context menu items from RVContextMenu class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@work-mmm-refactor1
Patch Set: fix incorrect dcheck + platform app logic Created 6 years, 10 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/tab_contents/render_view_context_menu.cc
diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc
index 040fb413454cb059e16af3f93730e32da2e70984..3b7d1580b20822dbe027c16c61f0ca3966d68c4f 100644
--- a/chrome/browser/tab_contents/render_view_context_menu.cc
+++ b/chrome/browser/tab_contents/render_view_context_menu.cc
@@ -47,6 +47,7 @@
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
+#include "chrome/browser/tab_contents/menu_category_factory.h"
#include "chrome/browser/tab_contents/retargeting_details.h"
#include "chrome/browser/tab_contents/spellchecker_submenu_observer.h"
#include "chrome/browser/tab_contents/spelling_menu_observer.h"
@@ -414,11 +415,9 @@ RenderViewContextMenu::RenderViewContextMenu(
protocol_handler_submenu_model_(this),
protocol_handler_registry_(
ProtocolHandlerRegistryFactory::GetForProfile(profile_)),
- command_executed_(false),
- is_guest_(false) {
- RenderViewHost* rvh = source_web_contents_->GetRenderViewHost();
- if (rvh && rvh->GetProcess()->IsGuest())
- is_guest_ = true;
+ command_executed_(false) {
+ menu_category_.reset(MenuCategoryFactory::Create(source_web_contents_,
+ render_frame_host, params));
}
RenderViewContextMenu::~RenderViewContextMenu() {
@@ -562,120 +561,85 @@ void RenderViewContextMenu::AppendAllExtensionItems() {
UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index);
}
-void RenderViewContextMenu::InitMenu() {
- if (chrome::IsRunningInForcedAppMode()) {
- AppendAppModeItems();
- return;
- }
-
- extensions::ViewType view_type =
- extensions::GetViewType(source_web_contents_);
- if (view_type == extensions::VIEW_TYPE_APP_WINDOW) {
- AppendPlatformAppItems();
- return;
- } else if (view_type == extensions::VIEW_TYPE_EXTENSION_POPUP) {
- AppendPopupExtensionItems();
- return;
- } else if (view_type == extensions::VIEW_TYPE_PANEL) {
- AppendPanelItems();
- return;
- }
-
- const bool has_link = !params_.unfiltered_link_url.is_empty();
- const bool has_selection = !params_.selection_text.empty();
-
- if (AppendCustomItems()) {
- // If there's a selection, don't early return when there are custom items,
- // but fall through to adding the normal ones after the custom ones.
- if (has_selection) {
- menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
- } else {
- // Don't add items for Pepper menu.
- if (!params_.custom_context.is_pepper_menu)
- AppendDeveloperItems();
- return;
- }
+void RenderViewContextMenu::AppendCurrentExtensionItems() {
+ // Avoid appending extension related items when |extension| is null. This
+ // happens when the panel is navigated to a url outside of the extension's
+ // package.
+ const Extension* extension = GetExtension();
+ if (extension) {
+ // Only add extension items from this extension.
+ int index = 0;
+ extension_items_.AppendExtensionItems(extension->id(),
+ PrintableSelectionText(), &index);
}
+}
- // When no special node or text is selected and selection has no link,
- // show page items.
- if (params_.media_type == WebContextMenuData::MediaTypeNone &&
- !has_link &&
- !params_.is_editable &&
- !is_guest_ &&
- !has_selection) {
- if (!params_.page_url.is_empty()) {
- bool is_devtools = IsDevToolsURL(params_.page_url);
- if (!is_devtools && !IsInternalResourcesURL(params_.page_url)) {
- AppendPageItems();
- // Merge in frame items if we clicked within a frame that needs them.
- if (!params_.frame_url.is_empty()) {
- is_devtools = IsDevToolsURL(params_.frame_url);
- if (!is_devtools && !IsInternalResourcesURL(params_.frame_url)) {
- menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
- AppendFrameItems();
- }
- }
- }
- } else {
- DCHECK(params_.frame_url.is_empty());
- }
+void RenderViewContextMenu::InitMenu() {
+ int items = menu_category_->GetCategories();
+
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_CUSTOM))
Fady Samuel 2014/02/24 19:07:29 I would prefer this syntax: if (context_->Support
lazyboy 2014/02/24 23:45:00 Done.
+ AppendCustomItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_PAGE))
+ AppendPageItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_FRAME)) {
+ DCHECK(items & MenuCategoryBase::ITEM_CATEGORY_PAGE);
+ AppendFrameItems();
}
-
- // Do not show link related items for guest.
- if (has_link && !is_guest_) {
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_LINK)) {
AppendLinkItems();
if (params_.media_type != WebContextMenuData::MediaTypeNone)
menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
}
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_MEDIA_IMAGE))
+ AppendImageItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_SEARCHWEBFORIMAGE))
+ AppendSearchWebForImageItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_MEDIA_VIDEO))
+ AppendVideoItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_MEDIA_AUDIO))
+ AppendAudioItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_MEDIA_PLUGIN))
+ AppendPluginItems();
+ // if (items & (1 << ITEM_CATEGORY_MEDIA_FILE)) ...
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_EDITABLE))
+ AppendEditableItems();
- switch (params_.media_type) {
- case WebContextMenuData::MediaTypeNone:
- break;
- case WebContextMenuData::MediaTypeImage:
- AppendImageItems();
- break;
- case WebContextMenuData::MediaTypeVideo:
- AppendVideoItems();
- break;
- case WebContextMenuData::MediaTypeAudio:
- AppendAudioItems();
- break;
- case WebContextMenuData::MediaTypePlugin:
- AppendPluginItems();
- break;
-#ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
- case WebContextMenuData::MediaTypeFile:
- break;
-#endif
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_COPY)) {
+ DCHECK(!(items & (1 << MenuCategoryBase::ITEM_CATEGORY_EDITABLE)));
+ AppendCopyItem();
}
- if (params_.is_editable)
- AppendEditableItems();
- else if (has_selection)
- AppendCopyItem();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_PRINT))
+ AppendPrintItem();
- if (!is_guest_ && has_selection) {
- AppendSearchProvider();
- if (!IsDevToolsURL(params_.page_url))
- AppendPrintItem();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_ALL_EXTENSION)) {
+ DCHECK(!(items & (1 << MenuCategoryBase::ITEM_CATEGORY_CURRENT_EXTENSION)));
+ AppendAllExtensionItems();
+ }
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_CURRENT_EXTENSION)) {
+ DCHECK(!(items & (1 << MenuCategoryBase::ITEM_CATEGORY_ALL_EXTENSION)));
+ AppendCurrentExtensionItems();
}
- if (!IsDevToolsURL(params_.page_url) && !is_guest_)
- AppendAllExtensionItems();
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_DEVELOPER))
+ AppendDeveloperItems();
- AppendDeveloperItems();
+ if (items &(1 << MenuCategoryBase::ITEM_CATEGORY_DEVTOOLS_UNPACKED_EXT))
+ AppendDevtoolsForUnpackedExtensions();
+
+ if (items & (1 << MenuCategoryBase::ITEM_CATEGORY_PRINT_PREVIEW))
+ AppendPrintPreviewItems();
+}
- if (!is_guest_) {
+void RenderViewContextMenu::AppendPrintPreviewItems() {
#if defined(ENABLE_FULL_PRINTING)
- if (!print_preview_menu_observer_.get()) {
- print_preview_menu_observer_.reset(
- new PrintPreviewContextMenuObserver(source_web_contents_));
- }
+ if (!print_preview_menu_observer_.get()) {
+ print_preview_menu_observer_.reset(
+ new PrintPreviewContextMenuObserver(source_web_contents_));
+ }
- observers_.AddObserver(print_preview_menu_observer_.get());
+ observers_.AddObserver(print_preview_menu_observer_.get());
#endif
- }
}
const Extension* RenderViewContextMenu::GetExtension() const {
@@ -689,93 +653,6 @@ const Extension* RenderViewContextMenu::GetExtension() const {
source_web_contents_->GetRenderViewHost());
}
-void RenderViewContextMenu::AppendAppModeItems() {
- const bool has_selection = !params_.selection_text.empty();
-
- if (params_.is_editable)
- AppendEditableItems();
- else if (has_selection)
- AppendCopyItem();
-}
-
-void RenderViewContextMenu::AppendPlatformAppItems() {
- const Extension* platform_app = GetExtension();
-
- // The RVH might be for a process sandboxed from the extension.
- if (!platform_app)
- return;
-
- DCHECK(platform_app->is_platform_app());
-
- const bool has_selection = !params_.selection_text.empty();
-
- // Add undo/redo, cut/copy/paste etc for text fields.
- if (params_.is_editable)
- AppendEditableItems();
- else if (has_selection)
- AppendCopyItem();
-
- int index = 0;
- extension_items_.AppendExtensionItems(platform_app->id(),
- PrintableSelectionText(), &index);
-
- // Add dev tools for unpacked extensions.
- if (extensions::Manifest::IsUnpackedLocation(platform_app->location()) ||
- CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kDebugPackedApps)) {
- // Add a separator if there are any items already in the menu.
- menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
-
- menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP,
- IDS_CONTENT_CONTEXT_RELOAD_PACKAGED_APP);
- menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP,
- IDS_CONTENT_CONTEXT_RESTART_APP);
- AppendDeveloperItems();
- menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE,
- IDS_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE);
- }
-}
-
-void RenderViewContextMenu::AppendPopupExtensionItems() {
- const bool has_selection = !params_.selection_text.empty();
-
- if (params_.is_editable)
- AppendEditableItems();
- else if (has_selection)
- AppendCopyItem();
-
- if (has_selection)
- AppendSearchProvider();
-
- AppendAllExtensionItems();
- AppendDeveloperItems();
-}
-
-void RenderViewContextMenu::AppendPanelItems() {
- bool has_selection = !params_.selection_text.empty();
-
- // Checking link should take precedence before checking selection since on Mac
- // right-clicking a link will also make it selected.
- if (params_.unfiltered_link_url.is_valid())
- AppendLinkItems();
-
- if (params_.is_editable)
- AppendEditableItems();
- else if (has_selection)
- AppendCopyItem();
-
- // Avoid appending extension related items when |extension| is null. This
- // happens when the panel is navigated to a url outside of the extension's
- // package.
- const Extension* extension = GetExtension();
- if (extension) {
- // Only add extension items from this extension.
- int index = 0;
- extension_items_.AppendExtensionItems(extension->id(),
- PrintableSelectionText(), &index);
- }
-}
-
void RenderViewContextMenu::AddMenuItem(int command_id,
const base::string16& title) {
menu_model_.AddItem(command_id, title);
@@ -842,6 +719,19 @@ void RenderViewContextMenu::AppendDeveloperItems() {
IDS_CONTENT_CONTEXT_INSPECTELEMENT);
}
+void RenderViewContextMenu::AppendDevtoolsForUnpackedExtensions() {
+ // Add a separator if there are any items already in the menu.
+ menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
+
+ menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP,
+ IDS_CONTENT_CONTEXT_RELOAD_PACKAGED_APP);
+ menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP,
+ IDS_CONTENT_CONTEXT_RESTART_APP);
+ AppendDeveloperItems();
+ menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE,
+ IDS_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE);
+}
+
void RenderViewContextMenu::AppendLinkItems() {
if (!params_.link_url.is_empty()) {
menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
@@ -874,10 +764,13 @@ void RenderViewContextMenu::AppendImageItems() {
IDS_CONTENT_CONTEXT_COPYIMAGE);
menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB,
IDS_CONTENT_CONTEXT_OPENIMAGENEWTAB);
+}
+
+void RenderViewContextMenu::AppendSearchWebForImageItems() {
const TemplateURL* const default_provider =
TemplateURLServiceFactory::GetForProfile(profile_)->
GetDefaultSearchProvider();
- if (!is_guest_ && params_.has_image_contents && default_provider &&
+ if (params_.has_image_contents && default_provider &&
!default_provider->image_url().empty() &&
default_provider->image_url_ref().IsValid()) {
menu_model_.AddItem(
@@ -885,7 +778,6 @@ void RenderViewContextMenu::AppendImageItems() {
l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFORIMAGE,
default_provider->short_name()));
}
- AppendPrintItem();
}
void RenderViewContextMenu::AppendAudioItems() {

Powered by Google App Engine
This is Rietveld 408576698