| 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return NULL; | 111 return NULL; |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Call from SavePackage for starting a saving job | 114 // Call from SavePackage for starting a saving job |
| 115 void SaveFileManager::SaveURL( | 115 void SaveFileManager::SaveURL( |
| 116 const GURL& url, | 116 const GURL& url, |
| 117 const Referrer& referrer, | 117 const Referrer& referrer, |
| 118 int render_process_host_id, | 118 int render_process_host_id, |
| 119 int render_view_id, | 119 int render_view_id, |
| 120 SaveFileCreateInfo::SaveFileSource save_source, | 120 SaveFileCreateInfo::SaveFileSource save_source, |
| 121 const FilePath& file_full_path, | 121 const base::FilePath& file_full_path, |
| 122 ResourceContext* context, | 122 ResourceContext* context, |
| 123 SavePackage* save_package) { | 123 SavePackage* save_package) { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 125 | 125 |
| 126 // Register a saving job. | 126 // Register a saving job. |
| 127 RegisterStartingRequest(url, save_package); | 127 RegisterStartingRequest(url, save_package); |
| 128 if (save_source == SaveFileCreateInfo::SAVE_FILE_FROM_NET) { | 128 if (save_source == SaveFileCreateInfo::SAVE_FILE_FROM_NET) { |
| 129 DCHECK(url.is_valid()); | 129 DCHECK(url.is_valid()); |
| 130 | 130 |
| 131 BrowserThread::PostTask( | 131 BrowserThread::PostTask( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 return NULL; | 182 return NULL; |
| 183 | 183 |
| 184 WebContentsImpl* contents = static_cast<WebContentsImpl*>( | 184 WebContentsImpl* contents = static_cast<WebContentsImpl*>( |
| 185 render_view_host->GetDelegate()->GetAsWebContents()); | 185 render_view_host->GetDelegate()->GetAsWebContents()); |
| 186 if (!contents) | 186 if (!contents) |
| 187 return NULL; | 187 return NULL; |
| 188 | 188 |
| 189 return contents->save_package(); | 189 return contents->save_package(); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void SaveFileManager::DeleteDirectoryOrFile(const FilePath& full_path, | 192 void SaveFileManager::DeleteDirectoryOrFile(const base::FilePath& full_path, |
| 193 bool is_dir) { | 193 bool is_dir) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 195 BrowserThread::PostTask( | 195 BrowserThread::PostTask( |
| 196 BrowserThread::FILE, FROM_HERE, | 196 BrowserThread::FILE, FROM_HERE, |
| 197 base::Bind(&SaveFileManager::OnDeleteDirectoryOrFile, | 197 base::Bind(&SaveFileManager::OnDeleteDirectoryOrFile, |
| 198 this, full_path, is_dir)); | 198 this, full_path, is_dir)); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void SaveFileManager::SendCancelRequest(int save_id) { | 201 void SaveFileManager::SendCancelRequest(int save_id) { |
| 202 // Cancel the request which has specific save id. | 202 // Cancel the request which has specific save id. |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 return; | 443 return; |
| 444 // If it has finished, just return. | 444 // If it has finished, just return. |
| 445 if (!save_file->InProgress()) | 445 if (!save_file->InProgress()) |
| 446 return; | 446 return; |
| 447 | 447 |
| 448 // Close the save file before the copy operation. | 448 // Close the save file before the copy operation. |
| 449 save_file->Finish(); | 449 save_file->Finish(); |
| 450 save_file->Detach(); | 450 save_file->Detach(); |
| 451 | 451 |
| 452 DCHECK(original_file_url.SchemeIsFile()); | 452 DCHECK(original_file_url.SchemeIsFile()); |
| 453 FilePath file_path; | 453 base::FilePath file_path; |
| 454 net::FileURLToFilePath(original_file_url, &file_path); | 454 net::FileURLToFilePath(original_file_url, &file_path); |
| 455 // If we can not get valid file path from original URL, treat it as | 455 // If we can not get valid file path from original URL, treat it as |
| 456 // disk error. | 456 // disk error. |
| 457 if (file_path.empty()) | 457 if (file_path.empty()) |
| 458 SaveFinished(save_id, original_file_url, render_process_id, false); | 458 SaveFinished(save_id, original_file_url, render_process_id, false); |
| 459 | 459 |
| 460 // Copy the local file to the temporary file. It will be renamed to its | 460 // Copy the local file to the temporary file. It will be renamed to its |
| 461 // final name later. | 461 // final name later. |
| 462 bool success = file_util::CopyFile(file_path, save_file->FullPath()); | 462 bool success = file_util::CopyFile(file_path, save_file->FullPath()); |
| 463 if (!success) | 463 if (!success) |
| 464 file_util::Delete(save_file->FullPath(), false); | 464 file_util::Delete(save_file->FullPath(), false); |
| 465 SaveFinished(save_id, original_file_url, render_process_id, success); | 465 SaveFinished(save_id, original_file_url, render_process_id, success); |
| 466 } | 466 } |
| 467 | 467 |
| 468 void SaveFileManager::OnDeleteDirectoryOrFile(const FilePath& full_path, | 468 void SaveFileManager::OnDeleteDirectoryOrFile(const base::FilePath& full_path, |
| 469 bool is_dir) { | 469 bool is_dir) { |
| 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 471 DCHECK(!full_path.empty()); | 471 DCHECK(!full_path.empty()); |
| 472 | 472 |
| 473 file_util::Delete(full_path, is_dir); | 473 file_util::Delete(full_path, is_dir); |
| 474 } | 474 } |
| 475 | 475 |
| 476 void SaveFileManager::RenameAllFiles( | 476 void SaveFileManager::RenameAllFiles( |
| 477 const FinalNameList& final_names, | 477 const FinalNameList& final_names, |
| 478 const FilePath& resource_dir, | 478 const base::FilePath& resource_dir, |
| 479 int render_process_id, | 479 int render_process_id, |
| 480 int render_view_id, | 480 int render_view_id, |
| 481 int save_package_id) { | 481 int save_package_id) { |
| 482 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 482 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 483 | 483 |
| 484 if (!resource_dir.empty() && !file_util::PathExists(resource_dir)) | 484 if (!resource_dir.empty() && !file_util::PathExists(resource_dir)) |
| 485 file_util::CreateDirectory(resource_dir); | 485 file_util::CreateDirectory(resource_dir); |
| 486 | 486 |
| 487 for (FinalNameList::const_iterator i = final_names.begin(); | 487 for (FinalNameList::const_iterator i = final_names.begin(); |
| 488 i != final_names.end(); ++i) { | 488 i != final_names.end(); ++i) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 SaveFile* save_file = it->second; | 525 SaveFile* save_file = it->second; |
| 526 DCHECK(!save_file->InProgress()); | 526 DCHECK(!save_file->InProgress()); |
| 527 file_util::Delete(save_file->FullPath(), false); | 527 file_util::Delete(save_file->FullPath(), false); |
| 528 delete save_file; | 528 delete save_file; |
| 529 save_file_map_.erase(it); | 529 save_file_map_.erase(it); |
| 530 } | 530 } |
| 531 } | 531 } |
| 532 } | 532 } |
| 533 | 533 |
| 534 } // namespace content | 534 } // namespace content |
| OLD | NEW |