1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_
6 #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_
8 #include "base/strings/string16.h"
16 // An interface that controls a RenderViewContextMenu instance from observers.
17 // This interface is designed mainly for controlling the instance while showing
18 // so we can add a context-menu item that takes long time to create its text,
19 // such as retrieving the item text from a server. The simplest usage is:
20 // 1. Adding an item with temporary text;
21 // 2. Posting a background task that creates the item text, and;
22 // 3. Calling UpdateMenuItem() in the callback function.
23 // The following snippet describes the simple usage that updates a context-menu
24 // item with this interface.
26 // class MyTask : public net::URLFetcherDelegate {
28 // MyTask(RenderViewContextMenuProxy* proxy, int id)
32 // virtual ~MyTask() {
34 // virtual void OnURLFetchComplete(const net::URLFetcher* source,
36 // const net::URLRequestStatus& status,
38 // const net::ResponseCookies& cookies,
39 // const std::string& data) {
40 // bool enabled = response == 200;
41 // const char* text = enabled ? "OK" : "ERROR";
42 // proxy_->UpdateMenuItem(id_, enabled, base::ASCIIToUTF16(text));
44 // void Start(const GURL* url, net::URLRequestContextGetter* context) {
45 // fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
46 // fetcher_->SetRequestContext(context);
47 // content::AssociateURLFetcherWithRenderView(
49 // proxy_->GetRenderViewHost()->GetSiteInstance()->GetSite(),
50 // proxy_->GetRenderViewHost()->GetProcess()->GetID(),
51 // proxy_->GetRenderViewHost()->GetRoutingID());
56 // URLFetcher fetcher_;
57 // RenderViewContextMenuProxy* proxy_;
61 // void RenderViewContextMenu::AppendEditableItems() {
62 // // Add a menu item with temporary text shown while we create the final
64 // menu_model_.AddItemWithStringId(IDC_MY_ITEM, IDC_MY_TEXT);
66 // // Start a task that creates the final text.
67 // my_task_ = new MyTask(this, IDC_MY_ITEM);
68 // my_task_->Start(...);
71 class RenderViewContextMenuProxy
{
73 // Add a menu item to a context menu.
74 virtual void AddMenuItem(int command_id
, const base::string16
& title
) = 0;
75 virtual void AddCheckItem(int command_id
, const base::string16
& title
) = 0;
76 virtual void AddSeparator() = 0;
78 // Add a submenu item to a context menu.
79 virtual void AddSubMenu(int command_id
,
80 const base::string16
& label
,
81 ui::MenuModel
* model
) = 0;
83 // Update the status and text of the specified context-menu item.
84 virtual void UpdateMenuItem(int command_id
,
87 const base::string16
& title
) = 0;
89 // Retrieve the given associated objects with a context menu.
90 virtual content::RenderViewHost
* GetRenderViewHost() const = 0;
91 virtual content::WebContents
* GetWebContents() const = 0;
92 virtual content::BrowserContext
* GetBrowserContext() const = 0;
95 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_