| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_updater.h" | 5 #include "chrome/browser/extensions/extension_updater.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 251 } |
| 252 | 252 |
| 253 DCHECK(!update_url.is_empty()); | 253 DCHECK(!update_url.is_empty()); |
| 254 DCHECK(update_url.is_valid()); | 254 DCHECK(update_url.is_valid()); |
| 255 | 255 |
| 256 ManifestFetchData* fetch = NULL; | 256 ManifestFetchData* fetch = NULL; |
| 257 std::multimap<GURL, ManifestFetchData*>::iterator existing_iter = | 257 std::multimap<GURL, ManifestFetchData*>::iterator existing_iter = |
| 258 fetches_.find(update_url); | 258 fetches_.find(update_url); |
| 259 | 259 |
| 260 // Find or create a ManifestFetchData to add this extension to. | 260 // Find or create a ManifestFetchData to add this extension to. |
| 261 int ping_days = CalculatePingDays(service_->LastPingDay(id)); | 261 int ping_days = |
| 262 CalculatePingDays(service_->extension_prefs()->LastPingDay(id)); |
| 262 while (existing_iter != fetches_.end()) { | 263 while (existing_iter != fetches_.end()) { |
| 263 if (existing_iter->second->AddExtension(id, version.GetString(), | 264 if (existing_iter->second->AddExtension(id, version.GetString(), |
| 264 ping_days)) { | 265 ping_days)) { |
| 265 fetch = existing_iter->second; | 266 fetch = existing_iter->second; |
| 266 break; | 267 break; |
| 267 } | 268 } |
| 268 existing_iter++; | 269 existing_iter++; |
| 269 } | 270 } |
| 270 if (!fetch) { | 271 if (!fetch) { |
| 271 fetch = new ManifestFetchData(update_url); | 272 fetch = new ManifestFetchData(update_url); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 293 } | 294 } |
| 294 if (file_util::WriteFile(path, data.c_str(), data.length()) != | 295 if (file_util::WriteFile(path, data.c_str(), data.length()) != |
| 295 static_cast<int>(data.length())) { | 296 static_cast<int>(data.length())) { |
| 296 // TODO(asargent) - It would be nice to back off updating alltogether if | 297 // TODO(asargent) - It would be nice to back off updating alltogether if |
| 297 // the disk is full. (http://crbug.com/12763). | 298 // the disk is full. (http://crbug.com/12763). |
| 298 LOG(ERROR) << "Failed to write temporary file"; | 299 LOG(ERROR) << "Failed to write temporary file"; |
| 299 file_util::Delete(path, false); | 300 file_util::Delete(path, false); |
| 300 return; | 301 return; |
| 301 } | 302 } |
| 302 | 303 |
| 303 // The ExtensionUpdater is now responsible for cleaning up the temp file | 304 // The ExtensionUpdater now owns the temp file. |
| 304 // from disk. | |
| 305 ChromeThread::PostTask( | 305 ChromeThread::PostTask( |
| 306 ChromeThread::UI, FROM_HERE, | 306 ChromeThread::UI, FROM_HERE, |
| 307 NewRunnableMethod( | 307 NewRunnableMethod( |
| 308 updater.get(), &ExtensionUpdater::OnCRXFileWritten, extension_id, | 308 updater.get(), &ExtensionUpdater::OnCRXFileWritten, extension_id, |
| 309 path, download_url)); | 309 path, download_url)); |
| 310 } | 310 } |
| 311 | 311 |
| 312 void DeleteFile(const FilePath& path) { | |
| 313 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | |
| 314 if (!file_util::Delete(path, false)) { | |
| 315 LOG(WARNING) << "Failed to delete temp file " << path.value(); | |
| 316 } | |
| 317 } | |
| 318 | |
| 319 private: | 312 private: |
| 320 friend class base::RefCountedThreadSafe<ExtensionUpdaterFileHandler>; | 313 friend class base::RefCountedThreadSafe<ExtensionUpdaterFileHandler>; |
| 321 | 314 |
| 322 ~ExtensionUpdaterFileHandler() {} | 315 ~ExtensionUpdaterFileHandler() {} |
| 323 }; | 316 }; |
| 324 | 317 |
| 325 ExtensionUpdater::ExtensionUpdater(ExtensionUpdateService* service, | 318 ExtensionUpdater::ExtensionUpdater(ExtensionUpdateService* service, |
| 326 PrefService* prefs, | 319 PrefService* prefs, |
| 327 int frequency_seconds) | 320 int frequency_seconds) |
| 328 : service_(service), frequency_seconds_(frequency_seconds), | 321 : service_(service), frequency_seconds_(frequency_seconds), |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 results.daystart_elapsed_seconds >= 0) { | 540 results.daystart_elapsed_seconds >= 0) { |
| 548 Time daystart = | 541 Time daystart = |
| 549 Time::Now() - TimeDelta::FromSeconds(results.daystart_elapsed_seconds); | 542 Time::Now() - TimeDelta::FromSeconds(results.daystart_elapsed_seconds); |
| 550 | 543 |
| 551 const std::set<std::string>& extension_ids = fetch_data.extension_ids(); | 544 const std::set<std::string>& extension_ids = fetch_data.extension_ids(); |
| 552 std::set<std::string>::const_iterator i; | 545 std::set<std::string>::const_iterator i; |
| 553 for (i = extension_ids.begin(); i != extension_ids.end(); i++) { | 546 for (i = extension_ids.begin(); i != extension_ids.end(); i++) { |
| 554 bool did_ping = fetch_data.DidPing(*i); | 547 bool did_ping = fetch_data.DidPing(*i); |
| 555 if (did_ping) { | 548 if (did_ping) { |
| 556 if (*i == kBlacklistAppID) { | 549 if (*i == kBlacklistAppID) { |
| 557 service_->SetBlacklistLastPingDay(daystart); | 550 service_->extension_prefs()->SetBlacklistLastPingDay(daystart); |
| 558 } else if (service_->GetExtensionById(*i, true) != NULL) { | 551 } else if (service_->GetExtensionById(*i, true) != NULL) { |
| 559 service_->SetLastPingDay(*i, daystart); | 552 service_->extension_prefs()->SetLastPingDay(*i, daystart); |
| 560 } | 553 } |
| 561 } | 554 } |
| 562 } | 555 } |
| 563 } | 556 } |
| 564 } | 557 } |
| 565 | 558 |
| 566 void ExtensionUpdater::ProcessBlacklist(const std::string& data) { | 559 void ExtensionUpdater::ProcessBlacklist(const std::string& data) { |
| 567 // Verify sha256 hash value. | 560 // Verify sha256 hash value. |
| 568 char sha256_hash_value[base::SHA256_LENGTH]; | 561 char sha256_hash_value[base::SHA256_LENGTH]; |
| 569 base::SHA256HashString(data, sha256_hash_value, base::SHA256_LENGTH); | 562 base::SHA256HashString(data, sha256_hash_value, base::SHA256_LENGTH); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 585 prefs_->SetString(kExtensionBlacklistUpdateVersion, | 578 prefs_->SetString(kExtensionBlacklistUpdateVersion, |
| 586 ASCIIToWide(current_extension_fetch_.version)); | 579 ASCIIToWide(current_extension_fetch_.version)); |
| 587 prefs_->ScheduleSavePersistentPrefs(); | 580 prefs_->ScheduleSavePersistentPrefs(); |
| 588 } | 581 } |
| 589 | 582 |
| 590 void ExtensionUpdater::OnCRXFetchComplete(const GURL& url, | 583 void ExtensionUpdater::OnCRXFetchComplete(const GURL& url, |
| 591 const URLRequestStatus& status, | 584 const URLRequestStatus& status, |
| 592 int response_code, | 585 int response_code, |
| 593 const std::string& data) { | 586 const std::string& data) { |
| 594 if (status.status() == URLRequestStatus::SUCCESS && | 587 if (status.status() == URLRequestStatus::SUCCESS && |
| 595 response_code == 200) { | 588 response_code == 200) { |
| 596 if (current_extension_fetch_.id == kBlacklistAppID) { | 589 if (current_extension_fetch_.id == kBlacklistAppID) { |
| 597 ProcessBlacklist(data); | 590 ProcessBlacklist(data); |
| 598 } else { | 591 } else { |
| 599 // Successfully fetched - now write crx to a file so we can have the | 592 // Successfully fetched - now write crx to a file so we can have the |
| 600 // ExtensionsService install it. | 593 // ExtensionsService install it. |
| 601 ChromeThread::PostTask( | 594 ChromeThread::PostTask( |
| 602 ChromeThread::FILE, FROM_HERE, | 595 ChromeThread::FILE, FROM_HERE, |
| 603 NewRunnableMethod( | 596 NewRunnableMethod( |
| 604 file_handler_.get(), &ExtensionUpdaterFileHandler::WriteTempFile, | 597 file_handler_.get(), &ExtensionUpdaterFileHandler::WriteTempFile, |
| 605 current_extension_fetch_.id, data, url, | 598 current_extension_fetch_.id, data, url, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 619 if (extensions_pending_.size() > 0) { | 612 if (extensions_pending_.size() > 0) { |
| 620 ExtensionFetch next = extensions_pending_.front(); | 613 ExtensionFetch next = extensions_pending_.front(); |
| 621 extensions_pending_.pop_front(); | 614 extensions_pending_.pop_front(); |
| 622 FetchUpdatedExtension(next.id, next.url, next.package_hash, next.version); | 615 FetchUpdatedExtension(next.id, next.url, next.package_hash, next.version); |
| 623 } | 616 } |
| 624 } | 617 } |
| 625 | 618 |
| 626 void ExtensionUpdater::OnCRXFileWritten(const std::string& id, | 619 void ExtensionUpdater::OnCRXFileWritten(const std::string& id, |
| 627 const FilePath& path, | 620 const FilePath& path, |
| 628 const GURL& download_url) { | 621 const GURL& download_url) { |
| 622 // The ExtensionsService is now responsible for cleaning up the temp file |
| 623 // at |path|. |
| 629 service_->UpdateExtension(id, path, download_url); | 624 service_->UpdateExtension(id, path, download_url); |
| 630 } | 625 } |
| 631 | 626 |
| 632 void ExtensionUpdater::OnExtensionInstallFinished(const FilePath& path, | |
| 633 Extension* extension) { | |
| 634 // Have the file_handler_ delete the temp file on the file I/O thread. | |
| 635 ChromeThread::PostTask( | |
| 636 ChromeThread::FILE, FROM_HERE, | |
| 637 NewRunnableMethod( | |
| 638 file_handler_.get(), &ExtensionUpdaterFileHandler::DeleteFile, path)); | |
| 639 } | |
| 640 | 627 |
| 641 void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) { | 628 void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) { |
| 642 DCHECK(!timer_.IsRunning()); | 629 DCHECK(!timer_.IsRunning()); |
| 643 DCHECK(target_delay >= TimeDelta::FromSeconds(1)); | 630 DCHECK(target_delay >= TimeDelta::FromSeconds(1)); |
| 644 | 631 |
| 645 // Add +/- 10% random jitter. | 632 // Add +/- 10% random jitter. |
| 646 double delay_ms = target_delay.InMillisecondsF(); | 633 double delay_ms = target_delay.InMillisecondsF(); |
| 647 double jitter_factor = (RandDouble() * .2) - 0.1; | 634 double jitter_factor = (RandDouble() * .2) - 0.1; |
| 648 delay_ms += delay_ms * jitter_factor; | 635 delay_ms += delay_ms * jitter_factor; |
| 649 TimeDelta actual_delay = TimeDelta::FromMilliseconds( | 636 TimeDelta actual_delay = TimeDelta::FromMilliseconds( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 | 686 |
| 700 fetches_builder.ReportStats(); | 687 fetches_builder.ReportStats(); |
| 701 | 688 |
| 702 std::vector<ManifestFetchData*> fetches(fetches_builder.GetFetches()); | 689 std::vector<ManifestFetchData*> fetches(fetches_builder.GetFetches()); |
| 703 | 690 |
| 704 // Start a fetch of the blacklist if needed. | 691 // Start a fetch of the blacklist if needed. |
| 705 if (blacklist_checks_enabled_ && service_->HasInstalledExtensions()) { | 692 if (blacklist_checks_enabled_ && service_->HasInstalledExtensions()) { |
| 706 ManifestFetchData* blacklist_fetch = | 693 ManifestFetchData* blacklist_fetch = |
| 707 new ManifestFetchData(GURL(kBlacklistUpdateUrl)); | 694 new ManifestFetchData(GURL(kBlacklistUpdateUrl)); |
| 708 std::wstring version = prefs_->GetString(kExtensionBlacklistUpdateVersion); | 695 std::wstring version = prefs_->GetString(kExtensionBlacklistUpdateVersion); |
| 709 int ping_days = CalculatePingDays(service_->BlacklistLastPingDay()); | 696 int ping_days = |
| 697 CalculatePingDays(service_->extension_prefs()->BlacklistLastPingDay()); |
| 710 blacklist_fetch->AddExtension(kBlacklistAppID, WideToASCII(version), | 698 blacklist_fetch->AddExtension(kBlacklistAppID, WideToASCII(version), |
| 711 ping_days); | 699 ping_days); |
| 712 StartUpdateCheck(blacklist_fetch); | 700 StartUpdateCheck(blacklist_fetch); |
| 713 } | 701 } |
| 714 | 702 |
| 715 // Now start fetching regular extension updates | 703 // Now start fetching regular extension updates |
| 716 for (std::vector<ManifestFetchData*>::const_iterator it = fetches.begin(); | 704 for (std::vector<ManifestFetchData*>::const_iterator it = fetches.begin(); |
| 717 it != fetches.end(); ++it) { | 705 it != fetches.end(); ++it) { |
| 718 // StartUpdateCheck makes sure the url isn't already downloading or | 706 // StartUpdateCheck makes sure the url isn't already downloading or |
| 719 // scheduled, so we don't need to check before calling it. Ownership of | 707 // scheduled, so we don't need to check before calling it. Ownership of |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 extension_fetcher_.reset( | 843 extension_fetcher_.reset( |
| 856 URLFetcher::Create(kExtensionFetcherId, url, URLFetcher::GET, this)); | 844 URLFetcher::Create(kExtensionFetcherId, url, URLFetcher::GET, this)); |
| 857 extension_fetcher_->set_request_context( | 845 extension_fetcher_->set_request_context( |
| 858 Profile::GetDefaultRequestContext()); | 846 Profile::GetDefaultRequestContext()); |
| 859 extension_fetcher_->set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES | | 847 extension_fetcher_->set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 860 net::LOAD_DO_NOT_SAVE_COOKIES); | 848 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 861 extension_fetcher_->Start(); | 849 extension_fetcher_->Start(); |
| 862 current_extension_fetch_ = ExtensionFetch(id, url, hash, version); | 850 current_extension_fetch_ = ExtensionFetch(id, url, hash, version); |
| 863 } | 851 } |
| 864 } | 852 } |
| OLD | NEW |