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_BASE_FILE_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
7 | 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
8 #include <string> | 11 #include <string> |
9 | 12 |
10 #include "base/files/file.h" | 13 #include "base/files/file.h" |
11 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
12 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
13 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" |
14 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
16 #include "base/time/time.h" | 20 #include "base/time/time.h" |
17 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
18 #include "content/public/browser/download_interrupt_reasons.h" | 22 #include "content/public/browser/download_interrupt_reasons.h" |
19 #include "crypto/sha2.h" | 23 #include "crypto/sha2.h" |
20 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
21 #include "net/log/net_log.h" | 25 #include "net/log/net_log.h" |
22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
23 | 27 |
24 namespace crypto { | 28 namespace crypto { |
25 class SecureHash; | 29 class SecureHash; |
26 } | 30 } |
27 | 31 |
28 namespace content { | 32 namespace content { |
29 | 33 |
30 // File being downloaded and saved to disk. This is a base class | 34 // File being downloaded and saved to disk. This is a base class |
31 // for DownloadFile and SaveFile, which keep more state information. | 35 // for DownloadFile and SaveFile, which keep more state information. |
32 class CONTENT_EXPORT BaseFile { | 36 class CONTENT_EXPORT BaseFile { |
33 public: | 37 public: |
34 // May be constructed on any thread. All other routines (including | 38 // May be constructed on any thread. All other routines (including |
35 // destruction) must occur on the FILE thread. | 39 // destruction) must occur on the FILE thread. |
36 BaseFile(const base::FilePath& full_path, | 40 BaseFile(const base::FilePath& full_path, |
37 const GURL& source_url, | 41 const GURL& source_url, |
38 const GURL& referrer_url, | 42 const GURL& referrer_url, |
39 int64 received_bytes, | 43 int64_t received_bytes, |
40 bool calculate_hash, | 44 bool calculate_hash, |
41 const std::string& hash_state, | 45 const std::string& hash_state, |
42 base::File file, | 46 base::File file, |
43 const net::BoundNetLog& bound_net_log); | 47 const net::BoundNetLog& bound_net_log); |
44 virtual ~BaseFile(); | 48 virtual ~BaseFile(); |
45 | 49 |
46 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a | 50 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a |
47 // DownloadInterruptReason on failure. |default_directory| specifies the | 51 // DownloadInterruptReason on failure. |default_directory| specifies the |
48 // directory to create the temporary file in if |full_path()| is empty. If | 52 // directory to create the temporary file in if |full_path()| is empty. If |
49 // |default_directory| and |full_path()| are empty, then a temporary file will | 53 // |default_directory| and |full_path()| are empty, then a temporary file will |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 88 |
85 // Returns the last known path to the download file. Can be empty if there's | 89 // Returns the last known path to the download file. Can be empty if there's |
86 // no file. | 90 // no file. |
87 const base::FilePath& full_path() const { return full_path_; } | 91 const base::FilePath& full_path() const { return full_path_; } |
88 | 92 |
89 // Returns true if the file is open. If true, the file can be written to or | 93 // Returns true if the file is open. If true, the file can be written to or |
90 // renamed. | 94 // renamed. |
91 bool in_progress() const { return file_.IsValid(); } | 95 bool in_progress() const { return file_.IsValid(); } |
92 | 96 |
93 // Returns the number of bytes in the file pointed to by full_path(). | 97 // Returns the number of bytes in the file pointed to by full_path(). |
94 int64 bytes_so_far() const { return bytes_so_far_; } | 98 int64_t bytes_so_far() const { return bytes_so_far_; } |
95 | 99 |
96 // Fills |hash| with the hash digest for the file. | 100 // Fills |hash| with the hash digest for the file. |
97 // Returns true if digest is successfully calculated. | 101 // Returns true if digest is successfully calculated. |
98 virtual bool GetHash(std::string* hash); | 102 virtual bool GetHash(std::string* hash); |
99 | 103 |
100 // Returns the current (intermediate) state of the hash as a byte string. | 104 // Returns the current (intermediate) state of the hash as a byte string. |
101 virtual std::string GetHashState(); | 105 virtual std::string GetHashState(); |
102 | 106 |
103 // Returns true if the given hash is considered empty. An empty hash is | 107 // Returns true if the given hash is considered empty. An empty hash is |
104 // a string of size crypto::kSHA256Length that contains only zeros (initial | 108 // a string of size crypto::kSHA256Length that contains only zeros (initial |
(...skipping 15 matching lines...) Expand all Loading... |
120 // Resets file_. | 124 // Resets file_. |
121 void ClearFile(); | 125 void ClearFile(); |
122 | 126 |
123 // Platform specific method that moves a file to a new path and adjusts the | 127 // Platform specific method that moves a file to a new path and adjusts the |
124 // security descriptor / permissions on the file to match the defaults for the | 128 // security descriptor / permissions on the file to match the defaults for the |
125 // new directory. | 129 // new directory. |
126 DownloadInterruptReason MoveFileAndAdjustPermissions( | 130 DownloadInterruptReason MoveFileAndAdjustPermissions( |
127 const base::FilePath& new_path); | 131 const base::FilePath& new_path); |
128 | 132 |
129 // Split out from CurrentSpeed to enable testing. | 133 // Split out from CurrentSpeed to enable testing. |
130 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; | 134 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) const; |
131 | 135 |
132 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error | 136 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error |
133 // on through, converting to a |DownloadInterruptReason|. | 137 // on through, converting to a |DownloadInterruptReason|. |
134 DownloadInterruptReason LogNetError(const char* operation, net::Error error); | 138 DownloadInterruptReason LogNetError(const char* operation, net::Error error); |
135 | 139 |
136 // Log the system error in |os_error| and converts it into a | 140 // Log the system error in |os_error| and converts it into a |
137 // |DownloadInterruptReason|. | 141 // |DownloadInterruptReason|. |
138 DownloadInterruptReason LogSystemError(const char* operation, | 142 DownloadInterruptReason LogSystemError(const char* operation, |
139 logging::SystemErrorCode os_error); | 143 logging::SystemErrorCode os_error); |
140 | 144 |
(...skipping 13 matching lines...) Expand all Loading... |
154 | 158 |
155 // The URL where the download was initiated. | 159 // The URL where the download was initiated. |
156 GURL referrer_url_; | 160 GURL referrer_url_; |
157 | 161 |
158 std::string client_guid_; | 162 std::string client_guid_; |
159 | 163 |
160 // OS file for writing | 164 // OS file for writing |
161 base::File file_; | 165 base::File file_; |
162 | 166 |
163 // Amount of data received up so far, in bytes. | 167 // Amount of data received up so far, in bytes. |
164 int64 bytes_so_far_; | 168 int64_t bytes_so_far_; |
165 | 169 |
166 // Start time for calculating speed. | 170 // Start time for calculating speed. |
167 base::TimeTicks start_tick_; | 171 base::TimeTicks start_tick_; |
168 | 172 |
169 // Indicates if hash should be calculated for the file. | 173 // Indicates if hash should be calculated for the file. |
170 bool calculate_hash_; | 174 bool calculate_hash_; |
171 | 175 |
172 // Used to calculate hash for the file when calculate_hash_ | 176 // Used to calculate hash for the file when calculate_hash_ |
173 // is set. | 177 // is set. |
174 scoped_ptr<crypto::SecureHash> secure_hash_; | 178 scoped_ptr<crypto::SecureHash> secure_hash_; |
175 | 179 |
176 unsigned char sha256_hash_[crypto::kSHA256Length]; | 180 unsigned char sha256_hash_[crypto::kSHA256Length]; |
177 | 181 |
178 // Indicates that this class no longer owns the associated file, and so | 182 // Indicates that this class no longer owns the associated file, and so |
179 // won't delete it on destruction. | 183 // won't delete it on destruction. |
180 bool detached_; | 184 bool detached_; |
181 | 185 |
182 net::BoundNetLog bound_net_log_; | 186 net::BoundNetLog bound_net_log_; |
183 | 187 |
184 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 188 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
185 }; | 189 }; |
186 | 190 |
187 } // namespace content | 191 } // namespace content |
188 | 192 |
189 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 193 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
OLD | NEW |