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