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/component_updater/url_fetcher_downloader.h"
7 #include "base/logging.h"
8 #include "base/sequenced_task_runner.h"
9 #include "components/component_updater/component_updater_utils.h"
10 #include "net/base/load_flags.h"
11 #include "net/url_request/url_fetcher.h"
14 namespace component_updater
{
16 UrlFetcherDownloader::UrlFetcherDownloader(
17 scoped_ptr
<CrxDownloader
> successor
,
18 net::URLRequestContextGetter
* context_getter
,
19 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
20 : CrxDownloader(successor
.Pass()),
21 context_getter_(context_getter
),
22 task_runner_(task_runner
),
23 downloaded_bytes_(-1),
27 UrlFetcherDownloader::~UrlFetcherDownloader() {
28 DCHECK(thread_checker_
.CalledOnValidThread());
31 void UrlFetcherDownloader::DoStartDownload(const GURL
& url
) {
32 DCHECK(thread_checker_
.CalledOnValidThread());
35 net::URLFetcher::Create(0, url
, net::URLFetcher::GET
, this));
36 url_fetcher_
->SetRequestContext(context_getter_
);
37 url_fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
38 net::LOAD_DO_NOT_SAVE_COOKIES
|
39 net::LOAD_DISABLE_CACHE
);
40 url_fetcher_
->SetAutomaticallyRetryOn5xx(false);
41 url_fetcher_
->SaveResponseToTemporaryFile(task_runner_
);
43 VLOG(1) << "Starting background download: " << url
.spec();
44 url_fetcher_
->Start();
46 download_start_time_
= base::Time::Now();
48 downloaded_bytes_
= -1;
52 void UrlFetcherDownloader::OnURLFetchComplete(const net::URLFetcher
* source
) {
53 DCHECK(thread_checker_
.CalledOnValidThread());
55 const base::Time
download_end_time(base::Time::Now());
56 const base::TimeDelta download_time
=
57 download_end_time
>= download_start_time_
58 ? download_end_time
- download_start_time_
61 // Consider a 5xx response from the server as an indication to terminate
62 // the request and avoid overloading the server in this case.
63 // is not accepting requests for the moment.
64 const int fetch_error(GetFetchError(*url_fetcher_
));
65 const bool is_handled
= fetch_error
== 0 || IsHttpServerError(fetch_error
);
68 result
.error
= fetch_error
;
70 source
->GetResponseAsFilePath(true, &result
.response
);
72 result
.downloaded_bytes
= downloaded_bytes_
;
73 result
.total_bytes
= total_bytes_
;
75 DownloadMetrics download_metrics
;
76 download_metrics
.url
= url();
77 download_metrics
.downloader
= DownloadMetrics::kUrlFetcher
;
78 download_metrics
.error
= fetch_error
;
79 download_metrics
.downloaded_bytes
= downloaded_bytes_
;
80 download_metrics
.total_bytes
= total_bytes_
;
81 download_metrics
.download_time_ms
= download_time
.InMilliseconds();
83 base::FilePath local_path_
;
84 source
->GetResponseAsFilePath(false, &local_path_
);
85 VLOG(1) << "Downloaded " << downloaded_bytes_
<< " bytes in "
86 << download_time
.InMilliseconds() << "ms from "
87 << source
->GetURL().spec() << " to " << local_path_
.value();
88 CrxDownloader::OnDownloadComplete(is_handled
, result
, download_metrics
);
91 void UrlFetcherDownloader::OnURLFetchDownloadProgress(
92 const net::URLFetcher
* source
,
95 DCHECK(thread_checker_
.CalledOnValidThread());
97 downloaded_bytes_
= current
;
101 result
.downloaded_bytes
= downloaded_bytes_
;
102 result
.total_bytes
= total_bytes_
;
104 OnDownloadProgress(result
);
107 } // namespace component_updater