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