blob: a3419fd77f65f423994858ce93aaf3bd0e3dee9e [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]afa378f22013-12-02 03:37:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
sorin52ac0882015-01-24 01:15:005#include "components/update_client/crx_downloader.h"
[email protected]ed6fb982014-07-23 16:56:526
sorin6bb8de42015-06-03 00:23:277#include "base/bind.h"
8#include "base/location.h"
[email protected]ed6fb982014-07-23 16:56:529#include "base/logging.h"
10#include "base/sequenced_task_runner.h"
sorin6bb8de42015-06-03 00:23:2711#include "base/thread_task_runner_handle.h"
sorin52ac0882015-01-24 01:15:0012#include "components/update_client/url_fetcher_downloader.h"
[email protected]afa378f22013-12-02 03:37:5413
[email protected]3cb2a4f2013-12-07 21:54:3414#if defined(OS_WIN)
sorin52ac0882015-01-24 01:15:0015#include "components/update_client/background_downloader_win.h"
[email protected]3cb2a4f2013-12-07 21:54:3416#endif
17
sorin52ac0882015-01-24 01:15:0018namespace update_client {
[email protected]afa378f22013-12-02 03:37:5419
[email protected]8a5ebd432014-05-02 00:21:2220CrxDownloader::Result::Result()
21 : error(0), downloaded_bytes(-1), total_bytes(-1) {
22}
[email protected]3a0092d2013-12-18 03:04:3523
24CrxDownloader::DownloadMetrics::DownloadMetrics()
25 : downloader(kNone),
26 error(0),
[email protected]8a5ebd432014-05-02 00:21:2227 downloaded_bytes(-1),
28 total_bytes(-1),
29 download_time_ms(0) {
30}
[email protected]3a0092d2013-12-18 03:04:3531
[email protected]3cb2a4f2013-12-07 21:54:3432// On Windows, the first downloader in the chain is a background downloader,
33// which uses the BITS service.
sorin9797aba2015-04-17 17:15:0334scoped_ptr<CrxDownloader> CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:3435 bool is_background_download,
[email protected]afa378f22013-12-02 03:37:5436 net::URLRequestContextGetter* context_getter,
sorin33012532015-10-26 21:05:5437 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
38 scoped_ptr<CrxDownloader> url_fetcher_downloader(
39 scoped_ptr<CrxDownloader>(new UrlFetcherDownloader(
40 scoped_ptr<CrxDownloader>().Pass(), context_getter, task_runner)));
[email protected]d0c8b8b42014-05-06 05:11:4541#if defined(OS_WIN)
[email protected]3cb2a4f2013-12-07 21:54:3442 if (is_background_download) {
sorin9797aba2015-04-17 17:15:0343 return scoped_ptr<CrxDownloader>(new BackgroundDownloader(
sorin33012532015-10-26 21:05:5444 url_fetcher_downloader.Pass(), context_getter, task_runner));
[email protected]3cb2a4f2013-12-07 21:54:3445 }
46#endif
[email protected]afa378f22013-12-02 03:37:5447
sorin9797aba2015-04-17 17:15:0348 return url_fetcher_downloader;
[email protected]afa378f22013-12-02 03:37:5449}
50
[email protected]1b6587dc52014-04-26 00:38:5551CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor)
52 : successor_(successor.Pass()) {
[email protected]afa378f22013-12-02 03:37:5453}
54
55CrxDownloader::~CrxDownloader() {
56}
57
[email protected]8a5ebd432014-05-02 00:21:2258void CrxDownloader::set_progress_callback(
59 const ProgressCallback& progress_callback) {
60 progress_callback_ = progress_callback;
61}
62
[email protected]3a0092d2013-12-18 03:04:3563GURL CrxDownloader::url() const {
64 return current_url_ != urls_.end() ? *current_url_ : GURL();
65}
66
67const std::vector<CrxDownloader::DownloadMetrics>
68CrxDownloader::download_metrics() const {
69 if (!successor_)
70 return download_metrics_;
71
72 std::vector<DownloadMetrics> retval(successor_->download_metrics());
sorin52ac0882015-01-24 01:15:0073 retval.insert(retval.begin(), download_metrics_.begin(),
74 download_metrics_.end());
[email protected]3a0092d2013-12-18 03:04:3575 return retval;
76}
77
[email protected]1b6587dc52014-04-26 00:38:5578void CrxDownloader::StartDownloadFromUrl(
79 const GURL& url,
80 const DownloadCallback& download_callback) {
[email protected]afa378f22013-12-02 03:37:5481 std::vector<GURL> urls;
82 urls.push_back(url);
[email protected]1b6587dc52014-04-26 00:38:5583 StartDownload(urls, download_callback);
[email protected]afa378f22013-12-02 03:37:5484}
85
[email protected]1b6587dc52014-04-26 00:38:5586void CrxDownloader::StartDownload(const std::vector<GURL>& urls,
87 const DownloadCallback& download_callback) {
[email protected]ed6fb982014-07-23 16:56:5288 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:5489
[email protected]da37c1d2013-12-19 01:04:3890 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]1b6587dc52014-04-26 00:38:5594 download_callback.Run(result);
[email protected]da37c1d2013-12-19 01:04:3895 return;
96 }
[email protected]afa378f22013-12-02 03:37:5497
[email protected]3cb2a4f2013-12-07 21:54:3498 // 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]afa378f22013-12-02 03:37:54102 urls_ = urls;
[email protected]3cb2a4f2013-12-07 21:54:34103 current_url_ = urls_.begin();
[email protected]1b6587dc52014-04-26 00:38:55104 download_callback_ = download_callback;
[email protected]afa378f22013-12-02 03:37:54105
[email protected]3cb2a4f2013-12-07 21:54:34106 DoStartDownload(*current_url_);
[email protected]afa378f22013-12-02 03:37:54107}
108
[email protected]3a0092d2013-12-18 03:04:35109void CrxDownloader::OnDownloadComplete(
110 bool is_handled,
111 const Result& result,
112 const DownloadMetrics& download_metrics) {
[email protected]ed6fb982014-07-23 16:56:52113 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:54114
[email protected]3a0092d2013-12-18 03:04:35115 download_metrics_.push_back(download_metrics);
116
[email protected]3cb2a4f2013-12-07 21:54:34117 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]afa378f22013-12-02 03:37:54133 return;
134 }
135
[email protected]3cb2a4f2013-12-07 21:54:34136 // 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]da37c1d2013-12-19 01:04:38140 if (successor_ && !urls_.empty()) {
[email protected]1b6587dc52014-04-26 00:38:55141 successor_->StartDownload(urls_, download_callback_);
[email protected]afa378f22013-12-02 03:37:54142 return;
[email protected]da37c1d2013-12-19 01:04:38143 }
[email protected]afa378f22013-12-02 03:37:54144 }
145
sorin6bb8de42015-06-03 00:23:27146 base::ThreadTaskRunnerHandle::Get()->PostTask(
147 FROM_HERE, base::Bind(download_callback_, result));
[email protected]afa378f22013-12-02 03:37:54148}
149
[email protected]8a5ebd432014-05-02 00:21:22150void CrxDownloader::OnDownloadProgress(const Result& result) {
[email protected]ed6fb982014-07-23 16:56:52151 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:54152
[email protected]8a5ebd432014-05-02 00:21:22153 if (progress_callback_.is_null())
154 return;
155
156 progress_callback_.Run(result);
157}
158
sorin52ac0882015-01-24 01:15:00159} // namespace update_client