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 "ui/base/models/simple_menu_model.h"
19 #include "ui/base/page_transition_types.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 ~RenderViewContextMenuBase() override
;
73 // Different platform will have their own implementation.
74 virtual void Show() = 0;
76 // Initializes the context menu.
79 // Programmatically closes the context menu.
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
,
106 const base::string16
& title
) override
;
107 content::RenderViewHost
* GetRenderViewHost() const override
;
108 content::WebContents
* GetWebContents() const override
;
109 content::BrowserContext
* GetBrowserContext() const override
;
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;
142 // Returns the accelerator for given |command_id|.
143 virtual bool GetAcceleratorForCommandId(
145 ui::Accelerator
* accelerator
) = 0;
147 // Subclasses should send notification.
148 virtual void NotifyMenuShown() = 0;
149 virtual void NotifyURLOpened(const GURL
& url
,
150 content::WebContents
* new_contents
) = 0;
152 // TODO(oshima): Remove this.
153 virtual void AppendPlatformEditableItems() {}
155 content::RenderFrameHost
* GetRenderFrameHost();
157 bool IsCustomItemChecked(int id
) const;
158 bool IsCustomItemEnabled(int id
) const;
160 // Opens the specified URL string in a new tab.
161 void OpenURL(const GURL
& url
, const GURL
& referrer
,
162 WindowOpenDisposition disposition
,
163 ui::PageTransition transition
);
165 content::ContextMenuParams params_
;
166 content::WebContents
* source_web_contents_
;
167 content::BrowserContext
* browser_context_
;
169 ui::SimpleMenuModel menu_model_
;
171 // Renderer's frame id.
172 int render_frame_id_
;
175 mutable ObserverList
<RenderViewContextMenuObserver
> observers_
;
177 // Whether a command has been executed. Used to track whether menu observers
178 // should be notified of menu closing without execution.
179 bool command_executed_
;
181 scoped_ptr
<ContextMenuContentType
> content_type_
;
184 bool AppendCustomItems();
186 // The RenderFrameHost's IDs.
187 int render_process_id_
;
189 scoped_ptr
<ToolkitDelegate
> toolkit_delegate_
;
191 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuBase
);
194 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_