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 |
39 using webkit_glue::CppVariant; | 40 namespace gin { |
| 41 |
| 42 template<> |
| 43 struct Converter<plugins::PluginPlaceholder*> { |
| 44 static bool FromV8(v8::Isolate* isolate, |
| 45 v8::Handle<v8::Value> val, |
| 46 plugins::PluginPlaceholder** out) { |
| 47 if (!val->IsObject()) |
| 48 return false; |
| 49 v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val); |
| 50 gin::WrapperInfo* info = WrapperInfo::From(obj); |
| 51 if (!info) |
| 52 return false; |
| 53 *out = static_cast<plugins::PluginPlaceholder*>( |
| 54 obj->GetAlignedPointerFromInternalField(gin::kEncodedValueIndex)); |
| 55 return *out != NULL; |
| 56 } |
| 57 }; |
| 58 |
| 59 } // namespace gin |
40 | 60 |
41 namespace plugins { | 61 namespace plugins { |
42 | 62 |
43 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, | 63 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, |
44 WebFrame* frame, | 64 WebFrame* frame, |
45 const WebPluginParams& params, | 65 const WebPluginParams& params, |
46 const std::string& html_data, | 66 const std::string& html_data, |
47 GURL placeholderDataUrl) | 67 GURL placeholderDataUrl) |
48 : content::RenderFrameObserver(render_frame), | 68 : content::RenderFrameObserver(render_frame), |
49 frame_(frame), | 69 frame_(frame), |
50 plugin_params_(params), | 70 plugin_params_(params), |
51 plugin_(WebViewPlugin::Create(this, | 71 plugin_(WebViewPlugin::Create(this, |
52 render_frame->GetWebkitPreferences(), | 72 render_frame->GetWebkitPreferences(), |
53 html_data, | 73 html_data, |
54 placeholderDataUrl)), | 74 placeholderDataUrl)), |
55 is_blocked_for_prerendering_(false), | 75 is_blocked_for_prerendering_(false), |
56 allow_loading_(false), | 76 allow_loading_(false), |
57 hidden_(false), | 77 hidden_(false), |
58 finished_loading_(false) {} | 78 finished_loading_(false) {} |
59 | 79 |
60 PluginPlaceholder::~PluginPlaceholder() {} | 80 PluginPlaceholder::~PluginPlaceholder() {} |
61 | 81 |
62 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { | 82 // static |
63 BindToJavascript(frame, "plugin"); | 83 gin::ObjectTemplateBuilder& PluginPlaceholder::GetObjectTemplateBuilder( |
64 BindCallback( | 84 v8::Isolate* isolate) { |
65 "load", | 85 return gin::ObjectTemplateBuilder(isolate) |
66 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); | 86 .SetMethod("load", &PluginPlaceholder::LoadCallback) |
67 BindCallback( | 87 .SetMethod("hide", &PluginPlaceholder::HideCallback) |
68 "hide", | 88 .SetMethod("didFinishLoading", |
69 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); | 89 &PluginPlaceholder::DidFinishLoadingCallback); |
70 BindCallback("didFinishLoading", | |
71 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, | |
72 base::Unretained(this))); | |
73 } | 90 } |
74 | 91 |
75 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | 92 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { |
76 CHECK(plugin_); | 93 CHECK(plugin_); |
77 if (!new_plugin) return; | 94 if (!new_plugin) return; |
78 WebPluginContainer* container = plugin_->container(); | 95 WebPluginContainer* container = plugin_->container(); |
79 // Set the new plug-in on the container before initializing it. | 96 // Set the new plug-in on the container before initializing it. |
80 container->setPlugin(new_plugin); | 97 container->setPlugin(new_plugin); |
81 // Save the element in case the plug-in is removed from the page during | 98 // Save the element in case the plug-in is removed from the page during |
82 // initialization. | 99 // initialization. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 if (element.hasAttribute("style")) { | 162 if (element.hasAttribute("style")) { |
146 std::string style_str = element.getAttribute("style").utf8(); | 163 std::string style_str = element.getAttribute("style").utf8(); |
147 if (RE2::PartialMatch(style_str, width_str) && | 164 if (RE2::PartialMatch(style_str, width_str) && |
148 RE2::PartialMatch(style_str, height_str)) | 165 RE2::PartialMatch(style_str, height_str)) |
149 element.setAttribute("style", "display: none;"); | 166 element.setAttribute("style", "display: none;"); |
150 } | 167 } |
151 } | 168 } |
152 } | 169 } |
153 } | 170 } |
154 | 171 |
155 void PluginPlaceholder::WillDestroyPlugin() { delete this; } | |
156 | |
157 void PluginPlaceholder::SetMessage(const base::string16& message) { | 172 void PluginPlaceholder::SetMessage(const base::string16& message) { |
158 message_ = message; | 173 message_ = message; |
159 if (finished_loading_) | 174 if (finished_loading_) |
160 UpdateMessage(); | 175 UpdateMessage(); |
161 } | 176 } |
162 | 177 |
163 void PluginPlaceholder::UpdateMessage() { | 178 void PluginPlaceholder::UpdateMessage() { |
164 std::string script = | 179 std::string script = |
165 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; | 180 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; |
166 plugin_->web_view()->mainFrame()->executeScript( | 181 plugin_->web_view()->mainFrame()->executeScript( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 215 } |
201 | 216 |
202 // TODO(mmenke): In the case of prerendering, feed into | 217 // TODO(mmenke): In the case of prerendering, feed into |
203 // ChromeContentRendererClient::CreatePlugin instead, to | 218 // ChromeContentRendererClient::CreatePlugin instead, to |
204 // reduce the chance of future regressions. | 219 // reduce the chance of future regressions. |
205 WebPlugin* plugin = | 220 WebPlugin* plugin = |
206 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); | 221 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); |
207 ReplacePlugin(plugin); | 222 ReplacePlugin(plugin); |
208 } | 223 } |
209 | 224 |
210 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, | 225 void PluginPlaceholder::LoadCallback() { |
211 CppVariant* result) { | |
212 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); | 226 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); |
213 LoadPlugin(); | 227 LoadPlugin(); |
214 } | 228 } |
215 | 229 |
216 void PluginPlaceholder::HideCallback(const CppArgumentList& args, | 230 void PluginPlaceholder::HideCallback() { |
217 CppVariant* result) { | |
218 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); | 231 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); |
219 HidePlugin(); | 232 HidePlugin(); |
220 } | 233 } |
221 | 234 |
222 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, | 235 void PluginPlaceholder::DidFinishLoadingCallback() { |
223 CppVariant* result) { | |
224 finished_loading_ = true; | 236 finished_loading_ = true; |
225 if (message_.length() > 0) | 237 if (message_.length() > 0) |
226 UpdateMessage(); | 238 UpdateMessage(); |
227 } | 239 } |
228 | 240 |
229 void PluginPlaceholder::SetPluginInfo( | 241 void PluginPlaceholder::SetPluginInfo( |
230 const content::WebPluginInfo& plugin_info) { | 242 const content::WebPluginInfo& plugin_info) { |
231 plugin_info_ = plugin_info; | 243 plugin_info_ = plugin_info; |
232 } | 244 } |
233 | 245 |
234 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { | 246 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { |
235 return plugin_info_; | 247 return plugin_info_; |
236 } | 248 } |
237 | 249 |
238 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { | 250 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { |
239 identifier_ = identifier; | 251 identifier_ = identifier; |
240 } | 252 } |
241 | 253 |
242 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } | 254 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } |
243 | 255 |
244 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 256 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
245 return plugin_params_; | 257 return plugin_params_; |
246 } | 258 } |
247 | 259 |
248 } // namespace plugins | 260 } // namespace plugins |
OLD | NEW |