| 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/download_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 const int kUpdatePeriodMs = 500; | 25 const int kUpdatePeriodMs = 500; |
| 26 const int kMaxTimeBlockingFileThreadMs = 1000; | 26 const int kMaxTimeBlockingFileThreadMs = 1000; |
| 27 | 27 |
| 28 int DownloadFile::number_active_objects_ = 0; | 28 int DownloadFile::number_active_objects_ = 0; |
| 29 | 29 |
| 30 DownloadFileImpl::DownloadFileImpl( | 30 DownloadFileImpl::DownloadFileImpl( |
| 31 scoped_ptr<DownloadSaveInfo> save_info, | 31 scoped_ptr<DownloadSaveInfo> save_info, |
| 32 const FilePath& default_download_directory, | 32 const base::FilePath& default_download_directory, |
| 33 const GURL& url, | 33 const GURL& url, |
| 34 const GURL& referrer_url, | 34 const GURL& referrer_url, |
| 35 bool calculate_hash, | 35 bool calculate_hash, |
| 36 scoped_ptr<ByteStreamReader> stream, | 36 scoped_ptr<ByteStreamReader> stream, |
| 37 const net::BoundNetLog& bound_net_log, | 37 const net::BoundNetLog& bound_net_log, |
| 38 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 38 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 39 base::WeakPtr<DownloadDestinationObserver> observer) | 39 base::WeakPtr<DownloadDestinationObserver> observer) |
| 40 : file_(save_info->file_path, | 40 : file_(save_info->file_path, |
| 41 url, | 41 url, |
| 42 referrer_url, | 42 referrer_url, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 if (!update_timer_->IsRunning()) { | 93 if (!update_timer_->IsRunning()) { |
| 94 update_timer_->Start(FROM_HERE, | 94 update_timer_->Start(FROM_HERE, |
| 95 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 95 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
| 96 this, &DownloadFileImpl::SendUpdate); | 96 this, &DownloadFileImpl::SendUpdate); |
| 97 } | 97 } |
| 98 return file_.AppendDataToFile(data, data_len); | 98 return file_.AppendDataToFile(data, data_len); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void DownloadFileImpl::RenameAndUniquify( | 101 void DownloadFileImpl::RenameAndUniquify( |
| 102 const FilePath& full_path, const RenameCompletionCallback& callback) { | 102 const base::FilePath& full_path, |
| 103 const RenameCompletionCallback& callback) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 104 | 105 |
| 105 FilePath new_path(full_path); | 106 base::FilePath new_path(full_path); |
| 106 | 107 |
| 107 int uniquifier = | 108 int uniquifier = |
| 108 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); | 109 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); |
| 109 if (uniquifier > 0) { | 110 if (uniquifier > 0) { |
| 110 new_path = new_path.InsertBeforeExtensionASCII( | 111 new_path = new_path.InsertBeforeExtensionASCII( |
| 111 StringPrintf(" (%d)", uniquifier)); | 112 StringPrintf(" (%d)", uniquifier)); |
| 112 } | 113 } |
| 113 | 114 |
| 114 DownloadInterruptReason reason = file_.Rename(new_path); | 115 DownloadInterruptReason reason = file_.Rename(new_path); |
| 115 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { | 116 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 116 // Make sure our information is updated, since we're about to | 117 // Make sure our information is updated, since we're about to |
| 117 // error out. | 118 // error out. |
| 118 SendUpdate(); | 119 SendUpdate(); |
| 119 | 120 |
| 120 // Null out callback so that we don't do any more stream processing. | 121 // Null out callback so that we don't do any more stream processing. |
| 121 stream_reader_->RegisterCallback(base::Closure()); | 122 stream_reader_->RegisterCallback(base::Closure()); |
| 122 | 123 |
| 123 new_path.clear(); | 124 new_path.clear(); |
| 124 } | 125 } |
| 125 | 126 |
| 126 BrowserThread::PostTask( | 127 BrowserThread::PostTask( |
| 127 BrowserThread::UI, FROM_HERE, | 128 BrowserThread::UI, FROM_HERE, |
| 128 base::Bind(callback, reason, new_path)); | 129 base::Bind(callback, reason, new_path)); |
| 129 } | 130 } |
| 130 | 131 |
| 131 void DownloadFileImpl::RenameAndAnnotate( | 132 void DownloadFileImpl::RenameAndAnnotate( |
| 132 const FilePath& full_path, const RenameCompletionCallback& callback) { | 133 const base::FilePath& full_path, |
| 134 const RenameCompletionCallback& callback) { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 134 | 136 |
| 135 FilePath new_path(full_path); | 137 base::FilePath new_path(full_path); |
| 136 | 138 |
| 137 DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE; | 139 DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 138 // Short circuit null rename. | 140 // Short circuit null rename. |
| 139 if (full_path != file_.full_path()) | 141 if (full_path != file_.full_path()) |
| 140 reason = file_.Rename(new_path); | 142 reason = file_.Rename(new_path); |
| 141 | 143 |
| 142 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { | 144 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 143 // Doing the annotation after the rename rather than before leaves | 145 // Doing the annotation after the rename rather than before leaves |
| 144 // a very small window during which the file has the final name but | 146 // a very small window during which the file has the final name but |
| 145 // hasn't been marked with the Mark Of The Web. However, it allows | 147 // hasn't been marked with the Mark Of The Web. However, it allows |
| (...skipping 20 matching lines...) Expand all Loading... |
| 166 } | 168 } |
| 167 | 169 |
| 168 void DownloadFileImpl::Detach() { | 170 void DownloadFileImpl::Detach() { |
| 169 file_.Detach(); | 171 file_.Detach(); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void DownloadFileImpl::Cancel() { | 174 void DownloadFileImpl::Cancel() { |
| 173 file_.Cancel(); | 175 file_.Cancel(); |
| 174 } | 176 } |
| 175 | 177 |
| 176 FilePath DownloadFileImpl::FullPath() const { | 178 base::FilePath DownloadFileImpl::FullPath() const { |
| 177 return file_.full_path(); | 179 return file_.full_path(); |
| 178 } | 180 } |
| 179 | 181 |
| 180 bool DownloadFileImpl::InProgress() const { | 182 bool DownloadFileImpl::InProgress() const { |
| 181 return file_.in_progress(); | 183 return file_.in_progress(); |
| 182 } | 184 } |
| 183 | 185 |
| 184 int64 DownloadFileImpl::BytesSoFar() const { | 186 int64 DownloadFileImpl::BytesSoFar() const { |
| 185 return file_.bytes_so_far(); | 187 return file_.bytes_so_far(); |
| 186 } | 188 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 base::Bind(&DownloadDestinationObserver::DestinationUpdate, | 305 base::Bind(&DownloadDestinationObserver::DestinationUpdate, |
| 304 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); | 306 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); |
| 305 } | 307 } |
| 306 | 308 |
| 307 // static | 309 // static |
| 308 int DownloadFile::GetNumberOfDownloadFiles() { | 310 int DownloadFile::GetNumberOfDownloadFiles() { |
| 309 return number_active_objects_; | 311 return number_active_objects_; |
| 310 } | 312 } |
| 311 | 313 |
| 312 } // namespace content | 314 } // namespace content |
| OLD | NEW |