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