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 "base/sequenced_task_runner.h" | |
6 #include "base/single_thread_task_runner.h" | |
5 #include "chrome/browser/component_updater/crx_downloader.h" | 7 #include "chrome/browser/component_updater/crx_downloader.h" |
Sorin Jianu
2014/07/21 21:42:14
crx_downloader.h must be the first header to inclu
tommycli
2014/07/21 22:00:07
Done.
| |
6 #include "chrome/browser/component_updater/url_fetcher_downloader.h" | 8 #include "chrome/browser/component_updater/url_fetcher_downloader.h" |
7 #include "content/public/browser/browser_thread.h" | |
8 | 9 |
9 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
10 #include "chrome/browser/component_updater/background_downloader_win.h" | 11 #include "chrome/browser/component_updater/background_downloader_win.h" |
11 #endif | 12 #endif |
12 | 13 |
13 using content::BrowserThread; | |
14 | |
15 namespace component_updater { | 14 namespace component_updater { |
16 | 15 |
17 CrxDownloader::Result::Result() | 16 CrxDownloader::Result::Result() |
18 : error(0), downloaded_bytes(-1), total_bytes(-1) { | 17 : error(0), downloaded_bytes(-1), total_bytes(-1) { |
19 } | 18 } |
20 | 19 |
21 CrxDownloader::DownloadMetrics::DownloadMetrics() | 20 CrxDownloader::DownloadMetrics::DownloadMetrics() |
22 : downloader(kNone), | 21 : downloader(kNone), |
23 error(0), | 22 error(0), |
24 downloaded_bytes(-1), | 23 downloaded_bytes(-1), |
25 total_bytes(-1), | 24 total_bytes(-1), |
26 download_time_ms(0) { | 25 download_time_ms(0) { |
27 } | 26 } |
28 | 27 |
29 // On Windows, the first downloader in the chain is a background downloader, | 28 // On Windows, the first downloader in the chain is a background downloader, |
30 // which uses the BITS service. | 29 // which uses the BITS service. |
31 CrxDownloader* CrxDownloader::Create( | 30 CrxDownloader* CrxDownloader::Create( |
32 bool is_background_download, | 31 bool is_background_download, |
33 net::URLRequestContextGetter* context_getter, | 32 net::URLRequestContextGetter* context_getter, |
34 scoped_refptr<base::SequencedTaskRunner> task_runner) { | 33 scoped_refptr<base::SequencedTaskRunner> url_fetcher_task_runner, |
34 scoped_refptr<base::SingleThreadTaskRunner> background_task_runner) { | |
35 scoped_ptr<CrxDownloader> url_fetcher_downloader( | 35 scoped_ptr<CrxDownloader> url_fetcher_downloader( |
36 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), | 36 new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), |
37 context_getter, | 37 context_getter, |
38 task_runner)); | 38 url_fetcher_task_runner)); |
39 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
40 if (is_background_download) { | 40 if (is_background_download) { |
41 return new BackgroundDownloader(url_fetcher_downloader.Pass(), | 41 return new BackgroundDownloader(url_fetcher_downloader.Pass(), |
42 context_getter, | 42 context_getter, |
43 task_runner); | 43 background_task_runner); |
44 } | 44 } |
45 #endif | 45 #endif |
46 | 46 |
47 return url_fetcher_downloader.release(); | 47 return url_fetcher_downloader.release(); |
48 } | 48 } |
49 | 49 |
50 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor) | 50 CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor) |
51 : successor_(successor.Pass()) { | 51 : successor_(successor.Pass()) { |
Sorin Jianu
2014/07/21 21:42:13
Maybe keep the thread checker assert.
tommycli
2014/07/21 22:00:07
Would always pass.
| |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
53 } | 52 } |
54 | 53 |
55 CrxDownloader::~CrxDownloader() { | 54 CrxDownloader::~CrxDownloader() { |
56 } | 55 } |
57 | 56 |
58 void CrxDownloader::set_progress_callback( | 57 void CrxDownloader::set_progress_callback( |
59 const ProgressCallback& progress_callback) { | 58 const ProgressCallback& progress_callback) { |
60 progress_callback_ = progress_callback; | 59 progress_callback_ = progress_callback; |
61 } | 60 } |
62 | 61 |
(...skipping 15 matching lines...) Expand all Loading... | |
78 void CrxDownloader::StartDownloadFromUrl( | 77 void CrxDownloader::StartDownloadFromUrl( |
79 const GURL& url, | 78 const GURL& url, |
80 const DownloadCallback& download_callback) { | 79 const DownloadCallback& download_callback) { |
81 std::vector<GURL> urls; | 80 std::vector<GURL> urls; |
82 urls.push_back(url); | 81 urls.push_back(url); |
83 StartDownload(urls, download_callback); | 82 StartDownload(urls, download_callback); |
84 } | 83 } |
85 | 84 |
86 void CrxDownloader::StartDownload(const std::vector<GURL>& urls, | 85 void CrxDownloader::StartDownload(const std::vector<GURL>& urls, |
87 const DownloadCallback& download_callback) { | 86 const DownloadCallback& download_callback) { |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 87 DCHECK(thread_checker_.CalledOnValidThread()); |
89 | 88 |
90 if (urls.empty()) { | 89 if (urls.empty()) { |
91 // Make a result and complete the download with a generic error for now. | 90 // Make a result and complete the download with a generic error for now. |
92 Result result; | 91 Result result; |
93 result.error = -1; | 92 result.error = -1; |
94 download_callback.Run(result); | 93 download_callback.Run(result); |
95 return; | 94 return; |
96 } | 95 } |
97 | 96 |
98 // If the urls are mutated while this downloader is active, then the | 97 // 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 | 98 // 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 | 99 // 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. | 100 // reset at this point, and the iterator will be valid in all conditions. |
102 urls_ = urls; | 101 urls_ = urls; |
103 current_url_ = urls_.begin(); | 102 current_url_ = urls_.begin(); |
104 download_callback_ = download_callback; | 103 download_callback_ = download_callback; |
105 | 104 |
106 DoStartDownload(*current_url_); | 105 DoStartDownload(*current_url_); |
107 } | 106 } |
108 | 107 |
109 void CrxDownloader::OnDownloadComplete( | 108 void CrxDownloader::OnDownloadComplete( |
110 bool is_handled, | 109 bool is_handled, |
111 const Result& result, | 110 const Result& result, |
112 const DownloadMetrics& download_metrics) { | 111 const DownloadMetrics& download_metrics) { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
114 | 113 |
115 download_metrics_.push_back(download_metrics); | 114 download_metrics_.push_back(download_metrics); |
116 | 115 |
117 if (result.error) { | 116 if (result.error) { |
118 // If an error has occured, in general try the next url if there is any, | 117 // 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. | 118 // 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, | 119 // 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 | 120 // 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 | 121 // urls so the url is never retried. In both cases, move on to the |
123 // next url. | 122 // next url. |
(...skipping 16 matching lines...) Expand all Loading... | |
140 if (successor_ && !urls_.empty()) { | 139 if (successor_ && !urls_.empty()) { |
141 successor_->StartDownload(urls_, download_callback_); | 140 successor_->StartDownload(urls_, download_callback_); |
142 return; | 141 return; |
143 } | 142 } |
144 } | 143 } |
145 | 144 |
146 download_callback_.Run(result); | 145 download_callback_.Run(result); |
147 } | 146 } |
148 | 147 |
149 void CrxDownloader::OnDownloadProgress(const Result& result) { | 148 void CrxDownloader::OnDownloadProgress(const Result& result) { |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
151 | 150 |
152 if (progress_callback_.is_null()) | 151 if (progress_callback_.is_null()) |
153 return; | 152 return; |
154 | 153 |
155 progress_callback_.Run(result); | 154 progress_callback_.Run(result); |
156 } | 155 } |
157 | 156 |
158 } // namespace component_updater | 157 } // namespace component_updater |
OLD | NEW |