| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/component_updater/url_fetcher_downloader.h" | 5 #include "chrome/browser/component_updater/url_fetcher_downloader.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/sequenced_task_runner.h" |
| 8 #include "chrome/browser/component_updater/component_updater_utils.h" | 9 #include "chrome/browser/component_updater/component_updater_utils.h" |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 11 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 13 | 13 |
| 14 using content::BrowserThread; | |
| 15 | |
| 16 namespace component_updater { | 14 namespace component_updater { |
| 17 | 15 |
| 18 UrlFetcherDownloader::UrlFetcherDownloader( | 16 UrlFetcherDownloader::UrlFetcherDownloader( |
| 19 scoped_ptr<CrxDownloader> successor, | 17 scoped_ptr<CrxDownloader> successor, |
| 20 net::URLRequestContextGetter* context_getter, | 18 net::URLRequestContextGetter* context_getter, |
| 21 scoped_refptr<base::SequencedTaskRunner> task_runner) | 19 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 22 : CrxDownloader(successor.Pass()), | 20 : CrxDownloader(successor.Pass()), |
| 23 context_getter_(context_getter), | 21 context_getter_(context_getter), |
| 24 task_runner_(task_runner), | 22 task_runner_(task_runner), |
| 25 downloaded_bytes_(-1), | 23 downloaded_bytes_(-1), |
| 26 total_bytes_(-1) { | 24 total_bytes_(-1) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 28 } | 25 } |
| 29 | 26 |
| 30 UrlFetcherDownloader::~UrlFetcherDownloader() { | 27 UrlFetcherDownloader::~UrlFetcherDownloader() { |
| 28 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 } | 29 } |
| 32 | 30 |
| 33 void UrlFetcherDownloader::DoStartDownload(const GURL& url) { | 31 void UrlFetcherDownloader::DoStartDownload(const GURL& url) { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 | 33 |
| 36 url_fetcher_.reset( | 34 url_fetcher_.reset( |
| 37 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this)); | 35 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this)); |
| 38 url_fetcher_->SetRequestContext(context_getter_); | 36 url_fetcher_->SetRequestContext(context_getter_); |
| 39 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 37 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 40 net::LOAD_DO_NOT_SAVE_COOKIES | | 38 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 41 net::LOAD_DISABLE_CACHE); | 39 net::LOAD_DISABLE_CACHE); |
| 42 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 40 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 43 url_fetcher_->SaveResponseToTemporaryFile(task_runner_); | 41 url_fetcher_->SaveResponseToTemporaryFile(task_runner_); |
| 44 | 42 |
| 45 VLOG(1) << "Starting background download: " << url.spec(); | 43 VLOG(1) << "Starting background download: " << url.spec(); |
| 46 url_fetcher_->Start(); | 44 url_fetcher_->Start(); |
| 47 | 45 |
| 48 download_start_time_ = base::Time::Now(); | 46 download_start_time_ = base::Time::Now(); |
| 49 | 47 |
| 50 downloaded_bytes_ = -1; | 48 downloaded_bytes_ = -1; |
| 51 total_bytes_ = -1; | 49 total_bytes_ = -1; |
| 52 } | 50 } |
| 53 | 51 |
| 54 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher* source) { | 52 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 | 54 |
| 57 const base::Time download_end_time(base::Time::Now()); | 55 const base::Time download_end_time(base::Time::Now()); |
| 58 const base::TimeDelta download_time = | 56 const base::TimeDelta download_time = |
| 59 download_end_time >= download_start_time_ | 57 download_end_time >= download_start_time_ |
| 60 ? download_end_time - download_start_time_ | 58 ? download_end_time - download_start_time_ |
| 61 : base::TimeDelta(); | 59 : base::TimeDelta(); |
| 62 | 60 |
| 63 // Consider a 5xx response from the server as an indication to terminate | 61 // Consider a 5xx response from the server as an indication to terminate |
| 64 // the request and avoid overloading the server in this case. | 62 // the request and avoid overloading the server in this case. |
| 65 // is not accepting requests for the moment. | 63 // is not accepting requests for the moment. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 87 VLOG(1) << "Downloaded " << downloaded_bytes_ << " bytes in " | 85 VLOG(1) << "Downloaded " << downloaded_bytes_ << " bytes in " |
| 88 << download_time.InMilliseconds() << "ms from " | 86 << download_time.InMilliseconds() << "ms from " |
| 89 << source->GetURL().spec() << " to " << local_path_.value(); | 87 << source->GetURL().spec() << " to " << local_path_.value(); |
| 90 CrxDownloader::OnDownloadComplete(is_handled, result, download_metrics); | 88 CrxDownloader::OnDownloadComplete(is_handled, result, download_metrics); |
| 91 } | 89 } |
| 92 | 90 |
| 93 void UrlFetcherDownloader::OnURLFetchDownloadProgress( | 91 void UrlFetcherDownloader::OnURLFetchDownloadProgress( |
| 94 const net::URLFetcher* source, | 92 const net::URLFetcher* source, |
| 95 int64 current, | 93 int64 current, |
| 96 int64 total) { | 94 int64 total) { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 | 96 |
| 99 downloaded_bytes_ = current; | 97 downloaded_bytes_ = current; |
| 100 total_bytes_ = total; | 98 total_bytes_ = total; |
| 101 | 99 |
| 102 Result result; | 100 Result result; |
| 103 result.downloaded_bytes = downloaded_bytes_; | 101 result.downloaded_bytes = downloaded_bytes_; |
| 104 result.total_bytes = total_bytes_; | 102 result.total_bytes = total_bytes_; |
| 105 | 103 |
| 106 OnDownloadProgress(result); | 104 OnDownloadProgress(result); |
| 107 } | 105 } |
| 108 | 106 |
| 109 } // namespace component_updater | 107 } // namespace component_updater |
| OLD | NEW |