Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(641)

Side by Side Diff: chrome/renderer/plugins/plugin_placeholder.cc

Issue 11083002: Allow custom context menus to be requested. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/plugins/plugin_placeholder.h" 5 #include "chrome/renderer/plugins/plugin_placeholder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/json/string_escape.h" 9 #include "base/json/string_escape.h"
10 #include "base/string_piece.h" 10 #include "base/string_piece.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/common/jstemplate_builder.h" 14 #include "chrome/common/jstemplate_builder.h"
15 #include "chrome/common/prerender_messages.h" 15 #include "chrome/common/prerender_messages.h"
16 #include "chrome/common/render_messages.h" 16 #include "chrome/common/render_messages.h"
17 #include "chrome/renderer/chrome_content_renderer_client.h" 17 #include "chrome/renderer/chrome_content_renderer_client.h"
18 #include "chrome/renderer/custom_menu_commands.h" 18 #include "chrome/renderer/custom_menu_commands.h"
19 #include "chrome/renderer/plugins/plugin_uma.h" 19 #include "chrome/renderer/plugins/plugin_uma.h"
20 #include "content/public/common/context_menu_params.h"
20 #include "content/public/renderer/render_thread.h" 21 #include "content/public/renderer/render_thread.h"
21 #include "content/public/renderer/render_view.h" 22 #include "content/public/renderer/render_view.h"
22 #include "grit/generated_resources.h" 23 #include "grit/generated_resources.h"
23 #include "grit/renderer_resources.h" 24 #include "grit/renderer_resources.h"
24 #include "grit/webkit_strings.h" 25 #include "grit/webkit_strings.h"
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" 26 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 GURL(kPluginPlaceholderDataURL))), 222 GURL(kPluginPlaceholderDataURL))),
222 title_(title), 223 title_(title),
223 status_(new ChromeViewHostMsg_GetPluginInfo_Status), 224 status_(new ChromeViewHostMsg_GetPluginInfo_Status),
224 is_blocked_for_prerendering_(false), 225 is_blocked_for_prerendering_(false),
225 allow_loading_(false), 226 allow_loading_(false),
226 #if defined(ENABLE_PLUGIN_INSTALLATION) 227 #if defined(ENABLE_PLUGIN_INSTALLATION)
227 placeholder_routing_id_(MSG_ROUTING_NONE), 228 placeholder_routing_id_(MSG_ROUTING_NONE),
228 #endif 229 #endif
229 hidden_(false), 230 hidden_(false),
230 has_host_(false), 231 has_host_(false),
231 finished_loading_(false) { 232 finished_loading_(false),
233 context_menu_request_id_(0) {
232 RenderThread::Get()->AddObserver(this); 234 RenderThread::Get()->AddObserver(this);
233 } 235 }
234 236
235 PluginPlaceholder::~PluginPlaceholder() { 237 PluginPlaceholder::~PluginPlaceholder() {
236 RenderThread::Get()->RemoveObserver(this); 238 RenderThread::Get()->RemoveObserver(this);
237 #if defined(ENABLE_PLUGIN_INSTALLATION) 239 #if defined(ENABLE_PLUGIN_INSTALLATION)
238 if (placeholder_routing_id_ == MSG_ROUTING_NONE) 240 if (placeholder_routing_id_ == MSG_ROUTING_NONE)
239 return; 241 return;
240 RenderThread::Get()->RemoveRoute(placeholder_routing_id_); 242 RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
241 if (has_host_) { 243 if (has_host_) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 if (output.status.value == status_->value) 447 if (output.status.value == status_->value)
446 return; 448 return;
447 chrome::ChromeContentRendererClient* client = 449 chrome::ChromeContentRendererClient* client =
448 static_cast<chrome::ChromeContentRendererClient*>( 450 static_cast<chrome::ChromeContentRendererClient*>(
449 content::GetContentClient()->renderer()); 451 content::GetContentClient()->renderer());
450 WebPlugin* new_plugin = 452 WebPlugin* new_plugin =
451 client->CreatePlugin(render_view(), frame_, plugin_params_, output); 453 client->CreatePlugin(render_view(), frame_, plugin_params_, output);
452 ReplacePlugin(new_plugin); 454 ReplacePlugin(new_plugin);
453 } 455 }
454 456
455 void PluginPlaceholder::SetMessage(const string16& message) { 457 void PluginPlaceholder::OnCustomContextMenuAction(int request_id,
456 message_ = message; 458 unsigned action) {
457 if (finished_loading_)
458 UpdateMessage();
459 }
460
461 void PluginPlaceholder::UpdateMessage() {
462 std::string script = "window.setMessage(" +
463 base::GetDoubleQuotedJson(message_) + ")";
464 plugin_->web_view()->mainFrame()->executeScript(
465 WebScriptSource(ASCIIToUTF16(script)));
466 }
467
468 void PluginPlaceholder::ContextMenuAction(unsigned id) {
469 if (g_last_active_menu != this) 459 if (g_last_active_menu != this)
470 return; 460 return;
471 switch (id) { 461 switch (action) {
472 case chrome::MENU_COMMAND_PLUGIN_RUN: { 462 case chrome::MENU_COMMAND_PLUGIN_RUN: {
473 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu"); 463 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu");
474 LoadPlugin(); 464 LoadPlugin();
475 break; 465 break;
476 } 466 }
477 case chrome::MENU_COMMAND_PLUGIN_HIDE: { 467 case chrome::MENU_COMMAND_PLUGIN_HIDE: {
478 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu"); 468 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu");
479 HidePlugin(); 469 HidePlugin();
480 break; 470 break;
481 } 471 }
482 default: 472 default:
483 NOTREACHED(); 473 NOTREACHED();
484 } 474 }
485 } 475 }
486 476
477 void PluginPlaceholder::OnCustomContextMenuClosed(int request_id) {
478 DCHECK(request_id == context_menu_request_id_);
479 context_menu_request_id_ = 0;
480 }
481
482 void PluginPlaceholder::SetMessage(const string16& message) {
483 message_ = message;
484 if (finished_loading_)
485 UpdateMessage();
486 }
487
488 void PluginPlaceholder::UpdateMessage() {
489 std::string script = "window.setMessage(" +
490 base::GetDoubleQuotedJson(message_) + ")";
491 plugin_->web_view()->mainFrame()->executeScript(
492 WebScriptSource(ASCIIToUTF16(script)));
493 }
494
487 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) { 495 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
488 WebContextMenuData menu_data; 496 if (context_menu_request_id_)
497 return; // Don't allow nested context menu requests.
489 498
490 size_t num_items = 3u; 499 content::ContextMenuParams params;
491 if (!plugin_info_.path.value().empty())
492 num_items++;
493
494 WebVector<WebMenuItemInfo> custom_items(num_items);
495 500
496 size_t i = 0; 501 size_t i = 0;
497 WebMenuItemInfo name_item; 502 WebMenuItemInfo name_item;
498 name_item.label = title_; 503 name_item.label = title_;
499 name_item.hasTextDirectionOverride = false; 504 name_item.hasTextDirectionOverride = false;
500 name_item.textDirection = WebKit::WebTextDirectionDefault; 505 name_item.textDirection = WebKit::WebTextDirectionDefault;
501 custom_items[i++] = name_item; 506 params.custom_items.push_back(WebMenuItem(name_item));
502 507
503 WebMenuItemInfo separator_item; 508 WebMenuItemInfo separator_item;
504 separator_item.type = WebMenuItemInfo::Separator; 509 separator_item.type = WebMenuItemInfo::Separator;
505 custom_items[i++] = separator_item; 510 params.custom_items.push_back(WebMenuItem(separator_item));
506 511
507 if (!plugin_info_.path.value().empty()) { 512 if (!plugin_info_.path.value().empty()) {
508 WebMenuItemInfo run_item; 513 WebMenuItemInfo run_item;
509 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN; 514 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
510 // Disable this menu item if the plugin is blocked by policy. 515 // Disable this menu item if the plugin is blocked by policy.
511 run_item.enabled = allow_loading_; 516 run_item.enabled = allow_loading_;
512 run_item.label = WebString::fromUTF8( 517 run_item.label = WebString::fromUTF8(
513 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_RUN).c_str()); 518 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_RUN).c_str());
514 run_item.hasTextDirectionOverride = false; 519 run_item.hasTextDirectionOverride = false;
515 run_item.textDirection = WebKit::WebTextDirectionDefault; 520 run_item.textDirection = WebKit::WebTextDirectionDefault;
516 custom_items[i++] = run_item; 521 params.custom_items.push_back(WebMenuItem(run_item));
517 } 522 }
518 523
519 WebMenuItemInfo hide_item; 524 WebMenuItemInfo hide_item;
520 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE; 525 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
521 hide_item.enabled = true; 526 hide_item.enabled = true;
522 hide_item.label = WebString::fromUTF8( 527 hide_item.label = WebString::fromUTF8(
523 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_HIDE).c_str()); 528 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_HIDE).c_str());
524 hide_item.hasTextDirectionOverride = false; 529 hide_item.hasTextDirectionOverride = false;
525 hide_item.textDirection = WebKit::WebTextDirectionDefault; 530 hide_item.textDirection = WebKit::WebTextDirectionDefault;
526 custom_items[i++] = hide_item; 531 params.custom_items.push_back(WebMenuItem(hide_item));
527 532
528 menu_data.customItems.swap(custom_items); 533 params.x = event.windowX;
529 menu_data.mousePosition = WebPoint(event.windowX, event.windowY); 534 params.y = event.windowY;
530 render_view()->ShowContextMenu(NULL, menu_data); 535
536 context_menu_request_id_ = render_view()->ShowContextMenu(this, params);
531 g_last_active_menu = this; 537 g_last_active_menu = this;
532 } 538 }
533 539
534 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) { 540 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) {
535 if (!identifier.empty() && identifier != identifier_) 541 if (!identifier.empty() && identifier != identifier_)
536 return; 542 return;
537 543
538 RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI"); 544 RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI");
539 LoadPlugin(); 545 LoadPlugin();
540 } 546 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 bool PluginPlaceholder::IsYouTubeURL(const GURL& url, 636 bool PluginPlaceholder::IsYouTubeURL(const GURL& url,
631 const std::string& mime_type) { 637 const std::string& mime_type) {
632 std::string host = url.host(); 638 std::string host = url.host();
633 bool is_youtube = EndsWith(host, "youtube.com", true) || 639 bool is_youtube = EndsWith(host, "youtube.com", true) ||
634 EndsWith(host, "youtube-nocookie.com", true); 640 EndsWith(host, "youtube-nocookie.com", true);
635 641
636 return is_youtube && IsValidYouTubeVideo(url.path()) && 642 return is_youtube && IsValidYouTubeVideo(url.path()) &&
637 LowerCaseEqualsASCII(mime_type, kFlashPluginSwfMimeType); 643 LowerCaseEqualsASCII(mime_type, kFlashPluginSwfMimeType);
638 } 644 }
639 #endif 645 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698