1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/render_view_context_menu_controller.h"
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/save_package.h"
#include "chrome/browser/navigation_controller.h"
#include "chrome/browser/navigation_entry.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/template_url_model.h"
#include "chrome/browser/views/page_info_window.h"
#include "chrome/browser/web_contents.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/clipboard_service.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/win_util.h"
#include "net/base/escape.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request.h"
#include "generated_resources.h"
RenderViewContextMenuController::RenderViewContextMenuController(
WebContents* source_web_contents,
const ViewHostMsg_ContextMenu_Params& params)
: source_web_contents_(source_web_contents),
params_(params) {
}
RenderViewContextMenuController::~RenderViewContextMenuController() {
}
///////////////////////////////////////////////////////////////////////////////
// Controller methods
void RenderViewContextMenuController::OpenURL(
const GURL& url,
WindowOpenDisposition disposition,
PageTransition::Type transition) {
source_web_contents_->OpenURL(url, disposition, transition);
}
void RenderViewContextMenuController::CopyImageAt(int x, int y) {
source_web_contents_->render_view_host()->CopyImageAt(x, y);
}
void RenderViewContextMenuController::Inspect(int x, int y) {
source_web_contents_->render_view_host()->InspectElementAt(x, y);
}
void RenderViewContextMenuController::WriteTextToClipboard(
const std::wstring& text) {
ClipboardService* clipboard = g_browser_process->clipboard_service();
if (!clipboard)
return;
clipboard->Clear();
clipboard->WriteText(text);
}
void RenderViewContextMenuController::WriteURLToClipboard(const GURL& url) {
if (url.SchemeIs("mailto"))
WriteTextToClipboard(UTF8ToWide(url.path()));
else
WriteTextToClipboard(UTF8ToWide(url.spec()));
}
///////////////////////////////////////////////////////////////////////////////
// Menu::Delegate methods
std::wstring RenderViewContextMenuController::GetLabel(int id) const {
switch (id) {
case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: {
const TemplateURL* const default_provider = source_web_contents_->
profile()->GetTemplateURLModel()->GetDefaultSearchProvider();
DCHECK(default_provider); // The context menu should not contain this
// item when there is no provider.
return l10n_util::GetStringF(id, default_provider->short_name(),
l10n_util::TruncateString(params_.selection_text, 50));
}
case IDS_CONTENT_CONTEXT_COPYLINKLOCATION:
if (params_.link_url.SchemeIs("mailto"))
return l10n_util::GetString(IDS_CONTENT_CONTEXT_COPYEMAILADDRESS);
default:
return l10n_util::GetString(id);
}
}
bool RenderViewContextMenuController::IsCommandEnabled(int id) const {
switch (id) {
case IDS_CONTENT_CONTEXT_BACK:
return source_web_contents_->controller()->CanGoBack();
case IDS_CONTENT_CONTEXT_FORWARD:
return source_web_contents_->controller()->CanGoForward();
case IDS_CONTENT_CONTEXT_VIEWPAGESOURCE:
case IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE:
case IDS_CONTENT_CONTEXT_INSPECTELEMENT:
return IsDevCommandEnabled(id);
case IDS_CONTENT_CONTEXT_OPENLINKNEWTAB:
case IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW:
case IDS_CONTENT_CONTEXT_COPYLINKLOCATION:
return params_.link_url.is_valid();
case IDS_CONTENT_CONTEXT_SAVELINKAS:
return params_.link_url.is_valid() &&
URLRequest::IsHandledURL(params_.link_url);
case IDS_CONTENT_CONTEXT_SAVEIMAGEAS:
return params_.image_url.is_valid() &&
URLRequest::IsHandledURL(params_.image_url);
case IDS_CONTENT_CONTEXT_OPENIMAGENEWTAB:
case IDS_CONTENT_CONTEXT_COPYIMAGELOCATION:
return params_.image_url.is_valid();
case IDS_CONTENT_CONTEXT_SAVEPAGEAS:
return SavePackage::IsSavableURL(source_web_contents_->GetURL());
case IDS_CONTENT_CONTEXT_OPENFRAMENEWTAB:
case IDS_CONTENT_CONTEXT_OPENFRAMENEWWINDOW:
return params_.frame_url.is_valid();
case IDS_CONTENT_CONTEXT_UNDO:
return !!(params_.edit_flags & ContextNode::CAN_UNDO);
case IDS_CONTENT_CONTEXT_REDO:
return !!(params_.edit_flags & ContextNode::CAN_REDO);
case IDS_CONTENT_CONTEXT_CUT:
return !!(params_.edit_flags & ContextNode::CAN_CUT);
case IDS_CONTENT_CONTEXT_COPY:
return !!(params_.edit_flags & ContextNode::CAN_COPY);
case IDS_CONTENT_CONTEXT_PASTE:
return !!(params_.edit_flags & ContextNode::CAN_PASTE);
case IDS_CONTENT_CONTEXT_DELETE:
return !!(params_.edit_flags & ContextNode::CAN_DELETE);
case IDS_CONTENT_CONTEXT_SELECTALL:
return !!(params_.edit_flags & ContextNode::CAN_SELECT_ALL);
case IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD:
return !source_web_contents_->profile()->IsOffTheRecord() &&
params_.link_url.is_valid();
case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD:
return !source_web_contents_->profile()->IsOffTheRecord() &&
params_.frame_url.is_valid();
case IDS_CONTENT_CONTEXT_COPYIMAGE:
case IDS_CONTENT_CONTEXT_PRINT:
case IDS_CONTENT_CONTEXT_SEARCHWEBFOR:
case IDC_USESPELLCHECKSUGGESTION_0:
case IDC_USESPELLCHECKSUGGESTION_1:
case IDC_USESPELLCHECKSUGGESTION_2:
case IDC_USESPELLCHECKSUGGESTION_3:
case IDC_USESPELLCHECKSUGGESTION_4:
return true;
case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY:
return !params_.misspelled_word.empty();
case IDS_CONTENT_CONTEXT_VIEWPAGEINFO:
return (source_web_contents_->controller()->GetActiveEntry() != NULL);
case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO:
return true;
case IDS_CONTENT_CONTEXT_SAVEFRAMEAS:
case IDS_CONTENT_CONTEXT_PRINTFRAME:
case IDS_CONTENT_CONTEXT_ADDSEARCHENGINE: // Not implemented.
default:
return false;
}
}
bool RenderViewContextMenuController::GetAcceleratorInfo(int id,
ChromeViews::Accelerator* accel) {
// There are no formally defined accelerators we can query so we assume
// that Ctrl+C, Ctrl+V, Ctrl+X, Ctrl-A, etc do what they normally do.
switch (id) {
case IDS_CONTENT_CONTEXT_UNDO:
*accel = ChromeViews::Accelerator(L'Z', false, true, false);
return true;
case IDS_CONTENT_CONTEXT_REDO:
*accel = ChromeViews::Accelerator(L'Z', true, true, false);
return true;
case IDS_CONTENT_CONTEXT_CUT:
*accel = ChromeViews::Accelerator(L'X', false, true, false);
return true;
case IDS_CONTENT_CONTEXT_COPY:
*accel = ChromeViews::Accelerator(L'C', false, true, false);
return true;
case IDS_CONTENT_CONTEXT_PASTE:
*accel = ChromeViews::Accelerator(L'V', false, true, false);
return true;
case IDS_CONTENT_CONTEXT_SELECTALL:
*accel = ChromeViews::Accelerator(L'A', false, true, false);
default:
return false;
}
}
void RenderViewContextMenuController::ExecuteCommand(int id) {
switch (id) {
case IDS_CONTENT_CONTEXT_OPENLINKNEWTAB:
OpenURL(params_.link_url, NEW_BACKGROUND_TAB, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW:
OpenURL(params_.link_url, NEW_WINDOW, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD:
OpenURL(params_.link_url, OFF_THE_RECORD, PageTransition::LINK);
break;
// TODO(paulg): Prompt the user for file name when saving links and images.
case IDS_CONTENT_CONTEXT_SAVEIMAGEAS:
case IDS_CONTENT_CONTEXT_SAVELINKAS: {
const GURL& referrer =
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url;
const GURL& url = id == IDS_CONTENT_CONTEXT_SAVELINKAS ? params_.link_url :
params_.image_url;
DownloadManager* dlm =
source_web_contents_->profile()->GetDownloadManager();
dlm->DownloadUrl(url, referrer, source_web_contents_);
break;
}
case IDS_CONTENT_CONTEXT_COPYLINKLOCATION:
WriteURLToClipboard(params_.link_url);
break;
case IDS_CONTENT_CONTEXT_COPYIMAGELOCATION:
WriteURLToClipboard(params_.image_url);
break;
case IDS_CONTENT_CONTEXT_COPYIMAGE:
CopyImageAt(params_.x, params_.y);
break;
case IDS_CONTENT_CONTEXT_OPENIMAGENEWTAB:
OpenURL(params_.image_url, NEW_BACKGROUND_TAB, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_BACK:
source_web_contents_->controller()->GoBack();
break;
case IDS_CONTENT_CONTEXT_FORWARD:
source_web_contents_->controller()->GoForward();
break;
case IDS_CONTENT_CONTEXT_SAVEPAGEAS:
source_web_contents_->OnSavePage();
break;
case IDS_CONTENT_CONTEXT_PRINT:
source_web_contents_->PrintPreview();
break;
case IDS_CONTENT_CONTEXT_VIEWPAGESOURCE:
OpenURL(GURL("view-source:" + params_.page_url.spec()),
NEW_FOREGROUND_TAB, PageTransition::GENERATED);
break;
case IDS_CONTENT_CONTEXT_INSPECTELEMENT:
Inspect(params_.x, params_.y);
break;
case IDS_CONTENT_CONTEXT_VIEWPAGEINFO: {
NavigationEntry* nav_entry =
source_web_contents_->controller()->GetActiveEntry();
PageInfoWindow::CreatePageInfo(source_web_contents_->profile(),
nav_entry,
source_web_contents_->GetContentHWND(),
PageInfoWindow::SECURITY);
break;
}
case IDS_CONTENT_CONTEXT_OPENFRAMENEWTAB:
OpenURL(params_.frame_url, NEW_BACKGROUND_TAB, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_OPENFRAMENEWWINDOW:
OpenURL(params_.frame_url, NEW_WINDOW, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD:
OpenURL(params_.frame_url, OFF_THE_RECORD, PageTransition::LINK);
break;
case IDS_CONTENT_CONTEXT_SAVEFRAMEAS:
win_util::MessageBox(NULL, L"Context Menu Action", L"Save Frame As",
MB_OK);
break;
case IDS_CONTENT_CONTEXT_PRINTFRAME:
win_util::MessageBox(NULL, L"Context Menu Action", L"Print Frame",
MB_OK);
break;
case IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE:
OpenURL(GURL("view-source:" + params_.frame_url.spec()),
NEW_FOREGROUND_TAB, PageTransition::GENERATED);
break;
case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO: {
// Deserialize the SSL info.
NavigationEntry::SSLStatus ssl;
if (!params_.security_info.empty()) {
int cert_id, cert_status, security_bits;
SSLManager::DeserializeSecurityInfo(params_.security_info,
&cert_id,
&cert_status,
&security_bits);
ssl.set_cert_id(cert_id);
ssl.set_cert_status(cert_status);
ssl.set_security_bits(security_bits);
}
PageInfoWindow::CreateFrameInfo(source_web_contents_->profile(),
params_.frame_url,
ssl,
source_web_contents_->GetContentHWND(),
PageInfoWindow::SECURITY);
break;
}
case IDS_CONTENT_CONTEXT_UNDO:
source_web_contents_->render_view_host()->Undo();
break;
case IDS_CONTENT_CONTEXT_REDO:
source_web_contents_->render_view_host()->Redo();
break;
case IDS_CONTENT_CONTEXT_CUT:
source_web_contents_->render_view_host()->Cut();
break;
case IDS_CONTENT_CONTEXT_COPY:
source_web_contents_->render_view_host()->Copy();
break;
case IDS_CONTENT_CONTEXT_PASTE:
source_web_contents_->render_view_host()->Paste();
break;
case IDS_CONTENT_CONTEXT_DELETE:
source_web_contents_->render_view_host()->Delete();
break;
case IDS_CONTENT_CONTEXT_SELECTALL:
source_web_contents_->render_view_host()->SelectAll();
break;
case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: {
const TemplateURL* const default_provider = source_web_contents_->
profile()->GetTemplateURLModel()->GetDefaultSearchProvider();
DCHECK(default_provider); // The context menu should not contain this
// item when there is no provider.
const TemplateURLRef* const search_url = default_provider->url();
DCHECK(search_url->SupportsReplacement());
OpenURL(GURL(search_url->ReplaceSearchTerms(*default_provider,
params_.selection_text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
std::wstring())), NEW_FOREGROUND_TAB, PageTransition::GENERATED);
break;
}
case IDC_USESPELLCHECKSUGGESTION_0:
case IDC_USESPELLCHECKSUGGESTION_1:
case IDC_USESPELLCHECKSUGGESTION_2:
case IDC_USESPELLCHECKSUGGESTION_3:
case IDC_USESPELLCHECKSUGGESTION_4:
source_web_contents_->render_view_host()->Replace(
params_.dictionary_suggestions[id - IDC_USESPELLCHECKSUGGESTION_0]);
break;
case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY:
source_web_contents_->render_view_host()->AddToDictionary(
params_.misspelled_word);
break;
case IDS_CONTENT_CONTEXT_ADDSEARCHENGINE: // Not implemented.
default:
break;
}
}
bool RenderViewContextMenuController::IsDevCommandEnabled(int id) const {
CommandLine command_line;
if (command_line.HasSwitch(switches::kAlwaysEnableDevTools))
return true;
NavigationEntry *active_entry =
source_web_contents_->controller()->GetActiveEntry();
if (!active_entry)
return false;
// Don't inspect HTML dialogs.
if (source_web_contents_->type() == TAB_CONTENTS_HTML_DIALOG)
return false;
// Don't inspect view source.
if (source_web_contents_->type() == TAB_CONTENTS_VIEW_SOURCE)
return false;
// Don't inspect inspector, new tab UI, etc.
if (active_entry->url().SchemeIs("chrome-resource"))
return false;
// Don't inspect about:network, about:memory, etc.
// However, we do want to inspect about:blank, which is often
// used by ordinary web pages.
if (active_entry->display_url().SchemeIs("about") &&
!LowerCaseEqualsASCII(active_entry->display_url().path(), "blank"))
return false;
// Don't enable the web inspector if JavaScript is disabled
if (id == IDS_CONTENT_CONTEXT_INSPECTELEMENT) {
PrefService* prefs = source_web_contents_->profile()->GetPrefs();
if (!prefs->GetBoolean(prefs::kWebKitJavascriptEnabled) ||
command_line.HasSwitch(switches::kDisableJavaScript))
return false;
}
return true;
}
|