blob: 8282a6631f21d4ae92467a8534983a022a0fc5c7 [file] [log] [blame]
treibcffa6502015-08-06 09:12:271// 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
8#include "base/callback.h"
maybellefae2b702015-10-12 10:11:589#include "base/files/file_path.h"
treibcffa6502015-08-06 09:12:2710#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "net/url_request/url_fetcher_delegate.h"
13
treibcffa6502015-08-06 09:12:2714namespace net {
15class URLFetcher;
16class URLRequestContextGetter;
17} // namespace net
18
19class GURL;
20
21// Helper class to download a file from a given URL and store it in a local
treibf79ed40d2015-09-22 12:38:0622// file. If |overwrite| is true, any existing file will be overwritten;
23// otherwise if the local file already exists, this will report success without
24// downloading anything.
treibcffa6502015-08-06 09:12:2725class FileDownloader : public net::URLFetcherDelegate {
26 public:
27 typedef base::Callback<void(bool /* success */)> DownloadFinishedCallback;
28
29 // Directly starts the download (if necessary) and runs |callback| when done.
30 // If the instance is destroyed before it is finished, |callback| is not run.
31 FileDownloader(const GURL& url,
32 const base::FilePath& path,
treibf79ed40d2015-09-22 12:38:0633 bool overwrite,
treibcffa6502015-08-06 09:12:2734 net::URLRequestContextGetter* request_context,
35 const DownloadFinishedCallback& callback);
36 ~FileDownloader() override;
37
38 private:
39 // net::URLFetcherDelegate implementation.
40 void OnURLFetchComplete(const net::URLFetcher* source) override;
41
42 void OnFileExistsCheckDone(bool exists);
43
maybellefae2b702015-10-12 10:11:5844 void OnFileMoveDone(bool success);
45
treibcffa6502015-08-06 09:12:2746 DownloadFinishedCallback callback_;
47
48 scoped_ptr<net::URLFetcher> fetcher_;
49
maybellefae2b702015-10-12 10:11:5850 base::FilePath local_path_;
51
treibcffa6502015-08-06 09:12:2752 base::WeakPtrFactory<FileDownloader> weak_ptr_factory_;
53
54 DISALLOW_COPY_AND_ASSIGN(FileDownloader);
55};
56
57#endif // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_