treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 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 | #ifndef CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| 6 | #define CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| 7 | |
dcheng | 4e7c042 | 2016-04-14 00:59:05 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 10 | #include "base/callback.h" |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 12 | #include "base/macros.h" |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "net/url_request/url_fetcher_delegate.h" |
| 15 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 16 | namespace net { |
| 17 | class URLFetcher; |
| 18 | class URLRequestContextGetter; |
| 19 | } // namespace net |
| 20 | |
| 21 | class GURL; |
| 22 | |
| 23 | // Helper class to download a file from a given URL and store it in a local |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 24 | // file. If |overwrite| is true, any existing file will be overwritten; |
| 25 | // otherwise if the local file already exists, this will report success without |
| 26 | // downloading anything. |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 27 | class FileDownloader : public net::URLFetcherDelegate { |
| 28 | public: |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 29 | enum Result { |
| 30 | // The file was successfully downloaded. |
| 31 | DOWNLOADED, |
| 32 | // A local file at the given path already existed and was kept. |
| 33 | EXISTS, |
| 34 | // Downloading failed. |
| 35 | FAILED |
| 36 | }; |
| 37 | using DownloadFinishedCallback = base::Callback<void(Result)>; |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 38 | |
| 39 | // Directly starts the download (if necessary) and runs |callback| when done. |
| 40 | // If the instance is destroyed before it is finished, |callback| is not run. |
| 41 | FileDownloader(const GURL& url, |
| 42 | const base::FilePath& path, |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 43 | bool overwrite, |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 44 | net::URLRequestContextGetter* request_context, |
| 45 | const DownloadFinishedCallback& callback); |
| 46 | ~FileDownloader() override; |
| 47 | |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 48 | static bool IsSuccess(Result result) { return result != FAILED; } |
| 49 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 50 | private: |
| 51 | // net::URLFetcherDelegate implementation. |
| 52 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 53 | |
| 54 | void OnFileExistsCheckDone(bool exists); |
| 55 | |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 56 | void OnFileMoveDone(bool success); |
| 57 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 58 | DownloadFinishedCallback callback_; |
| 59 | |
dcheng | 4e7c042 | 2016-04-14 00:59:05 | [diff] [blame] | 60 | std::unique_ptr<net::URLFetcher> fetcher_; |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 61 | |
maybelle | fae2b70 | 2015-10-12 10:11:58 | [diff] [blame] | 62 | base::FilePath local_path_; |
| 63 | |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 64 | base::WeakPtrFactory<FileDownloader> weak_ptr_factory_; |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
| 67 | }; |
| 68 | |
| 69 | #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |