summaryrefslogtreecommitdiffstats
path: root/components/renderer_context_menu
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-31 20:52:54 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-31 20:52:54 +0000
commit9dabe1df0d6aad616b7d20549455ba548f485a27 (patch)
tree7561cf91c5b77ebf2de21362bc58ec867ca90494 /components/renderer_context_menu
parent975f4e7b80873a0e60e43bdf4c5dc6251360afc8 (diff)
downloadchromium_src-9dabe1df0d6aad616b7d20549455ba548f485a27.zip
chromium_src-9dabe1df0d6aad616b7d20549455ba548f485a27.tar.gz
chromium_src-9dabe1df0d6aad616b7d20549455ba548f485a27.tar.bz2
Move ContextMenuContentType to components/renderer_context_menu
BUG=397320 R=lazyboy@chromium.org TBR=fsamuel@chromium.org,darin@chromium.org Review URL: https://codereview.chromium.org/426363003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286889 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/renderer_context_menu')
-rw-r--r--components/renderer_context_menu/DEPS5
-rw-r--r--components/renderer_context_menu/context_menu_content_type.cc180
-rw-r--r--components/renderer_context_menu/context_menu_content_type.h89
3 files changed, 274 insertions, 0 deletions
diff --git a/components/renderer_context_menu/DEPS b/components/renderer_context_menu/DEPS
index 1c35d9c..49e84e6 100644
--- a/components/renderer_context_menu/DEPS
+++ b/components/renderer_context_menu/DEPS
@@ -1,3 +1,8 @@
include_rules = [
"+content/public/browser",
+ "+content/public/common",
+ "+extensions/browser",
+ "+extensions/common",
+ "+ui/base",
+ "+third_party/WebKit/public/web",
]
diff --git a/components/renderer_context_menu/context_menu_content_type.cc b/components/renderer_context_menu/context_menu_content_type.cc
new file mode 100644
index 0000000..4b5d4b9
--- /dev/null
+++ b/components/renderer_context_menu/context_menu_content_type.cc
@@ -0,0 +1,180 @@
+// 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 "components/renderer_context_menu/context_menu_content_type.h"
+
+#include "base/bind.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::WebContents;
+using extensions::Extension;
+
+namespace {
+
+bool IsDevToolsURL(const GURL& url) {
+ return url.SchemeIs(content::kChromeDevToolsScheme);
+}
+
+bool DefaultIsInternalResourcesURL(const GURL& url) {
+ return url.SchemeIs(content::kChromeUIScheme);
+}
+
+} // namespace
+
+ContextMenuContentType::ContextMenuContentType(
+ content::WebContents* web_contents,
+ const content::ContextMenuParams& params,
+ bool supports_custom_items)
+ : params_(params),
+ source_web_contents_(web_contents),
+ supports_custom_items_(supports_custom_items),
+ internal_resources_url_checker_(
+ base::Bind(&DefaultIsInternalResourcesURL)) {
+}
+
+ContextMenuContentType::~ContextMenuContentType() {
+}
+
+const Extension* ContextMenuContentType::GetExtension() const {
+ extensions::ExtensionSystem* system = extensions::ExtensionSystem::Get(
+ source_web_contents_->GetBrowserContext());
+ // 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::SupportsGroup(int group) {
+ const bool has_selection = !params_.selection_text.empty();
+
+ if (supports_custom_items_ && !params_.custom_items.empty()) {
+ if (group == ITEM_GROUP_CUSTOM)
+ return true;
+
+ if (!has_selection) {
+ // For menus with custom items, if there is no selection, we do not
+ // add items other than developer items. And for Pepper menu, don't even
+ // add developer items.
+ if (!params_.custom_context.is_pepper_menu)
+ return group == ITEM_GROUP_DEVELOPER;
+
+ return false;
+ }
+
+ // If there's a selection when there are custom items, fall through to
+ // adding the normal ones after the custom ones.
+ }
+
+ 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:
+ return supports_custom_items_ && !params_.custom_items.empty();
+
+ 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_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE);
+ return page_group_supported && !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:
+ return params_.media_type == WebContextMenuData::MediaTypeImage;
+
+ case ITEM_GROUP_SEARCHWEBFORIMAGE:
+ // Image menu items imply search web for image item.
+ return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
+
+ 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_CANVAS:
+ return params_.media_type == WebContextMenuData::MediaTypeCanvas;
+
+ case ITEM_GROUP_MEDIA_PLUGIN:
+ return params_.media_type == WebContextMenuData::MediaTypePlugin;
+
+ case ITEM_GROUP_MEDIA_FILE:
+#if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED)
+ return params_.media_type == WebContextMenuData::MediaTypeFile;
+#else
+ return false;
+#endif
+
+ 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);
+ // Image menu items also imply print items.
+ return enable || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
+ }
+
+ case ITEM_GROUP_ALL_EXTENSION:
+ return !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
+
+ default:
+ NOTREACHED();
+ return false;
+ }
+}
+
+bool ContextMenuContentType::IsInternalResourcesURL(const GURL& url) {
+ return internal_resources_url_checker_.Run(url);
+}
diff --git a/components/renderer_context_menu/context_menu_content_type.h b/components/renderer_context_menu/context_menu_content_type.h
new file mode 100644
index 0000000..81003dd
--- /dev/null
+++ b/components/renderer_context_menu/context_menu_content_type.h
@@ -0,0 +1,89 @@
+// 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.
+
+#ifndef COMPONENTS_RENDERER_CONTEXT_MENU_CONTEXT_MENU_CONTENT_TYPE_H_
+#define COMPONENTS_RENDERER_CONTEXT_MENU_CONTEXT_MENU_CONTENT_TYPE_H_
+
+#include "base/callback.h"
+#include "content/public/common/context_menu_params.h"
+#include "ui/base/models/simple_menu_model.h"
+
+namespace content {
+class WebContents;
+}
+
+namespace extensions {
+class Extension;
+}
+
+// ContextMenuContentType is a helper to decide which category/group of items
+// are relevant for a given WebContents and a context.
+//
+// Subclasses can override the behavior of showing/hiding a category.
+class ContextMenuContentType {
+ public:
+ virtual ~ContextMenuContentType();
+
+ // Represents a group of menu items.
+ // Order matters as they are appended in the enum order.
+ enum ItemGroup {
+ ITEM_GROUP_CUSTOM,
+ ITEM_GROUP_PAGE,
+ ITEM_GROUP_FRAME,
+ ITEM_GROUP_LINK,
+ ITEM_GROUP_MEDIA_IMAGE,
+ ITEM_GROUP_SEARCHWEBFORIMAGE,
+ ITEM_GROUP_MEDIA_VIDEO,
+ ITEM_GROUP_MEDIA_AUDIO,
+ ITEM_GROUP_MEDIA_CANVAS,
+ ITEM_GROUP_MEDIA_PLUGIN,
+ ITEM_GROUP_MEDIA_FILE,
+ ITEM_GROUP_EDITABLE,
+ ITEM_GROUP_COPY,
+ ITEM_GROUP_SEARCH_PROVIDER,
+ ITEM_GROUP_PRINT,
+ ITEM_GROUP_ALL_EXTENSION,
+ ITEM_GROUP_CURRENT_EXTENSION,
+ ITEM_GROUP_DEVELOPER,
+ ITEM_GROUP_DEVTOOLS_UNPACKED_EXT,
+ ITEM_GROUP_PRINT_PREVIEW
+ };
+
+ typedef base::Callback<bool (const GURL& url)>
+ InternalResourcesURLChecker;
+
+ void set_internal_resources_url_checker(
+ const InternalResourcesURLChecker& checker) {
+ internal_resources_url_checker_ = checker;
+ }
+
+ // Returns if |group| is enabled.
+ virtual bool SupportsGroup(int group);
+
+ ContextMenuContentType(content::WebContents* web_contents,
+ const content::ContextMenuParams& params,
+ bool supports_custom_items);
+
+ protected:
+ const content::ContextMenuParams& params() const { return params_; }
+
+ const extensions::Extension* GetExtension() const;
+
+ private:
+ bool SupportsGroupInternal(int group);
+
+ bool IsInternalResourcesURL(const GURL& url);
+
+ const content::ContextMenuParams params_;
+ content::WebContents* source_web_contents_;
+ const bool supports_custom_items_;
+
+ // A boolean callback to check if the url points to the internal
+ // resources.
+ InternalResourcesURLChecker internal_resources_url_checker_;
+
+ DISALLOW_COPY_AND_ASSIGN(ContextMenuContentType);
+};
+
+#endif // COMPONENTS_RENDERER_CONTEXT_MENU_CONTEXT_MENU_CONTENT_TYPE_H_