1 // Copyright 2014 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 "components/update_client/url_fetcher_downloader.h"
9 #include "base/logging.h"
10 #include "base/sequenced_task_runner.h"
11 #include "components/update_client/utils.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_fetcher.h"
16 namespace update_client
{
18 UrlFetcherDownloader::UrlFetcherDownloader(
19 scoped_ptr
<CrxDownloader
> successor
,
20 net::URLRequestContextGetter
* context_getter
,
21 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
22 : CrxDownloader(successor
.Pass()),
23 context_getter_(context_getter
),
24 task_runner_(task_runner
),
25 downloaded_bytes_(-1),
29 UrlFetcherDownloader::~UrlFetcherDownloader() {
30 DCHECK(thread_checker_
.CalledOnValidThread());
33 void UrlFetcherDownloader::DoStartDownload(const GURL
& url
) {
34 DCHECK(thread_checker_
.CalledOnValidThread());
36 url_fetcher_
= net::URLFetcher::Create(0, url
, net::URLFetcher::GET
, this);
37 url_fetcher_
->SetRequestContext(context_getter_
);
38 url_fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
39 net::LOAD_DO_NOT_SAVE_COOKIES
|
40 net::LOAD_DISABLE_CACHE
);
41 url_fetcher_
->SetAutomaticallyRetryOn5xx(false);
42 url_fetcher_
->SaveResponseToTemporaryFile(task_runner_
);
44 VLOG(1) << "Starting background download: " << url
.spec();
45 url_fetcher_
->Start();
47 download_start_time_
= base::Time::Now();
49 downloaded_bytes_
= -1;
53 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher
* source
) {
54 DCHECK(thread_checker_
.CalledOnValidThread());
56 const base::Time
download_end_time(base::Time::Now());
57 const base::TimeDelta download_time
=
58 download_end_time
>= download_start_time_
59 ? download_end_time
- download_start_time_
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
);
73 result
.downloaded_bytes
= downloaded_bytes_
;
74 result
.total_bytes
= total_bytes_
;
76 DownloadMetrics download_metrics
;
77 download_metrics
.url
= url();
78 download_metrics
.downloader
= DownloadMetrics::kUrlFetcher
;
79 download_metrics
.error
= fetch_error
;
80 download_metrics
.downloaded_bytes
= downloaded_bytes_
;
81 download_metrics
.total_bytes
= total_bytes_
;
82 download_metrics
.download_time_ms
= download_time
.InMilliseconds();
84 base::FilePath local_path_
;
85 source
->GetResponseAsFilePath(false, &local_path_
);
86 VLOG(1) << "Downloaded " << downloaded_bytes_
<< " bytes in "
87 << download_time
.InMilliseconds() << "ms from "
88 << source
->GetURL().spec() << " to " << local_path_
.value();
89 CrxDownloader::OnDownloadComplete(is_handled
, result
, download_metrics
);
92 void UrlFetcherDownloader::OnURLFetchDownloadProgress(
93 const net::URLFetcher
* source
,
96 DCHECK(thread_checker_
.CalledOnValidThread());
98 downloaded_bytes_
= current
;
102 result
.downloaded_bytes
= downloaded_bytes_
;
103 result
.total_bytes
= total_bytes_
;
105 OnDownloadProgress(result
);
108 } // namespace update_client