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 "platform/scheduler/renderer/web_view_scheduler_impl.h" | 5 #include "platform/scheduler/renderer/web_view_scheduler_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "platform/RuntimeEnabledFeatures.h" | 9 #include "platform/RuntimeEnabledFeatures.h" |
| 10 #include "platform/WebFrameScheduler.h" | 10 #include "platform/WebFrameScheduler.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 // Given that we already align timers to 1Hz, do not report throttling if | 23 // Given that we already align timers to 1Hz, do not report throttling if |
| 24 // it is under 3s. | 24 // it is under 3s. |
| 25 constexpr base::TimeDelta kMinimalBackgroundThrottlingDurationToReport = | 25 constexpr base::TimeDelta kMinimalBackgroundThrottlingDurationToReport = |
| 26 base::TimeDelta::FromSeconds(3); | 26 base::TimeDelta::FromSeconds(3); |
| 27 constexpr base::TimeDelta kDefaultMaxBackgroundBudgetLevel = | 27 constexpr base::TimeDelta kDefaultMaxBackgroundBudgetLevel = |
| 28 base::TimeDelta::FromSeconds(3); | 28 base::TimeDelta::FromSeconds(3); |
| 29 constexpr base::TimeDelta kDefaultMaxBackgroundThrottlingDelay = | 29 constexpr base::TimeDelta kDefaultMaxBackgroundThrottlingDelay = |
| 30 base::TimeDelta::FromMinutes(1); | 30 base::TimeDelta::FromMinutes(1); |
| 31 constexpr base::TimeDelta kDefaultInitialBackgroundBudget = | 31 constexpr base::TimeDelta kDefaultInitialBackgroundBudget = |
| 32 base::TimeDelta::FromSeconds(1); | 32 base::TimeDelta::FromSeconds(1); |
| 33 constexpr base::TimeDelta kBackgroundBudgetThrottlingGracePeriod = | 33 constexpr base::TimeDelta kBackgroundThrottlingGracePeriod = |
| 34 base::TimeDelta::FromSeconds(10); | 34 base::TimeDelta::FromSeconds(10); |
| 35 | 35 |
| 36 // Values coming from WebViewSchedulerSettings are interpreted as follows: | 36 // Values coming from WebViewSchedulerSettings are interpreted as follows: |
| 37 // -1 is "not set". Scheduler should use a reasonable default. | 37 // -1 is "not set". Scheduler should use a reasonable default. |
| 38 // 0 is "none". base::nullopt will be used if value is optional. | 38 // 0 is "none". base::nullopt will be used if value is optional. |
| 39 // other values are left without changes. | 39 // other values are left without changes. |
| 40 | 40 |
| 41 double GetBackgroundBudgetRecoveryRate( | 41 double GetBackgroundBudgetRecoveryRate( |
| 42 WebViewScheduler::WebViewSchedulerSettings* settings) { | 42 WebViewScheduler::WebViewSchedulerSettings* settings) { |
| 43 if (!settings) | 43 if (!settings) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 disable_background_timer_throttling_(disable_background_timer_throttling), | 104 disable_background_timer_throttling_(disable_background_timer_throttling), |
| 105 allow_virtual_time_to_advance_(true), | 105 allow_virtual_time_to_advance_(true), |
| 106 have_seen_loading_task_(false), | 106 have_seen_loading_task_(false), |
| 107 virtual_time_(false), | 107 virtual_time_(false), |
| 108 is_audio_playing_(false), | 108 is_audio_playing_(false), |
| 109 reported_background_throttling_since_navigation_(false), | 109 reported_background_throttling_since_navigation_(false), |
| 110 background_time_budget_pool_(nullptr), | 110 background_time_budget_pool_(nullptr), |
| 111 settings_(settings) { | 111 settings_(settings) { |
| 112 renderer_scheduler->AddWebViewScheduler(this); | 112 renderer_scheduler->AddWebViewScheduler(this); |
| 113 | 113 |
| 114 delayed_background_budget_throttling_enabler_.Reset( | 114 delayed_background_throttling_enabler_.Reset( |
| 115 base::Bind(&WebViewSchedulerImpl::EnableBackgroundBudgetThrottling, | 115 base::Bind(&WebViewSchedulerImpl::EnableBackgroundThrottling, |
| 116 base::Unretained(this))); | 116 base::Unretained(this))); |
| 117 } | 117 } |
| 118 | 118 |
| 119 WebViewSchedulerImpl::~WebViewSchedulerImpl() { | 119 WebViewSchedulerImpl::~WebViewSchedulerImpl() { |
| 120 // TODO(alexclarke): Find out why we can't rely on the web view outliving the | 120 // TODO(alexclarke): Find out why we can't rely on the web view outliving the |
| 121 // frame. | 121 // frame. |
| 122 for (WebFrameSchedulerImpl* frame_scheduler : frame_schedulers_) { | 122 for (WebFrameSchedulerImpl* frame_scheduler : frame_schedulers_) { |
| 123 frame_scheduler->DetachFromWebViewScheduler(); | 123 frame_scheduler->DetachFromWebViewScheduler(); |
| 124 } | 124 } |
| 125 renderer_scheduler_->RemoveWebViewScheduler(this); | 125 renderer_scheduler_->RemoveWebViewScheduler(this); |
| 126 | 126 |
| 127 if (background_time_budget_pool_) | 127 if (background_time_budget_pool_) |
| 128 background_time_budget_pool_->Close(); | 128 background_time_budget_pool_->Close(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void WebViewSchedulerImpl::setPageVisible(bool page_visible) { | 131 void WebViewSchedulerImpl::setPageVisible(bool page_visible) { |
| 132 if (disable_background_timer_throttling_ || page_visible_ == page_visible) | 132 if (disable_background_timer_throttling_ || page_visible_ == page_visible) |
| 133 return; | 133 return; |
| 134 | 134 |
| 135 page_visible_ = page_visible; | 135 page_visible_ = page_visible; |
| 136 | 136 |
| 137 for (WebFrameSchedulerImpl* frame_scheduler : frame_schedulers_) { | 137 UpdateBackgroundThrottlingState(); |
| 138 frame_scheduler->setPageVisible(page_visible_); | |
| 139 } | |
| 140 | |
| 141 UpdateBackgroundBudgetThrottlingState(); | |
| 142 } | 138 } |
| 143 | 139 |
| 144 std::unique_ptr<WebFrameSchedulerImpl> | 140 std::unique_ptr<WebFrameSchedulerImpl> |
| 145 WebViewSchedulerImpl::createWebFrameSchedulerImpl( | 141 WebViewSchedulerImpl::createWebFrameSchedulerImpl( |
| 146 base::trace_event::BlameContext* blame_context) { | 142 base::trace_event::BlameContext* blame_context) { |
| 147 MaybeInitializeBackgroundTimeBudgetPool(); | 143 MaybeInitializeBackgroundTimeBudgetPool(); |
| 148 std::unique_ptr<WebFrameSchedulerImpl> frame_scheduler( | 144 std::unique_ptr<WebFrameSchedulerImpl> frame_scheduler( |
| 149 new WebFrameSchedulerImpl(renderer_scheduler_, this, blame_context)); | 145 new WebFrameSchedulerImpl(renderer_scheduler_, this, blame_context)); |
| 150 frame_scheduler->setPageVisible(page_visible_); | 146 frame_scheduler->setPageVisible(page_visible_); |
| 151 frame_schedulers_.insert(frame_scheduler.get()); | 147 frame_schedulers_.insert(frame_scheduler.get()); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 return; | 291 return; |
| 296 | 292 |
| 297 if (!RuntimeEnabledFeatures::expensiveBackgroundTimerThrottlingEnabled()) | 293 if (!RuntimeEnabledFeatures::expensiveBackgroundTimerThrottlingEnabled()) |
| 298 return; | 294 return; |
| 299 | 295 |
| 300 background_time_budget_pool_ = | 296 background_time_budget_pool_ = |
| 301 renderer_scheduler_->task_queue_throttler()->CreateTimeBudgetPool( | 297 renderer_scheduler_->task_queue_throttler()->CreateTimeBudgetPool( |
| 302 "background", GetMaxBudgetLevel(settings_), | 298 "background", GetMaxBudgetLevel(settings_), |
| 303 GetMaxThrottlingDelay(settings_)); | 299 GetMaxThrottlingDelay(settings_)); |
| 304 | 300 |
| 305 UpdateBackgroundBudgetThrottlingState(); | 301 UpdateBackgroundThrottlingState(); |
| 306 | 302 |
| 307 LazyNow lazy_now(renderer_scheduler_->tick_clock()); | 303 LazyNow lazy_now(renderer_scheduler_->tick_clock()); |
| 308 | 304 |
| 309 background_time_budget_pool_->SetTimeBudgetRecoveryRate( | 305 background_time_budget_pool_->SetTimeBudgetRecoveryRate( |
| 310 lazy_now.Now(), GetBackgroundBudgetRecoveryRate(settings_)); | 306 lazy_now.Now(), GetBackgroundBudgetRecoveryRate(settings_)); |
| 311 | 307 |
| 312 background_time_budget_pool_->GrantAdditionalBudget( | 308 background_time_budget_pool_->GrantAdditionalBudget( |
| 313 lazy_now.Now(), GetInitialBudget(settings_)); | 309 lazy_now.Now(), GetInitialBudget(settings_)); |
| 314 } | 310 } |
| 315 | 311 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 326 "Timer tasks have taken too much time while the page was in the " | 322 "Timer tasks have taken too much time while the page was in the " |
| 327 "background. " | 323 "background. " |
| 328 "As a result, they have been deferred for %.3f seconds. " | 324 "As a result, they have been deferred for %.3f seconds. " |
| 329 "See https://www.chromestatus.com/feature/6172836527865856 " | 325 "See https://www.chromestatus.com/feature/6172836527865856 " |
| 330 "for more details", | 326 "for more details", |
| 331 throttling_duration.InSecondsF()); | 327 throttling_duration.InSecondsF()); |
| 332 | 328 |
| 333 intervention_reporter_->ReportIntervention(WebString::fromUTF8(message)); | 329 intervention_reporter_->ReportIntervention(WebString::fromUTF8(message)); |
| 334 } | 330 } |
| 335 | 331 |
| 336 void WebViewSchedulerImpl::EnableBackgroundBudgetThrottling() { | 332 void WebViewSchedulerImpl::EnableBackgroundThrottling() { |
| 337 if (!background_time_budget_pool_) | 333 for (WebFrameSchedulerImpl* frame_scheduler : frame_schedulers_) { |
| 338 return; | 334 frame_scheduler->setPageVisible(false); |
|
Sami
2017/01/10 14:44:22
Should we rename |setPageVisible| to |setThrottlin
alex clarke (OOO till 29th)
2017/01/11 10:54:47
You're probably right. After this patch the WebFr
| |
| 339 | 335 } |
| 340 LazyNow lazy_now(renderer_scheduler_->tick_clock()); | 336 if (background_time_budget_pool_) { |
| 341 | 337 LazyNow lazy_now(renderer_scheduler_->tick_clock()); |
| 342 background_time_budget_pool_->EnableThrottling(&lazy_now); | 338 background_time_budget_pool_->EnableThrottling(&lazy_now); |
| 339 } | |
| 343 } | 340 } |
| 344 | 341 |
| 345 void WebViewSchedulerImpl::UpdateBackgroundBudgetThrottlingState() { | 342 void WebViewSchedulerImpl::UpdateBackgroundThrottlingState() { |
| 346 if (!background_time_budget_pool_) | 343 delayed_background_throttling_enabler_.Cancel(); |
| 347 return; | |
| 348 | |
| 349 delayed_background_budget_throttling_enabler_.Cancel(); | |
| 350 | |
| 351 LazyNow lazy_now(renderer_scheduler_->tick_clock()); | |
| 352 | 344 |
| 353 if (page_visible_) { | 345 if (page_visible_) { |
| 354 background_time_budget_pool_->DisableThrottling(&lazy_now); | 346 for (WebFrameSchedulerImpl* frame_scheduler : frame_schedulers_) { |
| 347 frame_scheduler->setPageVisible(true); | |
| 348 } | |
| 349 if (background_time_budget_pool_) { | |
| 350 LazyNow lazy_now(renderer_scheduler_->tick_clock()); | |
| 351 background_time_budget_pool_->DisableThrottling(&lazy_now); | |
| 352 } | |
| 355 } else { | 353 } else { |
| 356 // TODO(altimin): Consider moving this logic into PumpThrottledTasks. | 354 // TODO(altimin): Consider moving this logic into PumpThrottledTasks. |
| 357 renderer_scheduler_->ControlTaskRunner()->PostDelayedTask( | 355 renderer_scheduler_->ControlTaskRunner()->PostDelayedTask( |
| 358 FROM_HERE, delayed_background_budget_throttling_enabler_.callback(), | 356 FROM_HERE, delayed_background_throttling_enabler_.callback(), |
| 359 kBackgroundBudgetThrottlingGracePeriod); | 357 kBackgroundThrottlingGracePeriod); |
| 360 } | 358 } |
| 361 } | 359 } |
| 362 | 360 |
| 363 // static | 361 // static |
| 364 const char* WebViewSchedulerImpl::VirtualTimePolicyToString( | 362 const char* WebViewSchedulerImpl::VirtualTimePolicyToString( |
| 365 VirtualTimePolicy virtual_time_policy) { | 363 VirtualTimePolicy virtual_time_policy) { |
| 366 switch (virtual_time_policy) { | 364 switch (virtual_time_policy) { |
| 367 case VirtualTimePolicy::ADVANCE: | 365 case VirtualTimePolicy::ADVANCE: |
| 368 return "ADVANCE"; | 366 return "ADVANCE"; |
| 369 case VirtualTimePolicy::PAUSE: | 367 case VirtualTimePolicy::PAUSE: |
| 370 return "PAUSE"; | 368 return "PAUSE"; |
| 371 case VirtualTimePolicy::DETERMINISTIC_LOADING: | 369 case VirtualTimePolicy::DETERMINISTIC_LOADING: |
| 372 return "DETERMINISTIC_LOADING"; | 370 return "DETERMINISTIC_LOADING"; |
| 373 default: | 371 default: |
| 374 NOTREACHED(); | 372 NOTREACHED(); |
| 375 return nullptr; | 373 return nullptr; |
| 376 } | 374 } |
| 377 } | 375 } |
| 378 | 376 |
| 379 } // namespace scheduler | 377 } // namespace scheduler |
| 380 } // namespace blink | 378 } // namespace blink |
| OLD | NEW |