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

Unified Diff: chrome/browser/context_menu/context_menu_content_type.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: Forgot to add context_menu_content_type_webview.* 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/context_menu/context_menu_content_type.cc
diff --git a/chrome/browser/context_menu/context_menu_content_type.cc b/chrome/browser/context_menu/context_menu_content_type.cc
new file mode 100644
index 0000000000000000000000000000000000000000..35a53ae10752ea6b00f6c95e94616922724f1857
--- /dev/null
+++ b/chrome/browser/context_menu/context_menu_content_type.cc
@@ -0,0 +1,172 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/context_menu/context_menu_content_type.h"
+
+#include "chrome/app/chrome_command_ids.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_io_data.h"
+#include "chrome/common/url_constants.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/url_constants.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/browser/process_manager.h"
+#include "extensions/common/extension.h"
+#include "third_party/WebKit/public/web/WebContextMenuData.h"
+
+
+using blink::WebContextMenuData;
+using content::RenderFrameHost;
+using content::WebContents;
+using extensions::Extension;
+
+namespace {
+
+bool IsDevToolsURL(const GURL& url) {
+ return url.SchemeIs(content::kChromeDevToolsScheme);
+}
+
+bool IsInternalResourcesURL(const GURL& url) {
+ if (!url.SchemeIs(content::kChromeUIScheme))
+ return false;
+ return url.host() == chrome::kChromeUISyncResourcesHost;
+}
+
+} // namespace
+
+ContextMenuContentType::ContextMenuContentType(
+ RenderFrameHost* render_frame_host,
+ const content::ContextMenuParams& params)
+ : params_(params),
+ source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
+ profile_(Profile::FromBrowserContext(
+ source_web_contents_->GetBrowserContext())),
+ has_custom_items_(false) {
+ Initialize();
+}
+
+ContextMenuContentType::~ContextMenuContentType() {
+}
+
+void ContextMenuContentType::Initialize() {
+ // Save it in |has_custom_items_| so we don't have to repeatedly call
+ // HasCustomItems().
+ has_custom_items_ = HasCustomItems(params_.custom_items);
+}
+
+const Extension* ContextMenuContentType::GetExtension() const {
+ extensions::ExtensionSystem* system =
+ extensions::ExtensionSystem::Get(profile_);
+ // There is no process manager in some tests.
+ if (!system->process_manager())
+ return NULL;
+
+ return system->process_manager()->GetExtensionForRenderViewHost(
+ source_web_contents_->GetRenderViewHost());
+}
+
+bool ContextMenuContentType::HasCustomItems(
+ const std::vector<content::MenuItem>& items) const {
+ for (size_t i = 0; i < items.size(); ++i) {
+ if (IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action >=
+ IDC_CONTENT_CONTEXT_CUSTOM_LAST) {
+ return false;
+ }
+ return true;
+ }
+ return false;
+}
+
+bool ContextMenuContentType::SupportsGroup(int group) {
+ // For menus with custom items, we do not show any other items other than
+ // developer items.
+ if (has_custom_items_) {
+ if (group == ITEM_GROUP_CUSTOM)
+ return true;
+
+ const bool has_selection = !params_.selection_text.empty();
+ if (!has_selection && !params_.custom_context.is_pepper_menu)
+ return group == ITEM_GROUP_DEVELOPER;
+
+ return false;
+ }
+
+ // Image menu item also implies "Search web for image" and printing.
+ if (group == ITEM_GROUP_SEARCHWEBFORIMAGE || group == ITEM_GROUP_PRINT)
+ return SupportsGroup(ITEM_GROUP_MEDIA_IMAGE);
+
+ return SupportsGroupInternal(group);
+}
+
+bool ContextMenuContentType::SupportsGroupInternal(int group) {
+ const bool has_link = !params_.unfiltered_link_url.is_empty();
+ const bool has_selection = !params_.selection_text.empty();
+
+ switch (group) {
+ case ITEM_GROUP_CUSTOM:
+ // Doesn't matter, we manually add this section.
+ return true;
+ case ITEM_GROUP_PAGE: {
+ bool is_candidate =
+ params_.media_type == WebContextMenuData::MediaTypeNone &&
+ !has_link && !params_.is_editable && !has_selection;
+
+ if (!is_candidate && params_.page_url.is_empty())
+ DCHECK(params_.frame_url.is_empty());
+
+ return is_candidate && !params_.page_url.is_empty() &&
+ !IsDevToolsURL(params_.page_url) &&
+ !IsInternalResourcesURL(params_.page_url);
+ }
+ case ITEM_GROUP_FRAME: {
+ bool page_category = SupportsGroupInternal(ITEM_GROUP_PAGE);
+ return page_category && !params_.frame_url.is_empty() &&
+ !IsDevToolsURL(params_.frame_url) &&
+ !IsInternalResourcesURL(params_.page_url);
+ }
+ case ITEM_GROUP_LINK:
+ return has_link;
+ case ITEM_GROUP_MEDIA_IMAGE:
+ case ITEM_GROUP_SEARCHWEBFORIMAGE:
+ return params_.media_type == WebContextMenuData::MediaTypeImage;
+ case ITEM_GROUP_MEDIA_VIDEO:
+ return params_.media_type == WebContextMenuData::MediaTypeVideo;
+ case ITEM_GROUP_MEDIA_AUDIO:
+ return params_.media_type == WebContextMenuData::MediaTypeAudio;
+ case ITEM_GROUP_MEDIA_PLUGIN:
+ return params_.media_type == WebContextMenuData::MediaTypePlugin;
+ case ITEM_GROUP_MEDIA_FILE:
+#ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
+ return params_.media_type == WebContextMenuData::MediaTypeFile;
+#endif
+ return false;
+ case ITEM_GROUP_EDITABLE:
+ return params_.is_editable;
+ case ITEM_GROUP_COPY:
+ return !params_.is_editable && has_selection;
+ case ITEM_GROUP_SEARCH_PROVIDER:
+ return has_selection;
+ case ITEM_GROUP_PRINT: {
+ bool enable = has_selection && !IsDevToolsURL(params_.page_url);
+ return enable || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
+ }
+ case ITEM_GROUP_ALL_EXTENSION:
+ return GetExtension() != NULL && !IsDevToolsURL(params_.page_url);
+ case ITEM_GROUP_CURRENT_EXTENSION:
+ return false;
+ case ITEM_GROUP_DEVELOPER:
+ return true;
+ case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
+ return false;
+ case ITEM_GROUP_PRINT_PREVIEW:
+#if defined(ENABLE_FULL_PRINTING)
+ return true;
+#else
+ return false
+#endif
+ }
+
+ NOTREACHED();
+ return false;
+}

Powered by Google App Engine
This is Rietveld 408576698