treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 5 | #include "chrome/browser/net/file_downloader.h" |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 6 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 7 | #include "base/bind.h" |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
| 9 | #include "base/files/file_util.h" |
| 10 | #include "base/logging.h" |
Marc Treib | 6b8ccbd | 2017-06-16 15:18:49 | [diff] [blame] | 11 | #include "base/task_runner_util.h" |
fdoray | 15e8fb8c | 2017-04-27 12:10:38 | [diff] [blame] | 12 | #include "base/task_scheduler/post_task.h" |
Marc Treib | 6b8ccbd | 2017-06-16 15:18:49 | [diff] [blame] | 13 | #include "base/task_scheduler/task_traits.h" |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 14 | #include "net/base/load_flags.h" |
| 15 | #include "net/http/http_status_code.h" |
| 16 | #include "net/url_request/url_fetcher.h" |
| 17 | #include "url/gurl.h" |
| 18 | |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 19 | using net::URLFetcher; |
| 20 | |
| 21 | const int kNumRetries = 1; |
| 22 | |
rhalavati | 5f1caa2b | 2017-02-25 08:22:16 | [diff] [blame] | 23 | FileDownloader::FileDownloader( |
| 24 | const GURL& url, |
| 25 | const base::FilePath& path, |
| 26 | bool overwrite, |
| 27 | net::URLRequestContextGetter* request_context, |
| 28 | const DownloadFinishedCallback& callback, |
| 29 | const net::NetworkTrafficAnnotationTag& traffic_annotation) |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 30 | : callback_(callback), |
rhalavati | 5f1caa2b | 2017-02-25 08:22:16 | [diff] [blame] | 31 | fetcher_( |
| 32 | URLFetcher::Create(url, URLFetcher::GET, this, traffic_annotation)), |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 33 | local_path_(path), |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 34 | weak_ptr_factory_(this) { |
| 35 | fetcher_->SetRequestContext(request_context); |
| 36 | fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
mmenke | 3cb1d73d | 2015-04-16 22:57:19 | [diff] [blame] | 37 | net::LOAD_DO_NOT_SAVE_COOKIES); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 38 | fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 39 | fetcher_->SaveResponseToTemporaryFile( |
Marc Treib | 6b8ccbd | 2017-06-16 15:18:49 | [diff] [blame] | 40 | base::CreateSequencedTaskRunnerWithTraits( |
| 41 | {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 42 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 43 | |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 44 | if (overwrite) { |
| 45 | fetcher_->Start(); |
| 46 | } else { |
| 47 | base::PostTaskAndReplyWithResult( |
fdoray | 15e8fb8c | 2017-04-27 12:10:38 | [diff] [blame] | 48 | base::CreateTaskRunnerWithTraits( |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 49 | {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 50 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 51 | .get(), |
| 52 | FROM_HERE, base::Bind(&base::PathExists, local_path_), |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 53 | base::Bind(&FileDownloader::OnFileExistsCheckDone, |
| 54 | weak_ptr_factory_.GetWeakPtr())); |
| 55 | } |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 56 | } |
| 57 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 58 | FileDownloader::~FileDownloader() {} |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 59 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 60 | void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 61 | DCHECK_EQ(fetcher_.get(), source); |
| 62 | |
| 63 | const net::URLRequestStatus& status = source->GetStatus(); |
| 64 | if (!status.is_success()) { |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 65 | DLOG(WARNING) << "URLRequestStatus error " << status.error() |
| 66 | << " while trying to download " << source->GetURL().spec(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 67 | callback_.Run(FAILED); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | int response_code = source->GetResponseCode(); |
| 72 | if (response_code != net::HTTP_OK) { |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 73 | DLOG(WARNING) << "HTTP error " << response_code |
| 74 | << " while trying to download " << source->GetURL().spec(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 75 | callback_.Run(FAILED); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 79 | base::FilePath response_path; |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 80 | bool success = source->GetResponseAsFilePath(false, &response_path); |
| 81 | if (!success) { |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 82 | callback_.Run(FAILED); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | base::PostTaskAndReplyWithResult( |
fdoray | 15e8fb8c | 2017-04-27 12:10:38 | [diff] [blame] | 87 | base::CreateTaskRunnerWithTraits( |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 88 | {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 89 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 90 | .get(), |
| 91 | FROM_HERE, base::Bind(&base::Move, response_path, local_path_), |
| 92 | base::Bind(&FileDownloader::OnFileMoveDone, |
| 93 | weak_ptr_factory_.GetWeakPtr())); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 94 | } |
| 95 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 96 | void FileDownloader::OnFileExistsCheckDone(bool exists) { |
| 97 | if (exists) |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 98 | callback_.Run(EXISTS); |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 99 | else |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 100 | fetcher_->Start(); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 101 | } |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 102 | |
| 103 | void FileDownloader::OnFileMoveDone(bool success) { |
| 104 | if (!success) { |
| 105 | DLOG(WARNING) << "Could not move file to " |
| 106 | << local_path_.LossyDisplayName(); |
| 107 | } |
| 108 | |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 109 | callback_.Run(success ? DOWNLOADED : FAILED); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 110 | } |