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.
5 #ifndef CHROME_BROWSER_NET_FILE_DOWNLOADER_H_
6 #define CHROME_BROWSER_NET_FILE_DOWNLOADER_H_
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "net/url_request/url_fetcher_delegate.h"
19 class URLRequestContextGetter
;
24 // Helper class to download a file from a given URL and store it in a local
25 // file. If |overwrite| is true, any existing file will be overwritten;
26 // otherwise if the local file already exists, this will report success without
27 // downloading anything.
28 class FileDownloader
: public net::URLFetcherDelegate
{
30 typedef base::Callback
<void(bool /* success */)> DownloadFinishedCallback
;
32 // Directly starts the download (if necessary) and runs |callback| when done.
33 // If the instance is destroyed before it is finished, |callback| is not run.
34 FileDownloader(const GURL
& url
,
35 const base::FilePath
& path
,
37 net::URLRequestContextGetter
* request_context
,
38 const DownloadFinishedCallback
& callback
);
39 ~FileDownloader() override
;
42 // net::URLFetcherDelegate implementation.
43 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
45 void OnFileExistsCheckDone(bool exists
);
47 DownloadFinishedCallback callback_
;
49 scoped_ptr
<net::URLFetcher
> fetcher_
;
51 base::WeakPtrFactory
<FileDownloader
> weak_ptr_factory_
;
53 DISALLOW_COPY_AND_ASSIGN(FileDownloader
);
56 #endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_