1 // Copyright 2015 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.
4 
5 #include "chrome/browser/net/file_downloader.h"
6 
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/task/post_task.h"
12 #include "base/task/task_traits.h"
13 #include "base/task/thread_pool.h"
14 #include "base/task_runner_util.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "net/base/load_flags.h"
19 #include "net/http/http_status_code.h"
20 #include "services/network/public/cpp/resource_request.h"
21 #include "services/network/public/cpp/shared_url_loader_factory.h"
22 #include "services/network/public/cpp/simple_url_loader.h"
23 #include "url/gurl.h"
24 
25 const int kNumFileDownloaderRetries = 1;
26 
FileDownloader(const GURL & url,const base::FilePath & path,bool overwrite,scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,DownloadFinishedCallback callback,const net::NetworkTrafficAnnotationTag & traffic_annotation)27 FileDownloader::FileDownloader(
28     const GURL& url,
29     const base::FilePath& path,
30     bool overwrite,
31     scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
32     DownloadFinishedCallback callback,
33     const net::NetworkTrafficAnnotationTag& traffic_annotation)
34     : url_loader_factory_(url_loader_factory),
35       callback_(std::move(callback)),
36       local_path_(path) {
37   auto resource_request = std::make_unique<network::ResourceRequest>();
38   resource_request->url = url;
39   resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
40   simple_url_loader_ = network::SimpleURLLoader::Create(
41       std::move(resource_request), traffic_annotation);
42   simple_url_loader_->SetRetryOptions(
43       kNumFileDownloaderRetries,
44       network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);
45   if (overwrite) {
46     simple_url_loader_->DownloadToTempFile(
47         url_loader_factory_.get(),
48         base::BindOnce(&FileDownloader::OnSimpleDownloadComplete,
49                        base::Unretained(this)));
50   } else {
51     base::PostTaskAndReplyWithResult(
52         base::ThreadPool::CreateTaskRunner(
53             {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
54              base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})
55             .get(),
56         FROM_HERE, base::BindOnce(&base::PathExists, local_path_),
57         base::BindOnce(&FileDownloader::OnFileExistsCheckDone,
58                        weak_ptr_factory_.GetWeakPtr()));
59   }
60 }
61 
~FileDownloader()62 FileDownloader::~FileDownloader() {}
63 
OnSimpleDownloadComplete(base::FilePath response_path)64 void FileDownloader::OnSimpleDownloadComplete(base::FilePath response_path) {
65   if (response_path.empty()) {
66     if (simple_url_loader_->ResponseInfo() &&
67         simple_url_loader_->ResponseInfo()->headers) {
68       int response_code =
69           simple_url_loader_->ResponseInfo()->headers->response_code();
70       DLOG(WARNING) << "HTTP error " << response_code
71                     << " while trying to download "
72                     << simple_url_loader_->GetFinalURL().spec();
73     }
74     std::move(callback_).Run(FAILED);
75     return;
76   }
77 
78   base::PostTaskAndReplyWithResult(
79       base::ThreadPool::CreateTaskRunner(
80           {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
81            base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})
82           .get(),
83       FROM_HERE, base::BindOnce(&base::Move, response_path, local_path_),
84       base::BindOnce(&FileDownloader::OnFileMoveDone,
85                      weak_ptr_factory_.GetWeakPtr()));
86 }
87 
OnFileExistsCheckDone(bool exists)88 void FileDownloader::OnFileExistsCheckDone(bool exists) {
89   if (exists) {
90     std::move(callback_).Run(EXISTS);
91   } else {
92     simple_url_loader_->DownloadToTempFile(
93         url_loader_factory_.get(),
94         base::BindOnce(&FileDownloader::OnSimpleDownloadComplete,
95                        base::Unretained(this)));
96   }
97 }
98 
OnFileMoveDone(bool success)99 void FileDownloader::OnFileMoveDone(bool success) {
100   if (!success) {
101     DLOG(WARNING) << "Could not move file to "
102                   << local_path_.LossyDisplayName();
103   }
104 
105   std::move(callback_).Run(success ? DOWNLOADED : FAILED);
106 }
107