OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "components/update_client/crx_downloader.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/sequenced_task_runner.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "components/update_client/url_fetcher_downloader.h" |
| 11 |
| 12 #if defined(OS_WIN) |
| 13 #include "components/update_client/background_downloader_win.h" |
| 14 #endif |
| 15 |
| 16 namespace update_client { |
| 17 |
| 18 CrxDownloader::Result::Result() |
| 19 : error(0), downloaded_bytes(-1), total_bytes(-1) { |
| 20 } |
| 21 |
| 22 CrxDownloader::DownloadMetrics::DownloadMetrics() |
| 23 : downloader(kNone), |
| 24 error(0), |
| 25 downloaded_bytes(-1), |
| 26 total_bytes(-1), |
| 27 download_time_ms(0) { |
| 28 } |
| 29 |
| 30 // On Windows, the first downloader in the chain is a background downloader, |
| 31 // which uses the BITS service. |
| 32 CrxDownloader* CrxDownloader::Create( |
| 33 bool is_background_download, |
| 34 net::URLRequestContextGetter* context_getter, |
| 35 scoped_refptr<base::SequencedTaskRunner> url_fetcher_task_runner, |
| 36 scoped_refptr<base::SingleThreadTaskRunner> background_task_runner) { |
| 37 scoped_ptr<CrxDownloader> url_fetcher_downloader( |
| 38 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), |
| 39 context_getter, url_fetcher_task_runner)); |
| 40 #if defined(OS_WIN) |
| 41 if (is_background_download) { |
| 42 return new BackgroundDownloader(url_fetcher_downloader.Pass(), |
| 43 context_getter, background_task_runner); |
| 44 } |
| 45 #endif |
| 46 |
| 47 return url_fetcher_downloader.release(); |
| 48 } |
| 49 |
| 50 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor) |
| 51 : successor_(successor.Pass()) { |
| 52 } |
| 53 |
| 54 CrxDownloader::~CrxDownloader() { |
| 55 } |
| 56 |
| 57 void CrxDownloader::set_progress_callback( |
| 58 const ProgressCallback& progress_callback) { |
| 59 progress_callback_ = progress_callback; |
| 60 } |
| 61 |
| 62 GURL CrxDownloader::url() const { |
| 63 return current_url_ != urls_.end() ? *current_url_ : GURL(); |
| 64 } |
| 65 |
| 66 const std::vector<CrxDownloader::DownloadMetrics> |
| 67 CrxDownloader::download_metrics() const { |
| 68 if (!successor_) |
| 69 return download_metrics_; |
| 70 |
| 71 std::vector<DownloadMetrics> retval(successor_->download_metrics()); |
| 72 retval.insert(retval.begin(), download_metrics_.begin(), |
| 73 download_metrics_.end()); |
| 74 return retval; |
| 75 } |
| 76 |
| 77 void CrxDownloader::StartDownloadFromUrl( |
| 78 const GURL& url, |
| 79 const DownloadCallback& download_callback) { |
| 80 std::vector<GURL> urls; |
| 81 urls.push_back(url); |
| 82 StartDownload(urls, download_callback); |
| 83 } |
| 84 |
| 85 void CrxDownloader::StartDownload(const std::vector<GURL>& urls, |
| 86 const DownloadCallback& download_callback) { |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 |
| 89 if (urls.empty()) { |
| 90 // Make a result and complete the download with a generic error for now. |
| 91 Result result; |
| 92 result.error = -1; |
| 93 download_callback.Run(result); |
| 94 return; |
| 95 } |
| 96 |
| 97 // If the urls are mutated while this downloader is active, then the |
| 98 // behavior is undefined in the sense that the outcome of the download could |
| 99 // be inconsistent for the list of urls. At any rate, the |current_url_| is |
| 100 // reset at this point, and the iterator will be valid in all conditions. |
| 101 urls_ = urls; |
| 102 current_url_ = urls_.begin(); |
| 103 download_callback_ = download_callback; |
| 104 |
| 105 DoStartDownload(*current_url_); |
| 106 } |
| 107 |
| 108 void CrxDownloader::OnDownloadComplete( |
| 109 bool is_handled, |
| 110 const Result& result, |
| 111 const DownloadMetrics& download_metrics) { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 |
| 114 download_metrics_.push_back(download_metrics); |
| 115 |
| 116 if (result.error) { |
| 117 // If an error has occured, in general try the next url if there is any, |
| 118 // then move on to the successor in the chain if there is any successor. |
| 119 // If this downloader has received a 5xx error for the current url, |
| 120 // as indicated by the |is_handled| flag, remove that url from the list of |
| 121 // urls so the url is never retried. In both cases, move on to the |
| 122 // next url. |
| 123 if (!is_handled) { |
| 124 ++current_url_; |
| 125 } else { |
| 126 current_url_ = urls_.erase(current_url_); |
| 127 } |
| 128 |
| 129 // Try downloading from another url from the list. |
| 130 if (current_url_ != urls_.end()) { |
| 131 DoStartDownload(*current_url_); |
| 132 return; |
| 133 } |
| 134 |
| 135 // If there is another downloader that can accept this request, then hand |
| 136 // the request over to it so that the successor can try the pruned list |
| 137 // of urls. Otherwise, the request ends here since the current downloader |
| 138 // has tried all urls and it can't fall back on any other downloader. |
| 139 if (successor_ && !urls_.empty()) { |
| 140 successor_->StartDownload(urls_, download_callback_); |
| 141 return; |
| 142 } |
| 143 } |
| 144 |
| 145 download_callback_.Run(result); |
| 146 } |
| 147 |
| 148 void CrxDownloader::OnDownloadProgress(const Result& result) { |
| 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 150 |
| 151 if (progress_callback_.is_null()) |
| 152 return; |
| 153 |
| 154 progress_callback_.Run(result); |
| 155 } |
| 156 |
| 157 } // namespace update_client |
OLD | NEW |