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" |
| 11 | #include "content/public/browser/browser_thread.h" |
| 12 | #include "net/base/load_flags.h" |
| 13 | #include "net/http/http_status_code.h" |
| 14 | #include "net/url_request/url_fetcher.h" |
| 15 | #include "url/gurl.h" |
| 16 | |
| 17 | using content::BrowserThread; |
| 18 | using net::URLFetcher; |
| 19 | |
| 20 | const int kNumRetries = 1; |
| 21 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 22 | FileDownloader::FileDownloader(const GURL& url, |
| 23 | const base::FilePath& path, |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 24 | bool overwrite, |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 25 | net::URLRequestContextGetter* request_context, |
| 26 | const DownloadFinishedCallback& callback) |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 27 | : callback_(callback), |
| 28 | fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)), |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 29 | local_path_(path), |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 30 | weak_ptr_factory_(this) { |
| 31 | fetcher_->SetRequestContext(request_context); |
| 32 | fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
mmenke | 3cb1d73d | 2015-04-16 22:57:19 | [diff] [blame] | 33 | net::LOAD_DO_NOT_SAVE_COOKIES); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 34 | fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 35 | fetcher_->SaveResponseToTemporaryFile( |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame^] | 36 | BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 37 | |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 38 | if (overwrite) { |
| 39 | fetcher_->Start(); |
| 40 | } else { |
| 41 | base::PostTaskAndReplyWithResult( |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 42 | BrowserThread::GetBlockingPool() |
| 43 | ->GetTaskRunnerWithShutdownBehavior( |
| 44 | base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| 45 | .get(), |
| 46 | FROM_HERE, base::Bind(&base::PathExists, local_path_), |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 47 | base::Bind(&FileDownloader::OnFileExistsCheckDone, |
| 48 | weak_ptr_factory_.GetWeakPtr())); |
| 49 | } |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 50 | } |
| 51 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 52 | FileDownloader::~FileDownloader() {} |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 53 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 54 | void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 55 | DCHECK_EQ(fetcher_.get(), source); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 56 | |
| 57 | const net::URLRequestStatus& status = source->GetStatus(); |
| 58 | if (!status.is_success()) { |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 59 | DLOG(WARNING) << "URLRequestStatus error " << status.error() |
| 60 | << " while trying to download " << source->GetURL().spec(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 61 | callback_.Run(FAILED); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | int response_code = source->GetResponseCode(); |
| 66 | if (response_code != net::HTTP_OK) { |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 67 | DLOG(WARNING) << "HTTP error " << response_code |
| 68 | << " while trying to download " << source->GetURL().spec(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 69 | callback_.Run(FAILED); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 73 | base::FilePath response_path; |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 74 | bool success = source->GetResponseAsFilePath(false, &response_path); |
| 75 | if (!success) { |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 76 | callback_.Run(FAILED); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
| 80 | base::PostTaskAndReplyWithResult( |
| 81 | BrowserThread::GetBlockingPool() |
| 82 | ->GetTaskRunnerWithShutdownBehavior( |
| 83 | base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| 84 | .get(), |
| 85 | FROM_HERE, base::Bind(&base::Move, response_path, local_path_), |
| 86 | base::Bind(&FileDownloader::OnFileMoveDone, |
| 87 | weak_ptr_factory_.GetWeakPtr())); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 88 | } |
| 89 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 90 | void FileDownloader::OnFileExistsCheckDone(bool exists) { |
| 91 | if (exists) |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 92 | callback_.Run(EXISTS); |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 93 | else |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 94 | fetcher_->Start(); |
treib | f136dfb | 2014-09-25 17:37:47 | [diff] [blame] | 95 | } |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 96 | |
| 97 | void FileDownloader::OnFileMoveDone(bool success) { |
| 98 | if (!success) { |
| 99 | DLOG(WARNING) << "Could not move file to " |
| 100 | << local_path_.LossyDisplayName(); |
| 101 | } |
| 102 | |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 103 | callback_.Run(success ? DOWNLOADED : FAILED); |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 104 | } |