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/pending_extension_manager.h"
24 #include "chrome/browser/extensions/updater/extension_downloader.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/pref_names.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_source.h"
31 #include "crypto/sha2.h"
32 #include "extensions/browser/extension_registry.h"
33 #include "extensions/browser/pref_names.h"
34 #include "extensions/common/constants.h"
35 #include "extensions/common/extension.h"
36 #include "extensions/common/extension_set.h"
37 #include "extensions/common/manifest.h"
39 using base::RandDouble;
42 using base::TimeDelta;
43 using content::BrowserThread;
45 typedef extensions::ExtensionDownloaderDelegate::Error Error;
46 typedef extensions::ExtensionDownloaderDelegate::PingResult PingResult;
50 // Wait at least 5 minutes after browser startup before we do any checks. If you
51 // change this value, make sure to update comments where it is used.
52 const int kStartupWaitSeconds = 60 * 5;
54 // For sanity checking on update frequency - enforced in release mode only.
56 const int kMinUpdateFrequencySeconds = 30;
58 const int kMaxUpdateFrequencySeconds = 60 * 60 * 24 * 7; // 7 days
60 // Require at least 5 seconds between consecutive non-succesful extension update
62 const int kMinUpdateThrottleTime = 5;
64 // When we've computed a days value, we want to make sure we don't send a
65 // negative value (due to the system clock being set backwards, etc.), since -1
66 // is a special sentinel value that means "never pinged", and other negative
67 // values don't make sense.
68 int SanitizeDays(int days) {
74 // Calculates the value to use for the ping days parameter.
75 int CalculatePingDays(const Time& last_ping_day) {
76 int days = extensions::ManifestFetchData::kNeverPinged;
77 if (!last_ping_day.is_null()) {
78 days = SanitizeDays((Time::Now() - last_ping_day).InDays());
83 int CalculateActivePingDays(const Time& last_active_ping_day,
87 if (last_active_ping_day.is_null())
88 return extensions::ManifestFetchData::kNeverPinged;
89 return SanitizeDays((Time::Now() - last_active_ping_day).InDays());
94 namespace extensions {
96 ExtensionUpdater::CheckParams::CheckParams()
97 : install_immediately(false) {}
99 ExtensionUpdater::CheckParams::~CheckParams() {}
101 ExtensionUpdater::FetchedCRXFile::FetchedCRXFile(
102 const std::string& i,
103 const base::FilePath& p,
104 bool file_ownership_passed,
105 const std::set<int>& request_ids)
108 file_ownership_passed(file_ownership_passed),
109 request_ids(request_ids) {}
111 ExtensionUpdater::FetchedCRXFile::FetchedCRXFile()
112 : path(), file_ownership_passed(true) {}
114 ExtensionUpdater::FetchedCRXFile::~FetchedCRXFile() {}
116 ExtensionUpdater::InProgressCheck::InProgressCheck()
117 : install_immediately(false) {}
119 ExtensionUpdater::InProgressCheck::~InProgressCheck() {}
121 struct ExtensionUpdater::ThrottleInfo {
124 throttle_delay(kMinUpdateThrottleTime),
125 check_start(Time::Now()) {}
132 ExtensionUpdater::ExtensionUpdater(ExtensionServiceInterface* service,
133 ExtensionPrefs* extension_prefs,
136 int frequency_seconds,
137 ExtensionCache* cache)
139 weak_ptr_factory_(this),
140 service_(service), frequency_seconds_(frequency_seconds),
141 will_check_soon_(false), extension_prefs_(extension_prefs),
142 prefs_(prefs), profile_(profile),
144 crx_install_is_running_(false),
145 extension_cache_(cache) {
146 DCHECK_GE(frequency_seconds_, 5);
147 DCHECK_LE(frequency_seconds_, kMaxUpdateFrequencySeconds);
149 // In Release mode we enforce that update checks don't happen too often.
150 frequency_seconds_ = std::max(frequency_seconds_, kMinUpdateFrequencySeconds);
152 frequency_seconds_ = std::min(frequency_seconds_, kMaxUpdateFrequencySeconds);
154 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
155 content::NotificationService::AllBrowserContextsAndSources());
158 ExtensionUpdater::~ExtensionUpdater() {
162 // The overall goal here is to balance keeping clients up to date while
163 // avoiding a thundering herd against update servers.
164 TimeDelta ExtensionUpdater::DetermineFirstCheckDelay() {
166 // If someone's testing with a quick frequency, just allow it.
167 if (frequency_seconds_ < kStartupWaitSeconds)
168 return TimeDelta::FromSeconds(frequency_seconds_);
170 // If we've never scheduled a check before, start at frequency_seconds_.
171 if (!prefs_->HasPrefPath(pref_names::kNextUpdateCheck))
172 return TimeDelta::FromSeconds(frequency_seconds_);
174 // If it's been a long time since our last actual check, we want to do one
176 Time now = Time::Now();
177 Time last = Time::FromInternalValue(prefs_->GetInt64(
178 pref_names::kLastUpdateCheck));
179 int days = (now - last).InDays();
181 // Wait 5-10 minutes.
182 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds,
183 kStartupWaitSeconds * 2));
184 } else if (days >= 14) {
185 // Wait 10-20 minutes.
186 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 2,
187 kStartupWaitSeconds * 4));
188 } else if (days >= 3) {
189 // Wait 20-40 minutes.
190 return TimeDelta::FromSeconds(RandInt(kStartupWaitSeconds * 4,
191 kStartupWaitSeconds * 8));
194 // Read the persisted next check time, and use that if it isn't too soon
195 // or too late. Otherwise pick something random.
196 Time saved_next = Time::FromInternalValue(prefs_->GetInt64(
197 pref_names::kNextUpdateCheck));
198 Time earliest = now + TimeDelta::FromSeconds(kStartupWaitSeconds);
199 Time latest = now + TimeDelta::FromSeconds(frequency_seconds_);
200 if (saved_next >= earliest && saved_next <= latest) {
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 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
370 AddToDownloader(®istry->enabled_extensions(), pending_ids, request_id);
371 AddToDownloader(®istry->disabled_extensions(), pending_ids, request_id);
373 for (std::list<std::string>::const_iterator it = params.ids.begin();
374 it != params.ids.end(); ++it) {
375 const Extension* extension = service_->GetExtensionById(*it, true);
377 if (downloader_->AddExtension(*extension, request_id))
378 request.in_progress_ids_.push_back(extension->id());
382 // StartAllPending() might call OnExtensionDownloadFailed/Finished before
383 // it returns, which would cause NotifyIfFinished to incorrectly try to
384 // send out a notification. So check before we call StartAllPending if any
385 // extensions are going to be updated, and use that to figure out if
386 // NotifyIfFinished should be called.
387 bool noChecks = request.in_progress_ids_.empty();
389 // StartAllPending() will call OnExtensionDownloadFailed or
390 // OnExtensionDownloadFinished for each extension that was checked.
391 downloader_->StartAllPending(extension_cache_);
394 NotifyIfFinished(request_id);
397 bool ExtensionUpdater::CheckExtensionSoon(const std::string& extension_id,
398 const FinishedCallback& callback) {
399 bool have_throttle_info = ContainsKey(throttle_info_, extension_id);
400 ThrottleInfo& info = throttle_info_[extension_id];
401 if (have_throttle_info) {
402 // We already had a ThrottleInfo object for this extension, check if the
403 // update check request should be allowed.
405 // If another check is in progress, don't start a new check.
406 if (info.in_progress)
409 Time now = Time::Now();
410 Time last = info.check_start;
411 // If somehow time moved back, we don't want to infinitely keep throttling.
414 info.check_start = now;
416 Time earliest = last + TimeDelta::FromSeconds(info.throttle_delay);
417 // If check is too soon, throttle.
421 // TODO(mek): Somehow increase time between allowing checks when checks
422 // are repeatedly throttled and don't result in updates being installed.
424 // It's okay to start a check, update values.
425 info.check_start = now;
426 info.in_progress = true;
430 params.ids.push_back(extension_id);
431 params.callback = base::Bind(&ExtensionUpdater::ExtensionCheckFinished,
432 weak_ptr_factory_.GetWeakPtr(),
433 extension_id, callback);
438 void ExtensionUpdater::ExtensionCheckFinished(
439 const std::string& extension_id,
440 const FinishedCallback& callback) {
441 std::map<std::string, ThrottleInfo>::iterator it =
442 throttle_info_.find(extension_id);
443 if (it != throttle_info_.end()) {
444 it->second.in_progress = false;
449 void ExtensionUpdater::OnExtensionDownloadFailed(
450 const std::string& id,
452 const PingResult& ping,
453 const std::set<int>& request_ids) {
455 UpdatePingData(id, ping);
456 bool install_immediately = false;
457 for (std::set<int>::const_iterator it = request_ids.begin();
458 it != request_ids.end(); ++it) {
459 InProgressCheck& request = requests_in_progress_[*it];
460 install_immediately |= request.install_immediately;
461 request.in_progress_ids_.remove(id);
462 NotifyIfFinished(*it);
465 // This method is called if no updates were found. However a previous update
466 // check might have queued an update for this extension already. If a
467 // current update check has |install_immediately| set the previously
468 // queued update should be installed now.
469 if (install_immediately && service_->GetPendingExtensionUpdate(id))
470 service_->FinishDelayedInstallation(id);
473 void ExtensionUpdater::OnExtensionDownloadFinished(
474 const std::string& id,
475 const base::FilePath& path,
476 bool file_ownership_passed,
477 const GURL& download_url,
478 const std::string& version,
479 const PingResult& ping,
480 const std::set<int>& request_ids) {
482 UpdatePingData(id, ping);
484 VLOG(2) << download_url << " written to " << path.value();
486 FetchedCRXFile fetched(id, path, file_ownership_passed, request_ids);
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,
561 crx_install_is_running_ = true;
562 current_crx_file_ = crx_file;
564 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
565 it != crx_file.request_ids.end(); ++it) {
566 InProgressCheck& request = requests_in_progress_[*it];
567 if (request.install_immediately) {
568 installer->set_install_wait_for_idle(false);
573 // Source parameter ensures that we only see the completion event for the
574 // the installer we started.
576 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
577 content::Source<CrxInstaller>(installer));
579 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
580 it != crx_file.request_ids.end(); ++it) {
581 InProgressCheck& request = requests_in_progress_[*it];
582 request.in_progress_ids_.remove(crx_file.extension_id);
584 request_ids.insert(crx_file.request_ids.begin(),
585 crx_file.request_ids.end());
587 fetched_crx_files_.pop();
590 for (std::set<int>::const_iterator it = request_ids.begin();
591 it != request_ids.end(); ++it) {
592 NotifyIfFinished(*it);
596 void ExtensionUpdater::Observe(int type,
597 const content::NotificationSource& source,
598 const content::NotificationDetails& details) {
600 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: {
601 // No need to listen for CRX_INSTALLER_DONE anymore.
602 registrar_.Remove(this,
603 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
605 crx_install_is_running_ = false;
607 const FetchedCRXFile& crx_file = current_crx_file_;
608 for (std::set<int>::const_iterator it = crx_file.request_ids.begin();
609 it != crx_file.request_ids.end(); ++it) {
610 InProgressCheck& request = requests_in_progress_[*it];
611 request.in_progress_ids_.remove(crx_file.extension_id);
612 NotifyIfFinished(*it);
615 // If any files are available to update, start one.
616 MaybeInstallCRXFile();
619 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
620 const Extension* extension =
621 content::Details<const InstalledExtensionInfo>(details)->extension;
623 throttle_info_.erase(extension->id());
631 void ExtensionUpdater::NotifyStarted() {
632 content::NotificationService::current()->Notify(
633 chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED,
634 content::Source<Profile>(profile_),
635 content::NotificationService::NoDetails());
638 void ExtensionUpdater::NotifyIfFinished(int request_id) {
639 DCHECK(ContainsKey(requests_in_progress_, request_id));
640 const InProgressCheck& request = requests_in_progress_[request_id];
641 if (request.in_progress_ids_.empty()) {
642 VLOG(2) << "Finished update check " << request_id;
643 if (!request.callback.is_null())
644 request.callback.Run();
645 requests_in_progress_.erase(request_id);
649 } // namespace extensions