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" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 using content::RenderThread; | 27 using content::RenderThread; |
28 using blink::WebElement; | 28 using blink::WebElement; |
29 using blink::WebFrame; | 29 using blink::WebFrame; |
30 using blink::WebMouseEvent; | 30 using blink::WebMouseEvent; |
31 using blink::WebNode; | 31 using blink::WebNode; |
32 using blink::WebPlugin; | 32 using blink::WebPlugin; |
33 using blink::WebPluginContainer; | 33 using blink::WebPluginContainer; |
34 using blink::WebPluginParams; | 34 using blink::WebPluginParams; |
35 using blink::WebScriptSource; | 35 using blink::WebScriptSource; |
36 using blink::WebURLRequest; | 36 using blink::WebURLRequest; |
37 using webkit_glue::CppArgumentList; | |
38 using webkit_glue::CppVariant; | |
39 | 37 |
40 namespace plugins { | 38 namespace plugins { |
41 | 39 |
42 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view, | 40 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view, |
43 WebFrame* frame, | 41 WebFrame* frame, |
44 const WebPluginParams& params, | 42 const WebPluginParams& params, |
45 const std::string& html_data, | 43 const std::string& html_data, |
46 GURL placeholderDataUrl) | 44 GURL placeholderDataUrl) |
47 : content::RenderViewObserver(render_view), | 45 : content::RenderViewObserver(render_view), |
48 frame_(frame), | 46 frame_(frame), |
49 plugin_params_(params), | 47 plugin_params_(params), |
50 plugin_(WebViewPlugin::Create(this, | 48 plugin_(WebViewPlugin::Create(this, |
51 render_view->GetWebkitPreferences(), | 49 render_view->GetWebkitPreferences(), |
52 html_data, | 50 html_data, |
53 placeholderDataUrl)), | 51 placeholderDataUrl)), |
54 is_blocked_for_prerendering_(false), | 52 is_blocked_for_prerendering_(false), |
55 allow_loading_(false), | 53 allow_loading_(false), |
56 hidden_(false), | 54 hidden_(false), |
57 finished_loading_(false) {} | 55 finished_loading_(false) {} |
58 | 56 |
59 PluginPlaceholder::~PluginPlaceholder() {} | 57 PluginPlaceholder::~PluginPlaceholder() {} |
60 | 58 |
59 void PluginPlaceholder::InstallAdditionalCallbacks( | |
60 v8::Handle<v8::Template> prototype) {} | |
61 | |
61 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { | 62 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { |
62 BindToJavascript(frame, "plugin"); | 63 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
63 BindCallback( | 64 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
64 "load", | 65 |
65 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); | 66 if (context.IsEmpty()) |
66 BindCallback( | 67 return; |
67 "hide", | 68 |
68 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); | 69 v8::Context::Scope context_scope(context); |
69 BindCallback("didFinishLoading", | 70 |
70 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, | 71 v8::Handle<v8::FunctionTemplate> plugin_template = |
71 base::Unretained(this))); | 72 v8::FunctionTemplate::New(); |
73 v8::Handle<v8::Template> prototype = plugin_template->PrototypeTemplate(); | |
74 prototype->Set( | |
75 v8::String::New("load"), | |
76 v8::FunctionTemplate::New(&PluginPlaceholder::LoadCallback, | |
77 v8::External::New(this))->GetFunction()); | |
78 prototype->Set( | |
79 v8::String::New("hide"), | |
80 v8::FunctionTemplate::New(&PluginPlaceholder::HideCallback, | |
81 v8::External::New(this))->GetFunction()); | |
82 prototype->Set( | |
83 v8::String::New("didFinishLoading"), | |
84 v8::FunctionTemplate::New(&PluginPlaceholder::DidFinishLoadingCallback, | |
85 v8::External::New(this))->GetFunction()); | |
86 | |
87 InstallAdditionalCallbacks(prototype); | |
88 | |
89 v8::Handle<v8::Object> global = context->Global(); | |
90 global->Set(v8::String::New("plugin"), | |
91 plugin_template->GetFunction()->NewInstance()); | |
72 } | 92 } |
73 | 93 |
74 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | 94 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { |
75 CHECK(plugin_); | 95 CHECK(plugin_); |
76 if (!new_plugin) return; | 96 if (!new_plugin) return; |
77 WebPluginContainer* container = plugin_->container(); | 97 WebPluginContainer* container = plugin_->container(); |
78 // Set the new plug-in on the container before initializing it. | 98 // Set the new plug-in on the container before initializing it. |
79 container->setPlugin(new_plugin); | 99 container->setPlugin(new_plugin); |
80 // Save the element in case the plug-in is removed from the page during | 100 // Save the element in case the plug-in is removed from the page during |
81 // initialization. | 101 // initialization. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 } | 219 } |
200 | 220 |
201 // TODO(mmenke): In the case of prerendering, feed into | 221 // TODO(mmenke): In the case of prerendering, feed into |
202 // ChromeContentRendererClient::CreatePlugin instead, to | 222 // ChromeContentRendererClient::CreatePlugin instead, to |
203 // reduce the chance of future regressions. | 223 // reduce the chance of future regressions. |
204 WebPlugin* plugin = | 224 WebPlugin* plugin = |
205 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_); | 225 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_); |
206 ReplacePlugin(plugin); | 226 ReplacePlugin(plugin); |
207 } | 227 } |
208 | 228 |
209 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, | 229 // static |
210 CppVariant* result) { | 230 void PluginPlaceholder::LoadCallback( |
231 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
232 PluginPlaceholder* plugin = reinterpret_cast<PluginPlaceholder*>( | |
Bernhard Bauer
2013/11/12 16:37:20
This reinterpret_cast business looks kinda messy.
| |
233 v8::External::Cast(*args.Data())->Value()); | |
211 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); | 234 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); |
212 LoadPlugin(); | 235 plugin->LoadPlugin(); |
213 } | 236 } |
214 | 237 |
215 void PluginPlaceholder::HideCallback(const CppArgumentList& args, | 238 // static |
216 CppVariant* result) { | 239 void PluginPlaceholder::HideCallback( |
240 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
241 PluginPlaceholder* plugin = reinterpret_cast<PluginPlaceholder*>( | |
242 v8::External::Cast(*args.Data())->Value()); | |
217 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click"); | 243 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click"); |
218 HidePlugin(); | 244 plugin->HidePlugin(); |
219 } | 245 } |
220 | 246 |
221 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, | 247 // static |
222 CppVariant* result) { | 248 void PluginPlaceholder::DidFinishLoadingCallback( |
223 finished_loading_ = true; | 249 const v8::FunctionCallbackInfo<v8::Value>& args) { |
224 if (message_.length() > 0) | 250 PluginPlaceholder* plugin = reinterpret_cast<PluginPlaceholder*>( |
225 UpdateMessage(); | 251 v8::External::Cast(*args.Data())->Value()); |
252 plugin->finished_loading_ = true; | |
253 if (plugin->message_.length() > 0) | |
254 plugin->UpdateMessage(); | |
226 } | 255 } |
227 | 256 |
228 void PluginPlaceholder::SetPluginInfo( | 257 void PluginPlaceholder::SetPluginInfo( |
229 const content::WebPluginInfo& plugin_info) { | 258 const content::WebPluginInfo& plugin_info) { |
230 plugin_info_ = plugin_info; | 259 plugin_info_ = plugin_info; |
231 } | 260 } |
232 | 261 |
233 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { | 262 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { |
234 return plugin_info_; | 263 return plugin_info_; |
235 } | 264 } |
236 | 265 |
237 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { | 266 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { |
238 identifier_ = identifier; | 267 identifier_ = identifier; |
239 } | 268 } |
240 | 269 |
241 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } | 270 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } |
242 | 271 |
243 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 272 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
244 return plugin_params_; | 273 return plugin_params_; |
245 } | 274 } |
246 | 275 |
247 } // namespace plugins | 276 } // namespace plugins |
OLD | NEW |