Add owners for offlinepages
[chromium-blink-merge.git] / components / renderer_context_menu / render_view_context_menu_base.cc
blobdb5f6e732d99e0a42ae2667d970be9e108243808
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"
7 #include <algorithm>
8 #include <utility>
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;
22 using blink::WebURL;
23 using content::BrowserContext;
24 using content::OpenURLParams;
25 using content::RenderFrameHost;
26 using content::RenderViewHost;
27 using content::WebContents;
29 namespace {
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,
36 int id) {
37 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id));
38 for (size_t i = 0; i < items.size(); ++i) {
39 int action_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
40 items[i].action);
41 if (action_id == id)
42 return items[i].enabled;
43 if (items[i].type == content::MenuItem::SUBMENU) {
44 if (IsCustomItemEnabledInternal(items[i].submenu, id))
45 return true;
48 return false;
51 bool IsCustomItemCheckedInternal(const std::vector<content::MenuItem>& items,
52 int id) {
53 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id));
54 for (size_t i = 0; i < items.size(); ++i) {
55 int action_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
56 items[i].action);
57 if (action_id == id)
58 return items[i].checked;
59 if (items[i].type == content::MenuItem::SUBMENU) {
60 if (IsCustomItemCheckedInternal(items[i].submenu, id))
61 return true;
64 return false;
67 const size_t kMaxCustomMenuDepth = 5;
68 const size_t kMaxCustomMenuTotalItems = 1000;
70 void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items,
71 size_t depth,
72 size_t* total_items,
73 ui::SimpleMenuModel::Delegate* delegate,
74 ui::SimpleMenuModel* menu_model) {
75 if (depth > kMaxCustomMenuDepth) {
76 LOG(ERROR) << "Custom menu too deeply nested.";
77 return;
79 for (size_t i = 0; i < items.size(); ++i) {
80 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
81 items[i].action);
82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) {
83 LOG(ERROR) << "Custom menu action value out of range.";
84 return;
86 if (*total_items >= kMaxCustomMenuTotalItems) {
87 LOG(ERROR) << "Custom menu too large (too many items).";
88 return;
90 (*total_items)++;
91 switch (items[i].type) {
92 case content::MenuItem::OPTION:
93 menu_model->AddItem(
94 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
95 items[i].action),
96 items[i].label);
97 break;
98 case content::MenuItem::CHECKABLE_OPTION:
99 menu_model->AddCheckItem(
100 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
101 items[i].action),
102 items[i].label);
103 break;
104 case content::MenuItem::GROUP:
105 // TODO(viettrungluu): I don't know what this is supposed to do.
106 NOTREACHED();
107 break;
108 case content::MenuItem::SEPARATOR:
109 menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
110 break;
111 case content::MenuItem::SUBMENU: {
112 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate);
113 AddCustomItemsToMenu(items[i].submenu, depth + 1, total_items, delegate,
114 submenu);
115 menu_model->AddSubMenu(
116 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
117 items[i].action),
118 items[i].label,
119 submenu);
120 break;
122 default:
123 NOTREACHED();
124 break;
129 } // namespace
131 // static
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;
139 // static
140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50;
142 // static
143 int RenderViewContextMenuBase::ConvertToContentCustomCommandId(int id) {
144 return content_context_custom_first + id;
147 // static
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)
156 : params_(params),
157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
158 browser_context_(source_web_contents_->GetBrowserContext()),
159 menu_model_(this),
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);
175 InitMenu();
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)) {
187 AppendCustomItems();
189 const bool has_selection = !params_.selection_text.empty();
190 if (has_selection) {
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,
219 bool enabled,
220 bool hidden,
221 const base::string16& label) {
222 if (toolkit_delegate_) {
223 toolkit_delegate_->UpdateMenuItem(command_id,
224 enabled,
225 hidden,
226 label);
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,
245 &menu_model_);
246 return total_items > 0;
249 bool RenderViewContextMenuBase::IsCommandIdKnown(
250 int id,
251 bool* enabled) const {
252 // If this command is is added by one of our observers, we dispatch
253 // it to the observer.
254 base::ObserverListBase<RenderViewContextMenuObserver>::Iterator it(
255 &observers_);
256 RenderViewContextMenuObserver* observer;
257 while ((observer = it.GetNext()) != NULL) {
258 if (observer->IsCommandIdSupported(id)) {
259 *enabled = observer->IsCommandIdEnabled(id);
260 return true;
264 // Custom items.
265 if (IsContentCustomCommandId(id)) {
266 *enabled = IsCustomItemEnabled(id);
267 return true;
270 return false;
273 // Menu delegate functions -----------------------------------------------------
275 bool RenderViewContextMenuBase::IsCommandIdChecked(int id) const {
276 // If this command is is added by one of our observers, we dispatch it to the
277 // observer.
278 base::ObserverListBase<RenderViewContextMenuObserver>::Iterator it(
279 &observers_);
280 RenderViewContextMenuObserver* observer;
281 while ((observer = it.GetNext()) != NULL) {
282 if (observer->IsCommandIdSupported(id))
283 return observer->IsCommandIdChecked(id);
286 // Custom items.
287 if (IsContentCustomCommandId(id))
288 return IsCustomItemChecked(id);
290 return false;
293 void RenderViewContextMenuBase::ExecuteCommand(int id, int event_flags) {
294 command_executed_ = true;
295 RecordUsedItem(id);
297 // If this command is is added by one of our observers, we dispatch
298 // it to the observer.
299 base::ObserverListBase<RenderViewContextMenuObserver>::Iterator it(
300 &observers_);
301 RenderViewContextMenuObserver* observer;
302 while ((observer = it.GetNext()) != NULL) {
303 if (observer->IsCommandIdSupported(id))
304 return observer->ExecuteCommand(id);
307 // Process custom actions range.
308 if (IsContentCustomCommandId(id)) {
309 unsigned action = id - content_context_custom_first;
310 const content::CustomContextMenuContext& context = params_.custom_context;
311 #if defined(ENABLE_PLUGINS)
312 if (context.request_id && !context.is_pepper_menu)
313 HandleAuthorizeAllPlugins();
314 #endif
315 source_web_contents_->ExecuteCustomContextMenuCommand(action, context);
316 return;
318 command_executed_ = false;
321 void RenderViewContextMenuBase::MenuWillShow(ui::SimpleMenuModel* source) {
322 for (int i = 0; i < source->GetItemCount(); ++i) {
323 if (source->IsVisibleAt(i) &&
324 source->GetTypeAt(i) != ui::MenuModel::TYPE_SEPARATOR) {
325 RecordShownItem(source->GetCommandIdAt(i));
329 // Ignore notifications from submenus.
330 if (source != &menu_model_)
331 return;
333 content::RenderWidgetHostView* view =
334 source_web_contents_->GetRenderWidgetHostView();
335 if (view)
336 view->SetShowingContextMenu(true);
338 NotifyMenuShown();
341 void RenderViewContextMenuBase::MenuClosed(ui::SimpleMenuModel* source) {
342 // Ignore notifications from submenus.
343 if (source != &menu_model_)
344 return;
346 content::RenderWidgetHostView* view =
347 source_web_contents_->GetRenderWidgetHostView();
348 if (view)
349 view->SetShowingContextMenu(false);
350 source_web_contents_->NotifyContextMenuClosed(params_.custom_context);
352 if (!command_executed_) {
353 FOR_EACH_OBSERVER(RenderViewContextMenuObserver,
354 observers_,
355 OnMenuCancel());
359 RenderFrameHost* RenderViewContextMenuBase::GetRenderFrameHost() {
360 return RenderFrameHost::FromID(render_process_id_, render_frame_id_);
363 // Controller functions --------------------------------------------------------
365 void RenderViewContextMenuBase::OpenURL(
366 const GURL& url, const GURL& referring_url,
367 WindowOpenDisposition disposition,
368 ui::PageTransition transition) {
369 OpenURLWithExtraHeaders(url, referring_url, disposition, transition, "");
372 void RenderViewContextMenuBase::OpenURLWithExtraHeaders(
373 const GURL& url,
374 const GURL& referring_url,
375 WindowOpenDisposition disposition,
376 ui::PageTransition transition,
377 const std::string& extra_headers) {
378 content::Referrer referrer = content::Referrer::SanitizeForRequest(
379 url,
380 content::Referrer(referring_url.GetAsReferrer(),
381 params_.referrer_policy));
383 if (params_.link_url == url && disposition != OFF_THE_RECORD)
384 params_.custom_context.link_followed = url;
386 OpenURLParams open_url_params(url, referrer, disposition, transition, false);
387 if (!extra_headers.empty())
388 open_url_params.extra_headers = extra_headers;
390 WebContents* new_contents = source_web_contents_->OpenURL(open_url_params);
391 if (!new_contents)
392 return;
394 NotifyURLOpened(url, new_contents);
397 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const {
398 return IsCustomItemCheckedInternal(params_.custom_items, id);
401 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const {
402 return IsCustomItemEnabledInternal(params_.custom_items, id);