OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/renderer/plugins/plugin_placeholder.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/renderer/plugins/plugin_uma.h" |
| 9 #include "content/public/renderer/render_view.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression.
h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCaseSensitivit
y.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 18 |
| 19 using WebKit::WebElement; |
| 20 using WebKit::WebFrame; |
| 21 using WebKit::WebMouseEvent; |
| 22 using WebKit::WebNode; |
| 23 using WebKit::WebPlugin; |
| 24 using WebKit::WebPluginContainer; |
| 25 using WebKit::WebPluginParams; |
| 26 using WebKit::WebRegularExpression; |
| 27 using WebKit::WebString; |
| 28 using webkit::WebPluginInfo; |
| 29 using webkit::WebViewPlugin; |
| 30 |
| 31 static const char* const kPluginPlaceholderDataURL = |
| 32 "chrome://pluginplaceholderdata/"; |
| 33 |
| 34 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view, |
| 35 WebFrame* frame, |
| 36 const WebPluginParams& params, |
| 37 const std::string& html_data) |
| 38 : content::RenderViewObserver(render_view), |
| 39 frame_(frame), |
| 40 plugin_params_(params), |
| 41 plugin_(WebViewPlugin::Create( |
| 42 this, render_view->GetWebkitPreferences(), html_data, |
| 43 GURL(kPluginPlaceholderDataURL))) { |
| 44 } |
| 45 |
| 46 PluginPlaceholder::~PluginPlaceholder() { |
| 47 } |
| 48 |
| 49 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { |
| 50 BindToJavascript(frame, "plugin"); |
| 51 } |
| 52 |
| 53 void PluginPlaceholder::LoadPluginInternal(const WebPluginInfo& plugin_info) { |
| 54 CHECK(plugin_); |
| 55 WebPluginContainer* container = plugin_->container(); |
| 56 WebPlugin* new_plugin = |
| 57 render_view()->CreatePlugin(frame_, plugin_info, plugin_params_); |
| 58 if (new_plugin && new_plugin->initialize(container)) { |
| 59 plugin_->RestoreTitleText(); |
| 60 container->setPlugin(new_plugin); |
| 61 container->invalidate(); |
| 62 container->reportGeometry(); |
| 63 plugin_->ReplayReceivedData(new_plugin); |
| 64 plugin_->destroy(); |
| 65 } else { |
| 66 MissingPluginReporter::GetInstance()->ReportPluginMissing( |
| 67 plugin_params_.mimeType.utf8(), |
| 68 plugin_params_.url); |
| 69 } |
| 70 } |
| 71 |
| 72 void PluginPlaceholder::HidePluginInternal() { |
| 73 WebPluginContainer* container = plugin_->container(); |
| 74 WebElement element = container->element(); |
| 75 element.setAttribute("style", "display: none;"); |
| 76 // If we have a width and height, search for a parent (often <div>) with the |
| 77 // same dimensions. If we find such a parent, hide that as well. |
| 78 // This makes much more uncovered page content usable (including clickable) |
| 79 // as opposed to merely visible. |
| 80 // TODO(cevans) -- it's a foul heurisitc but we're going to tolerate it for |
| 81 // now for these reasons: |
| 82 // 1) Makes the user experience better. |
| 83 // 2) Foulness is encapsulated within this single function. |
| 84 // 3) Confidence in no fasle positives. |
| 85 // 4) Seems to have a good / low false negative rate at this time. |
| 86 if (element.hasAttribute("width") && element.hasAttribute("height")) { |
| 87 std::string width_str("width:[\\s]*"); |
| 88 width_str += element.getAttribute("width").utf8().data(); |
| 89 if (EndsWith(width_str, "px", false)) { |
| 90 width_str = width_str.substr(0, width_str.length() - 2); |
| 91 } |
| 92 TrimWhitespace(width_str, TRIM_TRAILING, &width_str); |
| 93 width_str += "[\\s]*px"; |
| 94 WebRegularExpression width_regex(WebString::fromUTF8(width_str.c_str()), |
| 95 WebKit::WebTextCaseSensitive); |
| 96 std::string height_str("height:[\\s]*"); |
| 97 height_str += element.getAttribute("height").utf8().data(); |
| 98 if (EndsWith(height_str, "px", false)) { |
| 99 height_str = height_str.substr(0, height_str.length() - 2); |
| 100 } |
| 101 TrimWhitespace(height_str, TRIM_TRAILING, &height_str); |
| 102 height_str += "[\\s]*px"; |
| 103 WebRegularExpression height_regex(WebString::fromUTF8(height_str.c_str()), |
| 104 WebKit::WebTextCaseSensitive); |
| 105 WebNode parent = element; |
| 106 while (!parent.parentNode().isNull()) { |
| 107 parent = parent.parentNode(); |
| 108 if (!parent.isElementNode()) |
| 109 continue; |
| 110 element = parent.toConst<WebElement>(); |
| 111 if (element.hasAttribute("style")) { |
| 112 WebString style_str = element.getAttribute("style"); |
| 113 if (width_regex.match(style_str) >= 0 && |
| 114 height_regex.match(style_str) >= 0) |
| 115 element.setAttribute("style", "display: none;"); |
| 116 } |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 void PluginPlaceholder::WillDestroyPlugin() { |
| 122 delete this; |
| 123 } |
| 124 |
| 125 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) { |
| 126 } |
OLD | NEW |