Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: components/plugins/renderer/plugin_placeholder.cc

Issue 69953006: Bind plugin placeholder directly to v8 instead of over CppBoundClass (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/plugins/renderer/plugin_placeholder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
40 namespace {
41
42 // This class is supposed to be stored in an v8::External. It holds a
43 // base::Closure and will delete itself as soon as the v8::External is garbage
44 // collected.
45 class V8ClosureWrapper {
46 public:
47 explicit V8ClosureWrapper(const base::Closure& closure) : closure_(closure) {}
48
49 void SetExternal(v8::Isolate* isolate, v8::Handle<v8::External> external) {
50 DCHECK(external_.IsEmpty());
51 external_.Reset(isolate, external);
52 external_.SetWeak(this, &V8ClosureWrapper::WeakCallback);
53 }
54
55 void Run() {
56 closure_.Run();
57 }
58
59 private:
60 static void WeakCallback(
61 const v8::WeakCallbackData<v8::External, V8ClosureWrapper>& data) {
62 V8ClosureWrapper* info = data.GetParameter();
63 info->external_.Reset();
64 delete info;
65 }
66
67 ~V8ClosureWrapper() {}
68
69 base::Closure closure_;
70 v8::Persistent<v8::External> external_;
71
72 DISALLOW_COPY_AND_ASSIGN(V8ClosureWrapper);
73 };
74
75 void RunV8ClosureWrapper(const v8::FunctionCallbackInfo<v8::Value>& args) {
76 V8ClosureWrapper* wrapper = reinterpret_cast<V8ClosureWrapper*>(
77 v8::External::Cast(*args.Data())->Value());
78 wrapper->Run();
79 }
80
81 } // namespace
82
42 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view, 83 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view,
43 WebFrame* frame, 84 WebFrame* frame,
44 const WebPluginParams& params, 85 const WebPluginParams& params,
45 const std::string& html_data, 86 const std::string& html_data,
46 GURL placeholderDataUrl) 87 GURL placeholderDataUrl)
47 : content::RenderViewObserver(render_view), 88 : content::RenderViewObserver(render_view),
48 frame_(frame), 89 frame_(frame),
49 plugin_params_(params), 90 plugin_params_(params),
50 plugin_(WebViewPlugin::Create(this, 91 plugin_(WebViewPlugin::Create(this,
51 render_view->GetWebkitPreferences(), 92 render_view->GetWebkitPreferences(),
52 html_data, 93 html_data,
53 placeholderDataUrl)), 94 placeholderDataUrl)),
54 is_blocked_for_prerendering_(false), 95 is_blocked_for_prerendering_(false),
55 allow_loading_(false), 96 allow_loading_(false),
56 hidden_(false), 97 hidden_(false),
57 finished_loading_(false) {} 98 finished_loading_(false),
99 weak_factory_(this) {
100 RegisterCallback(
101 "load",
102 base::Bind(&PluginPlaceholder::LoadCallback, weak_factory_.GetWeakPtr()));
103 RegisterCallback(
104 "hide",
105 base::Bind(&PluginPlaceholder::HideCallback, weak_factory_.GetWeakPtr()));
106 RegisterCallback("didFinishLoading",
107 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback,
108 weak_factory_.GetWeakPtr()));
109 }
58 110
59 PluginPlaceholder::~PluginPlaceholder() {} 111 PluginPlaceholder::~PluginPlaceholder() {}
60 112
113 void PluginPlaceholder::RegisterCallback(const std::string& callback_name,
114 const base::Closure& callback) {
115 DCHECK(callbacks_.find(callback_name) == callbacks_.end());
116 callbacks_[callback_name] = callback;
117 }
118
61 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { 119 void PluginPlaceholder::BindWebFrame(WebFrame* frame) {
62 BindToJavascript(frame, "plugin"); 120 v8::Isolate* isolate = v8::Isolate::GetCurrent();
63 BindCallback( 121 v8::HandleScope handle_scope(isolate);
64 "load", 122 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
65 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); 123
66 BindCallback( 124 if (context.IsEmpty())
67 "hide", 125 return;
68 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); 126
69 BindCallback("didFinishLoading", 127 v8::Context::Scope context_scope(context);
70 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, 128
71 base::Unretained(this))); 129 v8::Handle<v8::FunctionTemplate> plugin_template =
130 v8::FunctionTemplate::New();
131 v8::Handle<v8::Template> prototype = plugin_template->PrototypeTemplate();
132
133 for (std::map<std::string, base::Closure>::const_iterator callback =
134 callbacks_.begin();
135 callback != callbacks_.end();
136 ++callback) {
137 V8ClosureWrapper* wrapper = new V8ClosureWrapper(callback->second);
138 v8::Handle<v8::External> wrapper_holder = v8::External::New(wrapper);
139 wrapper->SetExternal(isolate, wrapper_holder);
140 prototype->Set(
141 v8::String::New(callback->first.c_str()),
142 v8::FunctionTemplate::New(RunV8ClosureWrapper, wrapper_holder));
143 }
144
145 v8::Handle<v8::Object> global = context->Global();
146 global->Set(v8::String::New("plugin"),
147 plugin_template->GetFunction()->NewInstance());
72 } 148 }
73 149
74 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { 150 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) {
75 CHECK(plugin_); 151 CHECK(plugin_);
76 if (!new_plugin) return; 152 if (!new_plugin) return;
77 WebPluginContainer* container = plugin_->container(); 153 WebPluginContainer* container = plugin_->container();
78 // Set the new plug-in on the container before initializing it. 154 // Set the new plug-in on the container before initializing it.
79 container->setPlugin(new_plugin); 155 container->setPlugin(new_plugin);
80 // Save the element in case the plug-in is removed from the page during 156 // Save the element in case the plug-in is removed from the page during
81 // initialization. 157 // initialization.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 } 275 }
200 276
201 // TODO(mmenke): In the case of prerendering, feed into 277 // TODO(mmenke): In the case of prerendering, feed into
202 // ChromeContentRendererClient::CreatePlugin instead, to 278 // ChromeContentRendererClient::CreatePlugin instead, to
203 // reduce the chance of future regressions. 279 // reduce the chance of future regressions.
204 WebPlugin* plugin = 280 WebPlugin* plugin =
205 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_); 281 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_);
206 ReplacePlugin(plugin); 282 ReplacePlugin(plugin);
207 } 283 }
208 284
209 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, 285 void PluginPlaceholder::LoadCallback() {
210 CppVariant* result) {
211 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); 286 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click");
212 LoadPlugin(); 287 LoadPlugin();
213 } 288 }
214 289
215 void PluginPlaceholder::HideCallback(const CppArgumentList& args, 290 void PluginPlaceholder::HideCallback() {
216 CppVariant* result) {
217 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click"); 291 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click");
218 HidePlugin(); 292 HidePlugin();
219 } 293 }
220 294
221 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, 295 void PluginPlaceholder::DidFinishLoadingCallback() {
222 CppVariant* result) {
223 finished_loading_ = true; 296 finished_loading_ = true;
224 if (message_.length() > 0) 297 if (message_.length() > 0)
225 UpdateMessage(); 298 UpdateMessage();
226 } 299 }
227 300
228 void PluginPlaceholder::SetPluginInfo( 301 void PluginPlaceholder::SetPluginInfo(
229 const content::WebPluginInfo& plugin_info) { 302 const content::WebPluginInfo& plugin_info) {
230 plugin_info_ = plugin_info; 303 plugin_info_ = plugin_info;
231 } 304 }
232 305
233 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { 306 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const {
234 return plugin_info_; 307 return plugin_info_;
235 } 308 }
236 309
237 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { 310 void PluginPlaceholder::SetIdentifier(const std::string& identifier) {
238 identifier_ = identifier; 311 identifier_ = identifier;
239 } 312 }
240 313
241 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } 314 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; }
242 315
243 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { 316 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const {
244 return plugin_params_; 317 return plugin_params_;
245 } 318 }
246 319
247 } // namespace plugins 320 } // namespace plugins
OLDNEW
« no previous file with comments | « components/plugins/renderer/plugin_placeholder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698