| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "content/browser/download/download_interrupt_reasons_impl.h" | 14 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 15 #include "content/browser/download/download_net_log_parameters.h" | 15 #include "content/browser/download/download_net_log_parameters.h" |
| 16 #include "content/browser/download/download_stats.h" | 16 #include "content/browser/download/download_stats.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
| 19 #include "crypto/secure_hash.h" | 19 #include "crypto/secure_hash.h" |
| 20 #include "net/base/file_stream.h" | 20 #include "net/base/file_stream.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 // This will initialize the entire array to zero. | 25 // This will initialize the entire array to zero. |
| 26 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; | 26 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; |
| 27 | 27 |
| 28 BaseFile::BaseFile(const FilePath& full_path, | 28 BaseFile::BaseFile(const base::FilePath& full_path, |
| 29 const GURL& source_url, | 29 const GURL& source_url, |
| 30 const GURL& referrer_url, | 30 const GURL& referrer_url, |
| 31 int64 received_bytes, | 31 int64 received_bytes, |
| 32 bool calculate_hash, | 32 bool calculate_hash, |
| 33 const std::string& hash_state_bytes, | 33 const std::string& hash_state_bytes, |
| 34 scoped_ptr<net::FileStream> file_stream, | 34 scoped_ptr<net::FileStream> file_stream, |
| 35 const net::BoundNetLog& bound_net_log) | 35 const net::BoundNetLog& bound_net_log) |
| 36 : full_path_(full_path), | 36 : full_path_(full_path), |
| 37 source_url_(source_url), | 37 source_url_(source_url), |
| 38 referrer_url_(referrer_url), | 38 referrer_url_(referrer_url), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 | 56 |
| 57 BaseFile::~BaseFile() { | 57 BaseFile::~BaseFile() { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 58 DCHECK(BrowserThread::CurrentlyOn(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 FilePath& default_directory) { | 66 const base::FilePath& default_directory) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 68 DCHECK(!detached_); | 68 DCHECK(!detached_); |
| 69 | 69 |
| 70 if (file_stream_.get()) { | 70 if (file_stream_.get()) { |
| 71 file_stream_->SetBoundNetLogSource(bound_net_log_); | 71 file_stream_->SetBoundNetLogSource(bound_net_log_); |
| 72 file_stream_->EnableErrorStatistics(); | 72 file_stream_->EnableErrorStatistics(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 if (full_path_.empty()) { | 75 if (full_path_.empty()) { |
| 76 FilePath initial_directory(default_directory); | 76 base::FilePath initial_directory(default_directory); |
| 77 FilePath temp_file; | 77 base::FilePath temp_file; |
| 78 if (initial_directory.empty()) { | 78 if (initial_directory.empty()) { |
| 79 initial_directory = | 79 initial_directory = |
| 80 GetContentClient()->browser()->GetDefaultDownloadDirectory(); | 80 GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
| 81 } | 81 } |
| 82 // |initial_directory| can still be empty if ContentBrowserClient returned | 82 // |initial_directory| can still be empty if ContentBrowserClient returned |
| 83 // an empty path for the downloads directory. | 83 // an empty path for the downloads directory. |
| 84 if ((initial_directory.empty() || | 84 if ((initial_directory.empty() || |
| 85 !file_util::CreateTemporaryFileInDir(initial_directory, &temp_file)) && | 85 !file_util::CreateTemporaryFileInDir(initial_directory, &temp_file)) && |
| 86 !file_util::CreateTemporaryFile(&temp_file)) { | 86 !file_util::CreateTemporaryFile(&temp_file)) { |
| 87 return LogInterruptReason("Unable to create", 0, | 87 return LogInterruptReason("Unable to create", 0, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 141 |
| 142 RecordDownloadWriteSize(data_len); | 142 RecordDownloadWriteSize(data_len); |
| 143 RecordDownloadWriteLoopCount(write_count); | 143 RecordDownloadWriteLoopCount(write_count); |
| 144 | 144 |
| 145 if (calculate_hash_) | 145 if (calculate_hash_) |
| 146 secure_hash_->Update(data, data_len); | 146 secure_hash_->Update(data, data_len); |
| 147 | 147 |
| 148 return DOWNLOAD_INTERRUPT_REASON_NONE; | 148 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 149 } | 149 } |
| 150 | 150 |
| 151 DownloadInterruptReason BaseFile::Rename(const FilePath& new_path) { | 151 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) { |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 153 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; | 153 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 154 | 154 |
| 155 // If the new path is same as the old one, there is no need to perform the | 155 // If the new path is same as the old one, there is no need to perform the |
| 156 // following renaming logic. | 156 // following renaming logic. |
| 157 if (new_path == full_path_) | 157 if (new_path == full_path_) |
| 158 return DOWNLOAD_INTERRUPT_REASON_NONE; | 158 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 159 | 159 |
| 160 // Save the information whether the download is in progress because | 160 // Save the information whether the download is in progress because |
| 161 // it will be overwritten by closing the file. | 161 // it will be overwritten by closing the file. |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 const char* operation, | 369 const char* operation, |
| 370 int os_error, | 370 int os_error, |
| 371 DownloadInterruptReason reason) { | 371 DownloadInterruptReason reason) { |
| 372 bound_net_log_.AddEvent( | 372 bound_net_log_.AddEvent( |
| 373 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, | 373 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, |
| 374 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); | 374 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); |
| 375 return reason; | 375 return reason; |
| 376 } | 376 } |
| 377 | 377 |
| 378 } // namespace content | 378 } // namespace content |
| OLD | NEW |