Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / plugins / renderer / plugin_placeholder.cc
blobbfb3ef155c2a168bd9e2b8cb5e049c7076dc787e
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"
7 #include "base/strings/string_util.h"
8 #include "content/public/common/web_preferences.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "gin/object_template_builder.h"
12 #include "third_party/WebKit/public/web/WebElement.h"
13 #include "third_party/WebKit/public/web/WebPluginContainer.h"
14 #include "third_party/re2/re2/re2.h"
16 namespace plugins {
18 // The placeholder is loaded in normal web renderer processes, so it should not
19 // have a chrome:// scheme that might let it be confused with a WebUI page.
20 const char kPluginPlaceholderDataURL[] = "data:text/html,pluginplaceholderdata";
22 PluginPlaceholderBase::PluginPlaceholderBase(
23 content::RenderFrame* render_frame,
24 blink::WebLocalFrame* frame,
25 const blink::WebPluginParams& params,
26 const std::string& html_data)
27 : content::RenderFrameObserver(render_frame),
28 frame_(frame),
29 plugin_params_(params),
30 plugin_(WebViewPlugin::Create(render_frame->GetRenderView(),
31 this,
32 render_frame
33 ? render_frame->GetWebkitPreferences()
34 : content::WebPreferences(),
35 html_data,
36 GURL(kPluginPlaceholderDataURL))),
37 hidden_(false) {}
39 PluginPlaceholderBase::~PluginPlaceholderBase() {
42 const blink::WebPluginParams& PluginPlaceholderBase::GetPluginParams() const {
43 return plugin_params_;
46 void PluginPlaceholderBase::ShowContextMenu(const blink::WebMouseEvent& event) {
47 // Does nothing by default. Will be overridden if a specific browser wants
48 // a context menu.
49 return;
52 void PluginPlaceholderBase::PluginDestroyed() {
53 plugin_ = NULL;
56 v8::Local<v8::Object> PluginPlaceholderBase::GetV8ScriptableObject(
57 v8::Isolate* isolate) const {
58 return v8::Local<v8::Object>();
61 void PluginPlaceholderBase::HidePlugin() {
62 hidden_ = true;
63 if (!plugin())
64 return;
65 blink::WebPluginContainer* container = plugin()->container();
66 blink::WebElement element = container->element();
67 element.setAttribute("style", "display: none;");
68 // If we have a width and height, search for a parent (often <div>) with the
69 // same dimensions. If we find such a parent, hide that as well.
70 // This makes much more uncovered page content usable (including clickable)
71 // as opposed to merely visible.
72 // TODO(cevans) -- it's a foul heuristic but we're going to tolerate it for
73 // now for these reasons:
74 // 1) Makes the user experience better.
75 // 2) Foulness is encapsulated within this single function.
76 // 3) Confidence in no fasle positives.
77 // 4) Seems to have a good / low false negative rate at this time.
78 if (element.hasAttribute("width") && element.hasAttribute("height")) {
79 std::string width_str("width:[\\s]*");
80 width_str += element.getAttribute("width").utf8().data();
81 if (base::EndsWith(width_str, "px", base::CompareCase::INSENSITIVE_ASCII)) {
82 width_str = width_str.substr(0, width_str.length() - 2);
84 base::TrimWhitespace(width_str, base::TRIM_TRAILING, &width_str);
85 width_str += "[\\s]*px";
86 std::string height_str("height:[\\s]*");
87 height_str += element.getAttribute("height").utf8().data();
88 if (base::EndsWith(height_str, "px",
89 base::CompareCase::INSENSITIVE_ASCII)) {
90 height_str = height_str.substr(0, height_str.length() - 2);
92 base::TrimWhitespace(height_str, base::TRIM_TRAILING, &height_str);
93 height_str += "[\\s]*px";
94 blink::WebNode parent = element;
95 while (!parent.parentNode().isNull()) {
96 parent = parent.parentNode();
97 if (!parent.isElementNode())
98 continue;
99 element = parent.toConst<blink::WebElement>();
100 if (element.hasAttribute("style")) {
101 std::string style_str = element.getAttribute("style").utf8();
102 if (RE2::PartialMatch(style_str, width_str) &&
103 RE2::PartialMatch(style_str, height_str))
104 element.setAttribute("style", "display: none;");
110 void PluginPlaceholderBase::HideCallback() {
111 content::RenderThread::Get()->RecordAction(
112 base::UserMetricsAction("Plugin_Hide_Click"));
113 HidePlugin();
116 void PluginPlaceholderBase::OnDestruct() {
117 frame_ = NULL;
120 blink::WebLocalFrame* PluginPlaceholderBase::GetFrame() {
121 return frame_;
124 // static
125 gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin};
127 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame,
128 blink::WebLocalFrame* frame,
129 const blink::WebPluginParams& params,
130 const std::string& html_data)
131 : PluginPlaceholderBase(render_frame, frame, params, html_data) {
134 PluginPlaceholder::~PluginPlaceholder() {
137 v8::Local<v8::Value> PluginPlaceholder::GetV8Handle(v8::Isolate* isolate) {
138 return gin::CreateHandle(isolate, this).ToV8();
141 gin::ObjectTemplateBuilder PluginPlaceholder::GetObjectTemplateBuilder(
142 v8::Isolate* isolate) {
143 return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate)
144 .SetMethod<void (plugins::PluginPlaceholder::*)()>(
145 "hide", &PluginPlaceholder::HideCallback);
148 } // namespace plugins