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 #include "components/renderer_context_menu/render_view_context_menu_base.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/menu_item.h"
18 #include "third_party/WebKit/public/web/WebContextMenuData.h"
20 using blink::WebContextMenuData;
21 using blink::WebString;
23 using content::BrowserContext;
24 using content::OpenURLParams;
25 using content::RenderFrameHost;
26 using content::RenderViewHost;
27 using content::WebContents;
31 // The (inclusive) range of command IDs reserved for content's custom menus.
32 int content_context_custom_first = -1;
33 int content_context_custom_last = -1;
35 bool IsCustomItemEnabledInternal(const std::vector<content::MenuItem>& items,
37 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id));
38 for (size_t i = 0; i < items.size(); ++i) {
39 int action_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
42 return items[i].enabled;
43 if (items[i].type == content::MenuItem::SUBMENU) {
44 if (IsCustomItemEnabledInternal(items[i].submenu, id))
51 bool IsCustomItemCheckedInternal(const std::vector<content::MenuItem>& items,
53 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id));
54 for (size_t i = 0; i < items.size(); ++i) {
55 int action_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
58 return items[i].checked;
59 if (items[i].type == content::MenuItem::SUBMENU) {
60 if (IsCustomItemCheckedInternal(items[i].submenu, id))
67 const size_t kMaxCustomMenuDepth = 5;
68 const size_t kMaxCustomMenuTotalItems = 1000;
70 void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items,
73 ui::SimpleMenuModel::Delegate* delegate,
74 ui::SimpleMenuModel* menu_model) {
75 if (depth > kMaxCustomMenuDepth) {
76 LOG(ERROR) << "Custom menu too deeply nested.";
79 for (size_t i = 0; i < items.size(); ++i) {
80 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) {
83 LOG(ERROR) << "Custom menu action value out of range.";
86 if (*total_items >= kMaxCustomMenuTotalItems) {
87 LOG(ERROR) << "Custom menu too large (too many items).";
91 switch (items[i].type) {
92 case content::MenuItem::OPTION:
94 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
98 case content::MenuItem::CHECKABLE_OPTION:
99 menu_model->AddCheckItem(
100 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
104 case content::MenuItem::GROUP:
105 // TODO(viettrungluu): I don't know what this is supposed to do.
108 case content::MenuItem::SEPARATOR:
109 menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
111 case content::MenuItem::SUBMENU: {
112 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate);
113 AddCustomItemsToMenu(items[i].submenu, depth + 1, total_items, delegate,
115 menu_model->AddSubMenu(
116 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
132 void RenderViewContextMenuBase::SetContentCustomCommandIdRange(
133 int first, int last) {
134 // The range is inclusive.
135 content_context_custom_first = first;
136 content_context_custom_last = last;
140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50;
143 int RenderViewContextMenuBase::ConvertToContentCustomCommandId(int id) {
144 return content_context_custom_first + id;
148 bool RenderViewContextMenuBase::IsContentCustomCommandId(int id) {
149 return id >= content_context_custom_first &&
150 id <= content_context_custom_last;
153 RenderViewContextMenuBase::RenderViewContextMenuBase(
154 content::RenderFrameHost* render_frame_host,
155 const content::ContextMenuParams& params)
157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
158 browser_context_(source_web_contents_->GetBrowserContext()),
160 render_frame_id_(render_frame_host->GetRoutingID()),
161 command_executed_(false),
162 render_process_id_(render_frame_host->GetProcess()->GetID()) {
165 RenderViewContextMenuBase::~RenderViewContextMenuBase() {
168 // Menu construction functions -------------------------------------------------
170 void RenderViewContextMenuBase::Init() {
171 // Command id range must have been already initializerd.
172 DCHECK_NE(-1, content_context_custom_first);
173 DCHECK_NE(-1, content_context_custom_last);
176 if (toolkit_delegate_)
177 toolkit_delegate_->Init(&menu_model_);
180 void RenderViewContextMenuBase::Cancel() {
181 if (toolkit_delegate_)
182 toolkit_delegate_->Cancel();
185 void RenderViewContextMenuBase::InitMenu() {
186 if (content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_CUSTOM)) {
189 const bool has_selection = !params_.selection_text.empty();
191 // We will add more items if there's a selection, so add a separator.
192 // TODO(lazyboy): Clean up separator logic.
193 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
198 void RenderViewContextMenuBase::AddMenuItem(int command_id,
199 const base::string16& title) {
200 menu_model_.AddItem(command_id, title);
203 void RenderViewContextMenuBase::AddCheckItem(int command_id,
204 const base::string16& title) {
205 menu_model_.AddCheckItem(command_id, title);
208 void RenderViewContextMenuBase::AddSeparator() {
209 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
212 void RenderViewContextMenuBase::AddSubMenu(int command_id,
213 const base::string16& label,
214 ui::MenuModel* model) {
215 menu_model_.AddSubMenu(command_id, label, model);
218 void RenderViewContextMenuBase::UpdateMenuItem(int command_id,
221 const base::string16& label) {
222 if (toolkit_delegate_) {
223 toolkit_delegate_->UpdateMenuItem(command_id,
230 RenderViewHost* RenderViewContextMenuBase::GetRenderViewHost() const {
231 return source_web_contents_->GetRenderViewHost();
234 WebContents* RenderViewContextMenuBase::GetWebContents() const {
235 return source_web_contents_;
238 BrowserContext* RenderViewContextMenuBase::GetBrowserContext() const {
239 return browser_context_;
242 bool RenderViewContextMenuBase::AppendCustomItems() {
243 size_t total_items = 0;
244 AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this,
246 return total_items > 0;
249 bool RenderViewContextMenuBase::IsCommandIdKnown(
251 bool* enabled) const {
252 // If this command is is added by one of our observers, we dispatch
253 // it to the observer.
254 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
255 RenderViewContextMenuObserver* observer;
256 while ((observer = it.GetNext()) != NULL) {
257 if (observer->IsCommandIdSupported(id)) {
258 *enabled = observer->IsCommandIdEnabled(id);
264 if (IsContentCustomCommandId(id)) {
265 *enabled = IsCustomItemEnabled(id);
272 // Menu delegate functions -----------------------------------------------------
274 bool RenderViewContextMenuBase::IsCommandIdChecked(int id) const {
275 // If this command is is added by one of our observers, we dispatch it to the
277 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
278 RenderViewContextMenuObserver* observer;
279 while ((observer = it.GetNext()) != NULL) {
280 if (observer->IsCommandIdSupported(id))
281 return observer->IsCommandIdChecked(id);
285 if (IsContentCustomCommandId(id))
286 return IsCustomItemChecked(id);
291 void RenderViewContextMenuBase::ExecuteCommand(int id, int event_flags) {
292 command_executed_ = true;
295 // If this command is is added by one of our observers, we dispatch
296 // it to the observer.
297 ObserverListBase<RenderViewContextMenuObserver>::Iterator it(observers_);
298 RenderViewContextMenuObserver* observer;
299 while ((observer = it.GetNext()) != NULL) {
300 if (observer->IsCommandIdSupported(id))
301 return observer->ExecuteCommand(id);
304 // Process custom actions range.
305 if (IsContentCustomCommandId(id)) {
306 unsigned action = id - content_context_custom_first;
307 const content::CustomContextMenuContext& context = params_.custom_context;
308 #if defined(ENABLE_PLUGINS)
309 if (context.request_id && !context.is_pepper_menu)
310 HandleAuthorizeAllPlugins();
312 source_web_contents_->ExecuteCustomContextMenuCommand(action, context);
315 command_executed_ = false;
318 void RenderViewContextMenuBase::MenuWillShow(ui::SimpleMenuModel* source) {
319 for (int i = 0; i < source->GetItemCount(); ++i) {
320 if (source->IsVisibleAt(i) &&
321 source->GetTypeAt(i) != ui::MenuModel::TYPE_SEPARATOR) {
322 RecordShownItem(source->GetCommandIdAt(i));
326 // Ignore notifications from submenus.
327 if (source != &menu_model_)
330 content::RenderWidgetHostView* view =
331 source_web_contents_->GetRenderWidgetHostView();
333 view->SetShowingContextMenu(true);
338 void RenderViewContextMenuBase::MenuClosed(ui::SimpleMenuModel* source) {
339 // Ignore notifications from submenus.
340 if (source != &menu_model_)
343 content::RenderWidgetHostView* view =
344 source_web_contents_->GetRenderWidgetHostView();
346 view->SetShowingContextMenu(false);
347 source_web_contents_->NotifyContextMenuClosed(params_.custom_context);
349 if (!command_executed_) {
350 FOR_EACH_OBSERVER(RenderViewContextMenuObserver,
356 RenderFrameHost* RenderViewContextMenuBase::GetRenderFrameHost() {
357 return RenderFrameHost::FromID(render_process_id_, render_frame_id_);
360 // Controller functions --------------------------------------------------------
362 void RenderViewContextMenuBase::OpenURL(
363 const GURL& url, const GURL& referring_url,
364 WindowOpenDisposition disposition,
365 ui::PageTransition transition) {
366 content::Referrer referrer = content::Referrer::SanitizeForRequest(
368 content::Referrer(referring_url.GetAsReferrer(),
369 params_.referrer_policy));
371 if (params_.link_url == url && disposition != OFF_THE_RECORD)
372 params_.custom_context.link_followed = url;
374 WebContents* new_contents = source_web_contents_->OpenURL(OpenURLParams(
375 url, referrer, disposition, transition, false));
379 NotifyURLOpened(url, new_contents);
382 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const {
383 return IsCustomItemCheckedInternal(params_.custom_items, id);
386 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const {
387 return IsCustomItemEnabledInternal(params_.custom_items, id);