summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-29 09:43:12 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-29 09:43:12 +0000
commit0a83f14ac8f941c07617a011beba6bd8f3b63f58 (patch)
tree1fb5d1245160eb7aa140ade7c55456a774352c40 /components
parent108d19ac9a88192dff52a3b7a471d893d6943d24 (diff)
downloadchromium_src-0a83f14ac8f941c07617a011beba6bd8f3b63f58.zip
chromium_src-0a83f14ac8f941c07617a011beba6bd8f3b63f58.tar.gz
chromium_src-0a83f14ac8f941c07617a011beba6bd8f3b63f58.tar.bz2
1st step to C14N RendererContextMenu
* Move context_menu_delegate/renderer_view_context_menu_observer * RenderViewContextMenuProxy has been extracted from render_view_context_menu.h and moved to component directory. * Inline empty observer methods. * Reduce the use of Profile and replaced them with content::BrowserContext BUG=397320 TBR=sky@chromium.org Review URL: https://codereview.chromium.org/425493002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286149 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r--components/OWNERS4
-rw-r--r--components/components.gyp1
-rw-r--r--components/renderer_context_menu.gypi25
-rw-r--r--components/renderer_context_menu/DEPS3
-rw-r--r--components/renderer_context_menu/context_menu_delegate.cc41
-rw-r--r--components/renderer_context_menu/context_menu_delegate.h39
-rw-r--r--components/renderer_context_menu/render_view_context_menu_observer.cc17
-rw-r--r--components/renderer_context_menu/render_view_context_menu_observer.h107
-rw-r--r--components/renderer_context_menu/render_view_context_menu_proxy.h95
9 files changed, 332 insertions, 0 deletions
diff --git a/components/OWNERS b/components/OWNERS
index 48cd62d..e721131 100644
--- a/components/OWNERS
+++ b/components/OWNERS
@@ -137,6 +137,10 @@ per-file query_parser.gypi=sky@chromium.org
per-file rappor*=asvitkine@chromium.org
# OWNER-to-be: per-file rappor*=holte@chromium.org
+per-file renderer_context_menu*=avi@chromium.org
+per-file renderer_context_menu*=brettw@chromium.org
+per-file renderer_context_menu*=lazyboy@chromium.org
+
per-file search.gypi=kmadhusu@chromium.org
per-file search.gypi=brettw@chromium.org
per-file search.gypi=jered@chromium.org
diff --git a/components/components.gyp b/components/components.gyp
index c14de82..a62fa88 100644
--- a/components/components.gyp
+++ b/components/components.gyp
@@ -45,6 +45,7 @@
'pref_registry.gypi',
'query_parser.gypi',
'rappor.gypi',
+ 'renderer_context_menu.gypi',
'search.gypi',
'search_provider_logos.gypi',
'signin.gypi',
diff --git a/components/renderer_context_menu.gypi b/components/renderer_context_menu.gypi
new file mode 100644
index 0000000..64efd85
--- /dev/null
+++ b/components/renderer_context_menu.gypi
@@ -0,0 +1,25 @@
+# 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.
+{
+ 'targets': [
+ {
+ 'target_name': 'renderer_context_menu',
+ 'type': 'static_library',
+ 'dependencies': [
+ '../base/base.gyp:base',
+ '../content/content.gyp:content_browser',
+ ],
+ 'include_dirs': [
+ '..',
+ ],
+ 'sources': [
+ 'renderer_context_menu/context_menu_delegate.cc',
+ 'renderer_context_menu/context_menu_delegate.h',
+ 'renderer_context_menu/render_view_context_menu_observer.cc',
+ 'renderer_context_menu/render_view_context_menu_observer.h',
+ 'renderer_context_menu/render_view_context_menu_proxy.h',
+ ],
+ },
+ ],
+}
diff --git a/components/renderer_context_menu/DEPS b/components/renderer_context_menu/DEPS
new file mode 100644
index 0000000..1c35d9c
--- /dev/null
+++ b/components/renderer_context_menu/DEPS
@@ -0,0 +1,3 @@
+include_rules = [
+ "+content/public/browser",
+]
diff --git a/components/renderer_context_menu/context_menu_delegate.cc b/components/renderer_context_menu/context_menu_delegate.cc
new file mode 100644
index 0000000..c6129b8
--- /dev/null
+++ b/components/renderer_context_menu/context_menu_delegate.cc
@@ -0,0 +1,41 @@
+// 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_delegate.h"
+
+#include "content/public/browser/web_contents.h"
+
+namespace {
+
+const char kMenuDelegateUserDataKey[] = "RendererContextMenuMenuDelegate";
+
+class ContextMenuDelegateUserData : public base::SupportsUserData::Data {
+ public:
+ explicit ContextMenuDelegateUserData(ContextMenuDelegate* menu_delegate)
+ : menu_delegate_(menu_delegate) {}
+ virtual ~ContextMenuDelegateUserData() {}
+ ContextMenuDelegate* menu_delegate() { return menu_delegate_; }
+
+ private:
+ ContextMenuDelegate* menu_delegate_; // not owned by us.
+};
+
+} // namespace
+
+ContextMenuDelegate::ContextMenuDelegate(content::WebContents* web_contents) {
+ web_contents->SetUserData(&kMenuDelegateUserDataKey,
+ new ContextMenuDelegateUserData(this));
+}
+
+ContextMenuDelegate::~ContextMenuDelegate() {
+}
+
+// static
+ContextMenuDelegate* ContextMenuDelegate::FromWebContents(
+ content::WebContents* web_contents) {
+ ContextMenuDelegateUserData* user_data =
+ static_cast<ContextMenuDelegateUserData*>(
+ web_contents->GetUserData(&kMenuDelegateUserDataKey));
+ return user_data ? user_data->menu_delegate() : NULL;
+}
diff --git a/components/renderer_context_menu/context_menu_delegate.h b/components/renderer_context_menu/context_menu_delegate.h
new file mode 100644
index 0000000..57327b1
--- /dev/null
+++ b/components/renderer_context_menu/context_menu_delegate.h
@@ -0,0 +1,39 @@
+// 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_DELEGATE_H_
+#define COMPONENTS_RENDERER_CONTEXT_MENU_CONTEXT_MENU_DELEGATE_H_
+
+#include "base/memory/scoped_ptr.h"
+
+class RenderViewContextMenu;
+
+namespace content {
+class WebContents;
+struct ContextMenuParams;
+}
+
+// A ContextMenuDelegate can build and show renderer context menu.
+class ContextMenuDelegate {
+ public:
+ explicit ContextMenuDelegate(content::WebContents* web_contents);
+ virtual ~ContextMenuDelegate();
+
+ static ContextMenuDelegate* FromWebContents(
+ content::WebContents* web_contents);
+
+ // Builds and returns a context menu for a context specified by |params|.
+ // The returned value can be used to display the context menu.
+ virtual scoped_ptr<RenderViewContextMenu> BuildMenu(
+ content::WebContents* web_contents,
+ const content::ContextMenuParams& params) = 0;
+
+ // Displays the context menu.
+ virtual void ShowMenu(scoped_ptr<RenderViewContextMenu> menu) = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ContextMenuDelegate);
+};
+
+#endif // COMPONENTS_RENDERER_CONTEXT_MENU_CONTEXT_MENU_DELEGATE_H_
diff --git a/components/renderer_context_menu/render_view_context_menu_observer.cc b/components/renderer_context_menu/render_view_context_menu_observer.cc
new file mode 100644
index 0000000..2e2d05f9
--- /dev/null
+++ b/components/renderer_context_menu/render_view_context_menu_observer.cc
@@ -0,0 +1,17 @@
+// 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/render_view_context_menu_observer.h"
+
+bool RenderViewContextMenuObserver::IsCommandIdSupported(int command_id) {
+ return false;
+}
+
+bool RenderViewContextMenuObserver::IsCommandIdChecked(int command_id) {
+ return false;
+}
+
+bool RenderViewContextMenuObserver::IsCommandIdEnabled(int command_id) {
+ return false;
+}
diff --git a/components/renderer_context_menu/render_view_context_menu_observer.h b/components/renderer_context_menu/render_view_context_menu_observer.h
new file mode 100644
index 0000000..7fbca52
--- /dev/null
+++ b/components/renderer_context_menu/render_view_context_menu_observer.h
@@ -0,0 +1,107 @@
+// 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_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
+#define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
+
+namespace content {
+struct ContextMenuParams;
+}
+
+// The interface used for implementing context-menu items. The following
+// instruction describe how to implement a context-menu item with this
+// interface.
+//
+// 1. Add command IDs for the context-menu items to 'chrome_command_ids.h'.
+//
+// #define IDC_MY_COMMAND 99999
+//
+// 2. Add strings for the context-menu items to 'generated_sources.grd'.
+//
+// <message name="IDS_MY_COMMAND" desc="...">
+// My command
+// </message>
+//
+// 3. Create a class that implements this interface. (It is a good idea to use
+// the RenderViewContextMenuDelegate interface to avoid accessing the
+// RenderViewContextMenu class directly.)
+//
+// class MyMenuObserver : public RenderViewContextMenuObserver {
+// public:
+// MyMenuObserver(RenderViewContextMenuDelegate* d);
+// ~MyMenuObserver();
+//
+// virtual void InitMenu(const content::ContextMenuParams& params) OVERRIDE;
+// virtual bool IsCommandIdSupported(int command_id) OVERRIDE;
+// virtual bool IsCommandIdEnabled(int command_id) OVERRIDE;
+// virtual void ExecuteCommand(int command_id) OVERRIDE;
+//
+// private:
+// RenderViewContextMenuDelgate* delegate_;
+// }
+//
+// void MyMenuObserver::InitMenu(const content::ContextMenuParams& params) {
+// delegate_->AddMenuItem(IDC_MY_COMMAND,...);
+// }
+//
+// bool MyMenuObserver::IsCommandIdSupported(int command_id) {
+// return command_id == IDC_MY_COMMAND;
+// }
+//
+// bool MyMenuObserver::IsCommandIdEnabled(int command_id) {
+// DCHECK(command_id == IDC_MY_COMMAND);
+// return true;
+// }
+//
+// void MyMenuObserver::ExecuteCommand(int command_id) {
+// DCHECK(command_id == IDC_MY_COMMAND);
+// }
+//
+// 4. Add this observer class to the RenderViewContextMenu class. (It is good
+// to use scoped_ptr<> so Chrome can create its instances only when it needs.)
+//
+// class RenderViewContextMenu {
+// ...
+// private:
+// scoped_ptr<MyMenuObserver> my_menu_observer_;
+// };
+//
+// 5. Create its instance in InitMenu() and add it to the observer list of the
+// RenderViewContextMenu class.
+//
+// void RenderViewContextMenu::InitMenu() {
+// ...
+// my_menu_observer_.reset(new MyMenuObserver(this));
+// observers_.AddObserver(my_menu_observer_.get());
+// }
+//
+//
+class RenderViewContextMenuObserver {
+ public:
+ virtual ~RenderViewContextMenuObserver() {}
+
+ // Called when the RenderViewContextMenu class initializes a context menu. We
+ // usually call RenderViewContextMenuDelegate::AddMenuItem() to add menu items
+ // in this function.
+ virtual void InitMenu(const content::ContextMenuParams& params) {}
+
+ // Called when the RenderViewContextMenu class asks whether an observer
+ // listens for the specified command ID. If this function returns true, the
+ // RenderViewContextMenu class calls IsCommandIdEnabled() or ExecuteCommand().
+ virtual bool IsCommandIdSupported(int command_id);
+
+ // Called when the RenderViewContextMenu class sets the initial status of the
+ // specified context-menu item. If we need to enable or disable a context-menu
+ // item while showing, use RenderViewContextMenuDelegate::UpdateMenuItem().
+ virtual bool IsCommandIdChecked(int command_id);
+ virtual bool IsCommandIdEnabled(int command_id);
+
+ // Called when a user selects the specified context-menu item.
+ virtual void ExecuteCommand(int command_id) {}
+
+ // Called when a user closes the context menu without selecting any items.
+ virtual void OnMenuCancel() {}
+};
+
+#endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
diff --git a/components/renderer_context_menu/render_view_context_menu_proxy.h b/components/renderer_context_menu/render_view_context_menu_proxy.h
new file mode 100644
index 0000000..789b149
--- /dev/null
+++ b/components/renderer_context_menu/render_view_context_menu_proxy.h
@@ -0,0 +1,95 @@
+// 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_RENDER_VIEW_CONTEXT_MENU_PROXY_H_
+#define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_
+
+#include "base/strings/string16.h"
+
+namespace content {
+class BrowserContext;
+class RenderViewHost;
+class WebContents;
+}
+
+// An interface that controls a RenderViewContextMenu instance from observers.
+// This interface is designed mainly for controlling the instance while showing
+// so we can add a context-menu item that takes long time to create its text,
+// such as retrieving the item text from a server. The simplest usage is:
+// 1. Adding an item with temporary text;
+// 2. Posting a background task that creates the item text, and;
+// 3. Calling UpdateMenuItem() in the callback function.
+// The following snippet describes the simple usage that updates a context-menu
+// item with this interface.
+//
+// class MyTask : public net::URLFetcherDelegate {
+// public:
+// MyTask(RenderViewContextMenuProxy* proxy, int id)
+// : proxy_(proxy),
+// id_(id) {
+// }
+// virtual ~MyTask() {
+// }
+// virtual void OnURLFetchComplete(const net::URLFetcher* source,
+// const GURL& url,
+// const net::URLRequestStatus& status,
+// int response,
+// const net::ResponseCookies& cookies,
+// const std::string& data) {
+// bool enabled = response == 200;
+// const char* text = enabled ? "OK" : "ERROR";
+// proxy_->UpdateMenuItem(id_, enabled, base::ASCIIToUTF16(text));
+// }
+// void Start(const GURL* url, net::URLRequestContextGetter* context) {
+// fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
+// fetcher_->SetRequestContext(context);
+// content::AssociateURLFetcherWithRenderView(
+// fetcher_.get(),
+// proxy_->GetRenderViewHost()->GetSiteInstance()->GetSite(),
+// proxy_->GetRenderViewHost()->GetProcess()->GetID(),
+// proxy_->GetRenderViewHost()->GetRoutingID());
+// fetcher_->Start();
+// }
+//
+// private:
+// URLFetcher fetcher_;
+// RenderViewContextMenuProxy* proxy_;
+// int id_;
+// };
+//
+// void RenderViewContextMenu::AppendEditableItems() {
+// // Add a menu item with temporary text shown while we create the final
+// // text.
+// menu_model_.AddItemWithStringId(IDC_MY_ITEM, IDC_MY_TEXT);
+//
+// // Start a task that creates the final text.
+// my_task_ = new MyTask(this, IDC_MY_ITEM);
+// my_task_->Start(...);
+// }
+//
+class RenderViewContextMenuProxy {
+ public:
+ // Add a menu item to a context menu.
+ virtual void AddMenuItem(int command_id, const base::string16& title) = 0;
+ virtual void AddCheckItem(int command_id, const base::string16& title) = 0;
+ virtual void AddSeparator() = 0;
+
+ // Add a submenu item to a context menu.
+ virtual void AddSubMenu(int command_id,
+ const base::string16& label,
+ ui::MenuModel* model) = 0;
+
+ // Update the status and text of the specified context-menu item.
+ virtual void UpdateMenuItem(int command_id,
+ bool enabled,
+ bool hidden,
+ const base::string16& title) = 0;
+
+ // Retrieve the given associated objects with a context menu.
+ virtual content::RenderViewHost* GetRenderViewHost() const = 0;
+ virtual content::WebContents* GetWebContents() const = 0;
+ virtual content::BrowserContext* GetBrowserContext() const = 0;
+};
+
+#endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_