| 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 #include "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 if ((bytes_so_far_ > 0) && // Not starting at the beginning. | 48 if ((bytes_so_far_ > 0) && // Not starting at the beginning. |
| 49 (!IsEmptyHash(hash_state_bytes))) { | 49 (!IsEmptyHash(hash_state_bytes))) { |
| 50 Pickle hash_state(hash_state_bytes.c_str(), hash_state_bytes.size()); | 50 Pickle hash_state(hash_state_bytes.c_str(), hash_state_bytes.size()); |
| 51 PickleIterator data_iterator(hash_state); | 51 PickleIterator data_iterator(hash_state); |
| 52 secure_hash_->Deserialize(&data_iterator); | 52 secure_hash_->Deserialize(&data_iterator); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 BaseFile::~BaseFile() { | 57 BaseFile::~BaseFile() { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 58 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 59 if (detached_) | 59 if (detached_) |
| 60 Close(); | 60 Close(); |
| 61 else | 61 else |
| 62 Cancel(); // Will delete the file. | 62 Cancel(); // Will delete the file. |
| 63 } | 63 } |
| 64 | 64 |
| 65 DownloadInterruptReason BaseFile::Initialize( | 65 DownloadInterruptReason BaseFile::Initialize( |
| 66 const base::FilePath& default_directory) { | 66 const base::FilePath& default_directory) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 67 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 68 DCHECK(!detached_); | 68 DCHECK(!detached_); |
| 69 | 69 |
| 70 if (full_path_.empty()) { | 70 if (full_path_.empty()) { |
| 71 base::FilePath initial_directory(default_directory); | 71 base::FilePath initial_directory(default_directory); |
| 72 base::FilePath temp_file; | 72 base::FilePath temp_file; |
| 73 if (initial_directory.empty()) { | 73 if (initial_directory.empty()) { |
| 74 initial_directory = | 74 initial_directory = |
| 75 GetContentClient()->browser()->GetDefaultDownloadDirectory(); | 75 GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
| 76 } | 76 } |
| 77 // |initial_directory| can still be empty if ContentBrowserClient returned | 77 // |initial_directory| can still be empty if ContentBrowserClient returned |
| 78 // an empty path for the downloads directory. | 78 // an empty path for the downloads directory. |
| 79 if ((initial_directory.empty() || | 79 if ((initial_directory.empty() || |
| 80 !base::CreateTemporaryFileInDir(initial_directory, &temp_file)) && | 80 !base::CreateTemporaryFileInDir(initial_directory, &temp_file)) && |
| 81 !base::CreateTemporaryFile(&temp_file)) { | 81 !base::CreateTemporaryFile(&temp_file)) { |
| 82 return LogInterruptReason("Unable to create", 0, | 82 return LogInterruptReason("Unable to create", 0, |
| 83 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 83 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 84 } | 84 } |
| 85 full_path_ = temp_file; | 85 full_path_ = temp_file; |
| 86 } | 86 } |
| 87 | 87 |
| 88 return Open(); | 88 return Open(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 DownloadInterruptReason BaseFile::AppendDataToFile(const char* data, | 91 DownloadInterruptReason BaseFile::AppendDataToFile(const char* data, |
| 92 size_t data_len) { | 92 size_t data_len) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 93 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 94 DCHECK(!detached_); | 94 DCHECK(!detached_); |
| 95 | 95 |
| 96 // NOTE(benwells): The above DCHECK won't be present in release builds, | 96 // NOTE(benwells): The above DCHECK won't be present in release builds, |
| 97 // so we log any occurences to see how common this error is in the wild. | 97 // so we log any occurences to see how common this error is in the wild. |
| 98 if (detached_) | 98 if (detached_) |
| 99 RecordDownloadCount(APPEND_TO_DETACHED_FILE_COUNT); | 99 RecordDownloadCount(APPEND_TO_DETACHED_FILE_COUNT); |
| 100 | 100 |
| 101 if (!file_.IsValid()) | 101 if (!file_.IsValid()) |
| 102 return LogInterruptReason("No file stream on append", 0, | 102 return LogInterruptReason("No file stream on append", 0, |
| 103 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 103 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 130 RecordDownloadWriteSize(data_len); | 130 RecordDownloadWriteSize(data_len); |
| 131 RecordDownloadWriteLoopCount(write_count); | 131 RecordDownloadWriteLoopCount(write_count); |
| 132 | 132 |
| 133 if (calculate_hash_) | 133 if (calculate_hash_) |
| 134 secure_hash_->Update(data, data_len); | 134 secure_hash_->Update(data, data_len); |
| 135 | 135 |
| 136 return DOWNLOAD_INTERRUPT_REASON_NONE; | 136 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 137 } | 137 } |
| 138 | 138 |
| 139 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) { | 139 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 141 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; | 141 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 142 | 142 |
| 143 // If the new path is same as the old one, there is no need to perform the | 143 // If the new path is same as the old one, there is no need to perform the |
| 144 // following renaming logic. | 144 // following renaming logic. |
| 145 if (new_path == full_path_) | 145 if (new_path == full_path_) |
| 146 return DOWNLOAD_INTERRUPT_REASON_NONE; | 146 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 147 | 147 |
| 148 // Save the information whether the download is in progress because | 148 // Save the information whether the download is in progress because |
| 149 // it will be overwritten by closing the file. | 149 // it will be overwritten by closing the file. |
| 150 bool was_in_progress = in_progress(); | 150 bool was_in_progress = in_progress(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 172 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result | 172 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result |
| 173 : rename_result; | 173 : rename_result; |
| 174 } | 174 } |
| 175 | 175 |
| 176 void BaseFile::Detach() { | 176 void BaseFile::Detach() { |
| 177 detached_ = true; | 177 detached_ = true; |
| 178 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED); | 178 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void BaseFile::Cancel() { | 181 void BaseFile::Cancel() { |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 182 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 183 DCHECK(!detached_); | 183 DCHECK(!detached_); |
| 184 | 184 |
| 185 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED); | 185 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED); |
| 186 | 186 |
| 187 Close(); | 187 Close(); |
| 188 | 188 |
| 189 if (!full_path_.empty()) { | 189 if (!full_path_.empty()) { |
| 190 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED); | 190 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED); |
| 191 base::DeleteFile(full_path_, false); | 191 base::DeleteFile(full_path_, false); |
| 192 } | 192 } |
| 193 | 193 |
| 194 Detach(); | 194 Detach(); |
| 195 } | 195 } |
| 196 | 196 |
| 197 void BaseFile::Finish() { | 197 void BaseFile::Finish() { |
| 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 198 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 199 | 199 |
| 200 if (calculate_hash_) | 200 if (calculate_hash_) |
| 201 secure_hash_->Finish(sha256_hash_, crypto::kSHA256Length); | 201 secure_hash_->Finish(sha256_hash_, crypto::kSHA256Length); |
| 202 | 202 |
| 203 Close(); | 203 Close(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void BaseFile::SetClientGuid(const std::string& guid) { | 206 void BaseFile::SetClientGuid(const std::string& guid) { |
| 207 client_guid_ = guid; | 207 client_guid_ = guid; |
| 208 } | 208 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 " full_path_ = \"%" PRFilePath "\"" | 244 " full_path_ = \"%" PRFilePath "\"" |
| 245 " bytes_so_far_ = %" PRId64 | 245 " bytes_so_far_ = %" PRId64 |
| 246 " detached_ = %c }", | 246 " detached_ = %c }", |
| 247 source_url_.spec().c_str(), | 247 source_url_.spec().c_str(), |
| 248 full_path_.value().c_str(), | 248 full_path_.value().c_str(), |
| 249 bytes_so_far_, | 249 bytes_so_far_, |
| 250 detached_ ? 'T' : 'F'); | 250 detached_ ? 'T' : 'F'); |
| 251 } | 251 } |
| 252 | 252 |
| 253 DownloadInterruptReason BaseFile::Open() { | 253 DownloadInterruptReason BaseFile::Open() { |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 254 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 255 DCHECK(!detached_); | 255 DCHECK(!detached_); |
| 256 DCHECK(!full_path_.empty()); | 256 DCHECK(!full_path_.empty()); |
| 257 | 257 |
| 258 bound_net_log_.BeginEvent( | 258 bound_net_log_.BeginEvent( |
| 259 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, | 259 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, |
| 260 base::Bind(&FileOpenedNetLogCallback, &full_path_, bytes_so_far_)); | 260 base::Bind(&FileOpenedNetLogCallback, &full_path_, bytes_so_far_)); |
| 261 | 261 |
| 262 // Create a new file if it is not provided. | 262 // Create a new file if it is not provided. |
| 263 if (!file_.IsValid()) { | 263 if (!file_.IsValid()) { |
| 264 file_.Initialize( | 264 file_.Initialize( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 290 // The file is shorter than we expected. Our hashes won't be valid. | 290 // The file is shorter than we expected. Our hashes won't be valid. |
| 291 ClearFile(); | 291 ClearFile(); |
| 292 return LogInterruptReason("Unable to seek to last written point", 0, | 292 return LogInterruptReason("Unable to seek to last written point", 0, |
| 293 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); | 293 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); |
| 294 } | 294 } |
| 295 | 295 |
| 296 return DOWNLOAD_INTERRUPT_REASON_NONE; | 296 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 297 } | 297 } |
| 298 | 298 |
| 299 void BaseFile::Close() { | 299 void BaseFile::Close() { |
| 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 300 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 301 | 301 |
| 302 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED); | 302 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED); |
| 303 | 303 |
| 304 if (file_.IsValid()) { | 304 if (file_.IsValid()) { |
| 305 // Currently we don't really care about the return value, since if it fails | 305 // Currently we don't really care about the return value, since if it fails |
| 306 // theres not much we can do. But we might in the future. | 306 // theres not much we can do. But we might in the future. |
| 307 file_.Flush(); | 307 file_.Flush(); |
| 308 ClearFile(); | 308 ClearFile(); |
| 309 } | 309 } |
| 310 } | 310 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 339 const char* operation, | 339 const char* operation, |
| 340 int os_error, | 340 int os_error, |
| 341 DownloadInterruptReason reason) { | 341 DownloadInterruptReason reason) { |
| 342 bound_net_log_.AddEvent( | 342 bound_net_log_.AddEvent( |
| 343 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, | 343 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, |
| 344 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); | 344 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); |
| 345 return reason; | 345 return reason; |
| 346 } | 346 } |
| 347 | 347 |
| 348 } // namespace content | 348 } // namespace content |
| OLD | NEW |