| 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" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "content/public/child/v8_value_converter.h" | 16 #include "content/public/child/v8_value_converter.h" |
| 16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
| 17 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
| 18 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 19 #include "gin/handle.h" | 20 #include "gin/handle.h" |
| 20 #include "gin/object_template_builder.h" | 21 #include "gin/object_template_builder.h" |
| 21 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" | 22 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" |
| 22 #include "third_party/WebKit/public/web/WebDocument.h" | 23 #include "third_party/WebKit/public/web/WebDocument.h" |
| 23 #include "third_party/WebKit/public/web/WebElement.h" | 24 #include "third_party/WebKit/public/web/WebElement.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 39 using blink::WebPluginContainer; | 40 using blink::WebPluginContainer; |
| 40 using blink::WebPluginParams; | 41 using blink::WebPluginParams; |
| 41 using blink::WebScriptSource; | 42 using blink::WebScriptSource; |
| 42 using blink::WebURLRequest; | 43 using blink::WebURLRequest; |
| 43 using content::PluginInstanceThrottler; | 44 using content::PluginInstanceThrottler; |
| 44 using content::RenderThread; | 45 using content::RenderThread; |
| 45 | 46 |
| 46 namespace plugins { | 47 namespace plugins { |
| 47 | 48 |
| 48 #if defined(ENABLE_PLUGINS) | 49 #if defined(ENABLE_PLUGINS) |
| 50 // 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 |
| 52 // compositing state is dirty. Chosen because it seems to work. |
| 53 const int kSizeChangeRecheckDelayMilliseconds = 100; |
| 54 |
| 49 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { | 55 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { |
| 50 DCHECK(!is_blocked_for_power_saver_poster_); | 56 DCHECK(!is_blocked_for_power_saver_poster_); |
| 51 is_blocked_for_power_saver_poster_ = true; | 57 is_blocked_for_power_saver_poster_ = true; |
| 52 | 58 |
| 53 render_frame()->RegisterPeripheralPlugin( | 59 render_frame()->RegisterPeripheralPlugin( |
| 54 GURL(GetPluginParams().url).GetOrigin(), | 60 GURL(GetPluginParams().url).GetOrigin(), |
| 55 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, | 61 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, |
| 56 weak_factory_.GetWeakPtr(), | 62 weak_factory_.GetWeakPtr(), |
| 57 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); | 63 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); |
| 58 } | 64 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 77 html_data, | 83 html_data, |
| 78 placeholderDataUrl), | 84 placeholderDataUrl), |
| 79 is_blocked_for_background_tab_(false), | 85 is_blocked_for_background_tab_(false), |
| 80 is_blocked_for_prerendering_(false), | 86 is_blocked_for_prerendering_(false), |
| 81 is_blocked_for_power_saver_poster_(false), | 87 is_blocked_for_power_saver_poster_(false), |
| 82 power_saver_enabled_(false), | 88 power_saver_enabled_(false), |
| 83 premade_throttler_(nullptr), | 89 premade_throttler_(nullptr), |
| 84 allow_loading_(false), | 90 allow_loading_(false), |
| 85 hidden_(false), | 91 hidden_(false), |
| 86 finished_loading_(false), | 92 finished_loading_(false), |
| 93 in_size_recheck_(false), |
| 87 weak_factory_(this) { | 94 weak_factory_(this) { |
| 88 } | 95 } |
| 89 | 96 |
| 90 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { | 97 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { |
| 91 } | 98 } |
| 92 | 99 |
| 93 #if defined(ENABLE_PLUGINS) | 100 #if defined(ENABLE_PLUGINS) |
| 94 void LoadablePluginPlaceholder::MarkPluginEssential( | 101 void LoadablePluginPlaceholder::MarkPluginEssential( |
| 95 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | 102 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { |
| 96 if (!power_saver_enabled_) | 103 if (!power_saver_enabled_) |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 v8::Isolate* isolate) const { | 265 v8::Isolate* isolate) const { |
| 259 #if defined(ENABLE_PLUGINS) | 266 #if defined(ENABLE_PLUGINS) |
| 260 // Pass through JavaScript access to the underlying throttled plugin. | 267 // Pass through JavaScript access to the underlying throttled plugin. |
| 261 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { | 268 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { |
| 262 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); | 269 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); |
| 263 } | 270 } |
| 264 #endif | 271 #endif |
| 265 return v8::Local<v8::Object>(); | 272 return v8::Local<v8::Object>(); |
| 266 } | 273 } |
| 267 | 274 |
| 275 #if defined(ENABLE_PLUGINS) |
| 276 void LoadablePluginPlaceholder::OnUnobscuredSizeUpdate( |
| 277 const gfx::Size& unobscured_size) { |
| 278 DCHECK( |
| 279 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 280 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_) |
| 281 return; |
| 282 |
| 283 unobscured_size_ = unobscured_size; |
| 284 |
| 285 // During a size recheck, we will get another notification into this method. |
| 286 // Use this flag to early exit to prevent reentrancy issues. |
| 287 if (in_size_recheck_) |
| 288 return; |
| 289 |
| 290 if (PluginInstanceThrottler::IsLargeContent(unobscured_size.width(), |
| 291 unobscured_size.height())) { |
| 292 if (!size_update_timer_.IsRunning()) { |
| 293 // TODO(tommycli): We have to post a delayed task to recheck the size, as |
| 294 // Blink can report wrong sizes for partially obscured plugins while the |
| 295 // compositing state is dirty. https://crbug.com/343769 |
| 296 size_update_timer_.Start( |
| 297 FROM_HERE, base::TimeDelta::FromMilliseconds( |
| 298 kSizeChangeRecheckDelayMilliseconds), |
| 299 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, |
| 300 weak_factory_.GetWeakPtr())); |
| 301 } |
| 302 } else { |
| 303 // Cancel any pending unthrottle due to resize calls. |
| 304 size_update_timer_.Stop(); |
| 305 } |
| 306 } |
| 307 #endif // defined(ENABLE_PLUGINS) |
| 308 |
| 268 void LoadablePluginPlaceholder::WasShown() { | 309 void LoadablePluginPlaceholder::WasShown() { |
| 269 if (is_blocked_for_background_tab_) { | 310 if (is_blocked_for_background_tab_) { |
| 270 is_blocked_for_background_tab_ = false; | 311 is_blocked_for_background_tab_ = false; |
| 271 if (!LoadingBlocked()) | 312 if (!LoadingBlocked()) |
| 272 LoadPlugin(); | 313 LoadPlugin(); |
| 273 } | 314 } |
| 274 } | 315 } |
| 275 | 316 |
| 276 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( | 317 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( |
| 277 const std::string& identifier) { | 318 const std::string& identifier) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { | 423 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { |
| 383 return identifier_; | 424 return identifier_; |
| 384 } | 425 } |
| 385 | 426 |
| 386 bool LoadablePluginPlaceholder::LoadingBlocked() const { | 427 bool LoadablePluginPlaceholder::LoadingBlocked() const { |
| 387 DCHECK(allow_loading_); | 428 DCHECK(allow_loading_); |
| 388 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || | 429 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || |
| 389 is_blocked_for_prerendering_; | 430 is_blocked_for_prerendering_; |
| 390 } | 431 } |
| 391 | 432 |
| 433 #if defined(ENABLE_PLUGINS) |
| 434 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() { |
| 435 DCHECK( |
| 436 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 437 DCHECK(!in_size_recheck_); |
| 438 in_size_recheck_ = true; |
| 439 |
| 440 // Re-check the size in case the reported size was incorrect. |
| 441 plugin()->container()->reportGeometry(); |
| 442 |
| 443 if (PluginInstanceThrottler::IsLargeContent(unobscured_size_.width(), |
| 444 unobscured_size_.height())) { |
| 445 MarkPluginEssential( |
| 446 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE); |
| 447 } |
| 448 |
| 449 in_size_recheck_ = false; |
| 450 } |
| 451 #endif // defined(ENABLE_PLUGINS) |
| 452 |
| 392 } // namespace plugins | 453 } // namespace plugins |
| OLD | NEW |