| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ | 5 #ifndef CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| 6 #define CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ | 6 #define CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 class URLFetcher; | 16 class URLFetcher; |
| 17 class URLRequestContextGetter; | 17 class URLRequestContextGetter; |
| 18 } // namespace net | 18 } // namespace net |
| 19 | 19 |
| 20 class GURL; | 20 class GURL; |
| 21 | 21 |
| 22 // Helper class to download a file from a given URL and store it in a local | 22 // Helper class to download a file from a given URL and store it in a local |
| 23 // file. If |overwrite| is true, any existing file will be overwritten; | 23 // file. If |overwrite| is true, any existing file will be overwritten; |
| 24 // otherwise if the local file already exists, this will report success without | 24 // otherwise if the local file already exists, this will report success without |
| 25 // downloading anything. | 25 // downloading anything. |
| 26 class FileDownloader : public net::URLFetcherDelegate { | 26 class FileDownloader : public net::URLFetcherDelegate { |
| 27 public: | 27 public: |
| 28 typedef base::Callback<void(bool /* success */)> DownloadFinishedCallback; | 28 enum Result { |
| 29 // The file was successfully downloaded. |
| 30 DOWNLOADED, |
| 31 // A local file at the given path already existed and was kept. |
| 32 EXISTS, |
| 33 // Downloading failed. |
| 34 FAILED |
| 35 }; |
| 36 using DownloadFinishedCallback = base::Callback<void(Result)>; |
| 29 | 37 |
| 30 // Directly starts the download (if necessary) and runs |callback| when done. | 38 // Directly starts the download (if necessary) and runs |callback| when done. |
| 31 // If the instance is destroyed before it is finished, |callback| is not run. | 39 // If the instance is destroyed before it is finished, |callback| is not run. |
| 32 FileDownloader(const GURL& url, | 40 FileDownloader(const GURL& url, |
| 33 const base::FilePath& path, | 41 const base::FilePath& path, |
| 34 bool overwrite, | 42 bool overwrite, |
| 35 net::URLRequestContextGetter* request_context, | 43 net::URLRequestContextGetter* request_context, |
| 36 const DownloadFinishedCallback& callback); | 44 const DownloadFinishedCallback& callback); |
| 37 ~FileDownloader() override; | 45 ~FileDownloader() override; |
| 38 | 46 |
| 47 static bool IsSuccess(Result result) { return result != FAILED; } |
| 48 |
| 39 private: | 49 private: |
| 40 // net::URLFetcherDelegate implementation. | 50 // net::URLFetcherDelegate implementation. |
| 41 void OnURLFetchComplete(const net::URLFetcher* source) override; | 51 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 42 | 52 |
| 43 void OnFileExistsCheckDone(bool exists); | 53 void OnFileExistsCheckDone(bool exists); |
| 44 | 54 |
| 45 void OnFileMoveDone(bool success); | 55 void OnFileMoveDone(bool success); |
| 46 | 56 |
| 47 DownloadFinishedCallback callback_; | 57 DownloadFinishedCallback callback_; |
| 48 | 58 |
| 49 scoped_ptr<net::URLFetcher> fetcher_; | 59 scoped_ptr<net::URLFetcher> fetcher_; |
| 50 | 60 |
| 51 base::FilePath local_path_; | 61 base::FilePath local_path_; |
| 52 | 62 |
| 53 base::WeakPtrFactory<FileDownloader> weak_ptr_factory_; | 63 base::WeakPtrFactory<FileDownloader> weak_ptr_factory_; |
| 54 | 64 |
| 55 DISALLOW_COPY_AND_ASSIGN(FileDownloader); | 65 DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
| 56 }; | 66 }; |
| 57 | 67 |
| 58 #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ | 68 #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| OLD | NEW |