OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/component_updater/crx_downloader.h" | 5 #include "chrome/browser/component_updater/crx_downloader.h" |
6 #include "chrome/browser/component_updater/url_fetcher_downloader.h" | 6 #include "chrome/browser/component_updater/url_fetcher_downloader.h" |
7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include "chrome/browser/component_updater/background_downloader_win.h" | 10 #include "chrome/browser/component_updater/background_downloader_win.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 error(0), | 21 error(0), |
22 bytes_downloaded(-1), | 22 bytes_downloaded(-1), |
23 bytes_total(-1), | 23 bytes_total(-1), |
24 download_time_ms(0) {} | 24 download_time_ms(0) {} |
25 | 25 |
26 // On Windows, the first downloader in the chain is a background downloader, | 26 // On Windows, the first downloader in the chain is a background downloader, |
27 // which uses the BITS service. | 27 // which uses the BITS service. |
28 CrxDownloader* CrxDownloader::Create( | 28 CrxDownloader* CrxDownloader::Create( |
29 bool is_background_download, | 29 bool is_background_download, |
30 net::URLRequestContextGetter* context_getter, | 30 net::URLRequestContextGetter* context_getter, |
31 scoped_refptr<base::SequencedTaskRunner> task_runner, | 31 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
32 const DownloadCallback& download_callback) { | |
33 scoped_ptr<CrxDownloader> url_fetcher_downloader( | 32 scoped_ptr<CrxDownloader> url_fetcher_downloader( |
34 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), | 33 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), |
35 context_getter, | 34 context_getter, |
36 task_runner, | 35 task_runner)); |
37 download_callback)); | |
38 #if defined (OS_WIN) | 36 #if defined (OS_WIN) |
39 if (is_background_download) { | 37 if (is_background_download) { |
40 return new BackgroundDownloader(url_fetcher_downloader.Pass(), | 38 return new BackgroundDownloader(url_fetcher_downloader.Pass(), |
41 context_getter, | 39 context_getter, |
42 task_runner, | 40 task_runner); |
43 download_callback); | |
44 } | 41 } |
45 #endif | 42 #endif |
46 | 43 |
47 return url_fetcher_downloader.release(); | 44 return url_fetcher_downloader.release(); |
48 } | 45 } |
49 | 46 |
50 CrxDownloader::CrxDownloader( | 47 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor) |
51 scoped_ptr<CrxDownloader> successor, | 48 : successor_(successor.Pass()) { |
52 const DownloadCallback& download_callback) | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
53 : successor_(successor.Pass()), | |
54 download_callback_(download_callback) { | |
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
56 } | 50 } |
57 | 51 |
58 CrxDownloader::~CrxDownloader() { | 52 CrxDownloader::~CrxDownloader() { |
59 } | 53 } |
60 | 54 |
61 GURL CrxDownloader::url() const { | 55 GURL CrxDownloader::url() const { |
62 return current_url_ != urls_.end() ? *current_url_ : GURL(); | 56 return current_url_ != urls_.end() ? *current_url_ : GURL(); |
63 } | 57 } |
64 | 58 |
65 const std::vector<CrxDownloader::DownloadMetrics> | 59 const std::vector<CrxDownloader::DownloadMetrics> |
66 CrxDownloader::download_metrics() const { | 60 CrxDownloader::download_metrics() const { |
67 if (!successor_) | 61 if (!successor_) |
68 return download_metrics_; | 62 return download_metrics_; |
69 | 63 |
70 std::vector<DownloadMetrics> retval(successor_->download_metrics()); | 64 std::vector<DownloadMetrics> retval(successor_->download_metrics()); |
71 retval.insert(retval.begin(), | 65 retval.insert(retval.begin(), |
72 download_metrics_.begin(), | 66 download_metrics_.begin(), |
73 download_metrics_.end()); | 67 download_metrics_.end()); |
74 return retval; | 68 return retval; |
75 } | 69 } |
76 | 70 |
77 void CrxDownloader::StartDownloadFromUrl(const GURL& url) { | 71 void CrxDownloader::StartDownloadFromUrl( |
| 72 const GURL& url, |
| 73 const DownloadCallback& download_callback) { |
78 std::vector<GURL> urls; | 74 std::vector<GURL> urls; |
79 urls.push_back(url); | 75 urls.push_back(url); |
80 StartDownload(urls); | 76 StartDownload(urls, download_callback); |
81 } | 77 } |
82 | 78 |
83 void CrxDownloader::StartDownload(const std::vector<GURL>& urls) { | 79 void CrxDownloader::StartDownload(const std::vector<GURL>& urls, |
| 80 const DownloadCallback& download_callback) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 | 82 |
86 if (urls.empty()) { | 83 if (urls.empty()) { |
87 // Make a result and complete the download with a generic error for now. | 84 // Make a result and complete the download with a generic error for now. |
88 Result result; | 85 Result result; |
89 result.error = -1; | 86 result.error = -1; |
90 download_callback_.Run(result); | 87 download_callback.Run(result); |
91 return; | 88 return; |
92 } | 89 } |
93 | 90 |
94 // If the urls are mutated while this downloader is active, then the | 91 // 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 | 92 // 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 | 93 // 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. | 94 // reset at this point, and the iterator will be valid in all conditions. |
98 urls_ = urls; | 95 urls_ = urls; |
99 current_url_ = urls_.begin(); | 96 current_url_ = urls_.begin(); |
| 97 download_callback_ = download_callback; |
100 | 98 |
101 DoStartDownload(*current_url_); | 99 DoStartDownload(*current_url_); |
102 } | 100 } |
103 | 101 |
104 void CrxDownloader::OnDownloadComplete( | 102 void CrxDownloader::OnDownloadComplete( |
105 bool is_handled, | 103 bool is_handled, |
106 const Result& result, | 104 const Result& result, |
107 const DownloadMetrics& download_metrics) { | 105 const DownloadMetrics& download_metrics) { |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
109 | 107 |
(...skipping 16 matching lines...) Expand all Loading... |
126 if (current_url_ != urls_.end()) { | 124 if (current_url_ != urls_.end()) { |
127 DoStartDownload(*current_url_); | 125 DoStartDownload(*current_url_); |
128 return; | 126 return; |
129 } | 127 } |
130 | 128 |
131 // If there is another downloader that can accept this request, then hand | 129 // 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 | 130 // 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 | 131 // 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. | 132 // has tried all urls and it can't fall back on any other downloader. |
135 if (successor_ && !urls_.empty()) { | 133 if (successor_ && !urls_.empty()) { |
136 successor_->StartDownload(urls_); | 134 successor_->StartDownload(urls_, download_callback_); |
137 return; | 135 return; |
138 } | 136 } |
139 } | 137 } |
140 | 138 |
141 download_callback_.Run(result); | 139 download_callback_.Run(result); |
142 } | 140 } |
143 | 141 |
144 } // namespace component_updater | 142 } // namespace component_updater |
145 | 143 |
OLD | NEW |