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

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

Issue 8461011: Clean up plug-in placeholders: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 9 years, 1 month 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
(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::npapi::WebViewPlugin;
28 using webkit::WebPluginInfo;
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::LoadPlugin(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::HidePlugin() {
72 CHECK(plugin_);
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698