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