[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 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #include "components/update_client/crx_downloader.h" |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 6 | |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 9 | #include "base/bind.h" |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 12 | #include "base/logging.h" |
Gabriel Charette | 41ea1415 | 2020-03-02 15:43:11 | [diff] [blame] | 13 | #include "base/task/thread_pool.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 16 | #if defined(OS_WIN) |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 17 | #include "components/update_client/background_downloader_win.h" |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 18 | #endif |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 19 | #include "components/update_client/network.h" |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 20 | #include "components/update_client/task_traits.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 21 | #include "components/update_client/update_client_errors.h" |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 22 | #include "components/update_client/url_fetcher_downloader.h" |
| 23 | #include "components/update_client/utils.h" |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 24 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 25 | namespace update_client { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 26 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 27 | CrxDownloader::DownloadMetrics::DownloadMetrics() |
| 28 | : downloader(kNone), |
| 29 | error(0), |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 30 | downloaded_bytes(-1), |
| 31 | total_bytes(-1), |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 32 | download_time_ms(0) {} |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 33 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 34 | // On Windows, the first downloader in the chain is a background downloader, |
| 35 | // which uses the BITS service. |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 36 | std::unique_ptr<CrxDownloader> CrxDownloader::Create( |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 37 | bool is_background_download, |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 38 | scoped_refptr<NetworkFetcherFactory> network_fetcher_factory) { |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 39 | std::unique_ptr<CrxDownloader> url_fetcher_downloader = |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 40 | std::make_unique<UrlFetcherDownloader>(nullptr, network_fetcher_factory); |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 41 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 42 | #if defined(OS_WIN) |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 43 | if (is_background_download) { |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 44 | return std::make_unique<BackgroundDownloader>( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 45 | std::move(url_fetcher_downloader)); |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 46 | } |
| 47 | #endif |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 48 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 49 | return url_fetcher_downloader; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 50 | } |
| 51 | |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 52 | CrxDownloader::CrxDownloader(std::unique_ptr<CrxDownloader> successor) |
| 53 | : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 54 | successor_(std::move(successor)) {} |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 55 | |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 56 | CrxDownloader::~CrxDownloader() = default; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 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()); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 73 | retval.insert(retval.begin(), download_metrics_.begin(), |
| 74 | download_metrics_.end()); |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 75 | return retval; |
| 76 | } |
| 77 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 78 | void CrxDownloader::StartDownloadFromUrl(const GURL& url, |
| 79 | const std::string& expected_hash, |
| 80 | DownloadCallback download_callback) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 81 | std::vector<GURL> urls; |
| 82 | urls.push_back(url); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 83 | StartDownload(urls, expected_hash, std::move(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, |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 87 | const std::string& expected_hash, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 88 | DownloadCallback download_callback) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 89 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 90 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 91 | auto error = CrxDownloaderError::NONE; |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 92 | if (urls.empty()) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 93 | error = CrxDownloaderError::NO_URL; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 94 | } else if (expected_hash.empty()) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 95 | error = CrxDownloaderError::NO_HASH; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 96 | } |
| 97 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 98 | if (error != CrxDownloaderError::NONE) { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 99 | Result result; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 100 | result.error = static_cast<int>(error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 101 | main_task_runner()->PostTask( |
| 102 | FROM_HERE, base::BindOnce(std::move(download_callback), result)); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 103 | return; |
| 104 | } |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 105 | |
| 106 | urls_ = urls; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 107 | expected_hash_ = expected_hash; |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 108 | current_url_ = urls_.begin(); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 109 | download_callback_ = std::move(download_callback); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 110 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 111 | DoStartDownload(*current_url_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 114 | void CrxDownloader::OnDownloadComplete( |
| 115 | bool is_handled, |
| 116 | const Result& result, |
| 117 | const DownloadMetrics& download_metrics) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 118 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 119 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 120 | if (!result.error) |
Gabriel Charette | 41ea1415 | 2020-03-02 15:43:11 | [diff] [blame] | 121 | base::ThreadPool::PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 122 | FROM_HERE, kTaskTraits, |
| 123 | base::BindOnce(&CrxDownloader::VerifyResponse, base::Unretained(this), |
| 124 | is_handled, result, download_metrics)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 125 | else |
| 126 | main_task_runner()->PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 127 | FROM_HERE, base::BindOnce(&CrxDownloader::HandleDownloadError, |
| 128 | base::Unretained(this), is_handled, result, |
| 129 | download_metrics)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 130 | } |
| 131 | |
Antonio Gomes | 31237fb | 2018-08-27 19:11:03 | [diff] [blame] | 132 | void CrxDownloader::OnDownloadProgress() { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 133 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 134 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 135 | if (progress_callback_.is_null()) |
| 136 | return; |
| 137 | |
Antonio Gomes | 31237fb | 2018-08-27 19:11:03 | [diff] [blame] | 138 | progress_callback_.Run(); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 139 | } |
| 140 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 141 | // The function mutates the values of the parameters |result| and |
| 142 | // |download_metrics|. |
| 143 | void CrxDownloader::VerifyResponse(bool is_handled, |
| 144 | Result result, |
| 145 | DownloadMetrics download_metrics) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 146 | DCHECK_EQ(0, result.error); |
| 147 | DCHECK_EQ(0, download_metrics.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 148 | DCHECK(is_handled); |
| 149 | |
| 150 | if (VerifyFileHash256(result.response, expected_hash_)) { |
| 151 | download_metrics_.push_back(download_metrics); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 152 | main_task_runner()->PostTask( |
| 153 | FROM_HERE, base::BindOnce(std::move(download_callback_), result)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | // The download was successful but the response is not trusted. Clean up |
| 158 | // the download, mutate the result, and try the remaining fallbacks when |
| 159 | // handling the error. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 160 | result.error = static_cast<int>(CrxDownloaderError::BAD_HASH); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 161 | download_metrics.error = result.error; |
| 162 | DeleteFileAndEmptyParentDirectory(result.response); |
| 163 | result.response.clear(); |
| 164 | |
| 165 | main_task_runner()->PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 166 | FROM_HERE, base::BindOnce(&CrxDownloader::HandleDownloadError, |
| 167 | base::Unretained(this), is_handled, result, |
| 168 | download_metrics)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void CrxDownloader::HandleDownloadError( |
| 172 | bool is_handled, |
| 173 | const Result& result, |
| 174 | const DownloadMetrics& download_metrics) { |
| 175 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 176 | DCHECK_NE(0, result.error); |
Sorin Jianu | 52de5fa | 2017-10-09 23:19:33 | [diff] [blame] | 177 | DCHECK(result.response.empty()); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 178 | DCHECK_NE(0, download_metrics.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 179 | |
| 180 | download_metrics_.push_back(download_metrics); |
| 181 | |
| 182 | // If an error has occured, try the next url if there is any, |
| 183 | // or try the successor in the chain if there is any successor. |
| 184 | // If this downloader has received a 5xx error for the current url, |
| 185 | // as indicated by the |is_handled| flag, remove that url from the list of |
| 186 | // urls so the url is never tried again down the chain. |
| 187 | if (is_handled) { |
| 188 | current_url_ = urls_.erase(current_url_); |
| 189 | } else { |
| 190 | ++current_url_; |
| 191 | } |
| 192 | |
| 193 | // Try downloading from another url from the list. |
| 194 | if (current_url_ != urls_.end()) { |
| 195 | DoStartDownload(*current_url_); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Try downloading using the next downloader. |
| 200 | if (successor_ && !urls_.empty()) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 201 | successor_->StartDownload(urls_, expected_hash_, |
| 202 | std::move(download_callback_)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | // The download ends here since there is no url nor downloader to handle this |
| 207 | // download request further. |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 208 | main_task_runner()->PostTask( |
| 209 | FROM_HERE, base::BindOnce(std::move(download_callback_), result)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 210 | } |
| 211 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 212 | } // namespace update_client |