| 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "content/browser/download/save_file_manager.h" | 7 #include "content/browser/download/save_file_manager.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "content/browser/child_process_security_policy_impl.h" | 15 #include "content/browser/child_process_security_policy_impl.h" |
| 16 #include "content/browser/download/download_task_runner.h" |
| 16 #include "content/browser/download/save_file.h" | 17 #include "content/browser/download/save_file.h" |
| 17 #include "content/browser/download/save_file_resource_handler.h" | 18 #include "content/browser/download/save_file_resource_handler.h" |
| 18 #include "content/browser/download/save_package.h" | 19 #include "content/browser/download/save_package.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 20 #include "content/browser/renderer_host/render_view_host_impl.h" | 21 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 21 #include "content/browser/web_contents/web_contents_impl.h" | 22 #include "content/browser/web_contents/web_contents_impl.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 24 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/resource_context.h" | 25 #include "content/public/browser/resource_context.h" |
| 25 #include "content/public/common/previews_state.h" | 26 #include "content/public/common/previews_state.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 53 } | 54 } |
| 54 | 55 |
| 55 // static | 56 // static |
| 56 SaveFileManager* SaveFileManager::Get() { | 57 SaveFileManager* SaveFileManager::Get() { |
| 57 return g_save_file_manager; | 58 return g_save_file_manager; |
| 58 } | 59 } |
| 59 | 60 |
| 60 // Called during the browser shutdown process to clean up any state (open files, | 61 // Called during the browser shutdown process to clean up any state (open files, |
| 61 // timers) that live on the saving thread (file thread). | 62 // timers) that live on the saving thread (file thread). |
| 62 void SaveFileManager::Shutdown() { | 63 void SaveFileManager::Shutdown() { |
| 63 BrowserThread::PostTask( | 64 GetDownloadTaskRunner()->PostTask( |
| 64 BrowserThread::FILE, FROM_HERE, | 65 FROM_HERE, base::Bind(&SaveFileManager::OnShutdown, this)); |
| 65 base::Bind(&SaveFileManager::OnShutdown, this)); | |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Stop file thread operations. | 68 // Stop file thread operations. |
| 69 void SaveFileManager::OnShutdown() { | 69 void SaveFileManager::OnShutdown() { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 70 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 71 save_file_map_.clear(); | 71 save_file_map_.clear(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 SaveFile* SaveFileManager::LookupSaveFile(SaveItemId save_item_id) { | 74 SaveFile* SaveFileManager::LookupSaveFile(SaveItemId save_item_id) { |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 75 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 76 auto it = save_file_map_.find(save_item_id); | 76 auto it = save_file_map_.find(save_item_id); |
| 77 return it == save_file_map_.end() ? nullptr : it->second.get(); | 77 return it == save_file_map_.end() ? nullptr : it->second.get(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Look up a SavePackage according to a save id. | 80 // Look up a SavePackage according to a save id. |
| 81 SavePackage* SaveFileManager::LookupPackage(SaveItemId save_item_id) { | 81 SavePackage* SaveFileManager::LookupPackage(SaveItemId save_item_id) { |
| 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 83 auto it = packages_.find(save_item_id); | 83 auto it = packages_.find(save_item_id); |
| 84 if (it != packages_.end()) | 84 if (it != packages_.end()) |
| 85 return it->second; | 85 return it->second; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 113 save_item_id, save_package->id(), render_process_host_id, | 113 save_item_id, save_package->id(), render_process_host_id, |
| 114 render_view_routing_id, render_frame_routing_id, context)); | 114 render_view_routing_id, render_frame_routing_id, context)); |
| 115 } else { | 115 } else { |
| 116 // We manually start the save job. | 116 // We manually start the save job. |
| 117 SaveFileCreateInfo* info = new SaveFileCreateInfo( | 117 SaveFileCreateInfo* info = new SaveFileCreateInfo( |
| 118 file_full_path, url, save_item_id, save_package->id(), | 118 file_full_path, url, save_item_id, save_package->id(), |
| 119 render_process_host_id, render_frame_routing_id, save_source); | 119 render_process_host_id, render_frame_routing_id, save_source); |
| 120 | 120 |
| 121 // Since the data will come from render process, so we need to start | 121 // Since the data will come from render process, so we need to start |
| 122 // this kind of save job by ourself. | 122 // this kind of save job by ourself. |
| 123 BrowserThread::PostTask( | 123 GetDownloadTaskRunner()->PostTask( |
| 124 BrowserThread::FILE, FROM_HERE, | 124 FROM_HERE, base::Bind(&SaveFileManager::StartSave, this, info)); |
| 125 base::Bind(&SaveFileManager::StartSave, this, info)); | |
| 126 } | 125 } |
| 127 } | 126 } |
| 128 | 127 |
| 129 // Utility function for look up table maintenance, called on the UI thread. | 128 // Utility function for look up table maintenance, called on the UI thread. |
| 130 // A manager may have multiple save page job (SavePackage) in progress, | 129 // A manager may have multiple save page job (SavePackage) in progress, |
| 131 // so we just look up the save id and remove it from the tracking table. | 130 // so we just look up the save id and remove it from the tracking table. |
| 132 void SaveFileManager::RemoveSaveFile(SaveItemId save_item_id, | 131 void SaveFileManager::RemoveSaveFile(SaveItemId save_item_id, |
| 133 SavePackage* save_package) { | 132 SavePackage* save_package) { |
| 134 DCHECK(save_package); | 133 DCHECK(save_package); |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 154 render_frame_host)); | 153 render_frame_host)); |
| 155 if (!web_contents) | 154 if (!web_contents) |
| 156 return nullptr; | 155 return nullptr; |
| 157 | 156 |
| 158 return web_contents->save_package(); | 157 return web_contents->save_package(); |
| 159 } | 158 } |
| 160 | 159 |
| 161 void SaveFileManager::DeleteDirectoryOrFile(const base::FilePath& full_path, | 160 void SaveFileManager::DeleteDirectoryOrFile(const base::FilePath& full_path, |
| 162 bool is_dir) { | 161 bool is_dir) { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 164 BrowserThread::PostTask( | 163 GetDownloadTaskRunner()->PostTask( |
| 165 BrowserThread::FILE, FROM_HERE, | 164 FROM_HERE, base::Bind(&SaveFileManager::OnDeleteDirectoryOrFile, this, |
| 166 base::Bind(&SaveFileManager::OnDeleteDirectoryOrFile, | 165 full_path, is_dir)); |
| 167 this, full_path, is_dir)); | |
| 168 } | 166 } |
| 169 | 167 |
| 170 void SaveFileManager::SendCancelRequest(SaveItemId save_item_id) { | 168 void SaveFileManager::SendCancelRequest(SaveItemId save_item_id) { |
| 171 // Cancel the request which has specific save id. | 169 // Cancel the request which has specific save id. |
| 172 DCHECK(!save_item_id.is_null()); | 170 DCHECK(!save_item_id.is_null()); |
| 173 BrowserThread::PostTask( | 171 GetDownloadTaskRunner()->PostTask( |
| 174 BrowserThread::FILE, FROM_HERE, | 172 FROM_HERE, base::Bind(&SaveFileManager::CancelSave, this, save_item_id)); |
| 175 base::Bind(&SaveFileManager::CancelSave, this, save_item_id)); | |
| 176 } | 173 } |
| 177 | 174 |
| 178 // Notifications sent from the IO thread and run on the file thread: | 175 // Notifications sent from the IO thread and run on the file thread: |
| 179 | 176 |
| 180 // The IO thread created |info|, but the file thread (this method) uses it | 177 // The IO thread created |info|, but the file thread (this method) uses it |
| 181 // to create a SaveFile which will hold and finally destroy |info|. It will | 178 // to create a SaveFile which will hold and finally destroy |info|. It will |
| 182 // then passes |info| to the UI thread for reporting saving status. | 179 // then passes |info| to the UI thread for reporting saving status. |
| 183 void SaveFileManager::StartSave(SaveFileCreateInfo* info) { | 180 void SaveFileManager::StartSave(SaveFileCreateInfo* info) { |
| 184 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 181 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 185 DCHECK(info); | 182 DCHECK(info); |
| 186 // No need to calculate hash. | 183 // No need to calculate hash. |
| 187 std::unique_ptr<SaveFile> save_file = base::MakeUnique<SaveFile>(info, false); | 184 std::unique_ptr<SaveFile> save_file = base::MakeUnique<SaveFile>(info, false); |
| 188 | 185 |
| 189 // TODO(phajdan.jr): We should check the return value and handle errors here. | 186 // TODO(phajdan.jr): We should check the return value and handle errors here. |
| 190 save_file->Initialize(); | 187 save_file->Initialize(); |
| 191 info->path = save_file->FullPath(); | 188 info->path = save_file->FullPath(); |
| 192 | 189 |
| 193 DCHECK(!LookupSaveFile(info->save_item_id)); | 190 DCHECK(!LookupSaveFile(info->save_item_id)); |
| 194 save_file_map_[info->save_item_id] = std::move(save_file); | 191 save_file_map_[info->save_item_id] = std::move(save_file); |
| 195 | 192 |
| 196 BrowserThread::PostTask( | 193 BrowserThread::PostTask( |
| 197 BrowserThread::UI, FROM_HERE, | 194 BrowserThread::UI, FROM_HERE, |
| 198 base::Bind(&SaveFileManager::OnStartSave, this, *info)); | 195 base::Bind(&SaveFileManager::OnStartSave, this, *info)); |
| 199 } | 196 } |
| 200 | 197 |
| 201 // We do forward an update to the UI thread here, since we do not use timer to | 198 // We do forward an update to the UI thread here, since we do not use timer to |
| 202 // update the UI. If the user has canceled the saving action (in the UI | 199 // update the UI. If the user has canceled the saving action (in the UI |
| 203 // thread). We may receive a few more updates before the IO thread gets the | 200 // thread). We may receive a few more updates before the IO thread gets the |
| 204 // cancel message. We just delete the data since the SaveFile has been deleted. | 201 // cancel message. We just delete the data since the SaveFile has been deleted. |
| 205 void SaveFileManager::UpdateSaveProgress(SaveItemId save_item_id, | 202 void SaveFileManager::UpdateSaveProgress(SaveItemId save_item_id, |
| 206 net::IOBuffer* data, | 203 net::IOBuffer* data, |
| 207 int data_len) { | 204 int data_len) { |
| 208 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 205 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 209 SaveFile* save_file = LookupSaveFile(save_item_id); | 206 SaveFile* save_file = LookupSaveFile(save_item_id); |
| 210 if (save_file) { | 207 if (save_file) { |
| 211 DCHECK(save_file->InProgress()); | 208 DCHECK(save_file->InProgress()); |
| 212 | 209 |
| 213 DownloadInterruptReason reason = | 210 DownloadInterruptReason reason = |
| 214 save_file->AppendDataToFile(data->data(), data_len); | 211 save_file->AppendDataToFile(data->data(), data_len); |
| 215 BrowserThread::PostTask( | 212 BrowserThread::PostTask( |
| 216 BrowserThread::UI, FROM_HERE, | 213 BrowserThread::UI, FROM_HERE, |
| 217 base::Bind(&SaveFileManager::OnUpdateSaveProgress, this, | 214 base::Bind(&SaveFileManager::OnUpdateSaveProgress, this, |
| 218 save_file->save_item_id(), save_file->BytesSoFar(), | 215 save_file->save_item_id(), save_file->BytesSoFar(), |
| 219 reason == DOWNLOAD_INTERRUPT_REASON_NONE)); | 216 reason == DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 220 } | 217 } |
| 221 } | 218 } |
| 222 | 219 |
| 223 // The IO thread will call this when saving is completed or it got error when | 220 // The IO thread will call this when saving is completed or it got error when |
| 224 // fetching data. We forward the message to OnSaveFinished in UI thread. | 221 // fetching data. We forward the message to OnSaveFinished in UI thread. |
| 225 void SaveFileManager::SaveFinished(SaveItemId save_item_id, | 222 void SaveFileManager::SaveFinished(SaveItemId save_item_id, |
| 226 SavePackageId save_package_id, | 223 SavePackageId save_package_id, |
| 227 bool is_success) { | 224 bool is_success) { |
| 228 DVLOG(20) << __func__ << "() save_item_id = " << save_item_id | 225 DVLOG(20) << __func__ << "() save_item_id = " << save_item_id |
| 229 << " save_package_id = " << save_package_id | 226 << " save_package_id = " << save_package_id |
| 230 << " is_success = " << is_success; | 227 << " is_success = " << is_success; |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 228 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 232 | 229 |
| 233 int64_t bytes_so_far = 0; | 230 int64_t bytes_so_far = 0; |
| 234 SaveFile* save_file = LookupSaveFile(save_item_id); | 231 SaveFile* save_file = LookupSaveFile(save_item_id); |
| 235 if (save_file != nullptr) { | 232 if (save_file != nullptr) { |
| 236 DCHECK(save_file->InProgress()); | 233 DCHECK(save_file->InProgress()); |
| 237 DVLOG(20) << __func__ << "() save_file = " << save_file->DebugString(); | 234 DVLOG(20) << __func__ << "() save_file = " << save_file->DebugString(); |
| 238 bytes_so_far = save_file->BytesSoFar(); | 235 bytes_so_far = save_file->BytesSoFar(); |
| 239 save_file->Finish(); | 236 save_file->Finish(); |
| 240 save_file->Detach(); | 237 save_file->Detach(); |
| 241 } else { | 238 } else { |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 } | 372 } |
| 376 | 373 |
| 377 // Notifications sent from the UI thread and run on the file thread. | 374 // Notifications sent from the UI thread and run on the file thread. |
| 378 | 375 |
| 379 // This method will be sent via a user action, or shutdown on the UI thread, | 376 // This method will be sent via a user action, or shutdown on the UI thread, |
| 380 // and run on the file thread. We don't post a message back for cancels, | 377 // and run on the file thread. We don't post a message back for cancels, |
| 381 // but we do forward the cancel to the IO thread. Since this message has been | 378 // but we do forward the cancel to the IO thread. Since this message has been |
| 382 // sent from the UI thread, the saving job may have already completed and | 379 // sent from the UI thread, the saving job may have already completed and |
| 383 // won't exist in our map. | 380 // won't exist in our map. |
| 384 void SaveFileManager::CancelSave(SaveItemId save_item_id) { | 381 void SaveFileManager::CancelSave(SaveItemId save_item_id) { |
| 385 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 382 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 386 auto it = save_file_map_.find(save_item_id); | 383 auto it = save_file_map_.find(save_item_id); |
| 387 if (it != save_file_map_.end()) { | 384 if (it != save_file_map_.end()) { |
| 388 std::unique_ptr<SaveFile> save_file = std::move(it->second); | 385 std::unique_ptr<SaveFile> save_file = std::move(it->second); |
| 389 | 386 |
| 390 if (!save_file->InProgress()) { | 387 if (!save_file->InProgress()) { |
| 391 // We've won a race with the UI thread--we finished the file before | 388 // We've won a race with the UI thread--we finished the file before |
| 392 // the UI thread cancelled it on us. Unfortunately, in this situation | 389 // the UI thread cancelled it on us. Unfortunately, in this situation |
| 393 // the cancel wins, so we need to delete the now detached file. | 390 // the cancel wins, so we need to delete the now detached file. |
| 394 base::DeleteFile(save_file->FullPath(), false); | 391 base::DeleteFile(save_file->FullPath(), false); |
| 395 } else if (save_file->save_source() == | 392 } else if (save_file->save_source() == |
| 396 SaveFileCreateInfo::SAVE_FILE_FROM_NET) { | 393 SaveFileCreateInfo::SAVE_FILE_FROM_NET) { |
| 397 // If the data comes from the net IO thread and hasn't completed | 394 // If the data comes from the net IO thread and hasn't completed |
| 398 // yet, then forward the cancel message to IO thread & cancel the | 395 // yet, then forward the cancel message to IO thread & cancel the |
| 399 // save locally. If the data doesn't come from the IO thread, | 396 // save locally. If the data doesn't come from the IO thread, |
| 400 // we can ignore the message. | 397 // we can ignore the message. |
| 401 BrowserThread::PostTask( | 398 BrowserThread::PostTask( |
| 402 BrowserThread::IO, FROM_HERE, | 399 BrowserThread::IO, FROM_HERE, |
| 403 base::Bind(&SaveFileManager::ExecuteCancelSaveRequest, this, | 400 base::Bind(&SaveFileManager::ExecuteCancelSaveRequest, this, |
| 404 save_file->render_process_id(), save_file->request_id())); | 401 save_file->render_process_id(), save_file->request_id())); |
| 405 } | 402 } |
| 406 | 403 |
| 407 // Whatever the save file is complete or not, just delete it. This | 404 // Whatever the save file is complete or not, just delete it. This |
| 408 // will delete the underlying file if InProgress() is true. | 405 // will delete the underlying file if InProgress() is true. |
| 409 save_file_map_.erase(it); | 406 save_file_map_.erase(it); |
| 410 } | 407 } |
| 411 } | 408 } |
| 412 | 409 |
| 413 void SaveFileManager::OnDeleteDirectoryOrFile(const base::FilePath& full_path, | 410 void SaveFileManager::OnDeleteDirectoryOrFile(const base::FilePath& full_path, |
| 414 bool is_dir) { | 411 bool is_dir) { |
| 415 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 412 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 416 DCHECK(!full_path.empty()); | 413 DCHECK(!full_path.empty()); |
| 417 | 414 |
| 418 base::DeleteFile(full_path, is_dir); | 415 base::DeleteFile(full_path, is_dir); |
| 419 } | 416 } |
| 420 | 417 |
| 421 void SaveFileManager::RenameAllFiles(const FinalNamesMap& final_names, | 418 void SaveFileManager::RenameAllFiles(const FinalNamesMap& final_names, |
| 422 const base::FilePath& resource_dir, | 419 const base::FilePath& resource_dir, |
| 423 int render_process_id, | 420 int render_process_id, |
| 424 int render_frame_routing_id, | 421 int render_frame_routing_id, |
| 425 SavePackageId save_package_id) { | 422 SavePackageId save_package_id) { |
| 426 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 423 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 427 | 424 |
| 428 if (!resource_dir.empty() && !base::PathExists(resource_dir)) | 425 if (!resource_dir.empty() && !base::PathExists(resource_dir)) |
| 429 base::CreateDirectory(resource_dir); | 426 base::CreateDirectory(resource_dir); |
| 430 | 427 |
| 431 for (const auto& i : final_names) { | 428 for (const auto& i : final_names) { |
| 432 SaveItemId save_item_id = i.first; | 429 SaveItemId save_item_id = i.first; |
| 433 const base::FilePath& final_name = i.second; | 430 const base::FilePath& final_name = i.second; |
| 434 | 431 |
| 435 auto it = save_file_map_.find(save_item_id); | 432 auto it = save_file_map_.find(save_item_id); |
| 436 if (it != save_file_map_.end()) { | 433 if (it != save_file_map_.end()) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 454 | 451 |
| 455 SavePackage* save_package = | 452 SavePackage* save_package = |
| 456 GetSavePackageFromRenderIds(render_process_id, render_frame_routing_id); | 453 GetSavePackageFromRenderIds(render_process_id, render_frame_routing_id); |
| 457 | 454 |
| 458 if (save_package && save_package->id() == save_package_id) | 455 if (save_package && save_package->id() == save_package_id) |
| 459 save_package->Finish(); | 456 save_package->Finish(); |
| 460 } | 457 } |
| 461 | 458 |
| 462 void SaveFileManager::RemoveSavedFileFromFileMap( | 459 void SaveFileManager::RemoveSavedFileFromFileMap( |
| 463 const std::vector<SaveItemId>& save_item_ids) { | 460 const std::vector<SaveItemId>& save_item_ids) { |
| 464 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 461 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 465 | 462 |
| 466 for (const SaveItemId save_item_id : save_item_ids) { | 463 for (const SaveItemId save_item_id : save_item_ids) { |
| 467 auto it = save_file_map_.find(save_item_id); | 464 auto it = save_file_map_.find(save_item_id); |
| 468 if (it != save_file_map_.end()) { | 465 if (it != save_file_map_.end()) { |
| 469 SaveFile* save_file = it->second.get(); | 466 SaveFile* save_file = it->second.get(); |
| 470 DCHECK(!save_file->InProgress()); | 467 DCHECK(!save_file->InProgress()); |
| 471 base::DeleteFile(save_file->FullPath(), false); | 468 base::DeleteFile(save_file->FullPath(), false); |
| 472 save_file_map_.erase(it); | 469 save_file_map_.erase(it); |
| 473 } | 470 } |
| 474 } | 471 } |
| 475 } | 472 } |
| 476 | 473 |
| 477 } // namespace content | 474 } // namespace content |
| OLD | NEW |