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;
25struct DownloadCreateInfo;
26
27class CONTENT_EXPORT DownloadFileImpl : virtual public DownloadFile {
28 public:
29  // Takes ownership of the object pointed to by |request_handle|.
30  // |bound_net_log| will be used for logging the download file's events.
31  // May be constructed on any thread.  All methods besides the constructor
32  // (including destruction) must occur on the FILE thread.
33  //
34  // Note that the DownloadFileImpl automatically reads from the passed in
35  // stream, and sends updates and status of those reads to the
36  // DownloadDestinationObserver.
37  DownloadFileImpl(
38    scoped_ptr<DownloadSaveInfo> save_info,
39    const base::FilePath& default_downloads_directory,
40    const GURL& url,
41    const GURL& referrer_url,
42    bool calculate_hash,
43    scoped_ptr<ByteStreamReader> stream,
44    const net::BoundNetLog& bound_net_log,
45    base::WeakPtr<DownloadDestinationObserver> observer);
46
47  virtual ~DownloadFileImpl();
48
49  // DownloadFile functions.
50  virtual void Initialize(const InitializeCallback& callback) OVERRIDE;
51  virtual void RenameAndUniquify(
52      const base::FilePath& full_path,
53      const RenameCompletionCallback& callback) OVERRIDE;
54  virtual void RenameAndAnnotate(
55      const base::FilePath& full_path,
56      const RenameCompletionCallback& callback) OVERRIDE;
57  virtual void Detach() OVERRIDE;
58  virtual void Cancel() OVERRIDE;
59  virtual base::FilePath FullPath() const OVERRIDE;
60  virtual bool InProgress() const OVERRIDE;
61  virtual int64 CurrentSpeed() const OVERRIDE;
62  virtual bool GetHash(std::string* hash) OVERRIDE;
63  virtual std::string GetHashState() OVERRIDE;
64  virtual void SetClientGuid(const std::string& guid) OVERRIDE;
65
66 protected:
67  // For test class overrides.
68  virtual DownloadInterruptReason AppendDataToFile(
69      const char* data, size_t data_len);
70
71  virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number);
72
73  virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason);
74
75 private:
76  friend class DownloadFileTest;
77
78  // Options for RenameWithRetryInternal.
79  enum RenameOption {
80    UNIQUIFY = 1 << 0,  // If there's already a file on disk that conflicts with
81                        // |new_path|, try to create a unique file by appending
82                        // a uniquifier.
83    ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1
84  };
85
86  // Rename file_ to |new_path|.
87  // |option| specifies additional operations to be performed during the rename.
88  //     See RenameOption above.
89  // |retries_left| indicates how many times to retry the operation if the
90  //     rename fails with a transient error.
91  // |time_of_first_failure| Set to an empty base::TimeTicks during the first
92  //     call. Once the first failure is seen, subsequent calls of
93  //     RenameWithRetryInternal will have a non-empty value keeping track of
94  //     the time of first observed failure.  Used for UMA.
95  void RenameWithRetryInternal(const base::FilePath& new_path,
96                               RenameOption option,
97                               int retries_left,
98                               base::TimeTicks time_of_first_failure,
99                               const RenameCompletionCallback& callback);
100
101  // Send an update on our progress.
102  void SendUpdate();
103
104  // Called when there's some activity on stream_reader_ that needs to be
105  // handled.
106  void StreamActive();
107
108  // The base file instance.
109  BaseFile file_;
110
111  // The default directory for creating the download file.
112  base::FilePath default_download_directory_;
113
114  // The stream through which data comes.
115  // TODO(rdsmith): Move this into BaseFile; requires using the same
116  // stream semantics in SavePackage.  Alternatively, replace SaveFile
117  // with DownloadFile and get rid of BaseFile.
118  scoped_ptr<ByteStreamReader> stream_reader_;
119
120  // Used to trigger progress updates.
121  scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_;
122
123  // Statistics
124  size_t bytes_seen_;
125  base::TimeDelta disk_writes_time_;
126  base::TimeTicks download_start_;
127  RateEstimator rate_estimator_;
128
129  net::BoundNetLog bound_net_log_;
130
131  base::WeakPtr<DownloadDestinationObserver> observer_;
132
133  base::WeakPtrFactory<DownloadFileImpl> weak_factory_;
134
135  DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
136};
137
138}  // namespace content
139
140#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
141