Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / component_updater / crx_downloader.cc
blob535ffbee1e7d40cf09545a753e1b11c053453001
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.
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"
9 #if defined(OS_WIN)
10 #include "chrome/browser/component_updater/background_downloader_win.h"
11 #endif
13 using content::BrowserThread;
15 namespace component_updater {
17 CrxDownloader::Result::Result() : error(0) {}
19 CrxDownloader::DownloadMetrics::DownloadMetrics()
20 : downloader(kNone),
21 error(0),
22 bytes_downloaded(-1),
23 bytes_total(-1),
24 download_time_ms(0) {}
26 // On Windows, the first downloader in the chain is a background downloader,
27 // which uses the BITS service.
28 CrxDownloader* CrxDownloader::Create(
29 bool is_background_download,
30 net::URLRequestContextGetter* context_getter,
31 scoped_refptr<base::SequencedTaskRunner> task_runner,
32 const DownloadCallback& download_callback) {
33 scoped_ptr<CrxDownloader> url_fetcher_downloader(
34 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(),
35 context_getter,
36 task_runner,
37 download_callback));
38 #if defined (OS_WIN)
39 if (is_background_download) {
40 return new BackgroundDownloader(url_fetcher_downloader.Pass(),
41 context_getter,
42 task_runner,
43 download_callback);
45 #endif
47 return url_fetcher_downloader.release();
50 CrxDownloader::CrxDownloader(
51 scoped_ptr<CrxDownloader> successor,
52 const DownloadCallback& download_callback)
53 : successor_(successor.Pass()),
54 download_callback_(download_callback) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
58 CrxDownloader::~CrxDownloader() {
61 GURL CrxDownloader::url() const {
62 return current_url_ != urls_.end() ? *current_url_ : GURL();
65 const std::vector<CrxDownloader::DownloadMetrics>
66 CrxDownloader::download_metrics() const {
67 if (!successor_)
68 return download_metrics_;
70 std::vector<DownloadMetrics> retval(successor_->download_metrics());
71 retval.insert(retval.begin(),
72 download_metrics_.begin(),
73 download_metrics_.end());
74 return retval;
77 void CrxDownloader::StartDownloadFromUrl(const GURL& url) {
78 std::vector<GURL> urls;
79 urls.push_back(url);
80 StartDownload(urls);
83 void CrxDownloader::StartDownload(const std::vector<GURL>& urls) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 if (urls.empty()) {
87 // Make a result and complete the download with a generic error for now.
88 Result result;
89 result.error = -1;
90 download_callback_.Run(result);
91 return;
94 // If the urls are mutated while this downloader is active, then the
95 // behavior is undefined in the sense that the outcome of the download could
96 // be inconsistent for the list of urls. At any rate, the |current_url_| is
97 // reset at this point, and the iterator will be valid in all conditions.
98 urls_ = urls;
99 current_url_ = urls_.begin();
101 DoStartDownload(*current_url_);
104 void CrxDownloader::OnDownloadComplete(
105 bool is_handled,
106 const Result& result,
107 const DownloadMetrics& download_metrics) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110 download_metrics_.push_back(download_metrics);
112 if (result.error) {
113 // If an error has occured, in general try the next url if there is any,
114 // then move on to the successor in the chain if there is any successor.
115 // If this downloader has received a 5xx error for the current url,
116 // as indicated by the |is_handled| flag, remove that url from the list of
117 // urls so the url is never retried. In both cases, move on to the
118 // next url.
119 if (!is_handled) {
120 ++current_url_;
121 } else {
122 current_url_ = urls_.erase(current_url_);
125 // Try downloading from another url from the list.
126 if (current_url_ != urls_.end()) {
127 DoStartDownload(*current_url_);
128 return;
131 // If there is another downloader that can accept this request, then hand
132 // the request over to it so that the successor can try the pruned list
133 // of urls. Otherwise, the request ends here since the current downloader
134 // has tried all urls and it can't fall back on any other downloader.
135 if (successor_ && !urls_.empty()) {
136 successor_->StartDownload(urls_);
137 return;
141 download_callback_.Run(result);
144 } // namespace component_updater