[Chromoting] Run jscompile over JS for HTML files (GN-only)
[chromium-blink-merge.git] / content / browser / download / download_file_impl.h
blob8d192c2e3a98fa38a6ac934e1f971ade8ef5350c
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.
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
8 #include "content/browser/download/download_file.h"
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/log/net_log.h"
21 namespace content {
22 class ByteStreamReader;
23 class DownloadDestinationObserver;
24 class DownloadManager;
25 struct DownloadCreateInfo;
27 class 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.
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);
47 ~DownloadFileImpl() override;
49 // DownloadFile functions.
50 void Initialize(const InitializeCallback& callback) override;
51 void RenameAndUniquify(const base::FilePath& full_path,
52 const RenameCompletionCallback& callback) override;
53 void RenameAndAnnotate(const base::FilePath& full_path,
54 const RenameCompletionCallback& callback) override;
55 void Detach() override;
56 void Cancel() override;
57 base::FilePath FullPath() const override;
58 bool InProgress() const override;
59 int64 CurrentSpeed() const override;
60 bool GetHash(std::string* hash) override;
61 std::string GetHashState() override;
62 void SetClientGuid(const std::string& guid) override;
64 protected:
65 // For test class overrides.
66 virtual DownloadInterruptReason AppendDataToFile(
67 const char* data, size_t data_len);
69 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number);
71 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason);
73 private:
74 friend class DownloadFileTest;
76 // Options for RenameWithRetryInternal.
77 enum RenameOption {
78 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with
79 // |new_path|, try to create a unique file by appending
80 // a uniquifier.
81 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1
84 // Rename file_ to |new_path|.
85 // |option| specifies additional operations to be performed during the rename.
86 // See RenameOption above.
87 // |retries_left| indicates how many times to retry the operation if the
88 // rename fails with a transient error.
89 // |time_of_first_failure| Set to an empty base::TimeTicks during the first
90 // call. Once the first failure is seen, subsequent calls of
91 // RenameWithRetryInternal will have a non-empty value keeping track of
92 // the time of first observed failure. Used for UMA.
93 void RenameWithRetryInternal(const base::FilePath& new_path,
94 RenameOption option,
95 int retries_left,
96 base::TimeTicks time_of_first_failure,
97 const RenameCompletionCallback& callback);
99 // Send an update on our progress.
100 void SendUpdate();
102 // Called when there's some activity on stream_reader_ that needs to be
103 // handled.
104 void StreamActive();
106 // The base file instance.
107 BaseFile file_;
109 // The default directory for creating the download file.
110 base::FilePath default_download_directory_;
112 // The stream through which data comes.
113 // TODO(rdsmith): Move this into BaseFile; requires using the same
114 // stream semantics in SavePackage. Alternatively, replace SaveFile
115 // with DownloadFile and get rid of BaseFile.
116 scoped_ptr<ByteStreamReader> stream_reader_;
118 // Used to trigger progress updates.
119 scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_;
121 // Statistics
122 size_t bytes_seen_;
123 base::TimeDelta disk_writes_time_;
124 base::TimeTicks download_start_;
125 RateEstimator rate_estimator_;
127 net::BoundNetLog bound_net_log_;
129 base::WeakPtr<DownloadDestinationObserver> observer_;
131 base::WeakPtrFactory<DownloadFileImpl> weak_factory_;
133 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
136 } // namespace content
138 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_