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