blob: 9bb7e1866d8bfb3a193e19ca180db1da79078234 [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
7#include "base/logging.h"
8#include "base/sequenced_task_runner.h"
9#include "base/single_thread_task_runner.h"
sorin52ac0882015-01-24 01:15:0010#include "components/update_client/url_fetcher_downloader.h"
[email protected]afa378f22013-12-02 03:37:5411
[email protected]3cb2a4f2013-12-07 21:54:3412#if defined(OS_WIN)
sorin52ac0882015-01-24 01:15:0013#include "components/update_client/background_downloader_win.h"
[email protected]3cb2a4f2013-12-07 21:54:3414#endif
15
sorin52ac0882015-01-24 01:15:0016namespace update_client {
[email protected]afa378f22013-12-02 03:37:5417
[email protected]8a5ebd432014-05-02 00:21:2218CrxDownloader::Result::Result()
19 : error(0), downloaded_bytes(-1), total_bytes(-1) {
20}
[email protected]3a0092d2013-12-18 03:04:3521
22CrxDownloader::DownloadMetrics::DownloadMetrics()
23 : downloader(kNone),
24 error(0),
[email protected]8a5ebd432014-05-02 00:21:2225 downloaded_bytes(-1),
26 total_bytes(-1),
27 download_time_ms(0) {
28}
[email protected]3a0092d2013-12-18 03:04:3529
[email protected]3cb2a4f2013-12-07 21:54:3430// On Windows, the first downloader in the chain is a background downloader,
31// which uses the BITS service.
sorin9797aba2015-04-17 17:15:0332scoped_ptr<CrxDownloader> CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:3433 bool is_background_download,
[email protected]afa378f22013-12-02 03:37:5434 net::URLRequestContextGetter* context_getter,
sorin9797aba2015-04-17 17:15:0335 const scoped_refptr<base::SequencedTaskRunner>& url_fetcher_task_runner,
36 const scoped_refptr<base::SingleThreadTaskRunner>& background_task_runner) {
37 scoped_ptr<CrxDownloader> url_fetcher_downloader(scoped_ptr<CrxDownloader>(
[email protected]3cb2a4f2013-12-07 21:54:3438 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(),
sorin9797aba2015-04-17 17:15:0339 context_getter, url_fetcher_task_runner)));
[email protected]d0c8b8b42014-05-06 05:11:4540#if defined(OS_WIN)
[email protected]3cb2a4f2013-12-07 21:54:3441 if (is_background_download) {
sorin9797aba2015-04-17 17:15:0342 return scoped_ptr<CrxDownloader>(new BackgroundDownloader(
43 url_fetcher_downloader.Pass(), context_getter, background_task_runner));
[email protected]3cb2a4f2013-12-07 21:54:3444 }
45#endif
[email protected]afa378f22013-12-02 03:37:5446
sorin9797aba2015-04-17 17:15:0347 return url_fetcher_downloader;
[email protected]afa378f22013-12-02 03:37:5448}
49
[email protected]1b6587dc52014-04-26 00:38:5550CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor)
51 : successor_(successor.Pass()) {
[email protected]afa378f22013-12-02 03:37:5452}
53
54CrxDownloader::~CrxDownloader() {
55}
56
[email protected]8a5ebd432014-05-02 00:21:2257void CrxDownloader::set_progress_callback(
58 const ProgressCallback& progress_callback) {
59 progress_callback_ = progress_callback;
60}
61
[email protected]3a0092d2013-12-18 03:04:3562GURL CrxDownloader::url() const {
63 return current_url_ != urls_.end() ? *current_url_ : GURL();
64}
65
66const std::vector<CrxDownloader::DownloadMetrics>
67CrxDownloader::download_metrics() const {
68 if (!successor_)
69 return download_metrics_;
70
71 std::vector<DownloadMetrics> retval(successor_->download_metrics());
sorin52ac0882015-01-24 01:15:0072 retval.insert(retval.begin(), download_metrics_.begin(),
73 download_metrics_.end());
[email protected]3a0092d2013-12-18 03:04:3574 return retval;
75}
76
[email protected]1b6587dc52014-04-26 00:38:5577void CrxDownloader::StartDownloadFromUrl(
78 const GURL& url,
79 const DownloadCallback& download_callback) {
[email protected]afa378f22013-12-02 03:37:5480 std::vector<GURL> urls;
81 urls.push_back(url);
[email protected]1b6587dc52014-04-26 00:38:5582 StartDownload(urls, download_callback);
[email protected]afa378f22013-12-02 03:37:5483}
84
[email protected]1b6587dc52014-04-26 00:38:5585void CrxDownloader::StartDownload(const std::vector<GURL>& urls,
86 const DownloadCallback& download_callback) {
[email protected]ed6fb982014-07-23 16:56:5287 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:5488
[email protected]da37c1d2013-12-19 01:04:3889 if (urls.empty()) {
90 // Make a result and complete the download with a generic error for now.
91 Result result;
92 result.error = -1;
[email protected]1b6587dc52014-04-26 00:38:5593 download_callback.Run(result);
[email protected]da37c1d2013-12-19 01:04:3894 return;
95 }
[email protected]afa378f22013-12-02 03:37:5496
[email protected]3cb2a4f2013-12-07 21:54:3497 // If the urls are mutated while this downloader is active, then the
98 // behavior is undefined in the sense that the outcome of the download could
99 // be inconsistent for the list of urls. At any rate, the |current_url_| is
100 // reset at this point, and the iterator will be valid in all conditions.
[email protected]afa378f22013-12-02 03:37:54101 urls_ = urls;
[email protected]3cb2a4f2013-12-07 21:54:34102 current_url_ = urls_.begin();
[email protected]1b6587dc52014-04-26 00:38:55103 download_callback_ = download_callback;
[email protected]afa378f22013-12-02 03:37:54104
[email protected]3cb2a4f2013-12-07 21:54:34105 DoStartDownload(*current_url_);
[email protected]afa378f22013-12-02 03:37:54106}
107
[email protected]3a0092d2013-12-18 03:04:35108void CrxDownloader::OnDownloadComplete(
109 bool is_handled,
110 const Result& result,
111 const DownloadMetrics& download_metrics) {
[email protected]ed6fb982014-07-23 16:56:52112 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:54113
[email protected]3a0092d2013-12-18 03:04:35114 download_metrics_.push_back(download_metrics);
115
[email protected]3cb2a4f2013-12-07 21:54:34116 if (result.error) {
117 // If an error has occured, in general try the next url if there is any,
118 // then move on to the successor in the chain if there is any successor.
119 // If this downloader has received a 5xx error for the current url,
120 // as indicated by the |is_handled| flag, remove that url from the list of
121 // urls so the url is never retried. In both cases, move on to the
122 // next url.
123 if (!is_handled) {
124 ++current_url_;
125 } else {
126 current_url_ = urls_.erase(current_url_);
127 }
128
129 // Try downloading from another url from the list.
130 if (current_url_ != urls_.end()) {
131 DoStartDownload(*current_url_);
[email protected]afa378f22013-12-02 03:37:54132 return;
133 }
134
[email protected]3cb2a4f2013-12-07 21:54:34135 // If there is another downloader that can accept this request, then hand
136 // the request over to it so that the successor can try the pruned list
137 // of urls. Otherwise, the request ends here since the current downloader
138 // has tried all urls and it can't fall back on any other downloader.
[email protected]da37c1d2013-12-19 01:04:38139 if (successor_ && !urls_.empty()) {
[email protected]1b6587dc52014-04-26 00:38:55140 successor_->StartDownload(urls_, download_callback_);
[email protected]afa378f22013-12-02 03:37:54141 return;
[email protected]da37c1d2013-12-19 01:04:38142 }
[email protected]afa378f22013-12-02 03:37:54143 }
144
[email protected]3cb2a4f2013-12-07 21:54:34145 download_callback_.Run(result);
[email protected]afa378f22013-12-02 03:37:54146}
147
[email protected]8a5ebd432014-05-02 00:21:22148void CrxDownloader::OnDownloadProgress(const Result& result) {
[email protected]ed6fb982014-07-23 16:56:52149 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]afa378f22013-12-02 03:37:54150
[email protected]8a5ebd432014-05-02 00:21:22151 if (progress_callback_.is_null())
152 return;
153
154 progress_callback_.Run(result);
155}
156
sorin52ac0882015-01-24 01:15:00157} // namespace update_client