| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "content/public/common/content_constants.h" | 14 #include "content/public/common/content_constants.h" |
| 15 #include "content/public/common/context_menu_params.h" | 15 #include "content/public/common/context_menu_params.h" |
| 16 #include "content/public/renderer/render_frame.h" | 16 #include "content/public/renderer/render_frame.h" |
| 17 #include "content/public/renderer/render_thread.h" | 17 #include "content/public/renderer/render_thread.h" |
| 18 #include "gin/object_template_builder.h" |
| 18 #include "third_party/WebKit/public/web/WebDocument.h" | 19 #include "third_party/WebKit/public/web/WebDocument.h" |
| 19 #include "third_party/WebKit/public/web/WebElement.h" | 20 #include "third_party/WebKit/public/web/WebElement.h" |
| 20 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebInputEvent.h" | 22 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 22 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 23 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
| 23 #include "third_party/WebKit/public/web/WebScriptSource.h" | 24 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 24 #include "third_party/WebKit/public/web/WebView.h" | 25 #include "third_party/WebKit/public/web/WebView.h" |
| 25 #include "third_party/re2/re2/re2.h" | 26 #include "third_party/re2/re2/re2.h" |
| 26 | 27 |
| 27 using content::RenderThread; | 28 using content::RenderThread; |
| 28 using content::UserMetricsAction; | 29 using content::UserMetricsAction; |
| 29 using blink::WebElement; | 30 using blink::WebElement; |
| 30 using blink::WebFrame; | 31 using blink::WebFrame; |
| 31 using blink::WebMouseEvent; | 32 using blink::WebMouseEvent; |
| 32 using blink::WebNode; | 33 using blink::WebNode; |
| 33 using blink::WebPlugin; | 34 using blink::WebPlugin; |
| 34 using blink::WebPluginContainer; | 35 using blink::WebPluginContainer; |
| 35 using blink::WebPluginParams; | 36 using blink::WebPluginParams; |
| 36 using blink::WebScriptSource; | 37 using blink::WebScriptSource; |
| 37 using blink::WebURLRequest; | 38 using blink::WebURLRequest; |
| 38 using webkit_glue::CppArgumentList; | |
| 39 using webkit_glue::CppVariant; | |
| 40 | 39 |
| 41 namespace plugins { | 40 namespace plugins { |
| 42 | 41 |
| 43 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, | 42 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, |
| 44 WebFrame* frame, | 43 WebFrame* frame, |
| 45 const WebPluginParams& params, | 44 const WebPluginParams& params, |
| 46 const std::string& html_data, | 45 const std::string& html_data, |
| 47 GURL placeholderDataUrl) | 46 GURL placeholderDataUrl) |
| 48 : content::RenderFrameObserver(render_frame), | 47 : content::RenderFrameObserver(render_frame), |
| 49 frame_(frame), | 48 frame_(frame), |
| 50 plugin_params_(params), | 49 plugin_params_(params), |
| 51 plugin_(WebViewPlugin::Create(this, | 50 plugin_(WebViewPlugin::Create(this, |
| 52 render_frame->GetWebkitPreferences(), | 51 render_frame->GetWebkitPreferences(), |
| 53 html_data, | 52 html_data, |
| 54 placeholderDataUrl)), | 53 placeholderDataUrl)), |
| 55 is_blocked_for_prerendering_(false), | 54 is_blocked_for_prerendering_(false), |
| 56 allow_loading_(false), | 55 allow_loading_(false), |
| 57 hidden_(false), | 56 hidden_(false), |
| 58 finished_loading_(false) {} | 57 finished_loading_(false) {} |
| 59 | 58 |
| 60 PluginPlaceholder::~PluginPlaceholder() {} | 59 PluginPlaceholder::~PluginPlaceholder() {} |
| 61 | 60 |
| 62 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { | 61 // static |
| 63 BindToJavascript(frame, "plugin"); | 62 gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 64 BindCallback( | 63 |
| 65 "load", | 64 gin::ObjectTemplateBuilder PluginPlaceholder::GetObjectTemplateBuilder( |
| 66 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); | 65 v8::Isolate* isolate) { |
| 67 BindCallback( | 66 return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate) |
| 68 "hide", | 67 .SetMethod("load", &PluginPlaceholder::LoadCallback) |
| 69 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); | 68 .SetMethod("hide", &PluginPlaceholder::HideCallback) |
| 70 BindCallback("didFinishLoading", | 69 .SetMethod("didFinishLoading", |
| 71 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, | 70 &PluginPlaceholder::DidFinishLoadingCallback); |
| 72 base::Unretained(this))); | |
| 73 } | 71 } |
| 74 | 72 |
| 75 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | 73 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { |
| 76 CHECK(plugin_); | 74 CHECK(plugin_); |
| 77 if (!new_plugin) return; | 75 if (!new_plugin) return; |
| 78 WebPluginContainer* container = plugin_->container(); | 76 WebPluginContainer* container = plugin_->container(); |
| 79 // Set the new plug-in on the container before initializing it. | 77 // Set the new plug-in on the container before initializing it. |
| 80 container->setPlugin(new_plugin); | 78 container->setPlugin(new_plugin); |
| 81 // Save the element in case the plug-in is removed from the page during | 79 // Save the element in case the plug-in is removed from the page during |
| 82 // initialization. | 80 // initialization. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 if (element.hasAttribute("style")) { | 143 if (element.hasAttribute("style")) { |
| 146 std::string style_str = element.getAttribute("style").utf8(); | 144 std::string style_str = element.getAttribute("style").utf8(); |
| 147 if (RE2::PartialMatch(style_str, width_str) && | 145 if (RE2::PartialMatch(style_str, width_str) && |
| 148 RE2::PartialMatch(style_str, height_str)) | 146 RE2::PartialMatch(style_str, height_str)) |
| 149 element.setAttribute("style", "display: none;"); | 147 element.setAttribute("style", "display: none;"); |
| 150 } | 148 } |
| 151 } | 149 } |
| 152 } | 150 } |
| 153 } | 151 } |
| 154 | 152 |
| 155 void PluginPlaceholder::WillDestroyPlugin() { delete this; } | |
| 156 | |
| 157 void PluginPlaceholder::SetMessage(const base::string16& message) { | 153 void PluginPlaceholder::SetMessage(const base::string16& message) { |
| 158 message_ = message; | 154 message_ = message; |
| 159 if (finished_loading_) | 155 if (finished_loading_) |
| 160 UpdateMessage(); | 156 UpdateMessage(); |
| 161 } | 157 } |
| 162 | 158 |
| 163 void PluginPlaceholder::UpdateMessage() { | 159 void PluginPlaceholder::UpdateMessage() { |
| 164 std::string script = | 160 std::string script = |
| 165 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; | 161 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; |
| 166 plugin_->web_view()->mainFrame()->executeScript( | 162 plugin_->web_view()->mainFrame()->executeScript( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 196 } |
| 201 | 197 |
| 202 // TODO(mmenke): In the case of prerendering, feed into | 198 // TODO(mmenke): In the case of prerendering, feed into |
| 203 // ChromeContentRendererClient::CreatePlugin instead, to | 199 // ChromeContentRendererClient::CreatePlugin instead, to |
| 204 // reduce the chance of future regressions. | 200 // reduce the chance of future regressions. |
| 205 WebPlugin* plugin = | 201 WebPlugin* plugin = |
| 206 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); | 202 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); |
| 207 ReplacePlugin(plugin); | 203 ReplacePlugin(plugin); |
| 208 } | 204 } |
| 209 | 205 |
| 210 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, | 206 void PluginPlaceholder::LoadCallback() { |
| 211 CppVariant* result) { | |
| 212 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); | 207 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); |
| 213 LoadPlugin(); | 208 LoadPlugin(); |
| 214 } | 209 } |
| 215 | 210 |
| 216 void PluginPlaceholder::HideCallback(const CppArgumentList& args, | 211 void PluginPlaceholder::HideCallback() { |
| 217 CppVariant* result) { | |
| 218 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); | 212 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); |
| 219 HidePlugin(); | 213 HidePlugin(); |
| 220 } | 214 } |
| 221 | 215 |
| 222 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, | 216 void PluginPlaceholder::DidFinishLoadingCallback() { |
| 223 CppVariant* result) { | |
| 224 finished_loading_ = true; | 217 finished_loading_ = true; |
| 225 if (message_.length() > 0) | 218 if (message_.length() > 0) |
| 226 UpdateMessage(); | 219 UpdateMessage(); |
| 227 } | 220 } |
| 228 | 221 |
| 229 void PluginPlaceholder::SetPluginInfo( | 222 void PluginPlaceholder::SetPluginInfo( |
| 230 const content::WebPluginInfo& plugin_info) { | 223 const content::WebPluginInfo& plugin_info) { |
| 231 plugin_info_ = plugin_info; | 224 plugin_info_ = plugin_info; |
| 232 } | 225 } |
| 233 | 226 |
| 234 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { | 227 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { |
| 235 return plugin_info_; | 228 return plugin_info_; |
| 236 } | 229 } |
| 237 | 230 |
| 238 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { | 231 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { |
| 239 identifier_ = identifier; | 232 identifier_ = identifier; |
| 240 } | 233 } |
| 241 | 234 |
| 242 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } | 235 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } |
| 243 | 236 |
| 244 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 237 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
| 245 return plugin_params_; | 238 return plugin_params_; |
| 246 } | 239 } |
| 247 | 240 |
| 248 } // namespace plugins | 241 } // namespace plugins |
| OLD | NEW |