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