| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/download/download_file_manager.h" | 5 #include "chrome/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Throttle updates to the UI thread so that a fast moving download doesn't | 29 // Throttle updates to the UI thread so that a fast moving download doesn't |
| 30 // cause it to become unresponsive (in milliseconds). | 30 // cause it to become unresponsive (in milliseconds). |
| 31 const int kUpdatePeriodMs = 500; | 31 const int kUpdatePeriodMs = 500; |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) | 35 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) |
| 36 : next_id_(0), | 36 : resource_dispatcher_host_(rdh) { |
| 37 resource_dispatcher_host_(rdh) { | |
| 38 } | 37 } |
| 39 | 38 |
| 40 DownloadFileManager::~DownloadFileManager() { | 39 DownloadFileManager::~DownloadFileManager() { |
| 41 DCHECK(downloads_.empty()); | 40 DCHECK(downloads_.empty()); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void DownloadFileManager::Shutdown() { | 43 void DownloadFileManager::Shutdown() { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 46 BrowserThread::PostTask( | 45 BrowserThread::PostTask( |
| 47 BrowserThread::FILE, FROM_HERE, | 46 BrowserThread::FILE, FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 64 // Life of |info| ends here. No more references to it after this method. | 63 // Life of |info| ends here. No more references to it after this method. |
| 65 scoped_ptr<DownloadCreateInfo> infop(info); | 64 scoped_ptr<DownloadCreateInfo> infop(info); |
| 66 | 65 |
| 67 scoped_ptr<DownloadFile> | 66 scoped_ptr<DownloadFile> |
| 68 download_file(new DownloadFile(info, download_manager)); | 67 download_file(new DownloadFile(info, download_manager)); |
| 69 if (!download_file->Initialize(get_hash)) { | 68 if (!download_file->Initialize(get_hash)) { |
| 70 info->request_handle.CancelRequest(); | 69 info->request_handle.CancelRequest(); |
| 71 return; | 70 return; |
| 72 } | 71 } |
| 73 | 72 |
| 74 int32 id = info->download_id; | 73 DownloadId global_id(download_manager, info->download_id); |
| 75 DCHECK(GetDownloadFile(id) == NULL); | 74 DCHECK(GetDownloadFile(global_id) == NULL); |
| 76 downloads_[id] = download_file.release(); | 75 downloads_[global_id] = download_file.release(); |
| 77 | 76 |
| 78 // The file is now ready, we can un-pause the request and start saving data. | 77 // The file is now ready, we can un-pause the request and start saving data. |
| 79 info->request_handle.ResumeRequest(); | 78 info->request_handle.ResumeRequest(); |
| 80 | 79 |
| 81 StartUpdateTimer(); | 80 StartUpdateTimer(); |
| 82 | 81 |
| 83 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
| 84 BrowserThread::UI, FROM_HERE, | 83 BrowserThread::UI, FROM_HERE, |
| 85 NewRunnableMethod(download_manager, | 84 NewRunnableMethod(download_manager, |
| 86 &DownloadManager::StartDownload, id)); | 85 &DownloadManager::StartDownload, info->download_id)); |
| 87 } | 86 } |
| 88 | 87 |
| 89 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { | 88 DownloadFile* DownloadFileManager::GetDownloadFile(DownloadId global_id) { |
| 90 DownloadFileMap::iterator it = downloads_.find(id); | 89 DownloadFileMap::iterator it = downloads_.find(global_id); |
| 91 return it == downloads_.end() ? NULL : it->second; | 90 return it == downloads_.end() ? NULL : it->second; |
| 92 } | 91 } |
| 93 | 92 |
| 94 void DownloadFileManager::StartUpdateTimer() { | 93 void DownloadFileManager::StartUpdateTimer() { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 96 if (!update_timer_.IsRunning()) { | 95 if (!update_timer_.IsRunning()) { |
| 97 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 96 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
| 98 this, &DownloadFileManager::UpdateInProgressDownloads); | 97 this, &DownloadFileManager::UpdateInProgressDownloads); |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 void DownloadFileManager::StopUpdateTimer() { | 101 void DownloadFileManager::StopUpdateTimer() { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 104 update_timer_.Stop(); | 103 update_timer_.Stop(); |
| 105 } | 104 } |
| 106 | 105 |
| 107 void DownloadFileManager::UpdateInProgressDownloads() { | 106 void DownloadFileManager::UpdateInProgressDownloads() { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 109 for (DownloadFileMap::iterator i = downloads_.begin(); | 108 for (DownloadFileMap::iterator i = downloads_.begin(); |
| 110 i != downloads_.end(); ++i) { | 109 i != downloads_.end(); ++i) { |
| 111 int id = i->first; | 110 DownloadId global_id = i->first; |
| 112 DownloadFile* download_file = i->second; | 111 DownloadFile* download_file = i->second; |
| 113 DownloadManager* manager = download_file->GetDownloadManager(); | 112 DownloadManager* manager = download_file->GetDownloadManager(); |
| 114 if (manager) { | 113 if (manager) { |
| 115 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 114 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 116 NewRunnableMethod(manager, &DownloadManager::UpdateDownload, | 115 NewRunnableMethod(manager, &DownloadManager::UpdateDownload, |
| 117 id, download_file->bytes_so_far())); | 116 global_id.local(), download_file->bytes_so_far())); |
| 118 } | 117 } |
| 119 } | 118 } |
| 120 } | 119 } |
| 121 | 120 |
| 122 // Called on the IO thread once the ResourceDispatcherHost has decided that a | |
| 123 // request is a download. | |
| 124 int DownloadFileManager::GetNextId() { | |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 126 return next_id_++; | |
| 127 } | |
| 128 | |
| 129 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { | 121 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 131 DCHECK(info); | 123 DCHECK(info); |
| 132 | 124 |
| 133 DownloadManager* manager = info->request_handle.GetDownloadManager(); | 125 DownloadManager* manager = info->request_handle.GetDownloadManager(); |
| 134 if (!manager) { | 126 if (!manager) { |
| 135 info->request_handle.CancelRequest(); | 127 info->request_handle.CancelRequest(); |
| 136 delete info; | 128 delete info; |
| 137 return; | 129 return; |
| 138 } | 130 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 153 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 145 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 154 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, | 146 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, |
| 155 info, make_scoped_refptr(manager), hash_needed)); | 147 info, make_scoped_refptr(manager), hash_needed)); |
| 156 } | 148 } |
| 157 | 149 |
| 158 // We don't forward an update to the UI thread here, since we want to throttle | 150 // We don't forward an update to the UI thread here, since we want to throttle |
| 159 // the UI update rate via a periodic timer. If the user has cancelled the | 151 // the UI update rate via a periodic timer. If the user has cancelled the |
| 160 // download (in the UI thread), we may receive a few more updates before the IO | 152 // download (in the UI thread), we may receive a few more updates before the IO |
| 161 // thread gets the cancel message: we just delete the data since the | 153 // thread gets the cancel message: we just delete the data since the |
| 162 // DownloadFile has been deleted. | 154 // DownloadFile has been deleted. |
| 163 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { | 155 void DownloadFileManager::UpdateDownload( |
| 156 DownloadId global_id, DownloadBuffer* buffer) { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 165 std::vector<DownloadBuffer::Contents> contents; | 158 std::vector<DownloadBuffer::Contents> contents; |
| 166 { | 159 { |
| 167 base::AutoLock auto_lock(buffer->lock); | 160 base::AutoLock auto_lock(buffer->lock); |
| 168 contents.swap(buffer->contents); | 161 contents.swap(buffer->contents); |
| 169 } | 162 } |
| 170 | 163 |
| 171 DownloadFile* download_file = GetDownloadFile(id); | 164 DownloadFile* download_file = GetDownloadFile(global_id); |
| 172 for (size_t i = 0; i < contents.size(); ++i) { | 165 for (size_t i = 0; i < contents.size(); ++i) { |
| 173 net::IOBuffer* data = contents[i].first; | 166 net::IOBuffer* data = contents[i].first; |
| 174 const int data_len = contents[i].second; | 167 const int data_len = contents[i].second; |
| 175 if (download_file) | 168 if (download_file) |
| 176 download_file->AppendDataToFile(data->data(), data_len); | 169 download_file->AppendDataToFile(data->data(), data_len); |
| 177 data->Release(); | 170 data->Release(); |
| 178 } | 171 } |
| 179 } | 172 } |
| 180 | 173 |
| 181 void DownloadFileManager::OnResponseCompleted( | 174 void DownloadFileManager::OnResponseCompleted( |
| 182 int id, | 175 DownloadId global_id, |
| 183 DownloadBuffer* buffer, | 176 DownloadBuffer* buffer, |
| 184 int os_error, | 177 int os_error, |
| 185 const std::string& security_info) { | 178 const std::string& security_info) { |
| 186 VLOG(20) << __FUNCTION__ << "()" << " id = " << id | 179 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id |
| 187 << " os_error = " << os_error | 180 << " os_error = " << os_error |
| 188 << " security_info = \"" << security_info << "\""; | 181 << " security_info = \"" << security_info << "\""; |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 190 delete buffer; | 183 delete buffer; |
| 191 DownloadFile* download_file = GetDownloadFile(id); | 184 DownloadFile* download_file = GetDownloadFile(global_id); |
| 192 if (!download_file) | 185 if (!download_file) |
| 193 return; | 186 return; |
| 194 | 187 |
| 195 download_file->Finish(); | 188 download_file->Finish(); |
| 196 | 189 |
| 197 DownloadManager* download_manager = download_file->GetDownloadManager(); | 190 DownloadManager* download_manager = download_file->GetDownloadManager(); |
| 198 if (!download_manager) { | 191 if (!download_manager) { |
| 199 CancelDownload(id); | 192 CancelDownload(global_id); |
| 200 return; | 193 return; |
| 201 } | 194 } |
| 202 | 195 |
| 203 std::string hash; | 196 std::string hash; |
| 204 if (!download_file->GetSha256Hash(&hash)) | 197 if (!download_file->GetSha256Hash(&hash)) |
| 205 hash.clear(); | 198 hash.clear(); |
| 206 | 199 |
| 207 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
| 208 BrowserThread::UI, FROM_HERE, | 201 BrowserThread::UI, FROM_HERE, |
| 209 NewRunnableMethod( | 202 NewRunnableMethod( |
| 210 download_manager, &DownloadManager::OnResponseCompleted, | 203 download_manager, &DownloadManager::OnResponseCompleted, |
| 211 id, download_file->bytes_so_far(), os_error, hash)); | 204 global_id.local(), download_file->bytes_so_far(), os_error, hash)); |
| 212 // We need to keep the download around until the UI thread has finalized | 205 // We need to keep the download around until the UI thread has finalized |
| 213 // the name. | 206 // the name. |
| 214 } | 207 } |
| 215 | 208 |
| 216 // This method will be sent via a user action, or shutdown on the UI thread, and | 209 // This method will be sent via a user action, or shutdown on the UI thread, and |
| 217 // run on the download thread. Since this message has been sent from the UI | 210 // run on the download thread. Since this message has been sent from the UI |
| 218 // thread, the download may have already completed and won't exist in our map. | 211 // thread, the download may have already completed and won't exist in our map. |
| 219 void DownloadFileManager::CancelDownload(int id) { | 212 void DownloadFileManager::CancelDownload(DownloadId global_id) { |
| 220 VLOG(20) << __FUNCTION__ << "()" << " id = " << id; | 213 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; |
| 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 222 DownloadFileMap::iterator it = downloads_.find(id); | 215 DownloadFileMap::iterator it = downloads_.find(global_id); |
| 223 if (it == downloads_.end()) | 216 if (it == downloads_.end()) |
| 224 return; | 217 return; |
| 225 | 218 |
| 226 DownloadFile* download_file = it->second; | 219 DownloadFile* download_file = it->second; |
| 227 VLOG(20) << __FUNCTION__ << "()" | 220 VLOG(20) << __FUNCTION__ << "()" |
| 228 << " download_file = " << download_file->DebugString(); | 221 << " download_file = " << download_file->DebugString(); |
| 229 download_file->Cancel(); | 222 download_file->Cancel(); |
| 230 | 223 |
| 231 EraseDownload(id); | 224 EraseDownload(global_id); |
| 232 } | 225 } |
| 233 | 226 |
| 234 void DownloadFileManager::CompleteDownload(int id) { | 227 void DownloadFileManager::CompleteDownload(DownloadId global_id) { |
| 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 236 | 229 |
| 237 if (!ContainsKey(downloads_, id)) | 230 if (!ContainsKey(downloads_, global_id)) |
| 238 return; | 231 return; |
| 239 | 232 |
| 240 DownloadFile* download_file = downloads_[id]; | 233 DownloadFile* download_file = downloads_[global_id]; |
| 241 | 234 |
| 242 VLOG(20) << " " << __FUNCTION__ << "()" | 235 VLOG(20) << " " << __FUNCTION__ << "()" |
| 243 << " id = " << id | 236 << " id = " << global_id |
| 244 << " download_file = " << download_file->DebugString(); | 237 << " download_file = " << download_file->DebugString(); |
| 245 | 238 |
| 246 download_file->Detach(); | 239 download_file->Detach(); |
| 247 | 240 |
| 248 EraseDownload(id); | 241 EraseDownload(global_id); |
| 249 } | 242 } |
| 250 | 243 |
| 251 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { | 244 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 253 DCHECK(manager); | 246 DCHECK(manager); |
| 254 | 247 |
| 255 std::set<DownloadFile*> to_remove; | 248 std::set<DownloadFile*> to_remove; |
| 256 | 249 |
| 257 for (DownloadFileMap::iterator i = downloads_.begin(); | 250 for (DownloadFileMap::iterator i = downloads_.begin(); |
| 258 i != downloads_.end(); ++i) { | 251 i != downloads_.end(); ++i) { |
| 259 DownloadFile* download_file = i->second; | 252 DownloadFile* download_file = i->second; |
| 260 if (download_file->GetDownloadManager() == manager) { | 253 if (download_file->GetDownloadManager() == manager) { |
| 261 download_file->CancelDownloadRequest(); | 254 download_file->CancelDownloadRequest(); |
| 262 to_remove.insert(download_file); | 255 to_remove.insert(download_file); |
| 263 } | 256 } |
| 264 } | 257 } |
| 265 | 258 |
| 266 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); | 259 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); |
| 267 i != to_remove.end(); ++i) { | 260 i != to_remove.end(); ++i) { |
| 268 downloads_.erase((*i)->id()); | 261 downloads_.erase(DownloadId((*i)->GetDownloadManager(), (*i)->id())); |
| 269 delete *i; | 262 delete *i; |
| 270 } | 263 } |
| 271 } | 264 } |
| 272 | 265 |
| 273 // Actions from the UI thread and run on the download thread | 266 // Actions from the UI thread and run on the download thread |
| 274 | 267 |
| 275 // The DownloadManager in the UI thread has provided an intermediate .crdownload | 268 // The DownloadManager in the UI thread has provided an intermediate .crdownload |
| 276 // name for the download specified by 'id'. Rename the in progress download. | 269 // name for the download specified by 'id'. Rename the in progress download. |
| 277 // | 270 // |
| 278 // There are 2 possible rename cases where this method can be called: | 271 // There are 2 possible rename cases where this method can be called: |
| 279 // 1. tmp -> foo.crdownload (not final, safe) | 272 // 1. tmp -> foo.crdownload (not final, safe) |
| 280 // 2. tmp-> Unconfirmed.xxx.crdownload (not final, dangerous) | 273 // 2. tmp-> Unconfirmed.xxx.crdownload (not final, dangerous) |
| 281 void DownloadFileManager::RenameInProgressDownloadFile( | 274 void DownloadFileManager::RenameInProgressDownloadFile( |
| 282 int id, const FilePath& full_path) { | 275 DownloadId global_id, const FilePath& full_path) { |
| 283 VLOG(20) << __FUNCTION__ << "()" << " id = " << id | 276 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id |
| 284 << " full_path = \"" << full_path.value() << "\""; | 277 << " full_path = \"" << full_path.value() << "\""; |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 286 | 279 |
| 287 DownloadFile* download_file = GetDownloadFile(id); | 280 DownloadFile* download_file = GetDownloadFile(global_id); |
| 288 if (!download_file) | 281 if (!download_file) |
| 289 return; | 282 return; |
| 290 | 283 |
| 291 VLOG(20) << __FUNCTION__ << "()" | 284 VLOG(20) << __FUNCTION__ << "()" |
| 292 << " download_file = " << download_file->DebugString(); | 285 << " download_file = " << download_file->DebugString(); |
| 293 | 286 |
| 294 if (!download_file->Rename(full_path)) { | 287 if (!download_file->Rename(full_path)) { |
| 295 // Error. Between the time the UI thread generated 'full_path' to the time | 288 // Error. Between the time the UI thread generated 'full_path' to the time |
| 296 // this code runs, something happened that prevents us from renaming. | 289 // this code runs, something happened that prevents us from renaming. |
| 297 CancelDownloadOnRename(id); | 290 CancelDownloadOnRename(global_id); |
| 298 } | 291 } |
| 299 } | 292 } |
| 300 | 293 |
| 301 // The DownloadManager in the UI thread has provided a final name for the | 294 // The DownloadManager in the UI thread has provided a final name for the |
| 302 // download specified by 'id'. Rename the download that's in the process | 295 // download specified by 'id'. Rename the download that's in the process |
| 303 // of completing. | 296 // of completing. |
| 304 // | 297 // |
| 305 // There are 2 possible rename cases where this method can be called: | 298 // There are 2 possible rename cases where this method can be called: |
| 306 // 1. foo.crdownload -> foo (final, safe) | 299 // 1. foo.crdownload -> foo (final, safe) |
| 307 // 2. Unconfirmed.xxx.crdownload -> xxx (final, validated) | 300 // 2. Unconfirmed.xxx.crdownload -> xxx (final, validated) |
| 308 void DownloadFileManager::RenameCompletingDownloadFile( | 301 void DownloadFileManager::RenameCompletingDownloadFile( |
| 309 int id, const FilePath& full_path, bool overwrite_existing_file) { | 302 DownloadId global_id, |
| 310 VLOG(20) << __FUNCTION__ << "()" << " id = " << id | 303 const FilePath& full_path, |
| 304 bool overwrite_existing_file) { |
| 305 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id |
| 311 << " overwrite_existing_file = " << overwrite_existing_file | 306 << " overwrite_existing_file = " << overwrite_existing_file |
| 312 << " full_path = \"" << full_path.value() << "\""; | 307 << " full_path = \"" << full_path.value() << "\""; |
| 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 314 | 309 |
| 315 DownloadFile* download_file = GetDownloadFile(id); | 310 DownloadFile* download_file = GetDownloadFile(global_id); |
| 316 if (!download_file) | 311 if (!download_file) |
| 317 return; | 312 return; |
| 318 | 313 |
| 319 DCHECK(download_file->GetDownloadManager()); | 314 DCHECK(download_file->GetDownloadManager()); |
| 320 DownloadManager* download_manager = download_file->GetDownloadManager(); | 315 DownloadManager* download_manager = download_file->GetDownloadManager(); |
| 321 | 316 |
| 322 VLOG(20) << __FUNCTION__ << "()" | 317 VLOG(20) << __FUNCTION__ << "()" |
| 323 << " download_file = " << download_file->DebugString(); | 318 << " download_file = " << download_file->DebugString(); |
| 324 | 319 |
| 325 int uniquifier = 0; | 320 int uniquifier = 0; |
| 326 FilePath new_path = full_path; | 321 FilePath new_path = full_path; |
| 327 if (!overwrite_existing_file) { | 322 if (!overwrite_existing_file) { |
| 328 // Make our name unique at this point, as if a dangerous file is | 323 // Make our name unique at this point, as if a dangerous file is |
| 329 // downloading and a 2nd download is started for a file with the same | 324 // downloading and a 2nd download is started for a file with the same |
| 330 // name, they would have the same path. This is because we uniquify | 325 // name, they would have the same path. This is because we uniquify |
| 331 // the name on download start, and at that time the first file does | 326 // the name on download start, and at that time the first file does |
| 332 // not exists yet, so the second file gets the same name. | 327 // not exists yet, so the second file gets the same name. |
| 333 // This should not happen in the SAFE case, and we check for that in the UI | 328 // This should not happen in the SAFE case, and we check for that in the UI |
| 334 // thread. | 329 // thread. |
| 335 uniquifier = download_util::GetUniquePathNumber(new_path); | 330 uniquifier = download_util::GetUniquePathNumber(new_path); |
| 336 if (uniquifier > 0) { | 331 if (uniquifier > 0) { |
| 337 download_util::AppendNumberToPath(&new_path, uniquifier); | 332 download_util::AppendNumberToPath(&new_path, uniquifier); |
| 338 } | 333 } |
| 339 } | 334 } |
| 340 | 335 |
| 341 // Rename the file, overwriting if necessary. | 336 // Rename the file, overwriting if necessary. |
| 342 if (!download_file->Rename(new_path)) { | 337 if (!download_file->Rename(new_path)) { |
| 343 // Error. Between the time the UI thread generated 'full_path' to the time | 338 // Error. Between the time the UI thread generated 'full_path' to the time |
| 344 // this code runs, something happened that prevents us from renaming. | 339 // this code runs, something happened that prevents us from renaming. |
| 345 CancelDownloadOnRename(id); | 340 CancelDownloadOnRename(global_id); |
| 346 return; | 341 return; |
| 347 } | 342 } |
| 348 | 343 |
| 349 #if defined(OS_MACOSX) | 344 #if defined(OS_MACOSX) |
| 350 // Done here because we only want to do this once; see | 345 // Done here because we only want to do this once; see |
| 351 // http://crbug.com/13120 for details. | 346 // http://crbug.com/13120 for details. |
| 352 download_file->AnnotateWithSourceInformation(); | 347 download_file->AnnotateWithSourceInformation(); |
| 353 #endif | 348 #endif |
| 354 | 349 |
| 355 BrowserThread::PostTask( | 350 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
| 356 BrowserThread::UI, FROM_HERE, | 351 download_manager, &DownloadManager::OnDownloadRenamedToFinalName, |
| 357 NewRunnableMethod( | 352 global_id.local(), new_path, uniquifier)); |
| 358 download_manager, &DownloadManager::OnDownloadRenamedToFinalName, id, | |
| 359 new_path, uniquifier)); | |
| 360 } | 353 } |
| 361 | 354 |
| 362 // Called only from RenameInProgressDownloadFile and | 355 // Called only from RenameInProgressDownloadFile and |
| 363 // RenameCompletingDownloadFile on the FILE thread. | 356 // RenameCompletingDownloadFile on the FILE thread. |
| 364 void DownloadFileManager::CancelDownloadOnRename(int id) { | 357 void DownloadFileManager::CancelDownloadOnRename(DownloadId global_id) { |
| 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 366 | 359 |
| 367 DownloadFile* download_file = GetDownloadFile(id); | 360 DownloadFile* download_file = GetDownloadFile(global_id); |
| 368 if (!download_file) | 361 if (!download_file) |
| 369 return; | 362 return; |
| 370 | 363 |
| 371 DownloadManager* download_manager = download_file->GetDownloadManager(); | 364 DownloadManager* download_manager = download_file->GetDownloadManager(); |
| 372 if (!download_manager) { | 365 if (!download_manager) { |
| 373 // Without a download manager, we can't cancel the request normally, so we | 366 // Without a download manager, we can't cancel the request normally, so we |
| 374 // need to do it here. The normal path will also update the download | 367 // need to do it here. The normal path will also update the download |
| 375 // history before cancelling the request. | 368 // history before cancelling the request. |
| 376 download_file->CancelDownloadRequest(); | 369 download_file->CancelDownloadRequest(); |
| 377 return; | 370 return; |
| 378 } | 371 } |
| 379 | 372 |
| 380 BrowserThread::PostTask( | 373 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
| 381 BrowserThread::UI, FROM_HERE, | 374 download_manager, &DownloadManager::DownloadCancelled, |
| 382 NewRunnableMethod(download_manager, | 375 global_id.local())); |
| 383 &DownloadManager::DownloadCancelled, id)); | |
| 384 } | 376 } |
| 385 | 377 |
| 386 void DownloadFileManager::EraseDownload(int id) { | 378 void DownloadFileManager::EraseDownload(DownloadId global_id) { |
| 387 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 388 | 380 |
| 389 if (!ContainsKey(downloads_, id)) | 381 if (!ContainsKey(downloads_, global_id)) |
| 390 return; | 382 return; |
| 391 | 383 |
| 392 DownloadFile* download_file = downloads_[id]; | 384 DownloadFile* download_file = downloads_[global_id]; |
| 393 | 385 |
| 394 VLOG(20) << " " << __FUNCTION__ << "()" | 386 VLOG(20) << " " << __FUNCTION__ << "()" |
| 395 << " id = " << id | 387 << " id = " << global_id |
| 396 << " download_file = " << download_file->DebugString(); | 388 << " download_file = " << download_file->DebugString(); |
| 397 | 389 |
| 398 downloads_.erase(id); | 390 downloads_.erase(global_id); |
| 399 | 391 |
| 400 delete download_file; | 392 delete download_file; |
| 401 | 393 |
| 402 if (downloads_.empty()) | 394 if (downloads_.empty()) |
| 403 StopUpdateTimer(); | 395 StopUpdateTimer(); |
| 404 } | 396 } |
| OLD | NEW |