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"
10 #include "chrome/browser/component_updater/background_downloader_win.h"
13 using content::BrowserThread
;
15 namespace component_updater
{
17 CrxDownloader::Result::Result() : error(0) {}
19 CrxDownloader::DownloadMetrics::DownloadMetrics()
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(),
39 if (is_background_download
) {
40 return new BackgroundDownloader(url_fetcher_downloader
.Pass(),
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 {
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());
77 void CrxDownloader::StartDownloadFromUrl(const GURL
& url
) {
78 std::vector
<GURL
> urls
;
83 void CrxDownloader::StartDownload(const std::vector
<GURL
>& urls
) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
87 // Make a result and complete the download with a generic error for now.
90 download_callback_
.Run(result
);
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.
99 current_url_
= urls_
.begin();
101 DoStartDownload(*current_url_
);
104 void CrxDownloader::OnDownloadComplete(
106 const Result
& result
,
107 const DownloadMetrics
& download_metrics
) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
110 download_metrics_
.push_back(download_metrics
);
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
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_
);
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_
);
141 download_callback_
.Run(result
);
144 } // namespace component_updater