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_
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 "content/public/common/page_transition_types.h"
19 #include "ui/base/models/simple_menu_model.h"
20 #include "ui/base/window_open_disposition.h"
23 class RenderFrameHost;
32 struct WebMediaPlayerAction;
33 struct WebPluginAction;
36 class RenderViewContextMenuBase : public ui::SimpleMenuModel::Delegate,
37 public RenderViewContextMenuProxy {
39 // A delegate interface to communicate with the toolkit used by
41 class ToolkitDelegate {
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,
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 virtual ~RenderViewContextMenuBase();
72 // Initializes the context menu.
75 // Programmatically closes the context menu.
78 const ui::SimpleMenuModel& menu_model() const { return menu_model_; }
79 const content::ContextMenuParams& params() const { return params_; }
81 // Returns true if the specified command id is known and valid for
82 // this menu. If the command is known |enabled| is set to indicate
83 // if the command is enabled.
84 bool IsCommandIdKnown(int command_id, bool* enabled) const;
86 // SimpleMenuModel::Delegate implementation.
87 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
88 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
89 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE;
90 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE;
92 // RenderViewContextMenuProxy implementation.
93 virtual void AddMenuItem(int command_id,
94 const base::string16& title) OVERRIDE;
95 virtual void AddCheckItem(int command_id,
96 const base::string16& title) OVERRIDE;
97 virtual void AddSeparator() OVERRIDE;
98 virtual void AddSubMenu(int command_id,
99 const base::string16& label,
100 ui::MenuModel* model) OVERRIDE;
101 virtual void UpdateMenuItem(int command_id,
104 const base::string16& title) OVERRIDE;
105 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE;
106 virtual content::WebContents* GetWebContents() const OVERRIDE;
107 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE;
110 friend class RenderViewContextMenuTest;
111 friend class RenderViewContextMenuPrefsTest;
113 void set_content_type(ContextMenuContentType* content_type) {
114 content_type_.reset(content_type);
117 void set_toolkit_delegate(scoped_ptr<ToolkitDelegate> delegate) {
118 toolkit_delegate_ = delegate.Pass();
121 ToolkitDelegate* toolkit_delegate() {
122 return toolkit_delegate_.get();
125 // TODO(oshima): Make these methods delegate.
127 // Menu Construction.
128 virtual void InitMenu();
130 // Increments histogram value for used items specified by |id|.
131 virtual void RecordUsedItem(int id) = 0;
133 // Increments histogram value for visible context menu item specified by |id|.
134 virtual void RecordShownItem(int id) = 0;
136 #if defined(ENABLE_PLUGINS)
137 virtual void HandleAuthorizeAllPlugins() = 0;
140 // Returns the accelerator for given |command_id|.
141 virtual bool GetAcceleratorForCommandId(
143 ui::Accelerator* accelerator) = 0;
145 // Subclasses should send notification.
146 virtual void NotifyMenuShown() = 0;
147 virtual void NotifyURLOpened(const GURL& url,
148 content::WebContents* new_contents) = 0;
150 // TODO(oshima): Remove this.
151 virtual void AppendPlatformEditableItems() {}
153 content::RenderFrameHost* GetRenderFrameHost();
155 bool IsCustomItemChecked(int id) const;
156 bool IsCustomItemEnabled(int id) const;
158 // Opens the specified URL string in a new tab.
159 void OpenURL(const GURL& url, const GURL& referrer,
160 WindowOpenDisposition disposition,
161 content::PageTransition transition);
163 content::ContextMenuParams params_;
164 content::WebContents* source_web_contents_;
165 content::BrowserContext* browser_context_;
167 ui::SimpleMenuModel menu_model_;
170 mutable ObserverList<RenderViewContextMenuObserver> observers_;
172 // Whether a command has been executed. Used to track whether menu observers
173 // should be notified of menu closing without execution.
174 bool command_executed_;
176 scoped_ptr<ContextMenuContentType> content_type_;
179 bool AppendCustomItems();
181 // The RenderFrameHost's IDs.
182 int render_process_id_;
183 int render_frame_id_;
185 scoped_ptr<ToolkitDelegate> toolkit_delegate_;
187 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuBase);
190 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_