Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / component_updater / url_fetcher_downloader.cc
blob4d2f0e2b490f63d3345a01046f05505979b2bb79
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/url_fetcher_downloader.h"
7 #include "chrome/browser/component_updater/component_updater_utils.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "net/base/load_flags.h"
10 #include "net/url_request/url_fetcher.h"
11 #include "url/gurl.h"
13 using content::BrowserThread;
15 namespace component_updater {
17 UrlFetcherDownloader::UrlFetcherDownloader(
18 scoped_ptr<CrxDownloader> successor,
19 net::URLRequestContextGetter* context_getter,
20 scoped_refptr<base::SequencedTaskRunner> task_runner,
21 const DownloadCallback& download_callback)
22 : CrxDownloader(successor.Pass(), download_callback),
23 context_getter_(context_getter),
24 task_runner_(task_runner),
25 downloaded_bytes_(-1),
26 total_bytes_(-1) {
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30 UrlFetcherDownloader::~UrlFetcherDownloader() {}
32 void UrlFetcherDownloader::DoStartDownload(const GURL& url) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 url_fetcher_.reset(net::URLFetcher::Create(0,
36 url,
37 net::URLFetcher::GET,
38 this));
39 url_fetcher_->SetRequestContext(context_getter_);
40 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
41 net::LOAD_DO_NOT_SAVE_COOKIES |
42 net::LOAD_DISABLE_CACHE);
43 url_fetcher_->SetAutomaticallyRetryOn5xx(false);
44 url_fetcher_->SaveResponseToTemporaryFile(task_runner_);
46 url_fetcher_->Start();
48 download_start_time_ = base::Time::Now();
50 downloaded_bytes_ = -1;
51 total_bytes_ = -1;
54 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher* source) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57 const base::Time download_end_time(base::Time::Now());
58 const base::TimeDelta download_time =
59 download_end_time >= download_start_time_ ?
60 download_end_time - download_start_time_ : base::TimeDelta();
62 // Consider a 5xx response from the server as an indication to terminate
63 // the request and avoid overloading the server in this case.
64 // is not accepting requests for the moment.
65 const int fetch_error(GetFetchError(*url_fetcher_));
66 const bool is_handled = fetch_error == 0 || IsHttpServerError(fetch_error);
68 Result result;
69 result.error = fetch_error;
70 if (!fetch_error) {
71 source->GetResponseAsFilePath(true, &result.response);
74 DownloadMetrics download_metrics;
75 download_metrics.url = url();
76 download_metrics.downloader = DownloadMetrics::kUrlFetcher;
77 download_metrics.error = fetch_error;
78 download_metrics.bytes_downloaded = downloaded_bytes_;
79 download_metrics.bytes_total = total_bytes_;
80 download_metrics.download_time_ms = download_time.InMilliseconds();
82 CrxDownloader::OnDownloadComplete(is_handled, result, download_metrics);
85 void UrlFetcherDownloader::OnURLFetchDownloadProgress(
86 const net::URLFetcher* source,
87 int64 current,
88 int64 total) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90 downloaded_bytes_ = current;
91 total_bytes_ = total;
94 } // namespace component_updater