| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/loadable_plugin_placeholder.h" | 5 #include "components/plugins/renderer/loadable_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/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 15 #include "base/values.h" | 14 #include "base/values.h" |
| 16 #include "content/public/child/v8_value_converter.h" | 15 #include "content/public/child/v8_value_converter.h" |
| 17 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 18 #include "content/public/renderer/render_frame.h" | 17 #include "content/public/renderer/render_frame.h" |
| 19 #include "content/public/renderer/render_thread.h" | 18 #include "content/public/renderer/render_thread.h" |
| 20 #include "gin/handle.h" | 19 #include "gin/handle.h" |
| 21 #include "gin/object_template_builder.h" | 20 #include "gin/object_template_builder.h" |
| 22 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" | 21 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" |
| 23 #include "third_party/WebKit/public/web/WebDocument.h" | 22 #include "third_party/WebKit/public/web/WebDocument.h" |
| 24 #include "third_party/WebKit/public/web/WebElement.h" | 23 #include "third_party/WebKit/public/web/WebElement.h" |
| 25 #include "third_party/WebKit/public/web/WebInputEvent.h" | 24 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 26 #include "third_party/WebKit/public/web/WebKit.h" | 25 #include "third_party/WebKit/public/web/WebKit.h" |
| 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 28 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 27 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
| 29 #include "third_party/WebKit/public/web/WebScriptSource.h" | 28 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 30 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | 29 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
| 31 #include "third_party/WebKit/public/web/WebView.h" | 30 #include "third_party/WebKit/public/web/WebView.h" |
| 32 #include "third_party/re2/re2/re2.h" | |
| 33 | 31 |
| 34 using base::UserMetricsAction; | 32 using base::UserMetricsAction; |
| 35 using blink::WebElement; | |
| 36 using blink::WebLocalFrame; | |
| 37 using blink::WebMouseEvent; | |
| 38 using blink::WebNode; | |
| 39 using blink::WebPlugin; | |
| 40 using blink::WebPluginContainer; | |
| 41 using blink::WebPluginParams; | |
| 42 using blink::WebScriptSource; | |
| 43 using blink::WebURLRequest; | |
| 44 using content::PluginInstanceThrottler; | 33 using content::PluginInstanceThrottler; |
| 45 using content::RenderThread; | 34 using content::RenderThread; |
| 46 | 35 |
| 47 namespace plugins { | 36 namespace plugins { |
| 48 | 37 |
| 49 #if defined(ENABLE_PLUGINS) | |
| 50 // TODO(tommycli): After an unthrottling size update, re-check the size after | 38 // TODO(tommycli): After an unthrottling size update, re-check the size after |
| 51 // this delay, as Blink can report incorrect sizes to plugins while the | 39 // this delay, as Blink can report incorrect sizes to plugins while the |
| 52 // compositing state is dirty. Chosen because it seems to work. | 40 // compositing state is dirty. Chosen because it seems to work. |
| 53 const int kSizeChangeRecheckDelayMilliseconds = 100; | 41 const int kSizeChangeRecheckDelayMilliseconds = 100; |
| 54 | 42 |
| 55 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { | 43 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { |
| 56 DCHECK(!is_blocked_for_power_saver_poster_); | 44 DCHECK(!is_blocked_for_power_saver_poster_); |
| 57 is_blocked_for_power_saver_poster_ = true; | 45 is_blocked_for_power_saver_poster_ = true; |
| 58 | 46 |
| 59 render_frame()->RegisterPeripheralPlugin( | 47 render_frame()->RegisterPeripheralPlugin( |
| 60 GURL(GetPluginParams().url).GetOrigin(), | 48 GURL(GetPluginParams().url).GetOrigin(), |
| 61 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, | 49 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, |
| 62 weak_factory_.GetWeakPtr(), | 50 weak_factory_.GetWeakPtr(), |
| 63 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); | 51 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); |
| 64 } | 52 } |
| 65 | 53 |
| 66 void LoadablePluginPlaceholder::SetPremadePlugin( | 54 void LoadablePluginPlaceholder::SetPremadePlugin( |
| 67 content::PluginInstanceThrottler* throttler) { | 55 content::PluginInstanceThrottler* throttler) { |
| 68 DCHECK(throttler); | 56 DCHECK(throttler); |
| 69 DCHECK(!premade_throttler_); | 57 DCHECK(!premade_throttler_); |
| 70 premade_throttler_ = throttler; | 58 premade_throttler_ = throttler; |
| 71 } | 59 } |
| 72 #endif | |
| 73 | 60 |
| 74 LoadablePluginPlaceholder::LoadablePluginPlaceholder( | 61 LoadablePluginPlaceholder::LoadablePluginPlaceholder( |
| 75 content::RenderFrame* render_frame, | 62 content::RenderFrame* render_frame, |
| 76 WebLocalFrame* frame, | 63 blink::WebLocalFrame* frame, |
| 77 const WebPluginParams& params, | 64 const blink::WebPluginParams& params, |
| 78 const std::string& html_data, | 65 const std::string& html_data) |
| 79 GURL placeholderDataUrl) | 66 : PluginPlaceholderBase(render_frame, frame, params, html_data), |
| 80 : PluginPlaceholder(render_frame, | |
| 81 frame, | |
| 82 params, | |
| 83 html_data, | |
| 84 placeholderDataUrl), | |
| 85 is_blocked_for_background_tab_(false), | 67 is_blocked_for_background_tab_(false), |
| 86 is_blocked_for_prerendering_(false), | 68 is_blocked_for_prerendering_(false), |
| 87 is_blocked_for_power_saver_poster_(false), | 69 is_blocked_for_power_saver_poster_(false), |
| 88 power_saver_enabled_(false), | 70 power_saver_enabled_(false), |
| 89 premade_throttler_(nullptr), | 71 premade_throttler_(nullptr), |
| 90 allow_loading_(false), | 72 allow_loading_(true), |
| 91 hidden_(false), | |
| 92 finished_loading_(false), | 73 finished_loading_(false), |
| 93 in_size_recheck_(false), | 74 in_size_recheck_(false), |
| 94 weak_factory_(this) { | 75 weak_factory_(this) { |
| 95 } | 76 } |
| 96 | 77 |
| 97 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { | 78 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { |
| 98 } | 79 } |
| 99 | 80 |
| 100 #if defined(ENABLE_PLUGINS) | |
| 101 void LoadablePluginPlaceholder::MarkPluginEssential( | 81 void LoadablePluginPlaceholder::MarkPluginEssential( |
| 102 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | 82 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { |
| 103 if (!power_saver_enabled_) | 83 if (!power_saver_enabled_) |
| 104 return; | 84 return; |
| 105 | 85 |
| 106 power_saver_enabled_ = false; | 86 power_saver_enabled_ = false; |
| 107 | 87 |
| 108 if (premade_throttler_) | 88 if (premade_throttler_) |
| 109 premade_throttler_->MarkPluginEssential(method); | 89 premade_throttler_->MarkPluginEssential(method); |
| 110 else | 90 else |
| 111 PluginInstanceThrottler::RecordUnthrottleMethodMetric(method); | 91 PluginInstanceThrottler::RecordUnthrottleMethodMetric(method); |
| 112 | 92 |
| 113 if (is_blocked_for_power_saver_poster_) { | 93 if (is_blocked_for_power_saver_poster_) { |
| 114 is_blocked_for_power_saver_poster_ = false; | 94 is_blocked_for_power_saver_poster_ = false; |
| 115 if (!LoadingBlocked()) | 95 if (!LoadingBlocked()) |
| 116 LoadPlugin(); | 96 LoadPlugin(); |
| 117 } | 97 } |
| 118 } | 98 } |
| 119 #endif | |
| 120 | 99 |
| 121 void LoadablePluginPlaceholder::BindWebFrame(blink::WebFrame* frame) { | 100 void LoadablePluginPlaceholder::ReplacePlugin(blink::WebPlugin* new_plugin) { |
| 122 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 123 v8::HandleScope handle_scope(isolate); | |
| 124 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
| 125 DCHECK(!context.IsEmpty()); | |
| 126 | |
| 127 v8::Context::Scope context_scope(context); | |
| 128 v8::Local<v8::Object> global = context->Global(); | |
| 129 global->Set(gin::StringToV8(isolate, "plugin"), | |
| 130 gin::CreateHandle(isolate, this).ToV8()); | |
| 131 } | |
| 132 | |
| 133 gin::ObjectTemplateBuilder LoadablePluginPlaceholder::GetObjectTemplateBuilder( | |
| 134 v8::Isolate* isolate) { | |
| 135 return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate) | |
| 136 .SetMethod("load", &LoadablePluginPlaceholder::LoadCallback) | |
| 137 .SetMethod("hide", &LoadablePluginPlaceholder::HideCallback) | |
| 138 .SetMethod("didFinishLoading", | |
| 139 &LoadablePluginPlaceholder::DidFinishLoadingCallback); | |
| 140 } | |
| 141 | |
| 142 void LoadablePluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | |
| 143 CHECK(plugin()); | 101 CHECK(plugin()); |
| 144 if (!new_plugin) | 102 if (!new_plugin) |
| 145 return; | 103 return; |
| 146 WebPluginContainer* container = plugin()->container(); | 104 blink::WebPluginContainer* container = plugin()->container(); |
| 147 // Set the new plugin on the container before initializing it. | 105 // Set the new plugin on the container before initializing it. |
| 148 container->setPlugin(new_plugin); | 106 container->setPlugin(new_plugin); |
| 149 // Save the element in case the plugin is removed from the page during | 107 // Save the element in case the plugin is removed from the page during |
| 150 // initialization. | 108 // initialization. |
| 151 WebElement element = container->element(); | 109 blink::WebElement element = container->element(); |
| 152 bool plugin_needs_initialization = | 110 bool plugin_needs_initialization = |
| 153 !premade_throttler_ || new_plugin != premade_throttler_->GetWebPlugin(); | 111 !premade_throttler_ || new_plugin != premade_throttler_->GetWebPlugin(); |
| 154 if (plugin_needs_initialization && !new_plugin->initialize(container)) { | 112 if (plugin_needs_initialization && !new_plugin->initialize(container)) { |
| 155 // We couldn't initialize the new plugin. Restore the old one and abort. | 113 // We couldn't initialize the new plugin. Restore the old one and abort. |
| 156 container->setPlugin(plugin()); | 114 container->setPlugin(plugin()); |
| 157 return; | 115 return; |
| 158 } | 116 } |
| 159 | 117 |
| 160 // The plugin has been removed from the page. Destroy the old plugin. We | 118 // The plugin has been removed from the page. Destroy the old plugin. We |
| 161 // will be destroyed as soon as V8 garbage collects us. | 119 // will be destroyed as soon as V8 garbage collects us. |
| 162 if (!element.pluginContainer()) { | 120 if (!element.pluginContainer()) { |
| 163 plugin()->destroy(); | 121 plugin()->destroy(); |
| 164 return; | 122 return; |
| 165 } | 123 } |
| 166 | 124 |
| 167 // During initialization, the new plugin might have replaced itself in turn | 125 // During initialization, the new plugin might have replaced itself in turn |
| 168 // with another plugin. Make sure not to use the passed in |new_plugin| after | 126 // with another plugin. Make sure not to use the passed in |new_plugin| after |
| 169 // this point. | 127 // this point. |
| 170 new_plugin = container->plugin(); | 128 new_plugin = container->plugin(); |
| 171 | 129 |
| 172 plugin()->RestoreTitleText(); | 130 plugin()->RestoreTitleText(); |
| 173 container->invalidate(); | 131 container->invalidate(); |
| 174 container->reportGeometry(); | 132 container->reportGeometry(); |
| 175 plugin()->ReplayReceivedData(new_plugin); | 133 plugin()->ReplayReceivedData(new_plugin); |
| 176 plugin()->destroy(); | 134 plugin()->destroy(); |
| 177 } | 135 } |
| 178 | 136 |
| 179 void LoadablePluginPlaceholder::HidePlugin() { | |
| 180 hidden_ = true; | |
| 181 if (!plugin()) | |
| 182 return; | |
| 183 WebPluginContainer* container = plugin()->container(); | |
| 184 WebElement element = container->element(); | |
| 185 element.setAttribute("style", "display: none;"); | |
| 186 // If we have a width and height, search for a parent (often <div>) with the | |
| 187 // same dimensions. If we find such a parent, hide that as well. | |
| 188 // This makes much more uncovered page content usable (including clickable) | |
| 189 // as opposed to merely visible. | |
| 190 // TODO(cevans) -- it's a foul heuristic but we're going to tolerate it for | |
| 191 // now for these reasons: | |
| 192 // 1) Makes the user experience better. | |
| 193 // 2) Foulness is encapsulated within this single function. | |
| 194 // 3) Confidence in no fasle positives. | |
| 195 // 4) Seems to have a good / low false negative rate at this time. | |
| 196 if (element.hasAttribute("width") && element.hasAttribute("height")) { | |
| 197 std::string width_str("width:[\\s]*"); | |
| 198 width_str += element.getAttribute("width").utf8().data(); | |
| 199 if (EndsWith(width_str, "px", false)) { | |
| 200 width_str = width_str.substr(0, width_str.length() - 2); | |
| 201 } | |
| 202 base::TrimWhitespace(width_str, base::TRIM_TRAILING, &width_str); | |
| 203 width_str += "[\\s]*px"; | |
| 204 std::string height_str("height:[\\s]*"); | |
| 205 height_str += element.getAttribute("height").utf8().data(); | |
| 206 if (EndsWith(height_str, "px", false)) { | |
| 207 height_str = height_str.substr(0, height_str.length() - 2); | |
| 208 } | |
| 209 base::TrimWhitespace(height_str, base::TRIM_TRAILING, &height_str); | |
| 210 height_str += "[\\s]*px"; | |
| 211 WebNode parent = element; | |
| 212 while (!parent.parentNode().isNull()) { | |
| 213 parent = parent.parentNode(); | |
| 214 if (!parent.isElementNode()) | |
| 215 continue; | |
| 216 element = parent.toConst<WebElement>(); | |
| 217 if (element.hasAttribute("style")) { | |
| 218 std::string style_str = element.getAttribute("style").utf8(); | |
| 219 if (RE2::PartialMatch(style_str, width_str) && | |
| 220 RE2::PartialMatch(style_str, height_str)) | |
| 221 element.setAttribute("style", "display: none;"); | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void LoadablePluginPlaceholder::SetMessage(const base::string16& message) { | 137 void LoadablePluginPlaceholder::SetMessage(const base::string16& message) { |
| 228 message_ = message; | 138 message_ = message; |
| 229 if (finished_loading_) | 139 if (finished_loading_) |
| 230 UpdateMessage(); | 140 UpdateMessage(); |
| 231 } | 141 } |
| 232 | 142 |
| 233 void LoadablePluginPlaceholder::UpdateMessage() { | 143 void LoadablePluginPlaceholder::UpdateMessage() { |
| 234 if (!plugin()) | 144 if (!plugin()) |
| 235 return; | 145 return; |
| 236 std::string script = | 146 std::string script = |
| 237 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; | 147 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; |
| 238 plugin()->web_view()->mainFrame()->executeScript( | 148 plugin()->web_view()->mainFrame()->executeScript( |
| 239 WebScriptSource(base::UTF8ToUTF16(script))); | 149 blink::WebScriptSource(base::UTF8ToUTF16(script))); |
| 240 } | 150 } |
| 241 | 151 |
| 242 void LoadablePluginPlaceholder::PluginDestroyed() { | 152 void LoadablePluginPlaceholder::PluginDestroyed() { |
| 243 #if defined(ENABLE_PLUGINS) | |
| 244 if (power_saver_enabled_) { | 153 if (power_saver_enabled_) { |
| 245 if (premade_throttler_) { | 154 if (premade_throttler_) { |
| 246 // Since the premade plugin has been detached from the container, it will | 155 // Since the premade plugin has been detached from the container, it will |
| 247 // not be automatically destroyed along with the page. | 156 // not be automatically destroyed along with the page. |
| 248 premade_throttler_->GetWebPlugin()->destroy(); | 157 premade_throttler_->GetWebPlugin()->destroy(); |
| 249 premade_throttler_ = nullptr; | 158 premade_throttler_ = nullptr; |
| 250 } else if (is_blocked_for_power_saver_poster_) { | 159 } else if (is_blocked_for_power_saver_poster_) { |
| 251 // Record the NEVER unthrottle count only if there is no throttler. | 160 // Record the NEVER unthrottle count only if there is no throttler. |
| 252 PluginInstanceThrottler::RecordUnthrottleMethodMetric( | 161 PluginInstanceThrottler::RecordUnthrottleMethodMetric( |
| 253 PluginInstanceThrottler::UNTHROTTLE_METHOD_NEVER); | 162 PluginInstanceThrottler::UNTHROTTLE_METHOD_NEVER); |
| 254 } | 163 } |
| 255 | 164 |
| 256 // Prevent processing subsequent calls to MarkPluginEssential. | 165 // Prevent processing subsequent calls to MarkPluginEssential. |
| 257 power_saver_enabled_ = false; | 166 power_saver_enabled_ = false; |
| 258 } | 167 } |
| 259 #endif | |
| 260 | 168 |
| 261 PluginPlaceholder::PluginDestroyed(); | 169 PluginPlaceholderBase::PluginDestroyed(); |
| 262 } | 170 } |
| 263 | 171 |
| 264 v8::Local<v8::Object> LoadablePluginPlaceholder::GetV8ScriptableObject( | 172 v8::Local<v8::Object> LoadablePluginPlaceholder::GetV8ScriptableObject( |
| 265 v8::Isolate* isolate) const { | 173 v8::Isolate* isolate) const { |
| 266 #if defined(ENABLE_PLUGINS) | |
| 267 // Pass through JavaScript access to the underlying throttled plugin. | 174 // Pass through JavaScript access to the underlying throttled plugin. |
| 268 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { | 175 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { |
| 269 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); | 176 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); |
| 270 } | 177 } |
| 271 #endif | |
| 272 return v8::Local<v8::Object>(); | 178 return v8::Local<v8::Object>(); |
| 273 } | 179 } |
| 274 | 180 |
| 275 #if defined(ENABLE_PLUGINS) | |
| 276 void LoadablePluginPlaceholder::OnUnobscuredSizeUpdate( | 181 void LoadablePluginPlaceholder::OnUnobscuredSizeUpdate( |
| 277 const gfx::Size& unobscured_size) { | 182 const gfx::Size& unobscured_size) { |
| 278 DCHECK( | 183 DCHECK( |
| 279 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 184 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 280 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_) | 185 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_) |
| 281 return; | 186 return; |
| 282 | 187 |
| 283 unobscured_size_ = unobscured_size; | 188 unobscured_size_ = unobscured_size; |
| 284 | 189 |
| 285 // During a size recheck, we will get another notification into this method. | 190 // During a size recheck, we will get another notification into this method. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 297 FROM_HERE, base::TimeDelta::FromMilliseconds( | 202 FROM_HERE, base::TimeDelta::FromMilliseconds( |
| 298 kSizeChangeRecheckDelayMilliseconds), | 203 kSizeChangeRecheckDelayMilliseconds), |
| 299 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, | 204 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, |
| 300 weak_factory_.GetWeakPtr())); | 205 weak_factory_.GetWeakPtr())); |
| 301 } | 206 } |
| 302 } else { | 207 } else { |
| 303 // Cancel any pending unthrottle due to resize calls. | 208 // Cancel any pending unthrottle due to resize calls. |
| 304 size_update_timer_.Stop(); | 209 size_update_timer_.Stop(); |
| 305 } | 210 } |
| 306 } | 211 } |
| 307 #endif // defined(ENABLE_PLUGINS) | |
| 308 | 212 |
| 309 void LoadablePluginPlaceholder::WasShown() { | 213 void LoadablePluginPlaceholder::WasShown() { |
| 310 if (is_blocked_for_background_tab_) { | 214 if (is_blocked_for_background_tab_) { |
| 311 is_blocked_for_background_tab_ = false; | 215 is_blocked_for_background_tab_ = false; |
| 312 if (!LoadingBlocked()) | 216 if (!LoadingBlocked()) |
| 313 LoadPlugin(); | 217 LoadPlugin(); |
| 314 } | 218 } |
| 315 } | 219 } |
| 316 | 220 |
| 317 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( | 221 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 330 if (is_blocked_for_prerendering_) { | 234 if (is_blocked_for_prerendering_) { |
| 331 is_blocked_for_prerendering_ = false; | 235 is_blocked_for_prerendering_ = false; |
| 332 if (!LoadingBlocked()) | 236 if (!LoadingBlocked()) |
| 333 LoadPlugin(); | 237 LoadPlugin(); |
| 334 } | 238 } |
| 335 } | 239 } |
| 336 | 240 |
| 337 void LoadablePluginPlaceholder::LoadPlugin() { | 241 void LoadablePluginPlaceholder::LoadPlugin() { |
| 338 // This is not strictly necessary but is an important defense in case the | 242 // This is not strictly necessary but is an important defense in case the |
| 339 // event propagation changes between "close" vs. "click-to-play". | 243 // event propagation changes between "close" vs. "click-to-play". |
| 340 if (hidden_) | 244 if (hidden()) |
| 341 return; | 245 return; |
| 342 if (!plugin()) | 246 if (!plugin()) |
| 343 return; | 247 return; |
| 344 if (!allow_loading_) { | 248 if (!allow_loading_) { |
| 345 NOTREACHED(); | 249 NOTREACHED(); |
| 346 return; | 250 return; |
| 347 } | 251 } |
| 348 | 252 |
| 349 if (premade_throttler_) { | 253 if (premade_throttler_) { |
| 350 premade_throttler_->SetHiddenForPlaceholder(false /* hidden */); | 254 premade_throttler_->SetHiddenForPlaceholder(false /* hidden */); |
| 351 ReplacePlugin(premade_throttler_->GetWebPlugin()); | 255 ReplacePlugin(premade_throttler_->GetWebPlugin()); |
| 352 premade_throttler_ = nullptr; | 256 premade_throttler_ = nullptr; |
| 353 } else { | 257 } else { |
| 354 ReplacePlugin(CreatePlugin()); | 258 ReplacePlugin(CreatePlugin()); |
| 355 } | 259 } |
| 356 } | 260 } |
| 357 | 261 |
| 358 void LoadablePluginPlaceholder::LoadCallback() { | 262 void LoadablePluginPlaceholder::LoadCallback() { |
| 359 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); | 263 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); |
| 360 #if defined(ENABLE_PLUGINS) | |
| 361 // If the user specifically clicks on the plugin content's placeholder, | 264 // If the user specifically clicks on the plugin content's placeholder, |
| 362 // disable power saver throttling for this instance. | 265 // disable power saver throttling for this instance. |
| 363 MarkPluginEssential(PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK); | 266 MarkPluginEssential(PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK); |
| 364 #endif | |
| 365 LoadPlugin(); | 267 LoadPlugin(); |
| 366 } | 268 } |
| 367 | 269 |
| 368 void LoadablePluginPlaceholder::HideCallback() { | |
| 369 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); | |
| 370 HidePlugin(); | |
| 371 } | |
| 372 | |
| 373 void LoadablePluginPlaceholder::DidFinishLoadingCallback() { | 270 void LoadablePluginPlaceholder::DidFinishLoadingCallback() { |
| 374 finished_loading_ = true; | 271 finished_loading_ = true; |
| 375 if (message_.length() > 0) | 272 if (message_.length() > 0) |
| 376 UpdateMessage(); | 273 UpdateMessage(); |
| 377 | 274 |
| 378 // Wait for the placeholder to finish loading to hide the premade plugin. | 275 // Wait for the placeholder to finish loading to hide the premade plugin. |
| 379 // This is necessary to prevent a flicker. | 276 // This is necessary to prevent a flicker. |
| 380 if (premade_throttler_ && power_saver_enabled_) | 277 if (premade_throttler_ && power_saver_enabled_) |
| 381 premade_throttler_->SetHiddenForPlaceholder(true /* hidden */); | 278 premade_throttler_->SetHiddenForPlaceholder(true /* hidden */); |
| 382 | 279 |
| 383 // Set an attribute and post an event, so browser tests can wait for the | 280 // Set an attribute and post an event, so browser tests can wait for the |
| 384 // placeholder to be ready to receive simulated user input. | 281 // placeholder to be ready to receive simulated user input. |
| 385 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 282 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 386 switches::kEnablePluginPlaceholderTesting)) { | 283 switches::kEnablePluginPlaceholderTesting)) { |
| 387 WebElement element = plugin()->container()->element(); | 284 blink::WebElement element = plugin()->container()->element(); |
| 388 element.setAttribute("placeholderLoaded", "true"); | 285 element.setAttribute("placeholderLoaded", "true"); |
| 389 | 286 |
| 390 scoped_ptr<content::V8ValueConverter> converter( | 287 scoped_ptr<content::V8ValueConverter> converter( |
| 391 content::V8ValueConverter::create()); | 288 content::V8ValueConverter::create()); |
| 392 base::StringValue value("placeholderLoaded"); | 289 base::StringValue value("placeholderLoaded"); |
| 393 blink::WebSerializedScriptValue message_data = | 290 blink::WebSerializedScriptValue message_data = |
| 394 blink::WebSerializedScriptValue::serialize(converter->ToV8Value( | 291 blink::WebSerializedScriptValue::serialize(converter->ToV8Value( |
| 395 &value, element.document().frame()->mainWorldScriptContext())); | 292 &value, element.document().frame()->mainWorldScriptContext())); |
| 396 | 293 |
| 397 blink::WebDOMEvent event = element.document().createEvent("MessageEvent"); | 294 blink::WebDOMEvent event = element.document().createEvent("MessageEvent"); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 423 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { | 320 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { |
| 424 return identifier_; | 321 return identifier_; |
| 425 } | 322 } |
| 426 | 323 |
| 427 bool LoadablePluginPlaceholder::LoadingBlocked() const { | 324 bool LoadablePluginPlaceholder::LoadingBlocked() const { |
| 428 DCHECK(allow_loading_); | 325 DCHECK(allow_loading_); |
| 429 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || | 326 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || |
| 430 is_blocked_for_prerendering_; | 327 is_blocked_for_prerendering_; |
| 431 } | 328 } |
| 432 | 329 |
| 433 #if defined(ENABLE_PLUGINS) | |
| 434 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() { | 330 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() { |
| 435 DCHECK( | 331 DCHECK( |
| 436 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 332 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 437 DCHECK(!in_size_recheck_); | 333 DCHECK(!in_size_recheck_); |
| 438 in_size_recheck_ = true; | 334 in_size_recheck_ = true; |
| 439 | 335 |
| 440 // Re-check the size in case the reported size was incorrect. | 336 // Re-check the size in case the reported size was incorrect. |
| 441 plugin()->container()->reportGeometry(); | 337 plugin()->container()->reportGeometry(); |
| 442 | 338 |
| 443 if (PluginInstanceThrottler::IsLargeContent(unobscured_size_.width(), | 339 if (PluginInstanceThrottler::IsLargeContent(unobscured_size_.width(), |
| 444 unobscured_size_.height())) { | 340 unobscured_size_.height())) { |
| 445 MarkPluginEssential( | 341 MarkPluginEssential( |
| 446 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE); | 342 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE); |
| 447 } | 343 } |
| 448 | 344 |
| 449 in_size_recheck_ = false; | 345 in_size_recheck_ = false; |
| 450 } | 346 } |
| 451 #endif // defined(ENABLE_PLUGINS) | |
| 452 | 347 |
| 453 } // namespace plugins | 348 } // namespace plugins |
| OLD | NEW |