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 "athena/content/render_view_context_menu_impl.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/renderer_context_menu/context_menu_content_type.h"
9 #include "components/renderer_context_menu/views/toolkit_delegate_views.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/web_contents.h"
12 #include "third_party/WebKit/public/web/WebContextMenuData.h"
15 using blink::WebContextMenuData;
27 CMD_OPEN_LINK_NEW_ACTIVITY,
35 CMD_PASTE_AND_MATCH_STYLE,
41 // Max number of custom command ids allowd.
42 const int kNumCustomCommandIds = 1000;
44 // TODO(oshima): Move IDS for context menus to components/renderer_context_menu
45 // and replace hardcoded strings below.
46 void AppendPageItems(ui::SimpleMenuModel* menu_model) {
47 menu_model->AddItem(CMD_BACK, base::ASCIIToUTF16("Back"));
48 menu_model->AddItem(CMD_FORWARD, base::ASCIIToUTF16("Forward"));
49 menu_model->AddItem(CMD_RELOAD, base::ASCIIToUTF16("Reload"));
50 menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
51 menu_model->AddItem(CMD_VIEW_SOURCE, base::ASCIIToUTF16("View Source"));
54 void AppendLinkItems(const content::ContextMenuParams& params,
55 ui::SimpleMenuModel* menu_model) {
56 if (!params.link_url.is_empty())
57 menu_model->AddItem(CMD_OPEN_LINK_NEW_ACTIVITY,
58 base::ASCIIToUTF16("Open Link In New Activity"));
61 void AppendEditableItems(ui::SimpleMenuModel* menu_model) {
62 menu_model->AddItem(CMD_UNDO, base::ASCIIToUTF16("Undo"));
63 menu_model->AddItem(CMD_REDO, base::ASCIIToUTF16("Redo"));
64 menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
65 menu_model->AddItem(CMD_CUT, base::ASCIIToUTF16("Cut"));
66 menu_model->AddItem(CMD_COPY, base::ASCIIToUTF16("Copy"));
67 menu_model->AddItem(CMD_PASTE, base::ASCIIToUTF16("Paste"));
68 menu_model->AddItem(CMD_PASTE_AND_MATCH_STYLE,
69 base::ASCIIToUTF16("Paste as plain text"));
70 menu_model->AddItem(CMD_DELETE, base::ASCIIToUTF16("Delete"));
71 menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
72 menu_model->AddItem(CMD_SELECT_ALL, base::ASCIIToUTF16("Select All"));
77 RenderViewContextMenuImpl::RenderViewContextMenuImpl(
78 content::RenderFrameHost* render_frame_host,
79 const content::ContextMenuParams& params)
80 : RenderViewContextMenuBase(render_frame_host, params) {
81 SetContentCustomCommandIdRange(CMD_LAST, CMD_LAST + kNumCustomCommandIds);
82 // TODO(oshima): Support other types
84 new ContextMenuContentType(source_web_contents_, params, true));
85 set_toolkit_delegate(scoped_ptr<ToolkitDelegate>(new ToolkitDelegateViews));
88 RenderViewContextMenuImpl::~RenderViewContextMenuImpl() {
91 void RenderViewContextMenuImpl::RunMenuAt(views::Widget* parent,
92 const gfx::Point& point,
93 ui::MenuSourceType type) {
94 static_cast<ToolkitDelegateViews*>(toolkit_delegate())
95 ->RunMenuAt(parent, point, type);
98 void RenderViewContextMenuImpl::InitMenu() {
99 RenderViewContextMenuBase::InitMenu();
100 bool needs_separator = false;
101 if (content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PAGE)) {
102 AppendPageItems(&menu_model_);
103 needs_separator = true;
106 if (content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_LINK)) {
109 AppendLinkItems(params_, &menu_model_);
110 needs_separator = true;
113 if (content_type_->SupportsGroup(
114 ContextMenuContentType::ITEM_GROUP_EDITABLE)) {
117 AppendEditableItems(&menu_model_);
121 void RenderViewContextMenuImpl::RecordShownItem(int id) {
122 // TODO(oshima): Imelement UMA stats. crbug.com/401673
126 void RenderViewContextMenuImpl::RecordUsedItem(int id) {
127 // TODO(oshima): Imelement UMA stats. crbug.com/401673
131 #if defined(ENABLE_PLUGINS)
132 void RenderViewContextMenuImpl::HandleAuthorizeAllPlugins() {
136 void RenderViewContextMenuImpl::NotifyMenuShown() {
139 void RenderViewContextMenuImpl::NotifyURLOpened(
141 content::WebContents* new_contents) {
144 bool RenderViewContextMenuImpl::GetAcceleratorForCommandId(
146 ui::Accelerator* accelerator) {
151 bool RenderViewContextMenuImpl::IsCommandIdChecked(int command_id) const {
155 bool RenderViewContextMenuImpl::IsCommandIdEnabled(int command_id) const {
157 bool enabled = false;
158 if (RenderViewContextMenuBase::IsCommandIdKnown(command_id, &enabled))
161 switch (command_id) {
164 return source_web_contents_->GetController().CanGoBack();
166 return source_web_contents_->GetController().CanGoForward();
169 case CMD_VIEW_SOURCE:
170 return source_web_contents_->GetController().CanViewSource();
173 case CMD_OPEN_LINK_NEW_ACTIVITY:
174 return params_.link_url.is_valid();
178 return !!(params_.edit_flags & WebContextMenuData::CanUndo);
181 return !!(params_.edit_flags & WebContextMenuData::CanRedo);
184 return !!(params_.edit_flags & WebContextMenuData::CanCut);
187 return !!(params_.edit_flags & WebContextMenuData::CanCopy);
190 case CMD_PASTE_AND_MATCH_STYLE:
191 return !!(params_.edit_flags & WebContextMenuData::CanPaste);
194 return !!(params_.edit_flags & WebContextMenuData::CanDelete);
197 return !!(params_.edit_flags & WebContextMenuData::CanSelectAll);
202 void RenderViewContextMenuImpl::ExecuteCommand(int command_id,
204 RenderViewContextMenuBase::ExecuteCommand(command_id, event_flags);
205 if (command_executed_)
207 command_executed_ = true;
208 switch (command_id) {
211 source_web_contents_->GetController().GoBack();
214 source_web_contents_->GetController().GoForward();
217 source_web_contents_->GetController().Reload(true);
219 case CMD_VIEW_SOURCE:
220 source_web_contents_->ViewSource();
224 case CMD_OPEN_LINK_NEW_ACTIVITY:
227 params_.frame_url.is_empty() ? params_.page_url : params_.frame_url,
229 content::PAGE_TRANSITION_LINK);
234 source_web_contents_->Undo();
238 source_web_contents_->Redo();
242 source_web_contents_->Cut();
246 source_web_contents_->Copy();
250 source_web_contents_->Paste();
253 case CMD_PASTE_AND_MATCH_STYLE:
254 source_web_contents_->PasteAndMatchStyle();
258 source_web_contents_->Delete();
262 source_web_contents_->SelectAll();
267 } // namespace athena