| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/updater/extension_updater.h" | 5 #include "chrome/browser/extensions/updater/extension_updater.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 typedef extensions::ExtensionDownloaderDelegate::Error Error; | 48 typedef extensions::ExtensionDownloaderDelegate::Error Error; |
| 49 typedef extensions::ExtensionDownloaderDelegate::PingResult PingResult; | 49 typedef extensions::ExtensionDownloaderDelegate::PingResult PingResult; |
| 50 | 50 |
| 51 namespace { | 51 namespace { |
| 52 | 52 |
| 53 // Wait at least 60 seconds after browser startup before we do any checks. If | 53 // Wait at least 60 seconds after browser startup before we do any checks. If |
| 54 // you change this value, make sure to update comments where it is used. | 54 // you change this value, make sure to update comments where it is used. |
| 55 const int kStartupWaitSeconds = 60; | 55 const int kStartupWaitSeconds = 60; |
| 56 | 56 |
| 57 // The minimum number of seconds there should be for the delay passed to |
| 58 // ScheduleNextCheck. |
| 59 const int kScheduleNextCheckMinGapSecs = 1; |
| 60 |
| 57 // For sanity checking on update frequency - enforced in release mode only. | 61 // For sanity checking on update frequency - enforced in release mode only. |
| 58 #if defined(NDEBUG) | 62 #if defined(NDEBUG) |
| 59 const int kMinUpdateFrequencySeconds = 30; | 63 const int kMinUpdateFrequencySeconds = 30; |
| 60 #endif | 64 #endif |
| 61 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days | 65 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days |
| 62 | 66 |
| 63 // When we've computed a days value, we want to make sure we don't send a | 67 // When we've computed a days value, we want to make sure we don't send a |
| 64 // negative value (due to the system clock being set backwards, etc.), since -1 | 68 // negative value (due to the system clock being set backwards, etc.), since -1 |
| 65 // is a special sentinel value that means "never pinged", and other negative | 69 // is a special sentinel value that means "never pinged", and other negative |
| 66 // values don't make sense. | 70 // values don't make sense. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // If someone's testing with a quick frequency, just allow it. | 175 // If someone's testing with a quick frequency, just allow it. |
| 172 if (frequency_seconds_ < kStartupWaitSeconds) | 176 if (frequency_seconds_ < kStartupWaitSeconds) |
| 173 return TimeDelta::FromSeconds(frequency_seconds_); | 177 return TimeDelta::FromSeconds(frequency_seconds_); |
| 174 | 178 |
| 175 // If we've never scheduled a check before, start at a random time up to | 179 // If we've never scheduled a check before, start at a random time up to |
| 176 // frequency_seconds_ away. | 180 // frequency_seconds_ away. |
| 177 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck)) | 181 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck)) |
| 178 return TimeDelta::FromSeconds( | 182 return TimeDelta::FromSeconds( |
| 179 RandInt(kStartupWaitSeconds, frequency_seconds_)); | 183 RandInt(kStartupWaitSeconds, frequency_seconds_)); |
| 180 | 184 |
| 185 // Read the persisted next check time, and use that if it isn't in the past |
| 186 // or too far in the future (this can happen with system clock changes). |
| 181 Time saved_next = Time::FromInternalValue(prefs_->GetInt64( | 187 Time saved_next = Time::FromInternalValue(prefs_->GetInt64( |
| 182 pref_names::kNextUpdateCheck)); | 188 pref_names::kNextUpdateCheck)); |
| 183 | |
| 184 Time now = Time::Now(); | 189 Time now = Time::Now(); |
| 185 | 190 base::Time earliest = |
| 186 // Read the persisted next check time, and use that if it isn't in the past | 191 now + TimeDelta::FromSeconds(kScheduleNextCheckMinGapSecs); |
| 187 // or too far in the future (this can happen with system clock changes). | 192 base::Time latest = now + TimeDelta::FromSeconds(frequency_seconds_); |
| 188 if (saved_next > now && | 193 if (saved_next > earliest && saved_next < latest) { |
| 189 saved_next < now + TimeDelta::FromSeconds(frequency_seconds_)) { | |
| 190 return saved_next - now; | 194 return saved_next - now; |
| 191 } | 195 } |
| 192 | 196 |
| 193 // In most cases we'll get here because the persisted next check time passed | 197 // In most cases we'll get here because the persisted next check time passed |
| 194 // while we weren't running, so pick something soon. | 198 // while we weren't running, so pick something soon. |
| 195 return TimeDelta::FromSeconds( | 199 return TimeDelta::FromSeconds( |
| 196 RandInt(kStartupWaitSeconds, kStartupWaitSeconds * 5)); | 200 RandInt(kStartupWaitSeconds, kStartupWaitSeconds * 5)); |
| 197 } | 201 } |
| 198 | 202 |
| 199 void ExtensionUpdater::Start() { | 203 void ExtensionUpdater::Start() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 218 prefs_ = NULL; | 222 prefs_ = NULL; |
| 219 profile_ = NULL; | 223 profile_ = NULL; |
| 220 timer_.Stop(); | 224 timer_.Stop(); |
| 221 will_check_soon_ = false; | 225 will_check_soon_ = false; |
| 222 downloader_.reset(); | 226 downloader_.reset(); |
| 223 } | 227 } |
| 224 | 228 |
| 225 void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) { | 229 void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) { |
| 226 DCHECK(alive_); | 230 DCHECK(alive_); |
| 227 DCHECK(!timer_.IsRunning()); | 231 DCHECK(!timer_.IsRunning()); |
| 228 DCHECK(target_delay >= TimeDelta::FromSeconds(1)); | 232 DCHECK(target_delay >= TimeDelta::FromSeconds(kScheduleNextCheckMinGapSecs)); |
| 229 | 233 |
| 230 // Add +/- 10% random jitter. | 234 // Add +/- 10% random jitter. |
| 231 double delay_ms = target_delay.InMillisecondsF(); | 235 double delay_ms = target_delay.InMillisecondsF(); |
| 232 double jitter_factor = (RandDouble() * .2) - 0.1; | 236 double jitter_factor = (RandDouble() * .2) - 0.1; |
| 233 delay_ms += delay_ms * jitter_factor; | 237 delay_ms += delay_ms * jitter_factor; |
| 234 TimeDelta actual_delay = | 238 TimeDelta actual_delay = |
| 235 TimeDelta::FromMilliseconds(static_cast<int64_t>(delay_ms)); | 239 TimeDelta::FromMilliseconds(static_cast<int64_t>(delay_ms)); |
| 236 | 240 |
| 237 // Save the time of next check. | 241 // Save the time of next check. |
| 238 Time next = Time::Now() + actual_delay; | 242 Time next = Time::Now() + actual_delay; |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 const InProgressCheck& request = requests_in_progress_[request_id]; | 602 const InProgressCheck& request = requests_in_progress_[request_id]; |
| 599 if (request.in_progress_ids_.empty()) { | 603 if (request.in_progress_ids_.empty()) { |
| 600 VLOG(2) << "Finished update check " << request_id; | 604 VLOG(2) << "Finished update check " << request_id; |
| 601 if (!request.callback.is_null()) | 605 if (!request.callback.is_null()) |
| 602 request.callback.Run(); | 606 request.callback.Run(); |
| 603 requests_in_progress_.erase(request_id); | 607 requests_in_progress_.erase(request_id); |
| 604 } | 608 } |
| 605 } | 609 } |
| 606 | 610 |
| 607 } // namespace extensions | 611 } // namespace extensions |
| OLD | NEW |