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 "athena/strings/grit/athena_strings.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"
13 #include "ui/base/l10n/l10n_util.h"
16 using blink::WebContextMenuData
;
28 CMD_OPEN_LINK_NEW_ACTIVITY
,
36 CMD_PASTE_AND_MATCH_STYLE
,
42 // Max number of custom command ids allowd.
43 const int kNumCustomCommandIds
= 1000;
45 // TODO(oshima): Move IDS for context menus to components/renderer_context_menu
46 // and replace hardcoded strings below.
47 void AppendPageItems(ui::SimpleMenuModel
* menu_model
) {
48 menu_model
->AddItem(CMD_BACK
,
49 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_BACK
));
50 menu_model
->AddItem(CMD_FORWARD
,
51 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_FORWARD
));
52 menu_model
->AddItem(CMD_RELOAD
,
53 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_RELOAD
));
54 menu_model
->AddSeparator(ui::NORMAL_SEPARATOR
);
57 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_VIEWPAGESOURCE
));
60 void AppendLinkItems(const content::ContextMenuParams
& params
,
61 ui::SimpleMenuModel
* menu_model
) {
62 if (!params
.link_url
.is_empty())
64 CMD_OPEN_LINK_NEW_ACTIVITY
,
65 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_OPENLINKNEWACTIVITY
));
68 void AppendEditableItems(ui::SimpleMenuModel
* menu_model
) {
69 menu_model
->AddItem(CMD_UNDO
,
70 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_UNDO
));
71 menu_model
->AddItem(CMD_REDO
,
72 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_REDO
));
73 menu_model
->AddSeparator(ui::NORMAL_SEPARATOR
);
74 menu_model
->AddItem(CMD_CUT
,
75 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_CUT
));
76 menu_model
->AddItem(CMD_COPY
,
77 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_COPY
));
78 menu_model
->AddItem(CMD_PASTE
,
79 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_PASTE
));
81 CMD_PASTE_AND_MATCH_STYLE
,
82 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_PASTE_AND_MATCH_STYLE
));
83 menu_model
->AddItem(CMD_DELETE
,
84 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_DELETE
));
85 menu_model
->AddSeparator(ui::NORMAL_SEPARATOR
);
86 menu_model
->AddItem(CMD_SELECT_ALL
,
87 l10n_util::GetStringUTF16(IDS_ATHENA_CONTEXT_SELECTALL
));
92 RenderViewContextMenuImpl::RenderViewContextMenuImpl(
93 content::RenderFrameHost
* render_frame_host
,
94 const content::ContextMenuParams
& params
)
95 : RenderViewContextMenuBase(render_frame_host
, params
) {
96 SetContentCustomCommandIdRange(CMD_LAST
, CMD_LAST
+ kNumCustomCommandIds
);
97 // TODO(oshima): Support other types
99 new ContextMenuContentType(source_web_contents_
, params
, true));
100 set_toolkit_delegate(scoped_ptr
<ToolkitDelegate
>(new ToolkitDelegateViews
));
103 RenderViewContextMenuImpl::~RenderViewContextMenuImpl() {
106 void RenderViewContextMenuImpl::RunMenuAt(views::Widget
* parent
,
107 const gfx::Point
& point
,
108 ui::MenuSourceType type
) {
109 static_cast<ToolkitDelegateViews
*>(toolkit_delegate())
110 ->RunMenuAt(parent
, point
, type
);
113 void RenderViewContextMenuImpl::InitMenu() {
114 RenderViewContextMenuBase::InitMenu();
115 bool needs_separator
= false;
116 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PAGE
)) {
117 AppendPageItems(&menu_model_
);
118 needs_separator
= true;
121 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_LINK
)) {
124 AppendLinkItems(params_
, &menu_model_
);
125 needs_separator
= true;
128 if (content_type_
->SupportsGroup(
129 ContextMenuContentType::ITEM_GROUP_EDITABLE
)) {
132 AppendEditableItems(&menu_model_
);
136 void RenderViewContextMenuImpl::RecordShownItem(int id
) {
137 // TODO(oshima): Imelement UMA stats. crbug.com/401673
141 void RenderViewContextMenuImpl::RecordUsedItem(int id
) {
142 // TODO(oshima): Imelement UMA stats. crbug.com/401673
146 #if defined(ENABLE_PLUGINS)
147 void RenderViewContextMenuImpl::HandleAuthorizeAllPlugins() {
151 void RenderViewContextMenuImpl::NotifyMenuShown() {
154 void RenderViewContextMenuImpl::NotifyURLOpened(
156 content::WebContents
* new_contents
) {
159 bool RenderViewContextMenuImpl::GetAcceleratorForCommandId(
161 ui::Accelerator
* accelerator
) {
166 bool RenderViewContextMenuImpl::IsCommandIdChecked(int command_id
) const {
170 bool RenderViewContextMenuImpl::IsCommandIdEnabled(int command_id
) const {
172 bool enabled
= false;
173 if (RenderViewContextMenuBase::IsCommandIdKnown(command_id
, &enabled
))
176 switch (command_id
) {
179 return source_web_contents_
->GetController().CanGoBack();
181 return source_web_contents_
->GetController().CanGoForward();
184 case CMD_VIEW_SOURCE
:
185 return source_web_contents_
->GetController().CanViewSource();
188 case CMD_OPEN_LINK_NEW_ACTIVITY
:
189 return params_
.link_url
.is_valid();
193 return !!(params_
.edit_flags
& WebContextMenuData::CanUndo
);
196 return !!(params_
.edit_flags
& WebContextMenuData::CanRedo
);
199 return !!(params_
.edit_flags
& WebContextMenuData::CanCut
);
202 return !!(params_
.edit_flags
& WebContextMenuData::CanCopy
);
205 case CMD_PASTE_AND_MATCH_STYLE
:
206 return !!(params_
.edit_flags
& WebContextMenuData::CanPaste
);
209 return !!(params_
.edit_flags
& WebContextMenuData::CanDelete
);
212 return !!(params_
.edit_flags
& WebContextMenuData::CanSelectAll
);
217 void RenderViewContextMenuImpl::ExecuteCommand(int command_id
,
219 RenderViewContextMenuBase::ExecuteCommand(command_id
, event_flags
);
220 if (command_executed_
)
222 command_executed_
= true;
223 switch (command_id
) {
226 source_web_contents_
->GetController().GoBack();
229 source_web_contents_
->GetController().GoForward();
232 source_web_contents_
->GetController().Reload(true);
234 case CMD_VIEW_SOURCE
:
235 source_web_contents_
->ViewSource();
239 case CMD_OPEN_LINK_NEW_ACTIVITY
:
242 params_
.frame_url
.is_empty() ? params_
.page_url
: params_
.frame_url
,
244 ui::PAGE_TRANSITION_LINK
);
249 source_web_contents_
->Undo();
253 source_web_contents_
->Redo();
257 source_web_contents_
->Cut();
261 source_web_contents_
->Copy();
265 source_web_contents_
->Paste();
268 case CMD_PASTE_AND_MATCH_STYLE
:
269 source_web_contents_
->PasteAndMatchStyle();
273 source_web_contents_
->Delete();
277 source_web_contents_
->SelectAll();
282 } // namespace athena