| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 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::Rename(const FilePath& full_path, | 101 void DownloadFileImpl::RenameAndUniquify( |
| 102 bool overwrite_existing_file, | 102 const FilePath& full_path, const RenameCompletionCallback& callback) { |
| 103 const RenameCompletionCallback& callback) { | |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 105 | 104 |
| 106 FilePath new_path(full_path); | 105 FilePath new_path(full_path); |
| 107 if (!overwrite_existing_file) { | 106 |
| 108 // Make the file unique if requested. | 107 int uniquifier = |
| 109 int uniquifier = | 108 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); |
| 110 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); | 109 if (uniquifier > 0) { |
| 111 if (uniquifier > 0) { | 110 new_path = new_path.InsertBeforeExtensionASCII( |
| 112 new_path = new_path.InsertBeforeExtensionASCII( | 111 StringPrintf(" (%d)", uniquifier)); |
| 113 StringPrintf(" (%d)", uniquifier)); | |
| 114 } | |
| 115 } | 112 } |
| 116 | 113 |
| 117 DownloadInterruptReason reason = file_.Rename(new_path); | 114 DownloadInterruptReason reason = file_.Rename(new_path); |
| 118 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { | 115 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 119 // Make sure our information is updated, since we're about to | 116 // Make sure our information is updated, since we're about to |
| 120 // error out. | 117 // error out. |
| 121 SendUpdate(); | 118 SendUpdate(); |
| 122 | 119 |
| 123 // Null out callback so that we don't do any more stream processing. | 120 // Null out callback so that we don't do any more stream processing. |
| 124 stream_reader_->RegisterCallback(base::Closure()); | 121 stream_reader_->RegisterCallback(base::Closure()); |
| 125 | 122 |
| 126 new_path.clear(); | 123 new_path.clear(); |
| 127 } | 124 } |
| 128 | 125 |
| 129 BrowserThread::PostTask( | 126 BrowserThread::PostTask( |
| 130 BrowserThread::UI, FROM_HERE, | 127 BrowserThread::UI, FROM_HERE, |
| 131 base::Bind(callback, reason, new_path)); | 128 base::Bind(callback, reason, new_path)); |
| 132 } | 129 } |
| 133 | 130 |
| 134 void DownloadFileImpl::Detach(const DetachCompletionCallback& callback) { | 131 void DownloadFileImpl::RenameAndAnnotate( |
| 135 // Doing the annotation here leaves a small window during | 132 const FilePath& full_path, const RenameCompletionCallback& callback) { |
| 136 // which the file has the final name but hasn't been marked with the | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 137 // Mark Of The Web. However, it allows anti-virus scanners on Windows | 134 |
| 138 // to actually see the data (http://crbug.com/127999), and the Window | 135 FilePath new_path(full_path); |
| 139 // is pretty small (round trip to the UI thread). | 136 |
| 140 DownloadInterruptReason interrupt_reason = | 137 DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 141 file_.AnnotateWithSourceInformation(); | 138 // Short circuit null rename. |
| 139 if (full_path != file_.full_path()) |
| 140 reason = file_.Rename(new_path); |
| 141 |
| 142 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 143 // Doing the annotation after the rename rather than before leaves |
| 144 // 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 |
| 146 // anti-virus scanners on Windows to actually see the data |
| 147 // (http://crbug.com/127999) under the correct name (which is information |
| 148 // it uses). |
| 149 reason = file_.AnnotateWithSourceInformation(); |
| 150 } |
| 151 |
| 152 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 153 // Make sure our information is updated, since we're about to |
| 154 // error out. |
| 155 SendUpdate(); |
| 156 |
| 157 // Null out callback so that we don't do any more stream processing. |
| 158 stream_reader_->RegisterCallback(base::Closure()); |
| 159 |
| 160 new_path.clear(); |
| 161 } |
| 162 |
| 163 BrowserThread::PostTask( |
| 164 BrowserThread::UI, FROM_HERE, |
| 165 base::Bind(callback, reason, new_path)); |
| 166 } |
| 167 |
| 168 void DownloadFileImpl::Detach() { |
| 142 file_.Detach(); | 169 file_.Detach(); |
| 143 | |
| 144 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 145 base::Bind(callback, interrupt_reason)); | |
| 146 } | 170 } |
| 147 | 171 |
| 148 void DownloadFileImpl::Cancel() { | 172 void DownloadFileImpl::Cancel() { |
| 149 file_.Cancel(); | 173 file_.Cancel(); |
| 150 } | 174 } |
| 151 | 175 |
| 152 FilePath DownloadFileImpl::FullPath() const { | 176 FilePath DownloadFileImpl::FullPath() const { |
| 153 return file_.full_path(); | 177 return file_.full_path(); |
| 154 } | 178 } |
| 155 | 179 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 base::Bind(&DownloadDestinationObserver::DestinationUpdate, | 303 base::Bind(&DownloadDestinationObserver::DestinationUpdate, |
| 280 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); | 304 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); |
| 281 } | 305 } |
| 282 | 306 |
| 283 // static | 307 // static |
| 284 int DownloadFile::GetNumberOfDownloadFiles() { | 308 int DownloadFile::GetNumberOfDownloadFiles() { |
| 285 return number_active_objects_; | 309 return number_active_objects_; |
| 286 } | 310 } |
| 287 | 311 |
| 288 } // namespace content | 312 } // namespace content |
| OLD | NEW |