Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / update_client / crx_downloader.cc
blob6ba832867bfabe61e67994d6364388bd589c601a
1 // Copyright 2014 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.
5 #include "components/update_client/crx_downloader.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/update_client/url_fetcher_downloader.h"
15 #if defined(OS_WIN)
16 #include "components/update_client/background_downloader_win.h"
17 #endif
19 namespace update_client {
21 CrxDownloader::Result::Result()
22 : error(0), downloaded_bytes(-1), total_bytes(-1) {
25 CrxDownloader::DownloadMetrics::DownloadMetrics()
26 : downloader(kNone),
27 error(0),
28 downloaded_bytes(-1),
29 total_bytes(-1),
30 download_time_ms(0) {
33 // On Windows, the first downloader in the chain is a background downloader,
34 // which uses the BITS service.
35 scoped_ptr<CrxDownloader> CrxDownloader::Create(
36 bool is_background_download,
37 net::URLRequestContextGetter* context_getter,
38 const scoped_refptr<base::SequencedTaskRunner>& url_fetcher_task_runner,
39 const scoped_refptr<base::SingleThreadTaskRunner>& background_task_runner) {
40 scoped_ptr<CrxDownloader> url_fetcher_downloader(scoped_ptr<CrxDownloader>(
41 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(),
42 context_getter, url_fetcher_task_runner)));
43 #if defined(OS_WIN)
44 if (is_background_download) {
45 return scoped_ptr<CrxDownloader>(new BackgroundDownloader(
46 url_fetcher_downloader.Pass(), context_getter, background_task_runner));
48 #endif
50 return url_fetcher_downloader;
53 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor)
54 : successor_(successor.Pass()) {
57 CrxDownloader::~CrxDownloader() {
60 void CrxDownloader::set_progress_callback(
61 const ProgressCallback& progress_callback) {
62 progress_callback_ = progress_callback;
65 GURL CrxDownloader::url() const {
66 return current_url_ != urls_.end() ? *current_url_ : GURL();
69 const std::vector<CrxDownloader::DownloadMetrics>
70 CrxDownloader::download_metrics() const {
71 if (!successor_)
72 return download_metrics_;
74 std::vector<DownloadMetrics> retval(successor_->download_metrics());
75 retval.insert(retval.begin(), download_metrics_.begin(),
76 download_metrics_.end());
77 return retval;
80 void CrxDownloader::StartDownloadFromUrl(
81 const GURL& url,
82 const DownloadCallback& download_callback) {
83 std::vector<GURL> urls;
84 urls.push_back(url);
85 StartDownload(urls, download_callback);
88 void CrxDownloader::StartDownload(const std::vector<GURL>& urls,
89 const DownloadCallback& download_callback) {
90 DCHECK(thread_checker_.CalledOnValidThread());
92 if (urls.empty()) {
93 // Make a result and complete the download with a generic error for now.
94 Result result;
95 result.error = -1;
96 download_callback.Run(result);
97 return;
100 // If the urls are mutated while this downloader is active, then the
101 // behavior is undefined in the sense that the outcome of the download could
102 // be inconsistent for the list of urls. At any rate, the |current_url_| is
103 // reset at this point, and the iterator will be valid in all conditions.
104 urls_ = urls;
105 current_url_ = urls_.begin();
106 download_callback_ = download_callback;
108 DoStartDownload(*current_url_);
111 void CrxDownloader::OnDownloadComplete(
112 bool is_handled,
113 const Result& result,
114 const DownloadMetrics& download_metrics) {
115 DCHECK(thread_checker_.CalledOnValidThread());
117 download_metrics_.push_back(download_metrics);
119 if (result.error) {
120 // If an error has occured, in general try the next url if there is any,
121 // then move on to the successor in the chain if there is any successor.
122 // If this downloader has received a 5xx error for the current url,
123 // as indicated by the |is_handled| flag, remove that url from the list of
124 // urls so the url is never retried. In both cases, move on to the
125 // next url.
126 if (!is_handled) {
127 ++current_url_;
128 } else {
129 current_url_ = urls_.erase(current_url_);
132 // Try downloading from another url from the list.
133 if (current_url_ != urls_.end()) {
134 DoStartDownload(*current_url_);
135 return;
138 // If there is another downloader that can accept this request, then hand
139 // the request over to it so that the successor can try the pruned list
140 // of urls. Otherwise, the request ends here since the current downloader
141 // has tried all urls and it can't fall back on any other downloader.
142 if (successor_ && !urls_.empty()) {
143 successor_->StartDownload(urls_, download_callback_);
144 return;
148 base::ThreadTaskRunnerHandle::Get()->PostTask(
149 FROM_HERE, base::Bind(download_callback_, result));
152 void CrxDownloader::OnDownloadProgress(const Result& result) {
153 DCHECK(thread_checker_.CalledOnValidThread());
155 if (progress_callback_.is_null())
156 return;
158 progress_callback_.Run(result);
161 } // namespace update_client