Chromium Code Reviews| 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 |
| 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 V8ClosureWrapper(const base::Closure& closure) : closure_(closure) {} | |
|
Bernhard Bauer
2013/11/14 16:25:43
Nit: add `explicit` to constructor
jochen (gone - plz use gerrit)
2013/11/15 10:09:39
Done.
| |
| 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("load"); | |
| 101 RegisterCallback("hide"); | |
| 102 RegisterCallback("didFinishLoading"); | |
| 103 } | |
| 58 | 104 |
| 59 PluginPlaceholder::~PluginPlaceholder() {} | 105 PluginPlaceholder::~PluginPlaceholder() {} |
| 60 | 106 |
| 107 void PluginPlaceholder::RegisterCallback(const std::string& callback) { | |
| 108 DCHECK(callbacks_.end() == | |
| 109 std::find(callbacks_.begin(), callbacks_.end(), callback)); | |
| 110 callbacks_.push_back(callback); | |
| 111 } | |
| 112 | |
| 113 void PluginPlaceholder::Observe(const std::string& callback) { | |
| 114 } | |
| 115 | |
| 116 void PluginPlaceholder::InternalObserve(const std::string& callback) { | |
| 117 if (callback == "load") { | |
| 118 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); | |
| 119 LoadPlugin(); | |
| 120 } else if (callback == "hide") { | |
| 121 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click"); | |
| 122 HidePlugin(); | |
| 123 } else if (callback == "didFinishLoading") { | |
| 124 finished_loading_ = true; | |
| 125 if (message_.length() > 0) | |
| 126 UpdateMessage(); | |
| 127 } else { | |
| 128 Observe(callback); | |
| 129 } | |
| 130 } | |
| 131 | |
| 61 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { | 132 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { |
| 62 BindToJavascript(frame, "plugin"); | 133 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 63 BindCallback( | 134 v8::HandleScope handle_scope(isolate); |
| 64 "load", | 135 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 65 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); | 136 |
| 66 BindCallback( | 137 if (context.IsEmpty()) |
| 67 "hide", | 138 return; |
| 68 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); | 139 |
| 69 BindCallback("didFinishLoading", | 140 v8::Context::Scope context_scope(context); |
| 70 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, | 141 |
| 71 base::Unretained(this))); | 142 v8::Handle<v8::FunctionTemplate> plugin_template = |
| 143 v8::FunctionTemplate::New(); | |
| 144 v8::Handle<v8::Template> prototype = plugin_template->PrototypeTemplate(); | |
| 145 | |
| 146 for (std::vector<std::string>::const_iterator callback = callbacks_.begin(); | |
| 147 callback != callbacks_.end(); | |
| 148 ++callback) { | |
| 149 V8ClosureWrapper* wrapper = | |
| 150 new V8ClosureWrapper(base::Bind(&PluginPlaceholder::InternalObserve, | |
|
Bernhard Bauer
2013/11/14 16:25:43
Out of curiosity, why do you do this instead of ha
jochen (gone - plz use gerrit)
2013/11/15 10:09:39
It strikes me as a safer to use API
Bernhard Bauer
2013/11/15 10:24:03
Why is that? It means that the subclass has to fin
| |
| 151 weak_factory_.GetWeakPtr(), | |
| 152 *callback)); | |
| 153 v8::Handle<v8::External> wrapper_holder = v8::External::New(wrapper); | |
| 154 wrapper->SetExternal(isolate, wrapper_holder); | |
| 155 prototype->Set( | |
| 156 v8::String::New(callback->c_str()), | |
| 157 v8::FunctionTemplate::New(RunV8ClosureWrapper, wrapper_holder)); | |
| 158 } | |
| 159 | |
| 160 v8::Handle<v8::Object> global = context->Global(); | |
| 161 global->Set(v8::String::New("plugin"), | |
| 162 plugin_template->GetFunction()->NewInstance()); | |
| 72 } | 163 } |
| 73 | 164 |
| 74 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | 165 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { |
| 75 CHECK(plugin_); | 166 CHECK(plugin_); |
| 76 if (!new_plugin) return; | 167 if (!new_plugin) return; |
| 77 WebPluginContainer* container = plugin_->container(); | 168 WebPluginContainer* container = plugin_->container(); |
| 78 // Set the new plug-in on the container before initializing it. | 169 // Set the new plug-in on the container before initializing it. |
| 79 container->setPlugin(new_plugin); | 170 container->setPlugin(new_plugin); |
| 80 // Save the element in case the plug-in is removed from the page during | 171 // Save the element in case the plug-in is removed from the page during |
| 81 // initialization. | 172 // initialization. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 } | 290 } |
| 200 | 291 |
| 201 // TODO(mmenke): In the case of prerendering, feed into | 292 // TODO(mmenke): In the case of prerendering, feed into |
| 202 // ChromeContentRendererClient::CreatePlugin instead, to | 293 // ChromeContentRendererClient::CreatePlugin instead, to |
| 203 // reduce the chance of future regressions. | 294 // reduce the chance of future regressions. |
| 204 WebPlugin* plugin = | 295 WebPlugin* plugin = |
| 205 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_); | 296 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_); |
| 206 ReplacePlugin(plugin); | 297 ReplacePlugin(plugin); |
| 207 } | 298 } |
| 208 | 299 |
| 209 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, | |
| 210 CppVariant* result) { | |
| 211 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); | |
| 212 LoadPlugin(); | |
| 213 } | |
| 214 | |
| 215 void PluginPlaceholder::HideCallback(const CppArgumentList& args, | |
| 216 CppVariant* result) { | |
| 217 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click"); | |
| 218 HidePlugin(); | |
| 219 } | |
| 220 | |
| 221 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, | |
| 222 CppVariant* result) { | |
| 223 finished_loading_ = true; | |
| 224 if (message_.length() > 0) | |
| 225 UpdateMessage(); | |
| 226 } | |
| 227 | |
| 228 void PluginPlaceholder::SetPluginInfo( | 300 void PluginPlaceholder::SetPluginInfo( |
| 229 const content::WebPluginInfo& plugin_info) { | 301 const content::WebPluginInfo& plugin_info) { |
| 230 plugin_info_ = plugin_info; | 302 plugin_info_ = plugin_info; |
| 231 } | 303 } |
| 232 | 304 |
| 233 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { | 305 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { |
| 234 return plugin_info_; | 306 return plugin_info_; |
| 235 } | 307 } |
| 236 | 308 |
| 237 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { | 309 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { |
| 238 identifier_ = identifier; | 310 identifier_ = identifier; |
| 239 } | 311 } |
| 240 | 312 |
| 241 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } | 313 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } |
| 242 | 314 |
| 243 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 315 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
| 244 return plugin_params_; | 316 return plugin_params_; |
| 245 } | 317 } |
| 246 | 318 |
| 247 } // namespace plugins | 319 } // namespace plugins |
| OLD | NEW |