1// Copyright (c) 2012 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 CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
6#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
7
8#include "content/browser/download/download_file.h"
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "base/time/time.h"
14#include "base/timer/timer.h"
15#include "content/browser/byte_stream.h"
16#include "content/browser/download/base_file.h"
17#include "content/browser/download/rate_estimator.h"
18#include "content/public/browser/download_save_info.h"
19#include "net/base/net_log.h"
20
21namespace content {
22class ByteStreamReader;
23class DownloadDestinationObserver;
24class DownloadManager;
25class PowerSaveBlocker;
26struct DownloadCreateInfo;
27
28class CONTENT_EXPORT DownloadFileImpl : virtual public DownloadFile {
29 public:
30  // Takes ownership of the object pointed to by |request_handle|.
31  // |bound_net_log| will be used for logging the download file's events.
32  // May be constructed on any thread.  All methods besides the constructor
33  // (including destruction) must occur on the FILE thread.
34  //
35  // Note that the DownloadFileImpl automatically reads from the passed in
36  // stream, and sends updates and status of those reads to the
37  // DownloadDestinationObserver.
38  DownloadFileImpl(
39    scoped_ptr<DownloadSaveInfo> save_info,
40    const base::FilePath& default_downloads_directory,
41    const GURL& url,
42    const GURL& referrer_url,
43    bool calculate_hash,
44    scoped_ptr<ByteStreamReader> stream,
45    const net::BoundNetLog& bound_net_log,
46    scoped_ptr<PowerSaveBlocker> power_save_blocker,
47    base::WeakPtr<DownloadDestinationObserver> observer);
48
49  virtual ~DownloadFileImpl();
50
51  // DownloadFile functions.
52  virtual void Initialize(const InitializeCallback& callback) OVERRIDE;
53  virtual void RenameAndUniquify(
54      const base::FilePath& full_path,
55      const RenameCompletionCallback& callback) OVERRIDE;
56  virtual void RenameAndAnnotate(
57      const base::FilePath& full_path,
58      const RenameCompletionCallback& callback) OVERRIDE;
59  virtual void Detach() OVERRIDE;
60  virtual void Cancel() OVERRIDE;
61  virtual base::FilePath FullPath() const OVERRIDE;
62  virtual bool InProgress() const OVERRIDE;
63  virtual int64 CurrentSpeed() const OVERRIDE;
64  virtual bool GetHash(std::string* hash) OVERRIDE;
65  virtual std::string GetHashState() OVERRIDE;
66  virtual void SetClientGuid(const std::string& guid) OVERRIDE;
67
68 protected:
69  // For test class overrides.
70  virtual DownloadInterruptReason AppendDataToFile(
71      const char* data, size_t data_len);
72
73 private:
74  // Send an update on our progress.
75  void SendUpdate();
76
77  // Called when there's some activity on stream_reader_ that needs to be
78  // handled.
79  void StreamActive();
80
81  // The base file instance.
82  BaseFile file_;
83
84  // The default directory for creating the download file.
85  base::FilePath default_download_directory_;
86
87  // The stream through which data comes.
88  // TODO(rdsmith): Move this into BaseFile; requires using the same
89  // stream semantics in SavePackage.  Alternatively, replace SaveFile
90  // with DownloadFile and get rid of BaseFile.
91  scoped_ptr<ByteStreamReader> stream_reader_;
92
93  // Used to trigger progress updates.
94  scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_;
95
96  // Statistics
97  size_t bytes_seen_;
98  base::TimeDelta disk_writes_time_;
99  base::TimeTicks download_start_;
100  RateEstimator rate_estimator_;
101
102  net::BoundNetLog bound_net_log_;
103
104  base::WeakPtr<DownloadDestinationObserver> observer_;
105
106  base::WeakPtrFactory<DownloadFileImpl> weak_factory_;
107
108  // RAII handle to keep the system from sleeping while we're downloading.
109  scoped_ptr<PowerSaveBlocker> power_save_blocker_;
110
111  DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
112};
113
114}  // namespace content
115
116#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
117