OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/plugins/renderer/plugin_placeholder.h" | 5 #include "components/plugins/renderer/plugin_placeholder.h" |
6 | 6 |
| 7 #include "base/strings/string_util.h" |
7 #include "content/public/common/web_preferences.h" | 8 #include "content/public/common/web_preferences.h" |
8 #include "content/public/renderer/render_frame.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" |
9 | 15 |
10 namespace plugins { | 16 namespace plugins { |
11 | 17 |
| 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"; |
| 21 |
12 gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin}; | 22 gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin}; |
13 | 23 |
14 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, | 24 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, |
15 blink::WebLocalFrame* frame, | 25 blink::WebLocalFrame* frame, |
16 const blink::WebPluginParams& params, | 26 const blink::WebPluginParams& params, |
17 const std::string& html_data, | 27 const std::string& html_data) |
18 GURL placeholderDataUrl) | |
19 : content::RenderFrameObserver(render_frame), | 28 : content::RenderFrameObserver(render_frame), |
20 frame_(frame), | 29 frame_(frame), |
21 plugin_params_(params), | 30 plugin_params_(params), |
22 plugin_(WebViewPlugin::Create(this, | 31 plugin_(WebViewPlugin::Create(this, |
23 render_frame | 32 render_frame |
24 ? render_frame->GetWebkitPreferences() | 33 ? render_frame->GetWebkitPreferences() |
25 : content::WebPreferences(), | 34 : content::WebPreferences(), |
26 html_data, | 35 html_data, |
27 placeholderDataUrl)) { | 36 GURL(kPluginPlaceholderDataURL))), |
28 DCHECK(placeholderDataUrl.is_valid()) | 37 hidden_(false) { |
29 << "Blink requires the placeholder to have a valid URL."; | |
30 } | 38 } |
31 | 39 |
32 PluginPlaceholder::~PluginPlaceholder() {} | 40 PluginPlaceholder::~PluginPlaceholder() {} |
33 | 41 |
34 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 42 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
35 return plugin_params_; | 43 return plugin_params_; |
36 } | 44 } |
37 | 45 |
| 46 v8::Local<v8::Value> PluginPlaceholder::GetV8Handle(v8::Isolate* isolate) { |
| 47 return gin::CreateHandle(isolate, this).ToV8(); |
| 48 } |
| 49 |
38 void PluginPlaceholder::ShowContextMenu(const blink::WebMouseEvent& event) { | 50 void PluginPlaceholder::ShowContextMenu(const blink::WebMouseEvent& event) { |
39 // Does nothing by default. Will be overridden if a specific browser wants | 51 // Does nothing by default. Will be overridden if a specific browser wants |
40 // a context menu. | 52 // a context menu. |
41 return; | 53 return; |
42 } | 54 } |
43 | 55 |
44 void PluginPlaceholder::PluginDestroyed() { | 56 void PluginPlaceholder::PluginDestroyed() { |
45 plugin_ = NULL; | 57 plugin_ = NULL; |
46 } | 58 } |
47 | 59 |
48 v8::Local<v8::Object> PluginPlaceholder::GetV8ScriptableObject( | 60 v8::Local<v8::Object> PluginPlaceholder::GetV8ScriptableObject( |
49 v8::Isolate* isolate) const { | 61 v8::Isolate* isolate) const { |
50 return v8::Local<v8::Object>(); | 62 return v8::Local<v8::Object>(); |
51 } | 63 } |
52 | 64 |
| 65 gin::ObjectTemplateBuilder PluginPlaceholder::GetObjectTemplateBuilder( |
| 66 v8::Isolate* isolate) { |
| 67 return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate) |
| 68 .SetMethod("hide", &PluginPlaceholder::HideCallback); |
| 69 } |
| 70 |
| 71 void PluginPlaceholder::HidePlugin() { |
| 72 hidden_ = true; |
| 73 if (!plugin()) |
| 74 return; |
| 75 blink::WebPluginContainer* container = plugin()->container(); |
| 76 blink::WebElement element = container->element(); |
| 77 element.setAttribute("style", "display: none;"); |
| 78 // If we have a width and height, search for a parent (often <div>) with the |
| 79 // same dimensions. If we find such a parent, hide that as well. |
| 80 // This makes much more uncovered page content usable (including clickable) |
| 81 // as opposed to merely visible. |
| 82 // TODO(cevans) -- it's a foul heuristic but we're going to tolerate it for |
| 83 // now for these reasons: |
| 84 // 1) Makes the user experience better. |
| 85 // 2) Foulness is encapsulated within this single function. |
| 86 // 3) Confidence in no fasle positives. |
| 87 // 4) Seems to have a good / low false negative rate at this time. |
| 88 if (element.hasAttribute("width") && element.hasAttribute("height")) { |
| 89 std::string width_str("width:[\\s]*"); |
| 90 width_str += element.getAttribute("width").utf8().data(); |
| 91 if (EndsWith(width_str, "px", false)) { |
| 92 width_str = width_str.substr(0, width_str.length() - 2); |
| 93 } |
| 94 base::TrimWhitespace(width_str, base::TRIM_TRAILING, &width_str); |
| 95 width_str += "[\\s]*px"; |
| 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 base::TrimWhitespace(height_str, base::TRIM_TRAILING, &height_str); |
| 102 height_str += "[\\s]*px"; |
| 103 blink::WebNode parent = element; |
| 104 while (!parent.parentNode().isNull()) { |
| 105 parent = parent.parentNode(); |
| 106 if (!parent.isElementNode()) |
| 107 continue; |
| 108 element = parent.toConst<blink::WebElement>(); |
| 109 if (element.hasAttribute("style")) { |
| 110 std::string style_str = element.getAttribute("style").utf8(); |
| 111 if (RE2::PartialMatch(style_str, width_str) && |
| 112 RE2::PartialMatch(style_str, height_str)) |
| 113 element.setAttribute("style", "display: none;"); |
| 114 } |
| 115 } |
| 116 } |
| 117 } |
| 118 |
53 void PluginPlaceholder::OnDestruct() { | 119 void PluginPlaceholder::OnDestruct() { |
54 frame_ = NULL; | 120 frame_ = NULL; |
55 } | 121 } |
56 | 122 |
| 123 void PluginPlaceholder::HideCallback() { |
| 124 content::RenderThread::Get()->RecordAction( |
| 125 base::UserMetricsAction("Plugin_Hide_Click")); |
| 126 HidePlugin(); |
| 127 } |
| 128 |
57 blink::WebLocalFrame* PluginPlaceholder::GetFrame() { return frame_; } | 129 blink::WebLocalFrame* PluginPlaceholder::GetFrame() { return frame_; } |
58 | 130 |
59 } // namespace plugins | 131 } // namespace plugins |
OLD | NEW |