1 // Copyright 2013 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.
5 #include "components/plugins/renderer/plugin_placeholder.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/string_escape.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/public/common/content_constants.h"
15 #include "content/public/common/context_menu_params.h"
16 #include "content/public/renderer/render_thread.h"
17 #include "content/public/renderer/render_view.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebElement.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "third_party/WebKit/public/web/WebPluginContainer.h"
23 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 #include "third_party/WebKit/public/web/WebView.h"
25 #include "third_party/re2/re2/re2.h"
27 using content::RenderThread
;
28 using WebKit::WebElement
;
29 using WebKit::WebFrame
;
30 using WebKit::WebMouseEvent
;
31 using WebKit::WebNode
;
32 using WebKit::WebPlugin
;
33 using WebKit::WebPluginContainer
;
34 using WebKit::WebPluginParams
;
35 using WebKit::WebScriptSource
;
36 using WebKit::WebURLRequest
;
37 using webkit_glue::CppArgumentList
;
38 using webkit_glue::CppVariant
;
42 PluginPlaceholder::PluginPlaceholder(content::RenderView
* render_view
,
44 const WebPluginParams
& params
,
45 const std::string
& html_data
,
46 GURL placeholderDataUrl
)
47 : content::RenderViewObserver(render_view
),
49 plugin_params_(params
),
50 plugin_(WebViewPlugin::Create(this,
51 render_view
->GetWebkitPreferences(),
54 is_blocked_for_prerendering_(false),
55 allow_loading_(false),
57 finished_loading_(false) {}
59 PluginPlaceholder::~PluginPlaceholder() {}
61 void PluginPlaceholder::BindWebFrame(WebFrame
* frame
) {
62 BindToJavascript(frame
, "plugin");
65 base::Bind(&PluginPlaceholder::LoadCallback
, base::Unretained(this)));
68 base::Bind(&PluginPlaceholder::HideCallback
, base::Unretained(this)));
69 BindCallback("didFinishLoading",
70 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback
,
71 base::Unretained(this)));
74 void PluginPlaceholder::ReplacePlugin(WebPlugin
* new_plugin
) {
76 if (!new_plugin
) return;
77 WebPluginContainer
* container
= plugin_
->container();
78 // Set the new plug-in on the container before initializing it.
79 container
->setPlugin(new_plugin
);
80 // Save the element in case the plug-in is removed from the page during
82 WebElement element
= container
->element();
83 if (!new_plugin
->initialize(container
)) {
84 // We couldn't initialize the new plug-in. Restore the old one and abort.
85 container
->setPlugin(plugin_
);
89 // The plug-in has been removed from the page. Destroy the old plug-in
90 // (which will destroy us).
91 if (!element
.pluginContainer()) {
96 // During initialization, the new plug-in might have replaced itself in turn
97 // with another plug-in. Make sure not to use the passed in |new_plugin| after
99 new_plugin
= container
->plugin();
101 plugin_
->RestoreTitleText();
102 container
->invalidate();
103 container
->reportGeometry();
104 plugin_
->ReplayReceivedData(new_plugin
);
108 void PluginPlaceholder::HidePlugin() {
110 WebPluginContainer
* container
= plugin_
->container();
111 WebElement element
= container
->element();
112 element
.setAttribute("style", "display: none;");
113 // If we have a width and height, search for a parent (often <div>) with the
114 // same dimensions. If we find such a parent, hide that as well.
115 // This makes much more uncovered page content usable (including clickable)
116 // as opposed to merely visible.
117 // TODO(cevans) -- it's a foul heurisitc but we're going to tolerate it for
118 // now for these reasons:
119 // 1) Makes the user experience better.
120 // 2) Foulness is encapsulated within this single function.
121 // 3) Confidence in no fasle positives.
122 // 4) Seems to have a good / low false negative rate at this time.
123 if (element
.hasAttribute("width") && element
.hasAttribute("height")) {
124 std::string
width_str("width:[\\s]*");
125 width_str
+= element
.getAttribute("width").utf8().data();
126 if (EndsWith(width_str
, "px", false)) {
127 width_str
= width_str
.substr(0, width_str
.length() - 2);
129 TrimWhitespace(width_str
, TRIM_TRAILING
, &width_str
);
130 width_str
+= "[\\s]*px";
131 std::string
height_str("height:[\\s]*");
132 height_str
+= element
.getAttribute("height").utf8().data();
133 if (EndsWith(height_str
, "px", false)) {
134 height_str
= height_str
.substr(0, height_str
.length() - 2);
136 TrimWhitespace(height_str
, TRIM_TRAILING
, &height_str
);
137 height_str
+= "[\\s]*px";
138 WebNode parent
= element
;
139 while (!parent
.parentNode().isNull()) {
140 parent
= parent
.parentNode();
141 if (!parent
.isElementNode())
143 element
= parent
.toConst
<WebElement
>();
144 if (element
.hasAttribute("style")) {
145 std::string style_str
= element
.getAttribute("style").utf8();
146 if (RE2::PartialMatch(style_str
, width_str
) &&
147 RE2::PartialMatch(style_str
, height_str
))
148 element
.setAttribute("style", "display: none;");
154 void PluginPlaceholder::WillDestroyPlugin() { delete this; }
156 void PluginPlaceholder::SetMessage(const string16
& message
) {
158 if (finished_loading_
)
162 void PluginPlaceholder::UpdateMessage() {
164 "window.setMessage(" + base::GetDoubleQuotedJson(message_
) + ")";
165 plugin_
->web_view()->mainFrame()->executeScript(
166 WebScriptSource(ASCIIToUTF16(script
)));
169 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent
& event
) {
170 // Does nothing by default. Will be overridden if a specific browser wants
175 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string
& identifier
) {
176 if (!identifier
.empty() && identifier
!= identifier_
)
179 RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI");
183 void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering
) {
184 // Prerendering can only be enabled prior to a RenderView's first navigation,
185 // so no BlockedPlugin should see the notification that enables prerendering.
186 DCHECK(!is_prerendering
);
187 if (is_blocked_for_prerendering_
&& !is_prerendering
)
191 void PluginPlaceholder::LoadPlugin() {
192 // This is not strictly necessary but is an important defense in case the
193 // event propagation changes between "close" vs. "click-to-play".
196 if (!allow_loading_
) {
201 // TODO(mmenke): In the case of prerendering, feed into
202 // ChromeContentRendererClient::CreatePlugin instead, to
203 // reduce the chance of future regressions.
205 render_view()->CreatePlugin(frame_
, plugin_info_
, plugin_params_
);
206 ReplacePlugin(plugin
);
209 void PluginPlaceholder::LoadCallback(const CppArgumentList
& args
,
210 CppVariant
* result
) {
211 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click");
215 void PluginPlaceholder::HideCallback(const CppArgumentList
& args
,
216 CppVariant
* result
) {
217 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click");
221 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList
& args
,
222 CppVariant
* result
) {
223 finished_loading_
= true;
224 if (message_
.length() > 0)
228 void PluginPlaceholder::SetPluginInfo(
229 const content::WebPluginInfo
& plugin_info
) {
230 plugin_info_
= plugin_info
;
233 const content::WebPluginInfo
& PluginPlaceholder::GetPluginInfo() const {
237 void PluginPlaceholder::SetIdentifier(const std::string
& identifier
) {
238 identifier_
= identifier
;
241 WebKit::WebFrame
* PluginPlaceholder::GetFrame() { return frame_
; }
243 const WebKit::WebPluginParams
& PluginPlaceholder::GetPluginParams() const {
244 return plugin_params_
;
247 } // namespace plugins