| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/linked_ptr.h" | 12 #include "base/linked_ptr.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/third_party/nss/blapi.h" |
| 13 #include "chrome/browser/power_save_blocker.h" | 15 #include "chrome/browser/power_save_blocker.h" |
| 14 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 15 | 17 |
| 16 namespace net { | 18 namespace net { |
| 17 class FileStream; | 19 class FileStream; |
| 18 } | 20 } |
| 19 | 21 |
| 20 // File being downloaded and saved to disk. This is a base class | 22 // File being downloaded and saved to disk. This is a base class |
| 21 // for DownloadFile and SaveFile, which keep more state information. | 23 // for DownloadFile and SaveFile, which keep more state information. |
| 22 class BaseFile { | 24 class BaseFile { |
| 23 public: | 25 public: |
| 24 BaseFile(const FilePath& full_path, | 26 BaseFile(const FilePath& full_path, |
| 25 const GURL& source_url, | 27 const GURL& source_url, |
| 26 const GURL& referrer_url, | 28 const GURL& referrer_url, |
| 27 int64 received_bytes, | 29 int64 received_bytes, |
| 28 const linked_ptr<net::FileStream>& file_stream); | 30 const linked_ptr<net::FileStream>& file_stream); |
| 29 ~BaseFile(); | 31 ~BaseFile(); |
| 30 | 32 |
| 31 bool Initialize(); | 33 // If calculate_hash is true, sha256 hash will be calculated. |
| 34 bool Initialize(bool calculate_hash); |
| 32 | 35 |
| 33 // Write a new chunk of data to the file. Returns true on success (all bytes | 36 // Write a new chunk of data to the file. Returns true on success (all bytes |
| 34 // written to the file). | 37 // written to the file). |
| 35 bool AppendDataToFile(const char* data, size_t data_len); | 38 bool AppendDataToFile(const char* data, size_t data_len); |
| 36 | 39 |
| 37 // Rename the download file. Returns true on success. | 40 // Rename the download file. Returns true on success. |
| 38 // |path_renamed_| is set to true only if |is_final_rename| is true. | 41 // |path_renamed_| is set to true only if |is_final_rename| is true. |
| 39 // Marked virtual for testing. | 42 // Marked virtual for testing. |
| 40 virtual bool Rename(const FilePath& full_path, bool is_final_rename); | 43 virtual bool Rename(const FilePath& full_path, bool is_final_rename); |
| 41 | 44 |
| 42 // Abort the download and automatically close the file. | 45 // Abort the download and automatically close the file. |
| 43 void Cancel(); | 46 void Cancel(); |
| 44 | 47 |
| 45 // Indicate that the download has finished. No new data will be received. | 48 // Indicate that the download has finished. No new data will be received. |
| 46 void Finish(); | 49 void Finish(); |
| 47 | 50 |
| 48 // Informs the OS that this file came from the internet. | 51 // Informs the OS that this file came from the internet. |
| 49 void AnnotateWithSourceInformation(); | 52 void AnnotateWithSourceInformation(); |
| 50 | 53 |
| 51 FilePath full_path() const { return full_path_; } | 54 FilePath full_path() const { return full_path_; } |
| 52 bool path_renamed() const { return path_renamed_; } | 55 bool path_renamed() const { return path_renamed_; } |
| 53 bool in_progress() const { return file_stream_ != NULL; } | 56 bool in_progress() const { return file_stream_ != NULL; } |
| 54 int64 bytes_so_far() const { return bytes_so_far_; } | 57 int64 bytes_so_far() const { return bytes_so_far_; } |
| 55 | 58 |
| 59 // Set |hash| with sha256 digest for the file. |
| 60 // Returns true if digest is successfully calculated. |
| 61 virtual bool GetSha256Hash(std::string* hash); |
| 62 |
| 56 virtual std::string DebugString() const; | 63 virtual std::string DebugString() const; |
| 57 | 64 |
| 58 protected: | 65 protected: |
| 59 bool Open(); | 66 bool Open(); |
| 60 void Close(); | 67 void Close(); |
| 61 | 68 |
| 62 // Full path to the file including the file name. | 69 // Full path to the file including the file name. |
| 63 FilePath full_path_; | 70 FilePath full_path_; |
| 64 | 71 |
| 65 // Whether the download is still using its initial temporary path. | 72 // Whether the download is still using its initial temporary path. |
| 66 bool path_renamed_; | 73 bool path_renamed_; |
| 67 | 74 |
| 68 private: | 75 private: |
| 76 static const size_t kSha256HashLen = 32; |
| 77 |
| 69 // Source URL for the file being downloaded. | 78 // Source URL for the file being downloaded. |
| 70 GURL source_url_; | 79 GURL source_url_; |
| 71 | 80 |
| 72 // The URL where the download was initiated. | 81 // The URL where the download was initiated. |
| 73 GURL referrer_url_; | 82 GURL referrer_url_; |
| 74 | 83 |
| 75 // OS file stream for writing | 84 // OS file stream for writing |
| 76 linked_ptr<net::FileStream> file_stream_; | 85 linked_ptr<net::FileStream> file_stream_; |
| 77 | 86 |
| 78 // Amount of data received up so far, in bytes. | 87 // Amount of data received up so far, in bytes. |
| 79 int64 bytes_so_far_; | 88 int64 bytes_so_far_; |
| 80 | 89 |
| 81 // RAII handle to keep the system from sleeping while we're downloading. | 90 // RAII handle to keep the system from sleeping while we're downloading. |
| 82 PowerSaveBlocker power_save_blocker_; | 91 PowerSaveBlocker power_save_blocker_; |
| 83 | 92 |
| 93 // Indicates if sha256 hash should be calculated for the file. |
| 94 bool calculate_hash_; |
| 95 |
| 96 // Used to calculate sha256 hash for the file when calculate_hash_ |
| 97 // is set. |
| 98 scoped_ptr<SHA256Context> sha_context_; |
| 99 |
| 100 unsigned char sha256_hash_[kSha256HashLen]; |
| 101 |
| 84 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 102 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
| 85 }; | 103 }; |
| 86 | 104 |
| 87 #endif // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ | 105 #endif // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ |
| OLD | NEW |