summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/file_downloader.h
blob: d6383188464f3af04f6a66d801c3785c61c02636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_NET_FILE_DOWNLOADER_H_
#define CHROME_BROWSER_NET_FILE_DOWNLOADER_H_

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"

namespace net {
class URLFetcher;
class URLRequestContextGetter;
}  // namespace net

class GURL;

// Helper class to download a file from a given URL and store it in a local
// file. If |overwrite| is true, any existing file will be overwritten;
// otherwise if the local file already exists, this will report success without
// downloading anything.
class FileDownloader : public net::URLFetcherDelegate {
 public:
  typedef base::Callback<void(bool /* success */)> DownloadFinishedCallback;

  // Directly starts the download (if necessary) and runs |callback| when done.
  // If the instance is destroyed before it is finished, |callback| is not run.
  FileDownloader(const GURL& url,
                 const base::FilePath& path,
                 bool overwrite,
                 net::URLRequestContextGetter* request_context,
                 const DownloadFinishedCallback& callback);
  ~FileDownloader() override;

 private:
  // net::URLFetcherDelegate implementation.
  void OnURLFetchComplete(const net::URLFetcher* source) override;

  void OnFileExistsCheckDone(bool exists);

  void OnFileMoveDone(bool success);

  DownloadFinishedCallback callback_;

  scoped_ptr<net::URLFetcher> fetcher_;

  base::FilePath local_path_;

  base::WeakPtrFactory<FileDownloader> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(FileDownloader);
};

#endif  // CHROME_BROWSER_NET_FILE_DOWNLOADER_H_