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