OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
7 | 7 |
8 #include "content/browser/download/download_file.h" | 8 #include "content/browser/download/download_file.h" |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 21 matching lines...) Expand all Loading... |
32 class CONTENT_EXPORT DownloadFileImpl : public DownloadFile { | 32 class CONTENT_EXPORT DownloadFileImpl : public DownloadFile { |
33 public: | 33 public: |
34 // Takes ownership of the object pointed to by |request_handle|. | 34 // Takes ownership of the object pointed to by |request_handle|. |
35 // |bound_net_log| will be used for logging the download file's events. | 35 // |bound_net_log| will be used for logging the download file's events. |
36 // May be constructed on any thread. All methods besides the constructor | 36 // May be constructed on any thread. All methods besides the constructor |
37 // (including destruction) must occur on the FILE thread. | 37 // (including destruction) must occur on the FILE thread. |
38 // | 38 // |
39 // Note that the DownloadFileImpl automatically reads from the passed in | 39 // Note that the DownloadFileImpl automatically reads from the passed in |
40 // stream, and sends updates and status of those reads to the | 40 // stream, and sends updates and status of those reads to the |
41 // DownloadDestinationObserver. | 41 // DownloadDestinationObserver. |
42 DownloadFileImpl(const DownloadSaveInfo& save_info, | 42 DownloadFileImpl(scoped_ptr<DownloadSaveInfo> save_info, |
43 const base::FilePath& default_downloads_directory, | 43 const base::FilePath& default_downloads_directory, |
44 const GURL& url, | |
45 const GURL& referrer_url, | |
46 bool calculate_hash, | |
47 base::File file, | |
48 scoped_ptr<ByteStreamReader> byte_stream, | 44 scoped_ptr<ByteStreamReader> byte_stream, |
49 const net::BoundNetLog& bound_net_log, | 45 const net::BoundNetLog& bound_net_log, |
50 base::WeakPtr<DownloadDestinationObserver> observer); | 46 base::WeakPtr<DownloadDestinationObserver> observer); |
51 | 47 |
52 ~DownloadFileImpl() override; | 48 ~DownloadFileImpl() override; |
53 | 49 |
54 // DownloadFile functions. | 50 // DownloadFile functions. |
55 void Initialize(const InitializeCallback& callback) override; | 51 void Initialize(const InitializeCallback& callback) override; |
56 void RenameAndUniquify(const base::FilePath& full_path, | 52 void RenameAndUniquify(const base::FilePath& full_path, |
57 const RenameCompletionCallback& callback) override; | 53 const RenameCompletionCallback& callback) override; |
58 void RenameAndAnnotate(const base::FilePath& full_path, | 54 void RenameAndAnnotate(const base::FilePath& full_path, |
| 55 const std::string& client_guid, |
| 56 const GURL& source_url, |
| 57 const GURL& referrer_url, |
59 const RenameCompletionCallback& callback) override; | 58 const RenameCompletionCallback& callback) override; |
60 void Detach() override; | 59 void Detach() override; |
61 void Cancel() override; | 60 void Cancel() override; |
62 base::FilePath FullPath() const override; | 61 const base::FilePath& FullPath() const override; |
63 bool InProgress() const override; | 62 bool InProgress() const override; |
64 int64_t CurrentSpeed() const override; | |
65 bool GetHash(std::string* hash) override; | |
66 std::string GetHashState() override; | |
67 void SetClientGuid(const std::string& guid) override; | |
68 | 63 |
69 protected: | 64 protected: |
70 // For test class overrides. | 65 // For test class overrides. |
71 virtual DownloadInterruptReason AppendDataToFile( | 66 virtual DownloadInterruptReason AppendDataToFile( |
72 const char* data, size_t data_len); | 67 const char* data, size_t data_len); |
73 | 68 |
74 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); | 69 virtual base::TimeDelta GetRetryDelayForFailedRename(int attempt_number); |
75 | 70 |
76 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); | 71 virtual bool ShouldRetryFailedRename(DownloadInterruptReason reason); |
77 | 72 |
78 private: | 73 private: |
79 friend class DownloadFileTest; | 74 friend class DownloadFileTest; |
80 | 75 |
81 // Options for RenameWithRetryInternal. | 76 // Options for RenameWithRetryInternal. |
82 enum RenameOption { | 77 enum RenameOption { |
83 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with | 78 UNIQUIFY = 1 << 0, // If there's already a file on disk that conflicts with |
84 // |new_path|, try to create a unique file by appending | 79 // |new_path|, try to create a unique file by appending |
85 // a uniquifier. | 80 // a uniquifier. |
86 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 | 81 ANNOTATE_WITH_SOURCE_INFORMATION = 1 << 1 |
87 }; | 82 }; |
88 | 83 |
89 // Rename file_ to |new_path|. | 84 struct RenameParameters { |
90 // |option| specifies additional operations to be performed during the rename. | 85 RenameParameters(RenameOption option, |
91 // See RenameOption above. | 86 const base::FilePath& new_path, |
92 // |retries_left| indicates how many times to retry the operation if the | 87 const RenameCompletionCallback& completion_callback); |
93 // rename fails with a transient error. | 88 ~RenameParameters(); |
94 // |time_of_first_failure| Set to an empty base::TimeTicks during the first | 89 |
95 // call. Once the first failure is seen, subsequent calls of | 90 RenameOption option; |
96 // RenameWithRetryInternal will have a non-empty value keeping track of | 91 base::FilePath new_path; |
97 // the time of first observed failure. Used for UMA. | 92 std::string client_guid; // See BaseFile::AnnotateWithSourceInformation() |
98 void RenameWithRetryInternal(const base::FilePath& new_path, | 93 GURL source_url; // See BaseFile::AnnotateWithSourceInformation() |
99 RenameOption option, | 94 GURL referrer_url; // See BaseFile::AnnotateWithSourceInformation() |
100 int retries_left, | 95 int retries_left; // RenameWithRetryInternal() will |
101 base::TimeTicks time_of_first_failure, | 96 // automatically retry until this |
102 const RenameCompletionCallback& callback); | 97 // count reaches 0. Each attempt |
| 98 // decrements this counter. |
| 99 base::TimeTicks time_of_first_failure; // Set to empty at first, but is set |
| 100 // when a failure is first |
| 101 // encountered. Used for UMA. |
| 102 RenameCompletionCallback completion_callback; |
| 103 }; |
| 104 |
| 105 // Rename file_ based on |parameters|. |
| 106 void RenameWithRetryInternal(scoped_ptr<RenameParameters> parameters); |
103 | 107 |
104 // Send an update on our progress. | 108 // Send an update on our progress. |
105 void SendUpdate(); | 109 void SendUpdate(); |
106 | 110 |
107 // Called when there's some activity on stream_reader_ that needs to be | 111 // Called when there's some activity on stream_reader_ that needs to be |
108 // handled. | 112 // handled. |
109 void StreamActive(); | 113 void StreamActive(); |
110 | 114 |
111 // The base file instance. | 115 // The base file instance. |
112 BaseFile file_; | 116 BaseFile file_; |
113 | 117 |
| 118 // DownloadSaveInfo provided during construction. Since the DownloadFileImpl |
| 119 // can be created on any thread, this holds the save_info_ until it can be |
| 120 // used to initialize file_ on the FILE thread. |
| 121 scoped_ptr<DownloadSaveInfo> save_info_; |
| 122 |
114 // The default directory for creating the download file. | 123 // The default directory for creating the download file. |
115 base::FilePath default_download_directory_; | 124 base::FilePath default_download_directory_; |
116 | 125 |
117 // The stream through which data comes. | 126 // The stream through which data comes. |
118 // TODO(rdsmith): Move this into BaseFile; requires using the same | 127 // TODO(rdsmith): Move this into BaseFile; requires using the same |
119 // stream semantics in SavePackage. Alternatively, replace SaveFile | 128 // stream semantics in SavePackage. Alternatively, replace SaveFile |
120 // with DownloadFile and get rid of BaseFile. | 129 // with DownloadFile and get rid of BaseFile. |
121 scoped_ptr<ByteStreamReader> stream_reader_; | 130 scoped_ptr<ByteStreamReader> stream_reader_; |
122 | 131 |
123 // Used to trigger progress updates. | 132 // Used to trigger progress updates. |
(...skipping 10 matching lines...) Expand all Loading... |
134 base::WeakPtr<DownloadDestinationObserver> observer_; | 143 base::WeakPtr<DownloadDestinationObserver> observer_; |
135 | 144 |
136 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; | 145 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; |
137 | 146 |
138 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); | 147 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); |
139 }; | 148 }; |
140 | 149 |
141 } // namespace content | 150 } // namespace content |
142 | 151 |
143 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 152 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
OLD | NEW |