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 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
* source_web_contents_
;
173 // In the case of a MimeHandlerView this will point to the WebContents that
174 // embeds the MimeHandlerViewGuest. Otherwise this will be the same as
175 // source_web_contents_.
176 content::WebContents
* embedder_web_contents_
;
177 content::BrowserContext
* browser_context_
;
179 ui::SimpleMenuModel menu_model_
;
181 // Renderer's frame id.
182 int render_frame_id_
;
185 mutable ObserverList
<RenderViewContextMenuObserver
> observers_
;
187 // Whether a command has been executed. Used to track whether menu observers
188 // should be notified of menu closing without execution.
189 bool command_executed_
;
191 scoped_ptr
<ContextMenuContentType
> content_type_
;
194 bool AppendCustomItems();
196 // The RenderFrameHost's IDs.
197 int render_process_id_
;
199 scoped_ptr
<ToolkitDelegate
> toolkit_delegate_
;
201 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuBase
);
204 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_