1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/component_updater/crx_downloader.h"
6 #include "chrome/browser/component_updater/url_fetcher_downloader.h"
7 #include "content/public/browser/browser_thread.h"
8
9 #if defined(OS_WIN)
10 #include "chrome/browser/component_updater/background_downloader_win.h"
11 #endif
12
13 using content::BrowserThread;
14
15 namespace component_updater {
16
Result()17 CrxDownloader::Result::Result()
18 : error(0), downloaded_bytes(-1), total_bytes(-1) {
19 }
20
DownloadMetrics()21 CrxDownloader::DownloadMetrics::DownloadMetrics()
22 : downloader(kNone),
23 error(0),
24 downloaded_bytes(-1),
25 total_bytes(-1),
26 download_time_ms(0) {
27 }
28
29 // On Windows, the first downloader in the chain is a background downloader,
30 // which uses the BITS service.
Create(bool is_background_download,net::URLRequestContextGetter * context_getter,scoped_refptr<base::SequencedTaskRunner> task_runner)31 CrxDownloader* CrxDownloader::Create(
32 bool is_background_download,
33 net::URLRequestContextGetter* context_getter,
34 scoped_refptr<base::SequencedTaskRunner> task_runner) {
35 scoped_ptr<CrxDownloader> url_fetcher_downloader(
36 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(),
37 context_getter,
38 task_runner));
39 #if defined(OS_WIN)
40 if (is_background_download) {
41 return new BackgroundDownloader(url_fetcher_downloader.Pass(),
42 context_getter,
43 task_runner);
44 }
45 #endif
46
47 return url_fetcher_downloader.release();
48 }
49
CrxDownloader(scoped_ptr<CrxDownloader> successor)50 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor)
51 : successor_(successor.Pass()) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
53 }
54
~CrxDownloader()55 CrxDownloader::~CrxDownloader() {
56 }
57
set_progress_callback(const ProgressCallback & progress_callback)58 void CrxDownloader::set_progress_callback(
59 const ProgressCallback& progress_callback) {
60 progress_callback_ = progress_callback;
61 }
62
url() const63 GURL CrxDownloader::url() const {
64 return current_url_ != urls_.end() ? *current_url_ : GURL();
65 }
66
67 const std::vector<CrxDownloader::DownloadMetrics>
download_metrics() const68 CrxDownloader::download_metrics() const {
69 if (!successor_)
70 return download_metrics_;
71
72 std::vector<DownloadMetrics> retval(successor_->download_metrics());
73 retval.insert(
74 retval.begin(), download_metrics_.begin(), download_metrics_.end());
75 return retval;
76 }
77
StartDownloadFromUrl(const GURL & url,const DownloadCallback & download_callback)78 void CrxDownloader::StartDownloadFromUrl(
79 const GURL& url,
80 const DownloadCallback& download_callback) {
81 std::vector<GURL> urls;
82 urls.push_back(url);
83 StartDownload(urls, download_callback);
84 }
85
StartDownload(const std::vector<GURL> & urls,const DownloadCallback & download_callback)86 void CrxDownloader::StartDownload(const std::vector<GURL>& urls,
87 const DownloadCallback& download_callback) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
89
90 if (urls.empty()) {
91 // Make a result and complete the download with a generic error for now.
92 Result result;
93 result.error = -1;
94 download_callback.Run(result);
95 return;
96 }
97
98 // 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.
102 urls_ = urls;
103 current_url_ = urls_.begin();
104 download_callback_ = download_callback;
105
106 DoStartDownload(*current_url_);
107 }
108
OnDownloadComplete(bool is_handled,const Result & result,const DownloadMetrics & download_metrics)109 void CrxDownloader::OnDownloadComplete(
110 bool is_handled,
111 const Result& result,
112 const DownloadMetrics& download_metrics) {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
114
115 download_metrics_.push_back(download_metrics);
116
117 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_);
133 return;
134 }
135
136 // 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.
140 if (successor_ && !urls_.empty()) {
141 successor_->StartDownload(urls_, download_callback_);
142 return;
143 }
144 }
145
146 download_callback_.Run(result);
147 }
148
OnDownloadProgress(const Result & result)149 void CrxDownloader::OnDownloadProgress(const Result& result) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151
152 if (progress_callback_.is_null())
153 return;
154
155 progress_callback_.Run(result);
156 }
157
158 } // namespace component_updater
159