Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / renderer_context_menu / render_view_context_menu_base.h
blobe11628fd434d22d25aa46f2151d1b2336389760e
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_BASE_H_
6 #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_
8 #include <map>
9 #include <string>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/strings/string16.h"
14 #include "components/renderer_context_menu/context_menu_content_type.h"
15 #include "components/renderer_context_menu/render_view_context_menu_observer.h"
16 #include "components/renderer_context_menu/render_view_context_menu_proxy.h"
17 #include "content/public/common/context_menu_params.h"
18 #include "ui/base/models/simple_menu_model.h"
19 #include "ui/base/page_transition_types.h"
20 #include "ui/base/window_open_disposition.h"
22 namespace content {
23 class RenderFrameHost;
24 class WebContents;
27 namespace gfx {
28 class Point;
31 namespace blink {
32 struct WebMediaPlayerAction;
33 struct WebPluginAction;
36 class RenderViewContextMenuBase : public ui::SimpleMenuModel::Delegate,
37 public RenderViewContextMenuProxy {
38 public:
39 // A delegate interface to communicate with the toolkit used by
40 // the embedder.
41 class ToolkitDelegate {
42 public:
43 virtual ~ToolkitDelegate() {}
44 // Initialize the toolkit's menu.
45 virtual void Init(ui::SimpleMenuModel* menu_model) = 0;
47 virtual void Cancel() = 0;
49 // Updates the actual menu items controlled by the toolkit.
50 virtual void UpdateMenuItem(int command_id,
51 bool enabled,
52 bool hidden,
53 const base::string16& title) = 0;
56 static const size_t kMaxSelectionTextLength;
58 static void SetContentCustomCommandIdRange(int first, int last);
60 // Convert a command ID so that it fits within the range for
61 // content context menu.
62 static int ConvertToContentCustomCommandId(int id);
64 // True if the given id is the one generated for content context menu.
65 static bool IsContentCustomCommandId(int id);
67 RenderViewContextMenuBase(content::RenderFrameHost* render_frame_host,
68 const content::ContextMenuParams& params);
70 ~RenderViewContextMenuBase() override;
72 // Displays the menu.
73 // Different platform will have their own implementation.
74 virtual void Show() = 0;
76 // Initializes the context menu.
77 void Init();
79 // Programmatically closes the context menu.
80 void Cancel();
82 const ui::SimpleMenuModel& menu_model() const { return menu_model_; }
83 const content::ContextMenuParams& params() const { return params_; }
85 // Returns true if the specified command id is known and valid for
86 // this menu. If the command is known |enabled| is set to indicate
87 // if the command is enabled.
88 bool IsCommandIdKnown(int command_id, bool* enabled) const;
90 // SimpleMenuModel::Delegate implementation.
91 bool IsCommandIdChecked(int command_id) const override;
92 void ExecuteCommand(int command_id, int event_flags) override;
93 void MenuWillShow(ui::SimpleMenuModel* source) override;
94 void MenuClosed(ui::SimpleMenuModel* source) override;
96 // RenderViewContextMenuProxy implementation.
97 void AddMenuItem(int command_id, const base::string16& title) override;
98 void AddCheckItem(int command_id, const base::string16& title) override;
99 void AddSeparator() override;
100 void AddSubMenu(int command_id,
101 const base::string16& label,
102 ui::MenuModel* model) override;
103 void UpdateMenuItem(int command_id,
104 bool enabled,
105 bool hidden,
106 const base::string16& title) override;
107 content::RenderViewHost* GetRenderViewHost() const override;
108 content::WebContents* GetWebContents() const override;
109 content::BrowserContext* GetBrowserContext() const override;
111 protected:
112 friend class RenderViewContextMenuTest;
113 friend class RenderViewContextMenuPrefsTest;
115 void set_content_type(ContextMenuContentType* content_type) {
116 content_type_.reset(content_type);
119 void set_toolkit_delegate(scoped_ptr<ToolkitDelegate> delegate) {
120 toolkit_delegate_ = delegate.Pass();
123 ToolkitDelegate* toolkit_delegate() {
124 return toolkit_delegate_.get();
127 // TODO(oshima): Make these methods delegate.
129 // Menu Construction.
130 virtual void InitMenu();
132 // Increments histogram value for used items specified by |id|.
133 virtual void RecordUsedItem(int id) = 0;
135 // Increments histogram value for visible context menu item specified by |id|.
136 virtual void RecordShownItem(int id) = 0;
138 #if defined(ENABLE_PLUGINS)
139 virtual void HandleAuthorizeAllPlugins() = 0;
140 #endif
142 // Returns the accelerator for given |command_id|.
143 bool GetAcceleratorForCommandId(int command_id,
144 ui::Accelerator* accelerator) override = 0;
146 // Subclasses should send notification.
147 virtual void NotifyMenuShown() = 0;
148 virtual void NotifyURLOpened(const GURL& url,
149 content::WebContents* new_contents) = 0;
151 // TODO(oshima): Remove this.
152 virtual void AppendPlatformEditableItems() {}
154 content::RenderFrameHost* GetRenderFrameHost();
156 bool IsCustomItemChecked(int id) const;
157 bool IsCustomItemEnabled(int id) const;
159 // Opens the specified URL string in a new tab.
160 void OpenURL(const GURL& url, const GURL& referrer,
161 WindowOpenDisposition disposition,
162 ui::PageTransition transition);
164 // Opens the specified URL string in a new tab with the extra headers.
165 void OpenURLWithExtraHeaders(const GURL& url,
166 const GURL& referrer,
167 WindowOpenDisposition disposition,
168 ui::PageTransition transition,
169 const std::string& extra_headers);
171 content::ContextMenuParams params_;
172 content::WebContents* const source_web_contents_;
173 content::BrowserContext* const browser_context_;
175 ui::SimpleMenuModel menu_model_;
177 // Renderer's frame id.
178 const int render_frame_id_;
180 // Our observers.
181 mutable base::ObserverList<RenderViewContextMenuObserver> observers_;
183 // Whether a command has been executed. Used to track whether menu observers
184 // should be notified of menu closing without execution.
185 bool command_executed_;
187 scoped_ptr<ContextMenuContentType> content_type_;
189 private:
190 bool AppendCustomItems();
192 // The RenderFrameHost's IDs.
193 const int render_process_id_;
195 scoped_ptr<ToolkitDelegate> toolkit_delegate_;
197 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuBase);
200 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_