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"
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),
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,
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;
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
);
69 result
.error
= 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
,
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
90 downloaded_bytes_
= current
;
94 } // namespace component_updater