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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/updater/extension_updater.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/rand_util.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/extensions/api/module/module.h"
21 #include "chrome/browser/extensions/crx_installer.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/updater/extension_downloader.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h"
30 #include "crypto/sha2.h"
31 #include "extensions/browser/pending_extension_manager.h"
32 #include "extensions/browser/pref_names.h"
33 #include "extensions/common/constants.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/extension_set.h"
36 #include "extensions/common/manifest.h"
38 using base::RandDouble;
41 using base::TimeDelta;
42 using content::BrowserThread;
44 typedef extensions::ExtensionDownloaderDelegate::Error Error;
45 typedef extensions::ExtensionDownloaderDelegate::PingResult PingResult;
49 // Wait at least 5 minutes after browser startup before we do any checks. If you
50 // change this value, make sure to update comments where it is used.
51 const int kStartupWaitSeconds = 60 * 5;
53 // For sanity checking on update frequency - enforced in release mode only.
55 const int kMinUpdateFrequencySeconds = 30;
57 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days
59 // Require at least 5 seconds between consecutive non-succesful extension update
61 const int kMinUpdateThrottleTime = 5;
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
65 // is a special sentinel value that means "never pinged", and other negative
66 // values don't make sense.
67 int SanitizeDays(int days) {
73 // Calculates the value to use for the ping days parameter.
74 int CalculatePingDays(const Time& last_ping_day) {
75 int days = extensions::ManifestFetchData::kNeverPinged;
76 if (!last_ping_day.is_null()) {
77 days = SanitizeDays((Time::Now() - last_ping_day).InDays());
82 int CalculateActivePingDays(const Time& last_active_ping_day,
86 if (last_active_ping_day.is_null())
87 return extensions::ManifestFetchData::kNeverPinged;
88 return SanitizeDays((Time::Now() - last_active_ping_day).InDays());
93 namespace extensions {
95 ExtensionUpdater::CheckParams::CheckParams()
96 : install_immediately(false) {}
98 ExtensionUpdater::CheckParams::~CheckParams() {}
100 ExtensionUpdater::FetchedCRXFile::FetchedCRXFile(
101 const std::string& i,
102 const base::FilePath& p,
103 bool file_ownership_passed,
105 const std::set<int>& request_ids)
108 file_ownership_passed(file_ownership_passed),
110 request_ids(request_ids) {}
112 ExtensionUpdater::FetchedCRXFile::FetchedCRXFile()
113 : path(), file_ownership_passed(true), download_url() {}
115 ExtensionUpdater::FetchedCRXFile::~FetchedCRXFile() {}
117 ExtensionUpdater::InProgressCheck::InProgressCheck()
118 : install_immediately(false) {}
120 ExtensionUpdater::InProgressCheck::~InProgressCheck() {}
122 struct ExtensionUpdater::ThrottleInfo {
125 throttle_delay(kMinUpdateThrottleTime),
126 check_start(Time::Now()) {}
133 ExtensionUpdater::ExtensionUpdater(ExtensionServiceInterface* service,
134 ExtensionPrefs* extension_prefs,
137 int frequency_seconds,
138 ExtensionCache* cache)
140 weak_ptr_factory_(this),
141 service_(service), frequency_seconds_(frequency_seconds),
142 will_check_soon_(false), extension_prefs_(extension_prefs),
143 prefs_(prefs), profile_(profile),
145 crx_install_is_running_(false),
146 extension_cache_(cache) {
147 DCHECK_GE(frequency_seconds_, 5);
148 DCHECK_LE(frequency_seconds_, kMaxUpdateFrequencySeconds);
150 // In Release mode we enforce that update checks don't happen too often.
151 frequency_seconds_ = std::max(frequency_seconds_, kMinUpdateFrequencySeconds);
153 frequency_seconds_ = std::min(frequency_seconds_, kMaxUpdateFrequencySeconds);
155 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
156 content::NotificationService::AllBrowserContextsAndSources());
159 ExtensionUpdater::~ExtensionUpdater() {
163 // The overall goal here is to balance keeping clients up to date while
164 // avoiding a thundering herd against update servers.
165 TimeDelta ExtensionUpdater::DetermineFirstCheckDelay() {
167 // If someone's testing with a quick frequency, just allow it.
168 if (frequency_seconds_ < kStartupWaitSeconds)
169 return TimeDelta::FromSeconds(frequency_seconds_);
171 // If we've never scheduled a check before, start at frequency_seconds_.
172 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck))
173 return TimeDelta::FromSeconds(frequency_seconds_);
175 // If it's been a long time since our last actual check, we want to do one
177 Time now = Time::Now();
178 Time last = Time::FromInternalValue(prefs_->GetInt64(
179 pref_names::kLastUpdateCheck));
180 int days = (now - last).InDays();
182 // Wait 5-10 minutes.
183 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds,
184 kStartupWaitSeconds * 2));
185 } else if (days >= 14) {
186 // Wait 10-20 minutes.
187 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 2,
188 kStartupWaitSeconds * 4));
189 } else if (days >= 3) {
190 // Wait 20-40 minutes.
191 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 4,
192 kStartupWaitSeconds * 8));
195 // Read the persisted next check time, and use that if it isn't too soon.
196 // Otherwise pick something random.
197 Time saved_next = Time::FromInternalValue(prefs_->GetInt64(
198 pref_names::kNextUpdateCheck));
199 Time earliest = now + TimeDelta::FromSeconds(kStartupWaitSeconds);
200 if (saved_next >= earliest) {
201 return saved_next - now;
203 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds,
204 frequency_seconds_));
208 void ExtensionUpdater::Start() {
210 // If these are NULL, then that means we've been called after Stop()
213 DCHECK(extension_prefs_);
216 DCHECK(!weak_ptr_factory_.HasWeakPtrs());
218 // Make sure our prefs are registered, then schedule the first check.
219 ScheduleNextCheck(DetermineFirstCheckDelay());
222 void ExtensionUpdater::Stop() {
223 weak_ptr_factory_.InvalidateWeakPtrs();
226 extension_prefs_ = NULL;
230 will_check_soon_ = false;
234 void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) {
236 DCHECK(!timer_.IsRunning());
237 DCHECK(target_delay >= TimeDelta::FromSeconds(1));
239 // Add +/- 10% random jitter.
240 double delay_ms = target_delay.InMillisecondsF();
241 double jitter_factor = (RandDouble() * .2) - 0.1;
242 delay_ms += delay_ms * jitter_factor;
243 TimeDelta actual_delay = TimeDelta::FromMilliseconds(
244 static_cast<int64>(delay_ms));
246 // Save the time of next check.
247 Time next = Time::Now() + actual_delay;
248 prefs_->SetInt64(pref_names::kNextUpdateCheck, next.ToInternalValue());
250 timer_.Start(FROM_HERE, actual_delay, this, &ExtensionUpdater::TimerFired);
253 void ExtensionUpdater::TimerFired() {
255 CheckNow(default_params_);
257 // If the user has overridden the update frequency, don't bother reporting
259 if (frequency_seconds_ == extensions::kDefaultUpdateFrequencySeconds) {
260 Time last = Time::FromInternalValue(prefs_->GetInt64(
261 pref_names::kLastUpdateCheck));
262 if (last.ToInternalValue() != 0) {
263 // Use counts rather than time so we can use minutes rather than millis.
264 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.UpdateCheckGap",
265 (Time::Now() - last).InMinutes(),
266 TimeDelta::FromSeconds(kStartupWaitSeconds).InMinutes(),
267 TimeDelta::FromDays(40).InMinutes(),
268 50); // 50 buckets seems to be the default.
272 // Save the last check time, and schedule the next check.
273 int64 now = Time::Now().ToInternalValue();
274 prefs_->SetInt64(pref_names::kLastUpdateCheck, now);
275 ScheduleNextCheck(TimeDelta::FromSeconds(frequency_seconds_));
278 void ExtensionUpdater::CheckSoon() {
280 if (will_check_soon_)
282 if (BrowserThread::PostTask(
283 BrowserThread::UI, FROM_HERE,
284 base::Bind(&ExtensionUpdater::DoCheckSoon,
285 weak_ptr_factory_.GetWeakPtr()))) {
286 will_check_soon_ = true;
292 bool ExtensionUpdater::WillCheckSoon() const {
293 return will_check_soon_;
296 void ExtensionUpdater::DoCheckSoon() {
297 DCHECK(will_check_soon_);
298 CheckNow(default_params_);
299 will_check_soon_ = false;
302 void ExtensionUpdater::AddToDownloader(
303 const ExtensionSet* extensions,
304 const std::list<std::string>& pending_ids,
306 InProgressCheck& request = requests_in_progress_[request_id];
307 for (ExtensionSet::const_iterator extension_iter = extensions->begin();
308 extension_iter != extensions->end(); ++extension_iter) {
309 const Extension& extension = *extension_iter->get();
310 if (!Manifest::IsAutoUpdateableLocation(extension.location())) {
311 VLOG(2) << "Extension " << extension.id() << " is not auto updateable";
314 // An extension might be overwritten by policy, and have its update url
315 // changed. Make sure existing extensions aren't fetched again, if a
316 // pending fetch for an extension with the same id already exists.
317 std::list<std::string>::const_iterator pending_id_iter = std::find(
318 pending_ids.begin(), pending_ids.end(), extension.id());
319 if (pending_id_iter == pending_ids.end()) {
320 if (downloader_->AddExtension(extension, request_id))
321 request.in_progress_ids_.push_back(extension.id());
326 void ExtensionUpdater::CheckNow(const CheckParams& params) {
327 int request_id = next_request_id_++;
329 VLOG(2) << "Starting update check " << request_id;
330 if (params.ids.empty())
335 InProgressCheck& request = requests_in_progress_[request_id];
336 request.callback = params.callback;
337 request.install_immediately = params.install_immediately;
339 if (!downloader_.get()) {
341 new ExtensionDownloader(this, profile_->GetRequestContext()));
344 // Add fetch records for extensions that should be fetched by an update URL.
345 // These extensions are not yet installed. They come from group policy
346 // and external install sources.
347 const PendingExtensionManager* pending_extension_manager =
348 service_->pending_extension_manager();
350 std::list<std::string> pending_ids;
352 if (params.ids.empty()) {
353 // If no extension ids are specified, check for updates for all extensions.
354 pending_extension_manager->GetPendingIdsForUpdateCheck(&pending_ids);
356 std::list<std::string>::const_iterator iter;
357 for (iter = pending_ids.begin(); iter != pending_ids.end(); ++iter) {
358 const PendingExtensionInfo* info = pending_extension_manager->GetById(
360 if (!Manifest::IsAutoUpdateableLocation(info->install_source())) {
361 VLOG(2) << "Extension " << *iter << " is not auto updateable";
364 if (downloader_->AddPendingExtension(*iter, info->update_url(),
366 request.in_progress_ids_.push_back(*iter);
369 AddToDownloader(service_->extensions(), pending_ids, request_id);
370 AddToDownloader(service_->disabled_extensions(), pending_ids, request_id);
372 for (std::list<std::string>::const_iterator it = params.ids.begin();
373 it != params.ids.end(); ++it) {
374 const Extension* extension = service_->GetExtensionById(*it, true);
376 if (downloader_->AddExtension(*extension, request_id))
377 request.in_progress_ids_.push_back(extension->id());
381 // StartAllPending() might call OnExtensionDownloadFailed/Finished before
382 // it returns, which would cause NotifyIfFinished to incorrectly try to
383 // send out a notification. So check before we call StartAllPending if any
384 // extensions are going to be updated, and use that to figure out if
385 // NotifyIfFinished should be called.
386 bool noChecks = request.in_progress_ids_.empty();
388 // StartAllPending() will call OnExtensionDownloadFailed or
389 // OnExtensionDownloadFinished for each extension that was checked.
390 downloader_->StartAllPending(extension_cache_);
393 NotifyIfFinished(request_id);
396 bool ExtensionUpdater::CheckExtensionSoon(const std::string& extension_id,
397 const FinishedCallback& callback) {
398 bool have_throttle_info = ContainsKey(throttle_info_, extension_id);
399 ThrottleInfo& info = throttle_info_[extension_id];
400 if (have_throttle_info) {
401 // We already had a ThrottleInfo object for this extension, check if the
402 // update check request should be allowed.
404 // If another check is in progress, don't start a new check.
405 if (info.in_progress)
408 Time now = Time::Now();
409 Time last = info.check_start;
410 // If somehow time moved back, we don't want to infinitely keep throttling.
413 info.check_start = now;
415 Time earliest = last + TimeDelta::FromSeconds(info.throttle_delay);
416 // If check is too soon, throttle.
420 // TODO(mek): Somehow increase time between allowing checks when checks
421 // are repeatedly throttled and don't result in updates being installed.
423 // It's okay to start a check, update values.
424 info.check_start = now;
425 info.in_progress = true;
429 params.ids.push_back(extension_id);
430 params.callback = base::Bind(&ExtensionUpdater::ExtensionCheckFinished,
431 weak_ptr_factory_.GetWeakPtr(),
432 extension_id, callback);
437 void ExtensionUpdater::ExtensionCheckFinished(
438 const std::string& extension_id,
439 const FinishedCallback& callback) {
440 std::map<std::string, ThrottleInfo>::iterator it =
441 throttle_info_.find(extension_id);
442 if (it != throttle_info_.end()) {
443 it->second.in_progress = false;
448 void ExtensionUpdater::OnExtensionDownloadFailed(
449 const std::string& id,
451 const PingResult& ping,
452 const std::set<int>& request_ids) {
454 UpdatePingData(id, ping);
455 bool install_immediately = false;
456 for (std::set<int>::const_iterator it = request_ids.begin();
457 it != request_ids.end(); ++it) {
458 InProgressCheck& request = requests_in_progress_[*it];
459 install_immediately |= request.install_immediately;
460 request.in_progress_ids_.remove(id);
461 NotifyIfFinished(*it);
464 // This method is called if no updates were found. However a previous update
465 // check might have queued an update for this extension already. If a
466 // current update check has |install_immediately| set the previously
467 // queued update should be installed now.
468 if (install_immediately && service_->GetPendingExtensionUpdate(id))
469 service_->FinishDelayedInstallation(id);
472 void ExtensionUpdater::OnExtensionDownloadFinished(
473 const std::string& id,
474 const base::FilePath& path,
475 bool file_ownership_passed,
476 const GURL& download_url,
477 const std::string& version,
478 const PingResult& ping,
479 const std::set<int>& request_ids) {
481 UpdatePingData(id, ping);
483 VLOG(2) << download_url << " written to " << path.value();
485 FetchedCRXFile fetched(id, path, file_ownership_passed, download_url,
487 fetched_crx_files_.push(fetched);
489 // MaybeInstallCRXFile() removes extensions from |in_progress_ids_| after
490 // starting the crx installer.
491 MaybeInstallCRXFile();
494 bool ExtensionUpdater::GetPingDataForExtension(
495 const std::string& id,
496 ManifestFetchData::PingData* ping_data) {
498 ping_data->rollcall_days = CalculatePingDays(
499 extension_prefs_->LastPingDay(id));
500 ping_data->is_enabled = service_->IsExtensionEnabled(id);
501 ping_data->active_days =
502 CalculateActivePingDays(extension_prefs_->LastActivePingDay(id),
503 extension_prefs_->GetActiveBit(id));
507 std::string ExtensionUpdater::GetUpdateUrlData(const std::string& id) {
509 return extension::GetUpdateURLData(extension_prefs_, id);
512 bool ExtensionUpdater::IsExtensionPending(const std::string& id) {
514 return service_->pending_extension_manager()->IsIdPending(id);
517 bool ExtensionUpdater::GetExtensionExistingVersion(const std::string& id,
518 std::string* version) {
520 const Extension* extension = service_->GetExtensionById(id, true);
523 const Extension* update = service_->GetPendingExtensionUpdate(id);
525 *version = update->VersionString();
527 *version = extension->VersionString();
531 void ExtensionUpdater::UpdatePingData(const std::string& id,
532 const PingResult& ping_result) {
534 if (ping_result.did_ping)
535 extension_prefs_->SetLastPingDay(id, ping_result.day_start);
536 if (extension_prefs_->GetActiveBit(id)) {
537 extension_prefs_->SetActiveBit(id, false);
538 extension_prefs_->SetLastActivePingDay(id, ping_result.day_start);
542 void ExtensionUpdater::MaybeInstallCRXFile() {
543 if (crx_install_is_running_ || fetched_crx_files_.empty())
546 std::set<int> request_ids;
548 while (!fetched_crx_files_.empty() && !crx_install_is_running_) {
549 const FetchedCRXFile& crx_file = fetched_crx_files_.top();
551 VLOG(2) << "updating " << crx_file.extension_id
552 << " with " << crx_file.path.value();
554 // The ExtensionService is now responsible for cleaning up the temp file
555 // at |crx_file.path|.
556 CrxInstaller* installer = NULL;
557 if (service_->UpdateExtension(crx_file.extension_id,
559 crx_file.file_ownership_passed,
560 crx_file.download_url,
562 crx_install_is_running_ = true;
563 current_crx_file_ = crx_file;
565 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
566 it != crx_file.request_ids.end(); ++it) {
567 InProgressCheck& request = requests_in_progress_[*it];
568 if (request.install_immediately) {
569 installer->set_install_wait_for_idle(false);
574 // Source parameter ensures that we only see the completion event for the
575 // the installer we started.
577 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
578 content::Source<CrxInstaller>(installer));
580 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
581 it != crx_file.request_ids.end(); ++it) {
582 InProgressCheck& request = requests_in_progress_[*it];
583 request.in_progress_ids_.remove(crx_file.extension_id);
585 request_ids.insert(crx_file.request_ids.begin(),
586 crx_file.request_ids.end());
588 fetched_crx_files_.pop();
591 for (std::set<int>::const_iterator it = request_ids.begin();
592 it != request_ids.end(); ++it) {
593 NotifyIfFinished(*it);
597 void ExtensionUpdater::Observe(int type,
598 const content::NotificationSource& source,
599 const content::NotificationDetails& details) {
601 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: {
602 // No need to listen for CRX_INSTALLER_DONE anymore.
603 registrar_.Remove(this,
604 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
606 crx_install_is_running_ = false;
608 const FetchedCRXFile& crx_file = current_crx_file_;
609 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
610 it != crx_file.request_ids.end(); ++it) {
611 InProgressCheck& request = requests_in_progress_[*it];
612 request.in_progress_ids_.remove(crx_file.extension_id);
613 NotifyIfFinished(*it);
616 // If any files are available to update, start one.
617 MaybeInstallCRXFile();
620 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
621 const Extension* extension =
622 content::Details<const InstalledExtensionInfo>(details)->extension;
624 throttle_info_.erase(extension->id());
632 void ExtensionUpdater::NotifyStarted() {
633 content::NotificationService::current()->Notify(
634 chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED,
635 content::Source<Profile>(profile_),
636 content::NotificationService::NoDetails());
639 void ExtensionUpdater::NotifyIfFinished(int request_id) {
640 DCHECK(ContainsKey(requests_in_progress_, request_id));
641 const InProgressCheck& request = requests_in_progress_[request_id];
642 if (request.in_progress_ids_.empty()) {
643 VLOG(2) << "Finished update check " << request_id;
644 if (!request.callback.is_null())
645 request.callback.Run();
646 requests_in_progress_.erase(request_id);
650 } // namespace extensions