Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / athena / content / render_view_context_menu_impl.cc
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.
4
5 #include "athena/content/render_view_context_menu_impl.h"
6
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"
13
14 namespace athena {
15 using blink::WebContextMenuData;
16
17 namespace {
18
19 enum {
20   // Nativation
21   CMD_BACK = 0,
22   CMD_FORWARD,
23   CMD_RELOAD,
24   CMD_VIEW_SOURCE,
25
26   // Link
27   CMD_OPEN_LINK_NEW_ACTIVITY,
28
29   // Edit
30   CMD_UNDO,
31   CMD_REDO,
32   CMD_CUT,
33   CMD_COPY,
34   CMD_PASTE,
35   CMD_PASTE_AND_MATCH_STYLE,
36   CMD_DELETE,
37   CMD_SELECT_ALL,
38   CMD_LAST,
39 };
40
41 // Max number of custom command ids allowd.
42 const int kNumCustomCommandIds = 1000;
43
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"));
52 }
53
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"));
59 }
60
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"));
73 }
74
75 }  // namespace
76
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
83   set_content_type(
84       new ContextMenuContentType(source_web_contents_, params, true));
85   set_toolkit_delegate(scoped_ptr<ToolkitDelegate>(new ToolkitDelegateViews));
86 }
87
88 RenderViewContextMenuImpl::~RenderViewContextMenuImpl() {
89 }
90
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);
96 }
97
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;
104   }
105
106   if (content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_LINK)) {
107     if (needs_separator)
108       AddSeparator();
109     AppendLinkItems(params_, &menu_model_);
110     needs_separator = true;
111   }
112
113   if (content_type_->SupportsGroup(
114           ContextMenuContentType::ITEM_GROUP_EDITABLE)) {
115     if (needs_separator)
116       AddSeparator();
117     AppendEditableItems(&menu_model_);
118   }
119 }
120
121 void RenderViewContextMenuImpl::RecordShownItem(int id) {
122   // TODO(oshima): Imelement UMA stats. crbug.com/401673
123   NOTIMPLEMENTED();
124 }
125
126 void RenderViewContextMenuImpl::RecordUsedItem(int id) {
127   // TODO(oshima): Imelement UMA stats. crbug.com/401673
128   NOTIMPLEMENTED();
129 }
130
131 #if defined(ENABLE_PLUGINS)
132 void RenderViewContextMenuImpl::HandleAuthorizeAllPlugins() {
133 }
134 #endif
135
136 void RenderViewContextMenuImpl::NotifyMenuShown() {
137 }
138
139 void RenderViewContextMenuImpl::NotifyURLOpened(
140     const GURL& url,
141     content::WebContents* new_contents) {
142 }
143
144 bool RenderViewContextMenuImpl::GetAcceleratorForCommandId(
145     int command_id,
146     ui::Accelerator* accelerator) {
147   NOTIMPLEMENTED();
148   return false;
149 }
150
151 bool RenderViewContextMenuImpl::IsCommandIdChecked(int command_id) const {
152   return false;
153 }
154
155 bool RenderViewContextMenuImpl::IsCommandIdEnabled(int command_id) const {
156   {
157     bool enabled = false;
158     if (RenderViewContextMenuBase::IsCommandIdKnown(command_id, &enabled))
159       return enabled;
160   }
161   switch (command_id) {
162     // Navigation
163     case CMD_BACK:
164       return source_web_contents_->GetController().CanGoBack();
165     case CMD_FORWARD:
166       return source_web_contents_->GetController().CanGoForward();
167     case CMD_RELOAD:
168       return true;
169     case CMD_VIEW_SOURCE:
170       return source_web_contents_->GetController().CanViewSource();
171
172     // Link
173     case CMD_OPEN_LINK_NEW_ACTIVITY:
174       return params_.link_url.is_valid();
175
176     // Editable
177     case CMD_UNDO:
178       return !!(params_.edit_flags & WebContextMenuData::CanUndo);
179
180     case CMD_REDO:
181       return !!(params_.edit_flags & WebContextMenuData::CanRedo);
182
183     case CMD_CUT:
184       return !!(params_.edit_flags & WebContextMenuData::CanCut);
185
186     case CMD_COPY:
187       return !!(params_.edit_flags & WebContextMenuData::CanCopy);
188
189     case CMD_PASTE:
190     case CMD_PASTE_AND_MATCH_STYLE:
191       return !!(params_.edit_flags & WebContextMenuData::CanPaste);
192
193     case CMD_DELETE:
194       return !!(params_.edit_flags & WebContextMenuData::CanDelete);
195
196     case CMD_SELECT_ALL:
197       return !!(params_.edit_flags & WebContextMenuData::CanSelectAll);
198   }
199   return false;
200 }
201
202 void RenderViewContextMenuImpl::ExecuteCommand(int command_id,
203                                                int event_flags) {
204   RenderViewContextMenuBase::ExecuteCommand(command_id, event_flags);
205   if (command_executed_)
206     return;
207   command_executed_ = true;
208   switch (command_id) {
209     // Navigation
210     case CMD_BACK:
211       source_web_contents_->GetController().GoBack();
212       break;
213     case CMD_FORWARD:
214       source_web_contents_->GetController().GoForward();
215       break;
216     case CMD_RELOAD:
217       source_web_contents_->GetController().Reload(true);
218       break;
219     case CMD_VIEW_SOURCE:
220       source_web_contents_->ViewSource();
221       break;
222
223     // Link
224     case CMD_OPEN_LINK_NEW_ACTIVITY:
225       OpenURL(
226           params_.link_url,
227           params_.frame_url.is_empty() ? params_.page_url : params_.frame_url,
228           NEW_FOREGROUND_TAB,
229           content::PAGE_TRANSITION_LINK);
230       break;
231
232     // Editable
233     case CMD_UNDO:
234       source_web_contents_->Undo();
235       break;
236
237     case CMD_REDO:
238       source_web_contents_->Redo();
239       break;
240
241     case CMD_CUT:
242       source_web_contents_->Cut();
243       break;
244
245     case CMD_COPY:
246       source_web_contents_->Copy();
247       break;
248
249     case CMD_PASTE:
250       source_web_contents_->Paste();
251       break;
252
253     case CMD_PASTE_AND_MATCH_STYLE:
254       source_web_contents_->PasteAndMatchStyle();
255       break;
256
257     case CMD_DELETE:
258       source_web_contents_->Delete();
259       break;
260
261     case CMD_SELECT_ALL:
262       source_web_contents_->SelectAll();
263       break;
264   }
265 }
266
267 }  // namespace athena