| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/push_messaging/background_budget_service.h" | 5 #include "chrome/browser/push_messaging/background_budget_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, | 122 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, |
| 123 &last_updated_msec)) { | 123 &last_updated_msec)) { |
| 124 // If there is no stored data or the data can't be parsed, just return the | 124 // If there is no stored data or the data can't be parsed, just return the |
| 125 // SES. | 125 // SES. |
| 126 return ses_score; | 126 return ses_score; |
| 127 } | 127 } |
| 128 | 128 |
| 129 base::Time now = clock_->Now(); | 129 base::Time now = clock_->Now(); |
| 130 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); | 130 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); |
| 131 | 131 |
| 132 // The user can set their clock backwards, so if the last updated time is in |
| 133 // the future, don't update the budget based on elapsed time. Eventually the |
| 134 // clock will reach the future, and the budget calculations will catch up. |
| 135 // TODO(harkness): Consider what to do if the clock jumps forward by a |
| 136 // significant amount. |
| 137 if (elapsed.InMicroseconds() < 0) |
| 138 return old_budget; |
| 139 |
| 132 // For each time period that elapses, calculate the carryover ratio as the | 140 // For each time period that elapses, calculate the carryover ratio as the |
| 133 // ratio of time remaining in our max period to the total period. | 141 // ratio of time remaining in our max period to the total period. |
| 134 // The carryover component is then the old budget multiplied by the ratio. | 142 // The carryover component is then the old budget multiplied by the ratio. |
| 135 double carryover_ratio = std::max( | 143 double carryover_ratio = std::max( |
| 136 0.0, | 144 0.0, |
| 137 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); | 145 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); |
| 138 double budget_carryover = old_budget * carryover_ratio; | 146 double budget_carryover = old_budget * carryover_ratio; |
| 139 | 147 |
| 140 // The ses component is an average of the last ses score used for budget | 148 // The ses component is an average of the last ses score used for budget |
| 141 // calculation and the current ses score. | 149 // calculation and the current ses score. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 165 | 173 |
| 166 base::Time time = clock_->Now(); | 174 base::Time time = clock_->Now(); |
| 167 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | 175 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
| 168 } | 176 } |
| 169 | 177 |
| 170 // Override the default clock with the specified clock. Only used for testing. | 178 // Override the default clock with the specified clock. Only used for testing. |
| 171 void BackgroundBudgetService::SetClockForTesting( | 179 void BackgroundBudgetService::SetClockForTesting( |
| 172 std::unique_ptr<base::Clock> clock) { | 180 std::unique_ptr<base::Clock> clock) { |
| 173 clock_ = std::move(clock); | 181 clock_ = std::move(clock); |
| 174 } | 182 } |
| OLD | NEW |