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/plugin_power_saver_helper.h"
17 #include "content/public/renderer/render_frame.h"
18 #include "content/public/renderer/render_thread.h"
19 #include "gin/object_template_builder.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebInputEvent.h"
23 #include "third_party/WebKit/public/web/WebLocalFrame.h"
24 #include "third_party/WebKit/public/web/WebPluginContainer.h"
25 #include "third_party/WebKit/public/web/WebScriptSource.h"
26 #include "third_party/WebKit/public/web/WebView.h"
27 #include "third_party/re2/re2/re2.h"
29 using base::UserMetricsAction
;
30 using blink::WebElement
;
31 using blink::WebLocalFrame
;
32 using blink::WebMouseEvent
;
34 using blink::WebPlugin
;
35 using blink::WebPluginContainer
;
36 using blink::WebPluginParams
;
37 using blink::WebScriptSource
;
38 using blink::WebURLRequest
;
39 using content::RenderThread
;
43 gin::WrapperInfo
PluginPlaceholder::kWrapperInfo
= {gin::kEmbedderNativeGin
};
45 #if defined(ENABLE_PLUGINS)
46 void PluginPlaceholder::BlockForPowerSaver() {
47 DCHECK(!is_blocked_for_power_saver_
);
48 is_blocked_for_power_saver_
= true;
50 render_frame()->GetPluginPowerSaverHelper()->RegisterPeripheralPlugin(
51 GURL(plugin_params_
.url
).GetOrigin(),
52 base::Bind(&PluginPlaceholder::UnblockForPowerSaver
,
53 weak_factory_
.GetWeakPtr()));
57 PluginPlaceholder::PluginPlaceholder(content::RenderFrame
* render_frame
,
59 const WebPluginParams
& params
,
60 const std::string
& html_data
,
61 GURL placeholderDataUrl
)
62 : content::RenderFrameObserver(render_frame
),
64 plugin_params_(params
),
65 plugin_(WebViewPlugin::Create(this,
66 render_frame
->GetWebkitPreferences(),
69 is_blocked_for_prerendering_(false),
70 is_blocked_for_power_saver_(false),
71 allow_loading_(false),
73 finished_loading_(false),
77 PluginPlaceholder::~PluginPlaceholder() {}
79 #if defined(ENABLE_PLUGINS)
80 void PluginPlaceholder::UnblockForPowerSaver() {
81 is_blocked_for_power_saver_
= false;
82 if (!is_blocked_for_prerendering_
)
83 LoadPlugin(content::RenderFrame::CREATE_PLUGIN_GESTURE_NO_USER_GESTURE
);
87 gin::ObjectTemplateBuilder
PluginPlaceholder::GetObjectTemplateBuilder(
88 v8::Isolate
* isolate
) {
89 return gin::Wrappable
<PluginPlaceholder
>::GetObjectTemplateBuilder(isolate
)
90 .SetMethod("load", &PluginPlaceholder::LoadCallback
)
91 .SetMethod("hide", &PluginPlaceholder::HideCallback
)
92 .SetMethod("didFinishLoading",
93 &PluginPlaceholder::DidFinishLoadingCallback
);
96 void PluginPlaceholder::ReplacePlugin(WebPlugin
* new_plugin
) {
98 if (!new_plugin
) return;
99 WebPluginContainer
* container
= plugin_
->container();
100 // Set the new plug-in on the container before initializing it.
101 container
->setPlugin(new_plugin
);
102 // Save the element in case the plug-in is removed from the page during
104 WebElement element
= container
->element();
105 if (!new_plugin
->initialize(container
)) {
106 // We couldn't initialize the new plug-in. Restore the old one and abort.
107 container
->setPlugin(plugin_
);
111 // The plug-in has been removed from the page. Destroy the old plug-in. We
112 // will be destroyed as soon as V8 garbage collects us.
113 if (!element
.pluginContainer()) {
118 // During initialization, the new plug-in might have replaced itself in turn
119 // with another plug-in. Make sure not to use the passed in |new_plugin| after
121 new_plugin
= container
->plugin();
123 plugin_
->RestoreTitleText();
124 container
->invalidate();
125 container
->reportGeometry();
126 plugin_
->ReplayReceivedData(new_plugin
);
130 void PluginPlaceholder::HidePlugin() {
134 WebPluginContainer
* container
= plugin_
->container();
135 WebElement element
= container
->element();
136 element
.setAttribute("style", "display: none;");
137 // If we have a width and height, search for a parent (often <div>) with the
138 // same dimensions. If we find such a parent, hide that as well.
139 // This makes much more uncovered page content usable (including clickable)
140 // as opposed to merely visible.
141 // TODO(cevans) -- it's a foul heurisitc but we're going to tolerate it for
142 // now for these reasons:
143 // 1) Makes the user experience better.
144 // 2) Foulness is encapsulated within this single function.
145 // 3) Confidence in no fasle positives.
146 // 4) Seems to have a good / low false negative rate at this time.
147 if (element
.hasAttribute("width") && element
.hasAttribute("height")) {
148 std::string
width_str("width:[\\s]*");
149 width_str
+= element
.getAttribute("width").utf8().data();
150 if (EndsWith(width_str
, "px", false)) {
151 width_str
= width_str
.substr(0, width_str
.length() - 2);
153 base::TrimWhitespace(width_str
, base::TRIM_TRAILING
, &width_str
);
154 width_str
+= "[\\s]*px";
155 std::string
height_str("height:[\\s]*");
156 height_str
+= element
.getAttribute("height").utf8().data();
157 if (EndsWith(height_str
, "px", false)) {
158 height_str
= height_str
.substr(0, height_str
.length() - 2);
160 base::TrimWhitespace(height_str
, base::TRIM_TRAILING
, &height_str
);
161 height_str
+= "[\\s]*px";
162 WebNode parent
= element
;
163 while (!parent
.parentNode().isNull()) {
164 parent
= parent
.parentNode();
165 if (!parent
.isElementNode())
167 element
= parent
.toConst
<WebElement
>();
168 if (element
.hasAttribute("style")) {
169 std::string style_str
= element
.getAttribute("style").utf8();
170 if (RE2::PartialMatch(style_str
, width_str
) &&
171 RE2::PartialMatch(style_str
, height_str
))
172 element
.setAttribute("style", "display: none;");
178 void PluginPlaceholder::SetMessage(const base::string16
& message
) {
180 if (finished_loading_
)
184 void PluginPlaceholder::UpdateMessage() {
188 "window.setMessage(" + base::GetQuotedJSONString(message_
) + ")";
189 plugin_
->web_view()->mainFrame()->executeScript(
190 WebScriptSource(base::UTF8ToUTF16(script
)));
193 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent
& event
) {
194 // Does nothing by default. Will be overridden if a specific browser wants
199 void PluginPlaceholder::PluginDestroyed() {
203 void PluginPlaceholder::OnDestruct() {
207 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string
& identifier
) {
208 if (!identifier
.empty() && identifier
!= identifier_
)
211 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI"));
212 LoadPlugin(content::RenderFrame::CREATE_PLUGIN_GESTURE_NO_USER_GESTURE
);
215 void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering
) {
216 // Prerendering can only be enabled prior to a RenderView's first navigation,
217 // so no BlockedPlugin should see the notification that enables prerendering.
218 DCHECK(!is_prerendering
);
219 if (is_blocked_for_prerendering_
&& !is_prerendering
&&
220 !is_blocked_for_power_saver_
) {
221 LoadPlugin(content::RenderFrame::CREATE_PLUGIN_GESTURE_NO_USER_GESTURE
);
225 void PluginPlaceholder::LoadPlugin(
226 content::RenderFrame::CreatePluginGesture gesture
) {
227 // This is not strictly necessary but is an important defense in case the
228 // event propagation changes between "close" vs. "click-to-play".
233 if (!allow_loading_
) {
238 // TODO(mmenke): In the case of prerendering, feed into
239 // ChromeContentRendererClient::CreatePlugin instead, to
240 // reduce the chance of future regressions.
241 WebPlugin
* plugin
= render_frame()->CreatePlugin(frame_
, plugin_info_
,
242 plugin_params_
, gesture
);
243 ReplacePlugin(plugin
);
246 void PluginPlaceholder::LoadCallback() {
247 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click"));
248 LoadPlugin(content::RenderFrame::CREATE_PLUGIN_GESTURE_HAS_USER_GESTURE
);
251 void PluginPlaceholder::HideCallback() {
252 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click"));
256 void PluginPlaceholder::DidFinishLoadingCallback() {
257 finished_loading_
= true;
258 if (message_
.length() > 0)
262 void PluginPlaceholder::SetPluginInfo(
263 const content::WebPluginInfo
& plugin_info
) {
264 plugin_info_
= plugin_info
;
267 const content::WebPluginInfo
& PluginPlaceholder::GetPluginInfo() const {
271 void PluginPlaceholder::SetIdentifier(const std::string
& identifier
) {
272 identifier_
= identifier
;
275 blink::WebLocalFrame
* PluginPlaceholder::GetFrame() { return frame_
; }
277 const blink::WebPluginParams
& PluginPlaceholder::GetPluginParams() const {
278 return plugin_params_
;
281 } // namespace plugins