| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "build/build_config.h" |
| 15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 16 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 16 #include "content/browser/download/download_net_log_parameters.h" | 17 #include "content/browser/download/download_net_log_parameters.h" |
| 17 #include "content/browser/download/download_stats.h" | 18 #include "content/browser/download/download_stats.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/content_browser_client.h" | 20 #include "content/public/browser/content_browser_client.h" |
| 20 #include "crypto/secure_hash.h" | 21 #include "crypto/secure_hash.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 25 // This will initialize the entire array to zero. | 26 // This will initialize the entire array to zero. |
| 26 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; | 27 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; |
| 27 | 28 |
| 28 BaseFile::BaseFile(const base::FilePath& full_path, | 29 BaseFile::BaseFile(const base::FilePath& full_path, |
| 29 const GURL& source_url, | 30 const GURL& source_url, |
| 30 const GURL& referrer_url, | 31 const GURL& referrer_url, |
| 31 int64 received_bytes, | 32 int64_t received_bytes, |
| 32 bool calculate_hash, | 33 bool calculate_hash, |
| 33 const std::string& hash_state_bytes, | 34 const std::string& hash_state_bytes, |
| 34 base::File file, | 35 base::File file, |
| 35 const net::BoundNetLog& bound_net_log) | 36 const net::BoundNetLog& bound_net_log) |
| 36 : full_path_(full_path), | 37 : full_path_(full_path), |
| 37 source_url_(source_url), | 38 source_url_(source_url), |
| 38 referrer_url_(referrer_url), | 39 referrer_url_(referrer_url), |
| 39 file_(file.Pass()), | 40 file_(file.Pass()), |
| 40 bytes_so_far_(received_bytes), | 41 bytes_so_far_(received_bytes), |
| 41 start_tick_(base::TimeTicks::Now()), | 42 start_tick_(base::TimeTicks::Now()), |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 file_.Initialize( | 266 file_.Initialize( |
| 266 full_path_, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); | 267 full_path_, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
| 267 if (!file_.IsValid()) { | 268 if (!file_.IsValid()) { |
| 268 return LogNetError("Open", | 269 return LogNetError("Open", |
| 269 net::FileErrorToNetError(file_.error_details())); | 270 net::FileErrorToNetError(file_.error_details())); |
| 270 } | 271 } |
| 271 } | 272 } |
| 272 | 273 |
| 273 // We may be re-opening the file after rename. Always make sure we're | 274 // We may be re-opening the file after rename. Always make sure we're |
| 274 // writing at the end of the file. | 275 // writing at the end of the file. |
| 275 int64 file_size = file_.Seek(base::File::FROM_END, 0); | 276 int64_t file_size = file_.Seek(base::File::FROM_END, 0); |
| 276 if (file_size < 0) { | 277 if (file_size < 0) { |
| 277 logging::SystemErrorCode error = logging::GetLastSystemErrorCode(); | 278 logging::SystemErrorCode error = logging::GetLastSystemErrorCode(); |
| 278 ClearFile(); | 279 ClearFile(); |
| 279 return LogSystemError("Seek", error); | 280 return LogSystemError("Seek", error); |
| 280 } else if (file_size > bytes_so_far_) { | 281 } else if (file_size > bytes_so_far_) { |
| 281 // The file is larger than we expected. | 282 // The file is larger than we expected. |
| 282 // This is OK, as long as we don't use the extra. | 283 // This is OK, as long as we don't use the extra. |
| 283 // Truncate the file. | 284 // Truncate the file. |
| 284 if (!file_.SetLength(bytes_so_far_) || | 285 if (!file_.SetLength(bytes_so_far_) || |
| 285 file_.Seek(base::File::FROM_BEGIN, bytes_so_far_) != bytes_so_far_) { | 286 file_.Seek(base::File::FROM_BEGIN, bytes_so_far_) != bytes_so_far_) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 const char* operation, | 341 const char* operation, |
| 341 int os_error, | 342 int os_error, |
| 342 DownloadInterruptReason reason) { | 343 DownloadInterruptReason reason) { |
| 343 bound_net_log_.AddEvent( | 344 bound_net_log_.AddEvent( |
| 344 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, | 345 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, |
| 345 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); | 346 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); |
| 346 return reason; | 347 return reason; |
| 347 } | 348 } |
| 348 | 349 |
| 349 } // namespace content | 350 } // namespace content |
| OLD | NEW |