Chromium Code Reviews| 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 | |
|
groby-ooo-7-16
2015/12/15 01:28:13
Do we not have the "incorrect sizes" issue any mor
tommycli
2015/12/15 02:09:23
The incorrect size report bug has been mostly, but
| |
| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 // Pass through JavaScript access to the underlying throttled plugin. | 168 // Pass through JavaScript access to the underlying throttled plugin. |
| 176 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { | 169 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { |
| 177 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); | 170 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); |
| 178 } | 171 } |
| 179 return v8::Local<v8::Object>(); | 172 return v8::Local<v8::Object>(); |
| 180 } | 173 } |
| 181 | 174 |
| 182 void LoadablePluginPlaceholder::OnUnobscuredRectUpdate( | 175 void LoadablePluginPlaceholder::OnUnobscuredRectUpdate( |
| 183 const gfx::Rect& unobscured_rect) { | 176 const gfx::Rect& unobscured_rect) { |
| 184 DCHECK(content::RenderThread::Get()); | 177 DCHECK(content::RenderThread::Get()); |
| 185 if (!power_saver_enabled_ || !finished_loading_) | 178 if (!plugin() || !power_saver_enabled_ || !finished_loading_) |
| 186 return; | 179 return; |
| 187 | 180 |
| 188 // Only update the unobscured rect during the recheck phase. Also early exit | 181 if (unobscured_rect_ == unobscured_rect) |
| 189 // to prevent reentrancy issues. | |
| 190 if (in_size_recheck_) { | |
| 191 unobscured_rect_ = unobscured_rect; | |
| 192 return; | 182 return; |
| 193 } | |
| 194 | 183 |
| 195 if (!size_update_timer_.IsRunning()) { | 184 unobscured_rect_ = unobscured_rect; |
| 196 // TODO(tommycli): We have to post a delayed task to recheck the size, as | 185 |
| 197 // Blink can report wrong sizes for partially obscured plugins while the | 186 float zoom_factor = plugin()->container()->pageZoomFactor(); |
| 198 // compositing state is dirty. https://crbug.com/343769 | 187 int width = roundf(unobscured_rect_.width() / zoom_factor); |
| 199 size_update_timer_.Start( | 188 int height = roundf(unobscured_rect_.height() / zoom_factor); |
| 200 FROM_HERE, | 189 |
| 201 base::TimeDelta::FromMilliseconds(kSizeChangeRecheckDelayMilliseconds), | 190 if (is_blocked_for_power_saver_poster_) { |
| 202 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, | 191 // Adjust poster container padding and dimensions to center play button for |
| 203 weak_factory_.GetWeakPtr())); | 192 // plugins and plugin posters that have their top or left portions obscured. |
| 193 int x = roundf(unobscured_rect_.x() / zoom_factor); | |
| 194 int y = roundf(unobscured_rect_.y() / zoom_factor); | |
| 195 std::string script = base::StringPrintf( | |
| 196 "window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width, | |
| 197 height); | |
| 198 plugin()->web_view()->mainFrame()->executeScript( | |
| 199 blink::WebScriptSource(base::UTF8ToUTF16(script))); | |
| 200 | |
| 201 // On a size update check if we now qualify as a essential plugin. | |
| 202 url::Origin content_origin = url::Origin(GetPluginParams().url); | |
| 203 auto status = render_frame()->GetPeripheralContentStatus( | |
| 204 render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin, | |
| 205 gfx::Size(width, height)); | |
| 206 if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) { | |
| 207 MarkPluginEssential( | |
| 208 heuristic_run_before_ | |
| 209 ? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE | |
| 210 : PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD); | |
| 211 | |
| 212 if (!heuristic_run_before_ && | |
| 213 status == | |
| 214 content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) { | |
| 215 render_frame()->WhitelistContentOrigin(content_origin); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 heuristic_run_before_ = true; | |
| 204 } | 220 } |
| 205 } | 221 } |
| 206 | 222 |
| 207 void LoadablePluginPlaceholder::WasShown() { | 223 void LoadablePluginPlaceholder::WasShown() { |
| 208 if (is_blocked_for_background_tab_) { | 224 if (is_blocked_for_background_tab_) { |
| 209 is_blocked_for_background_tab_ = false; | 225 is_blocked_for_background_tab_ = false; |
| 210 if (!LoadingBlocked()) | 226 if (!LoadingBlocked()) |
| 211 LoadPlugin(); | 227 LoadPlugin(); |
| 212 } | 228 } |
| 213 } | 229 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { | 325 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { |
| 310 return identifier_; | 326 return identifier_; |
| 311 } | 327 } |
| 312 | 328 |
| 313 bool LoadablePluginPlaceholder::LoadingBlocked() const { | 329 bool LoadablePluginPlaceholder::LoadingBlocked() const { |
| 314 DCHECK(allow_loading_); | 330 DCHECK(allow_loading_); |
| 315 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || | 331 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || |
| 316 is_blocked_for_prerendering_; | 332 is_blocked_for_prerendering_; |
| 317 } | 333 } |
| 318 | 334 |
| 319 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() { | |
| 320 DCHECK(content::RenderThread::Get()); | |
| 321 DCHECK(!in_size_recheck_); | |
| 322 DCHECK(finished_loading_); | |
| 323 | |
| 324 base::AutoReset<bool> recheck_scope(&in_size_recheck_, true); | |
| 325 | |
| 326 if (!plugin()) | |
| 327 return; | |
| 328 | |
| 329 gfx::Rect old_rect = unobscured_rect_; | |
| 330 | |
| 331 // Re-check the size in case the reported size was incorrect. | |
| 332 plugin()->container()->reportGeometry(); | |
| 333 | |
| 334 if (old_rect == unobscured_rect_) | |
| 335 return; | |
| 336 | |
| 337 float zoom_factor = plugin()->container()->pageZoomFactor(); | |
| 338 int width = roundf(unobscured_rect_.width() / zoom_factor); | |
| 339 int height = roundf(unobscured_rect_.height() / zoom_factor); | |
| 340 | |
| 341 if (is_blocked_for_power_saver_poster_) { | |
| 342 // Adjust poster container padding and dimensions to center play button for | |
| 343 // plugins and plugin posters that have their top or left portions obscured. | |
| 344 int x = roundf(unobscured_rect_.x() / zoom_factor); | |
| 345 int y = roundf(unobscured_rect_.y() / zoom_factor); | |
| 346 std::string script = base::StringPrintf( | |
| 347 "window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width, | |
| 348 height); | |
| 349 plugin()->web_view()->mainFrame()->executeScript( | |
| 350 blink::WebScriptSource(base::UTF8ToUTF16(script))); | |
| 351 | |
| 352 // On a size update check if we now qualify as a essential plugin. | |
| 353 url::Origin content_origin = url::Origin(GetPluginParams().url); | |
| 354 auto status = render_frame()->GetPeripheralContentStatus( | |
| 355 render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin, | |
| 356 gfx::Size(width, height)); | |
| 357 if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) { | |
| 358 MarkPluginEssential( | |
| 359 heuristic_run_before_ | |
| 360 ? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE | |
| 361 : PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD); | |
| 362 | |
| 363 if (!heuristic_run_before_ && | |
| 364 status == | |
| 365 content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) { | |
| 366 render_frame()->WhitelistContentOrigin(content_origin); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 heuristic_run_before_ = true; | |
| 371 } | |
| 372 } | |
| 373 | |
| 374 } // namespace plugins | 335 } // namespace plugins |
| OLD | NEW |