base_file.h revision 868fa2fe829687343ffae624259930155e16dbd8
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. 4 5#ifndef CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ 6#define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ 7 8#include <string> 9 10#include "base/files/file_path.h" 11#include "base/gtest_prod_util.h" 12#include "base/memory/linked_ptr.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/time.h" 15#include "content/common/content_export.h" 16#include "content/public/browser/download_interrupt_reasons.h" 17#include "googleurl/src/gurl.h" 18#include "net/base/file_stream.h" 19#include "net/base/net_errors.h" 20#include "net/base/net_log.h" 21 22namespace crypto { 23class SecureHash; 24} 25namespace net { 26class FileStream; 27} 28 29namespace content { 30 31// File being downloaded and saved to disk. This is a base class 32// for DownloadFile and SaveFile, which keep more state information. 33class CONTENT_EXPORT BaseFile { 34 public: 35 // May be constructed on any thread. All other routines (including 36 // destruction) must occur on the FILE thread. 37 BaseFile(const base::FilePath& full_path, 38 const GURL& source_url, 39 const GURL& referrer_url, 40 int64 received_bytes, 41 bool calculate_hash, 42 const std::string& hash_state, 43 scoped_ptr<net::FileStream> file_stream, 44 const net::BoundNetLog& bound_net_log); 45 virtual ~BaseFile(); 46 47 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a 48 // DownloadInterruptReason on failure. |default_directory| specifies the 49 // directory to create the temporary file in if |full_path()| is empty. If 50 // |default_directory| and |full_path()| are empty, then a temporary file will 51 // be created in the default download location as determined by 52 // ContentBrowserClient. 53 DownloadInterruptReason Initialize(const base::FilePath& default_directory); 54 55 // Write a new chunk of data to the file. Returns a DownloadInterruptReason 56 // indicating the result of the operation. 57 DownloadInterruptReason AppendDataToFile(const char* data, size_t data_len); 58 59 // Rename the download file. Returns a DownloadInterruptReason indicating the 60 // result of the operation. 61 virtual DownloadInterruptReason Rename(const base::FilePath& full_path); 62 63 // Detach the file so it is not deleted on destruction. 64 virtual void Detach(); 65 66 // Abort the download and automatically close the file. 67 void Cancel(); 68 69 // Indicate that the download has finished. No new data will be received. 70 void Finish(); 71 72 // Informs the OS that this file came from the internet. Returns a 73 // DownloadInterruptReason indicating the result of the operation. 74 DownloadInterruptReason AnnotateWithSourceInformation(); 75 76 base::FilePath full_path() const { return full_path_; } 77 bool in_progress() const { return file_stream_.get() != NULL; } 78 int64 bytes_so_far() const { return bytes_so_far_; } 79 80 // Fills |hash| with the hash digest for the file. 81 // Returns true if digest is successfully calculated. 82 virtual bool GetHash(std::string* hash); 83 84 // Returns the current (intermediate) state of the hash as a byte string. 85 virtual std::string GetHashState(); 86 87 // Returns true if the given hash is considered empty. An empty hash is 88 // a string of size kSha256HashLen that contains only zeros (initial value 89 // for the hash). 90 static bool IsEmptyHash(const std::string& hash); 91 92 virtual std::string DebugString() const; 93 94 private: 95 friend class BaseFileTest; 96 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); 97 98 // Re-initializes file_stream_ with a newly allocated net::FileStream(). 99 void CreateFileStream(); 100 101 // Creates and opens the file_stream_ if it is NULL. 102 DownloadInterruptReason Open(); 103 104 // Closes and resets file_stream_. 105 void Close(); 106 107 // Resets file_stream_. 108 void ClearStream(); 109 110 // Platform specific method that moves a file to a new path and adjusts the 111 // security descriptor / permissions on the file to match the defaults for the 112 // new directory. 113 DownloadInterruptReason MoveFileAndAdjustPermissions( 114 const base::FilePath& new_path); 115 116 // Split out from CurrentSpeed to enable testing. 117 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; 118 119 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error 120 // on through, converting to a |DownloadInterruptReason|. 121 DownloadInterruptReason LogNetError(const char* operation, net::Error error); 122 123 // Log the system error in |os_error| and converts it into a 124 // |DownloadInterruptReason|. 125 DownloadInterruptReason LogSystemError(const char* operation, int os_error); 126 127 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|. 128 // Returns |reason|. 129 DownloadInterruptReason LogInterruptReason( 130 const char* operation, int os_error, 131 DownloadInterruptReason reason); 132 133 static const size_t kSha256HashLen = 32; 134 static const unsigned char kEmptySha256Hash[kSha256HashLen]; 135 136 // Full path to the file including the file name. 137 base::FilePath full_path_; 138 139 // Source URL for the file being downloaded. 140 GURL source_url_; 141 142 // The URL where the download was initiated. 143 GURL referrer_url_; 144 145 // OS file stream for writing 146 scoped_ptr<net::FileStream> file_stream_; 147 148 // Amount of data received up so far, in bytes. 149 int64 bytes_so_far_; 150 151 // Start time for calculating speed. 152 base::TimeTicks start_tick_; 153 154 // Indicates if hash should be calculated for the file. 155 bool calculate_hash_; 156 157 // Used to calculate hash for the file when calculate_hash_ 158 // is set. 159 scoped_ptr<crypto::SecureHash> secure_hash_; 160 161 unsigned char sha256_hash_[kSha256HashLen]; 162 163 // Indicates that this class no longer owns the associated file, and so 164 // won't delete it on destruction. 165 bool detached_; 166 167 net::BoundNetLog bound_net_log_; 168 169 DISALLOW_COPY_AND_ASSIGN(BaseFile); 170}; 171 172} // namespace content 173 174#endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ 175