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/url_fetcher_downloader.h" | 5 #include "chrome/browser/component_updater/url_fetcher_downloader.h" |
| 6 |
6 #include "chrome/browser/component_updater/component_updater_utils.h" | 7 #include "chrome/browser/component_updater/component_updater_utils.h" |
7 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
8 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
9 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
10 #include "url/gurl.h" | 11 #include "url/gurl.h" |
11 | 12 |
12 using content::BrowserThread; | 13 using content::BrowserThread; |
13 | 14 |
14 namespace component_updater { | 15 namespace component_updater { |
15 | 16 |
16 UrlFetcherDownloader::UrlFetcherDownloader( | 17 UrlFetcherDownloader::UrlFetcherDownloader( |
| 18 scoped_ptr<CrxDownloader> successor, |
17 net::URLRequestContextGetter* context_getter, | 19 net::URLRequestContextGetter* context_getter, |
18 scoped_refptr<base::SequencedTaskRunner> task_runner) | 20 scoped_refptr<base::SequencedTaskRunner> task_runner, |
19 : context_getter_(context_getter), | 21 const DownloadCallback& download_callback) |
| 22 : CrxDownloader(successor.Pass(), download_callback), |
| 23 context_getter_(context_getter), |
20 task_runner_(task_runner) { | 24 task_runner_(task_runner) { |
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
22 } | 26 } |
23 | 27 |
24 UrlFetcherDownloader::~UrlFetcherDownloader() {} | 28 UrlFetcherDownloader::~UrlFetcherDownloader() {} |
25 | 29 |
26 void UrlFetcherDownloader::DoStartDownload(const GURL& url) { | 30 void UrlFetcherDownloader::DoStartDownload(const GURL& url) { |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
28 | 32 |
29 url_fetcher_.reset(net::URLFetcher::Create(0, | 33 url_fetcher_.reset(net::URLFetcher::Create(0, |
30 url, | 34 url, |
31 net::URLFetcher::GET, | 35 net::URLFetcher::GET, |
32 this)); | 36 this)); |
33 url_fetcher_->SetRequestContext(context_getter_); | 37 url_fetcher_->SetRequestContext(context_getter_); |
34 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 38 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
35 net::LOAD_DO_NOT_SAVE_COOKIES | | 39 net::LOAD_DO_NOT_SAVE_COOKIES | |
36 net::LOAD_DISABLE_CACHE); | 40 net::LOAD_DISABLE_CACHE); |
37 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 41 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
38 url_fetcher_->SaveResponseToTemporaryFile(task_runner_); | 42 url_fetcher_->SaveResponseToTemporaryFile(task_runner_); |
39 | 43 |
40 url_fetcher_->Start(); | 44 url_fetcher_->Start(); |
41 } | 45 } |
42 | 46 |
43 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher* source) { | 47 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
45 | 49 |
| 50 // Consider a 5xx response from the server as an indication to terminate |
| 51 // the request and avoid overloading the server in this case. |
| 52 // is not accepting requests for the moment. |
46 const int fetch_error(GetFetchError(*url_fetcher_)); | 53 const int fetch_error(GetFetchError(*url_fetcher_)); |
47 base::FilePath response; | 54 const bool is_handled = fetch_error == 0 || IsHttpServerError(fetch_error); |
| 55 |
| 56 Result result; |
| 57 result.error = fetch_error; |
| 58 result.is_background_download = false; |
48 if (!fetch_error) { | 59 if (!fetch_error) { |
49 source->GetResponseAsFilePath(true, &response); | 60 source->GetResponseAsFilePath(true, &result.response); |
50 } | 61 } |
51 | 62 |
52 // Consider a "503 Service Unavailable" response from the server as an | 63 CrxDownloader::OnDownloadComplete(is_handled, result); |
53 // indication to terminate the request and avoid overloading a server which | |
54 // is not accepting requests for the moment. | |
55 const bool is_handled = fetch_error == 0 || fetch_error == 503; | |
56 OnDownloadComplete(is_handled, fetch_error, response); | |
57 } | 64 } |
58 | 65 |
59 } // namespace component_updater | 66 } // namespace component_updater |
60 | 67 |
OLD | NEW |