| 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/auto_reset.h" | |
| 8 #include "base/bind.h" | 7 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 10 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 11 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.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/renderer/render_frame.h" | 16 #include "content/public/renderer/render_frame.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 28 #include "third_party/WebKit/public/web/WebView.h" | 27 #include "third_party/WebKit/public/web/WebView.h" |
| 29 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 30 #include "url/origin.h" | 29 #include "url/origin.h" |
| 31 | 30 |
| 32 using base::UserMetricsAction; | 31 using base::UserMetricsAction; |
| 33 using content::PluginInstanceThrottler; | 32 using content::PluginInstanceThrottler; |
| 34 using content::RenderThread; | 33 using content::RenderThread; |
| 35 | 34 |
| 36 namespace plugins { | 35 namespace plugins { |
| 37 | 36 |
| 38 // TODO(tommycli): After a size update, re-check the size after this delay, as | |
| 39 // Blink can report incorrect sizes to plugins while the compositing state is | |
| 40 // dirty. Chosen because it seems to work. | |
| 41 const int kSizeChangeRecheckDelayMilliseconds = 100; | |
| 42 | |
| 43 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { | 37 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { |
| 44 DCHECK(!is_blocked_for_power_saver_poster_); | 38 DCHECK(!is_blocked_for_power_saver_poster_); |
| 45 is_blocked_for_power_saver_poster_ = true; | 39 is_blocked_for_power_saver_poster_ = true; |
| 46 | 40 |
| 47 render_frame()->RegisterPeripheralPlugin( | 41 render_frame()->RegisterPeripheralPlugin( |
| 48 url::Origin(GURL(GetPluginParams().url)), | 42 url::Origin(GURL(GetPluginParams().url)), |
| 49 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, | 43 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, |
| 50 weak_factory_.GetWeakPtr(), | 44 weak_factory_.GetWeakPtr(), |
| 51 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); | 45 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); |
| 52 } | 46 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 64 const blink::WebPluginParams& params, | 58 const blink::WebPluginParams& params, |
| 65 const std::string& html_data) | 59 const std::string& html_data) |
| 66 : PluginPlaceholderBase(render_frame, frame, params, html_data), | 60 : PluginPlaceholderBase(render_frame, frame, params, html_data), |
| 67 is_blocked_for_background_tab_(false), | 61 is_blocked_for_background_tab_(false), |
| 68 is_blocked_for_prerendering_(false), | 62 is_blocked_for_prerendering_(false), |
| 69 is_blocked_for_power_saver_poster_(false), | 63 is_blocked_for_power_saver_poster_(false), |
| 70 power_saver_enabled_(false), | 64 power_saver_enabled_(false), |
| 71 premade_throttler_(nullptr), | 65 premade_throttler_(nullptr), |
| 72 allow_loading_(false), | 66 allow_loading_(false), |
| 73 finished_loading_(false), | 67 finished_loading_(false), |
| 74 in_size_recheck_(false), | |
| 75 heuristic_run_before_(premade_throttler_ != nullptr), | 68 heuristic_run_before_(premade_throttler_ != nullptr), |
| 76 weak_factory_(this) {} | 69 weak_factory_(this) {} |
| 77 | 70 |
| 78 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { | 71 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { |
| 79 } | 72 } |
| 80 | 73 |
| 81 void LoadablePluginPlaceholder::MarkPluginEssential( | 74 void LoadablePluginPlaceholder::MarkPluginEssential( |
| 82 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | 75 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { |
| 83 if (!power_saver_enabled_) | 76 if (!power_saver_enabled_) |
| 84 return; | 77 return; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // Pass through JavaScript access to the underlying throttled plugin. | 176 // Pass through JavaScript access to the underlying throttled plugin. |
| 184 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { | 177 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { |
| 185 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); | 178 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); |
| 186 } | 179 } |
| 187 return v8::Local<v8::Object>(); | 180 return v8::Local<v8::Object>(); |
| 188 } | 181 } |
| 189 | 182 |
| 190 void LoadablePluginPlaceholder::OnUnobscuredRectUpdate( | 183 void LoadablePluginPlaceholder::OnUnobscuredRectUpdate( |
| 191 const gfx::Rect& unobscured_rect) { | 184 const gfx::Rect& unobscured_rect) { |
| 192 DCHECK(content::RenderThread::Get()); | 185 DCHECK(content::RenderThread::Get()); |
| 193 if (!power_saver_enabled_ || !finished_loading_) | 186 if (!plugin() || !power_saver_enabled_ || !finished_loading_) |
| 194 return; | 187 return; |
| 195 | 188 |
| 196 // Only update the unobscured rect during the recheck phase. Also early exit | 189 if (unobscured_rect_ == unobscured_rect) |
| 197 // to prevent reentrancy issues. | |
| 198 if (in_size_recheck_) { | |
| 199 unobscured_rect_ = unobscured_rect; | |
| 200 return; | 190 return; |
| 201 } | |
| 202 | 191 |
| 203 if (!size_update_timer_.IsRunning()) { | 192 unobscured_rect_ = unobscured_rect; |
| 204 // TODO(tommycli): We have to post a delayed task to recheck the size, as | 193 |
| 205 // Blink can report wrong sizes for partially obscured plugins while the | 194 float zoom_factor = plugin()->container()->pageZoomFactor(); |
| 206 // compositing state is dirty. https://crbug.com/343769 | 195 int width = roundf(unobscured_rect_.width() / zoom_factor); |
| 207 size_update_timer_.Start( | 196 int height = roundf(unobscured_rect_.height() / zoom_factor); |
| 208 FROM_HERE, | 197 |
| 209 base::TimeDelta::FromMilliseconds(kSizeChangeRecheckDelayMilliseconds), | 198 if (is_blocked_for_power_saver_poster_) { |
| 210 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, | 199 // Adjust poster container padding and dimensions to center play button for |
| 211 weak_factory_.GetWeakPtr())); | 200 // plugins and plugin posters that have their top or left portions obscured. |
| 201 int x = roundf(unobscured_rect_.x() / zoom_factor); |
| 202 int y = roundf(unobscured_rect_.y() / zoom_factor); |
| 203 std::string script = base::StringPrintf( |
| 204 "window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width, |
| 205 height); |
| 206 plugin()->web_view()->mainFrame()->executeScript( |
| 207 blink::WebScriptSource(base::UTF8ToUTF16(script))); |
| 208 |
| 209 // On a size update check if we now qualify as a essential plugin. |
| 210 url::Origin content_origin = url::Origin(GetPluginParams().url); |
| 211 auto status = render_frame()->GetPeripheralContentStatus( |
| 212 render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin, |
| 213 gfx::Size(width, height)); |
| 214 if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) { |
| 215 MarkPluginEssential( |
| 216 heuristic_run_before_ |
| 217 ? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE |
| 218 : PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD); |
| 219 |
| 220 if (!heuristic_run_before_ && |
| 221 status == |
| 222 content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) { |
| 223 render_frame()->WhitelistContentOrigin(content_origin); |
| 224 } |
| 225 } |
| 226 |
| 227 heuristic_run_before_ = true; |
| 212 } | 228 } |
| 213 } | 229 } |
| 214 | 230 |
| 215 void LoadablePluginPlaceholder::WasShown() { | 231 void LoadablePluginPlaceholder::WasShown() { |
| 216 if (is_blocked_for_background_tab_) { | 232 if (is_blocked_for_background_tab_) { |
| 217 is_blocked_for_background_tab_ = false; | 233 is_blocked_for_background_tab_ = false; |
| 218 if (!LoadingBlocked()) | 234 if (!LoadingBlocked()) |
| 219 LoadPlugin(); | 235 LoadPlugin(); |
| 220 } | 236 } |
| 221 } | 237 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { | 336 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { |
| 321 return identifier_; | 337 return identifier_; |
| 322 } | 338 } |
| 323 | 339 |
| 324 bool LoadablePluginPlaceholder::LoadingBlocked() const { | 340 bool LoadablePluginPlaceholder::LoadingBlocked() const { |
| 325 DCHECK(allow_loading_); | 341 DCHECK(allow_loading_); |
| 326 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || | 342 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || |
| 327 is_blocked_for_prerendering_; | 343 is_blocked_for_prerendering_; |
| 328 } | 344 } |
| 329 | 345 |
| 330 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() { | |
| 331 DCHECK(content::RenderThread::Get()); | |
| 332 DCHECK(!in_size_recheck_); | |
| 333 DCHECK(finished_loading_); | |
| 334 | |
| 335 base::AutoReset<bool> recheck_scope(&in_size_recheck_, true); | |
| 336 | |
| 337 if (!plugin()) | |
| 338 return; | |
| 339 | |
| 340 gfx::Rect old_rect = unobscured_rect_; | |
| 341 | |
| 342 // Re-check the size in case the reported size was incorrect. | |
| 343 plugin()->container()->reportGeometry(); | |
| 344 | |
| 345 if (old_rect == unobscured_rect_) | |
| 346 return; | |
| 347 | |
| 348 float zoom_factor = plugin()->container()->pageZoomFactor(); | |
| 349 int width = roundf(unobscured_rect_.width() / zoom_factor); | |
| 350 int height = roundf(unobscured_rect_.height() / zoom_factor); | |
| 351 | |
| 352 if (is_blocked_for_power_saver_poster_) { | |
| 353 // Adjust poster container padding and dimensions to center play button for | |
| 354 // plugins and plugin posters that have their top or left portions obscured. | |
| 355 int x = roundf(unobscured_rect_.x() / zoom_factor); | |
| 356 int y = roundf(unobscured_rect_.y() / zoom_factor); | |
| 357 std::string script = base::StringPrintf( | |
| 358 "window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width, | |
| 359 height); | |
| 360 plugin()->web_view()->mainFrame()->executeScript( | |
| 361 blink::WebScriptSource(base::UTF8ToUTF16(script))); | |
| 362 | |
| 363 // On a size update check if we now qualify as a essential plugin. | |
| 364 url::Origin content_origin = url::Origin(GetPluginParams().url); | |
| 365 auto status = render_frame()->GetPeripheralContentStatus( | |
| 366 render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin, | |
| 367 gfx::Size(width, height)); | |
| 368 if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) { | |
| 369 MarkPluginEssential( | |
| 370 heuristic_run_before_ | |
| 371 ? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE | |
| 372 : PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD); | |
| 373 | |
| 374 if (!heuristic_run_before_ && | |
| 375 status == | |
| 376 content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) { | |
| 377 render_frame()->WhitelistContentOrigin(content_origin); | |
| 378 } | |
| 379 } | |
| 380 | |
| 381 heuristic_run_before_ = true; | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 } // namespace plugins | 346 } // namespace plugins |
| OLD | NEW |