Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 } | 27 } |
| 28 | 28 |
| 29 // File being downloaded and saved to disk. This is a base class | 29 // File being downloaded and saved to disk. This is a base class |
| 30 // for DownloadFile and SaveFile, which keep more state information. | 30 // for DownloadFile and SaveFile, which keep more state information. |
| 31 class CONTENT_EXPORT BaseFile { | 31 class CONTENT_EXPORT BaseFile { |
| 32 public: | 32 public: |
| 33 BaseFile(const FilePath& full_path, | 33 BaseFile(const FilePath& full_path, |
| 34 const GURL& source_url, | 34 const GURL& source_url, |
| 35 const GURL& referrer_url, | 35 const GURL& referrer_url, |
| 36 int64 received_bytes, | 36 int64 received_bytes, |
| 37 bool calculate_hash, | |
| 38 const std::string& hash_state, | |
| 37 const linked_ptr<net::FileStream>& file_stream); | 39 const linked_ptr<net::FileStream>& file_stream); |
| 38 virtual ~BaseFile(); | 40 virtual ~BaseFile(); |
| 39 | 41 |
| 40 // If calculate_hash is true, sha256 hash will be calculated. | |
| 41 // Returns net::OK on success, or a network error code on failure. | 42 // Returns net::OK on success, or a network error code on failure. |
| 42 net::Error Initialize(bool calculate_hash); | 43 net::Error Initialize(); |
| 43 | 44 |
| 44 // Write a new chunk of data to the file. | 45 // Write a new chunk of data to the file. |
| 45 // Returns net::OK on success (all bytes written to the file), | 46 // Returns net::OK on success (all bytes written to the file), |
| 46 // or a network error code on failure. | 47 // or a network error code on failure. |
| 47 net::Error AppendDataToFile(const char* data, size_t data_len); | 48 net::Error AppendDataToFile(const char* data, size_t data_len); |
| 48 | 49 |
| 49 // Rename the download file. | 50 // Rename the download file. |
| 50 // Returns net::OK on success, or a network error code on failure. | 51 // Returns net::OK on success, or a network error code on failure. |
| 51 virtual net::Error Rename(const FilePath& full_path); | 52 virtual net::Error Rename(const FilePath& full_path); |
| 52 | 53 |
| 53 // Detach the file so it is not deleted on destruction. | 54 // Detach the file so it is not deleted on destruction. |
| 54 virtual void Detach(); | 55 virtual void Detach(); |
| 55 | 56 |
| 56 // Abort the download and automatically close the file. | 57 // Abort the download and automatically close the file. |
| 57 void Cancel(); | 58 void Cancel(); |
| 58 | 59 |
| 59 // Indicate that the download has finished. No new data will be received. | 60 // Indicate that the download has finished. No new data will be received. |
| 60 void Finish(); | 61 void Finish(); |
| 61 | 62 |
| 62 // Informs the OS that this file came from the internet. | 63 // Informs the OS that this file came from the internet. |
| 63 void AnnotateWithSourceInformation(); | 64 void AnnotateWithSourceInformation(); |
| 64 | 65 |
| 65 // Calculate and return the current speed in bytes per second. | 66 // Calculate and return the current speed in bytes per second. |
| 66 int64 CurrentSpeed() const; | 67 int64 CurrentSpeed() const; |
| 67 | 68 |
| 68 FilePath full_path() const { return full_path_; } | 69 FilePath full_path() const { return full_path_; } |
| 69 bool in_progress() const { return file_stream_ != NULL; } | 70 bool in_progress() const { return file_stream_ != NULL; } |
| 70 int64 bytes_so_far() const { return bytes_so_far_; } | 71 int64 bytes_so_far() const { return bytes_so_far_; } |
| 71 | 72 |
| 72 // Set |hash| with sha256 digest for the file. | 73 // Fills |hash| with the sha256 digest for the file. |
|
Randy Smith (Not in Mondays)
2011/12/20 17:05:40
Basic question: Is the fact that the hash is an sh
ahendrickson
2011/12/20 22:06:54
Changed the comments to match the functions.
| |
| 73 // Returns true if digest is successfully calculated. | 74 // Returns true if digest is successfully calculated. |
| 74 virtual bool GetSha256Hash(std::string* hash); | 75 virtual bool GetHash(std::string* hash); |
| 76 | |
| 77 // Returns the current (intermediate) state of the hash as a byte string. | |
| 78 virtual std::string GetHashState(); | |
| 75 | 79 |
| 76 // Returns true if the given hash is considered empty. An empty hash is | 80 // Returns true if the given hash is considered empty. An empty hash is |
| 77 // a string of size kSha256HashLen that contains only zeros (initial value | 81 // a string of size kSha256HashLen that contains only zeros (initial value |
| 78 // for the hash). | 82 // for the hash). |
| 79 static bool IsEmptySha256Hash(const std::string& hash); | 83 static bool IsEmptyHash(const std::string& hash); |
| 80 | 84 |
| 81 virtual std::string DebugString() const; | 85 virtual std::string DebugString() const; |
| 82 | 86 |
| 83 protected: | 87 protected: |
| 84 virtual void CreateFileStream(); // For testing. | 88 virtual void CreateFileStream(); // For testing. |
| 85 // Returns net::OK on success, or a network error code on failure. | 89 // Returns net::OK on success, or a network error code on failure. |
| 86 net::Error Open(); | 90 net::Error Open(); |
| 87 void Close(); | 91 void Close(); |
| 88 | 92 |
| 89 // Full path to the file including the file name. | 93 // Full path to the file including the file name. |
| 90 FilePath full_path_; | 94 FilePath full_path_; |
| 91 | 95 |
| 92 private: | 96 private: |
| 93 friend class BaseFileTest; | 97 friend class BaseFileTest; |
| 94 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptySha256Hash); | 98 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); |
| 95 | 99 |
| 96 // Split out from CurrentSpeed to enable testing. | 100 // Split out from CurrentSpeed to enable testing. |
| 97 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; | 101 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; |
| 98 | 102 |
| 103 // Resets the current state of the hash to the contents of |hash_state_bytes|. | |
| 104 virtual bool SetHashState(const std::string& hash_state_bytes); | |
| 105 | |
| 99 static const size_t kSha256HashLen = 32; | 106 static const size_t kSha256HashLen = 32; |
| 100 static const unsigned char kEmptySha256Hash[kSha256HashLen]; | 107 static const unsigned char kEmptySha256Hash[kSha256HashLen]; |
| 101 | 108 |
| 102 // Source URL for the file being downloaded. | 109 // Source URL for the file being downloaded. |
| 103 GURL source_url_; | 110 GURL source_url_; |
| 104 | 111 |
| 105 // The URL where the download was initiated. | 112 // The URL where the download was initiated. |
| 106 GURL referrer_url_; | 113 GURL referrer_url_; |
| 107 | 114 |
| 108 // OS file stream for writing | 115 // OS file stream for writing |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 127 unsigned char sha256_hash_[kSha256HashLen]; | 134 unsigned char sha256_hash_[kSha256HashLen]; |
| 128 | 135 |
| 129 // Indicates that this class no longer owns the associated file, and so | 136 // Indicates that this class no longer owns the associated file, and so |
| 130 // won't delete it on destruction. | 137 // won't delete it on destruction. |
| 131 bool detached_; | 138 bool detached_; |
| 132 | 139 |
| 133 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 140 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
| 134 }; | 141 }; |
| 135 | 142 |
| 136 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 143 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
| OLD | NEW |