Chromium Code Reviews| 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 { DOWNLOADED, EXISTS, FAILED }; |
|
Bernhard Bauer
2016/04/01 15:49:47
Add comments what these mean?
Marc Treib
2016/04/01 16:33:28
Done.
| |
| 29 using DownloadFinishedCallback = base::Callback<void(Result)>; | |
| 29 | 30 |
| 30 // Directly starts the download (if necessary) and runs |callback| when done. | 31 // 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. | 32 // If the instance is destroyed before it is finished, |callback| is not run. |
| 32 FileDownloader(const GURL& url, | 33 FileDownloader(const GURL& url, |
| 33 const base::FilePath& path, | 34 const base::FilePath& path, |
| 34 bool overwrite, | 35 bool overwrite, |
| 35 net::URLRequestContextGetter* request_context, | 36 net::URLRequestContextGetter* request_context, |
| 36 const DownloadFinishedCallback& callback); | 37 const DownloadFinishedCallback& callback); |
| 37 ~FileDownloader() override; | 38 ~FileDownloader() override; |
| 38 | 39 |
| 40 static bool IsSuccess(Result result) { return result != FAILED; } | |
| 41 | |
| 39 private: | 42 private: |
| 40 // net::URLFetcherDelegate implementation. | 43 // net::URLFetcherDelegate implementation. |
| 41 void OnURLFetchComplete(const net::URLFetcher* source) override; | 44 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 42 | 45 |
| 43 void OnFileExistsCheckDone(bool exists); | 46 void OnFileExistsCheckDone(bool exists); |
| 44 | 47 |
| 45 void OnFileMoveDone(bool success); | 48 void OnFileMoveDone(bool success); |
| 46 | 49 |
| 47 DownloadFinishedCallback callback_; | 50 DownloadFinishedCallback callback_; |
| 48 | 51 |
| 49 scoped_ptr<net::URLFetcher> fetcher_; | 52 scoped_ptr<net::URLFetcher> fetcher_; |
| 50 | 53 |
| 51 base::FilePath local_path_; | 54 base::FilePath local_path_; |
| 52 | 55 |
| 53 base::WeakPtrFactory<FileDownloader> weak_ptr_factory_; | 56 base::WeakPtrFactory<FileDownloader> weak_ptr_factory_; |
| 54 | 57 |
| 55 DISALLOW_COPY_AND_ASSIGN(FileDownloader); | 58 DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ | 61 #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_ |
| OLD | NEW |