Chromium Code Reviews| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 using base::RandInt; | 43 using base::RandInt; |
| 44 using base::Time; | 44 using base::Time; |
| 45 using base::TimeDelta; | 45 using base::TimeDelta; |
| 46 using content::BrowserThread; | 46 using content::BrowserThread; |
| 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 5 minutes after browser startup before we do any checks. If you | 53 // Wait at least 30 seconds after browser startup before we do any checks. If |
|
lazyboy
2016/08/05 00:04:19
As discussed offline, I'd be comfortable if this v
asargent_no_longer_on_chrome
2016/08/05 06:23:31
Done.
| |
| 54 // 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 * 5; | 55 const int kStartupWaitSeconds = 30; |
| 56 | 56 |
| 57 // For sanity checking on update frequency - enforced in release mode only. | 57 // For sanity checking on update frequency - enforced in release mode only. |
| 58 #if defined(NDEBUG) | 58 #if defined(NDEBUG) |
| 59 const int kMinUpdateFrequencySeconds = 30; | 59 const int kMinUpdateFrequencySeconds = 30; |
| 60 #endif | 60 #endif |
| 61 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days | 61 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days |
| 62 | 62 |
| 63 // When we've computed a days value, we want to make sure we don't send a | 63 // 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 | 64 // 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 | 65 // is a special sentinel value that means "never pinged", and other negative |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 } | 165 } |
| 166 | 166 |
| 167 // The overall goal here is to balance keeping clients up to date while | 167 // The overall goal here is to balance keeping clients up to date while |
| 168 // avoiding a thundering herd against update servers. | 168 // avoiding a thundering herd against update servers. |
| 169 TimeDelta ExtensionUpdater::DetermineFirstCheckDelay() { | 169 TimeDelta ExtensionUpdater::DetermineFirstCheckDelay() { |
| 170 DCHECK(alive_); | 170 DCHECK(alive_); |
| 171 // If someone's testing with a quick frequency, just allow it. | 171 // If someone's testing with a quick frequency, just allow it. |
| 172 if (frequency_seconds_ < kStartupWaitSeconds) | 172 if (frequency_seconds_ < kStartupWaitSeconds) |
| 173 return TimeDelta::FromSeconds(frequency_seconds_); | 173 return TimeDelta::FromSeconds(frequency_seconds_); |
| 174 | 174 |
| 175 // If we've never scheduled a check before, start at frequency_seconds_. | 175 // If we've never scheduled a check before, start at a random time up to |
| 176 // frequency_seconds_ away. | |
| 176 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck)) | 177 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck)) |
| 177 return TimeDelta::FromSeconds(frequency_seconds_); | 178 return TimeDelta::FromSeconds( |
| 179 RandInt(kStartupWaitSeconds, frequency_seconds_)); | |
| 178 | 180 |
| 179 // If it's been a long time since our last actual check, we want to do one | |
| 180 // relatively soon. | |
| 181 Time now = Time::Now(); | |
| 182 Time last = Time::FromInternalValue(prefs_->GetInt64( | |
| 183 pref_names::kLastUpdateCheck)); | |
| 184 int days = (now - last).InDays(); | |
| 185 if (days >= 30) { | |
| 186 // Wait 5-10 minutes. | |
| 187 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds, | |
| 188 kStartupWaitSeconds * 2)); | |
| 189 } else if (days >= 14) { | |
| 190 // Wait 10-20 minutes. | |
| 191 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 2, | |
| 192 kStartupWaitSeconds * 4)); | |
| 193 } else if (days >= 3) { | |
| 194 // Wait 20-40 minutes. | |
| 195 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 4, | |
| 196 kStartupWaitSeconds * 8)); | |
| 197 } | |
| 198 | |
| 199 // Read the persisted next check time, and use that if it isn't too soon | |
| 200 // or too late. Otherwise pick something random. | |
| 201 Time saved_next = Time::FromInternalValue(prefs_->GetInt64( | 181 Time saved_next = Time::FromInternalValue(prefs_->GetInt64( |
| 202 pref_names::kNextUpdateCheck)); | 182 pref_names::kNextUpdateCheck)); |
| 203 Time earliest = now + TimeDelta::FromSeconds(kStartupWaitSeconds); | 183 |
| 204 Time latest = now + TimeDelta::FromSeconds(frequency_seconds_); | 184 Time now = Time::Now(); |
| 205 if (saved_next >= earliest && saved_next <= latest) { | 185 |
| 186 // Read the persisted next check time, and use that if it isn't in the past | |
| 187 // or too far in the future (this can happen with system clock changes). | |
| 188 if (saved_next > now && | |
| 189 saved_next < now + TimeDelta::FromSeconds(frequency_seconds_)) | |
|
lazyboy
2016/08/05 00:04:19
nit: this requires {}
asargent_no_longer_on_chrome
2016/08/05 06:23:32
Done.
| |
| 206 return saved_next - now; | 190 return saved_next - now; |
| 207 } else { | 191 |
| 208 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds, | 192 // In most cases we'll get here because the persisted next check time passed |
| 209 frequency_seconds_)); | 193 // while we weren't running, so pick something soon. |
| 210 } | 194 return TimeDelta::FromSeconds( |
| 195 RandInt(kStartupWaitSeconds, kStartupWaitSeconds * 2)); | |
| 211 } | 196 } |
| 212 | 197 |
| 213 void ExtensionUpdater::Start() { | 198 void ExtensionUpdater::Start() { |
| 214 DCHECK(!alive_); | 199 DCHECK(!alive_); |
| 215 // If these are NULL, then that means we've been called after Stop() | 200 // If these are NULL, then that means we've been called after Stop() |
| 216 // has been called. | 201 // has been called. |
| 217 DCHECK(service_); | 202 DCHECK(service_); |
| 218 DCHECK(extension_prefs_); | 203 DCHECK(extension_prefs_); |
| 219 DCHECK(prefs_); | 204 DCHECK(prefs_); |
| 220 DCHECK(profile_); | 205 DCHECK(profile_); |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 609 const InProgressCheck& request = requests_in_progress_[request_id]; | 594 const InProgressCheck& request = requests_in_progress_[request_id]; |
| 610 if (request.in_progress_ids_.empty()) { | 595 if (request.in_progress_ids_.empty()) { |
| 611 VLOG(2) << "Finished update check " << request_id; | 596 VLOG(2) << "Finished update check " << request_id; |
| 612 if (!request.callback.is_null()) | 597 if (!request.callback.is_null()) |
| 613 request.callback.Run(); | 598 request.callback.Run(); |
| 614 requests_in_progress_.erase(request_id); | 599 requests_in_progress_.erase(request_id); |
| 615 } | 600 } |
| 616 } | 601 } |
| 617 | 602 |
| 618 } // namespace extensions | 603 } // namespace extensions |
| OLD | NEW |