| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/i18n/file_util_icu.h" | 10 #include "base/i18n/file_util_icu.h" |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 Profile* profile = tab_contents_->profile(); | 297 Profile* profile = tab_contents_->profile(); |
| 298 if (!profile) { | 298 if (!profile) { |
| 299 NOTREACHED(); | 299 NOTREACHED(); |
| 300 return false; | 300 return false; |
| 301 } | 301 } |
| 302 | 302 |
| 303 request_context_getter_ = profile->GetRequestContext(); | 303 request_context_getter_ = profile->GetRequestContext(); |
| 304 | 304 |
| 305 // Create the fake DownloadItem and display the view. | 305 // Create the fake DownloadItem and display the view. |
| 306 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, GURL(), | 306 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, GURL(), |
| 307 "", FilePath(), Time::Now(), 0, -1, -1, false, false, false); | 307 "", FilePath(), Time::Now(), 0, -1, -1, false, false, false, false); |
| 308 download_->set_manager(tab_contents_->profile()->GetDownloadManager()); | 308 download_->set_manager(tab_contents_->profile()->GetDownloadManager()); |
| 309 tab_contents_->OnStartDownload(download_); | 309 tab_contents_->OnStartDownload(download_); |
| 310 | 310 |
| 311 // Check save type and process the save page job. | 311 // Check save type and process the save page job. |
| 312 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 312 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
| 313 // Get directory | 313 // Get directory |
| 314 DCHECK(!saved_main_directory_path_.empty()); | 314 DCHECK(!saved_main_directory_path_.empty()); |
| 315 GetAllSavableResourceLinksForCurrentPage(); | 315 GetAllSavableResourceLinksForCurrentPage(); |
| 316 } else { | 316 } else { |
| 317 wait_state_ = NET_FILES; | 317 wait_state_ = NET_FILES; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 328 all_save_items_count_ = 1; | 328 all_save_items_count_ = 1; |
| 329 download_->set_total_bytes(1); | 329 download_->set_total_bytes(1); |
| 330 | 330 |
| 331 DoSavingProcess(); | 331 DoSavingProcess(); |
| 332 } | 332 } |
| 333 | 333 |
| 334 return true; | 334 return true; |
| 335 } | 335 } |
| 336 | 336 |
| 337 // Generate name for saving resource. | 337 // Generate name for saving resource. |
| 338 bool SavePackage::GenerateFilename(const std::string& disposition, | 338 bool SavePackage::GenerateFileName(const std::string& disposition, |
| 339 const GURL& url, | 339 const GURL& url, |
| 340 bool need_html_ext, | 340 bool need_html_ext, |
| 341 FilePath::StringType* generated_name) { | 341 FilePath::StringType* generated_name) { |
| 342 // TODO(jungshik): Figure out the referrer charset when having one | 342 // TODO(jungshik): Figure out the referrer charset when having one |
| 343 // makes sense and pass it to GetSuggestedFilename. | 343 // makes sense and pass it to GetSuggestedFilename. |
| 344 FilePath file_path = net::GetSuggestedFilename(url, disposition, "", | 344 FilePath file_path = net::GetSuggestedFilename(url, disposition, "", |
| 345 FilePath(kDefaultSaveName)); | 345 FilePath(kDefaultSaveName)); |
| 346 | 346 |
| 347 DCHECK(!file_path.empty()); | 347 DCHECK(!file_path.empty()); |
| 348 FilePath::StringType pure_file_name = | 348 FilePath::StringType pure_file_name = |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 // save directory, or prompting the user. | 441 // save directory, or prompting the user. |
| 442 DCHECK(!save_item->has_final_name()); | 442 DCHECK(!save_item->has_final_name()); |
| 443 if (info->url != page_url_) { | 443 if (info->url != page_url_) { |
| 444 FilePath::StringType generated_name; | 444 FilePath::StringType generated_name; |
| 445 // For HTML resource file, make sure it will have .htm as extension name, | 445 // For HTML resource file, make sure it will have .htm as extension name, |
| 446 // otherwise, when you open the saved page in Chrome again, download | 446 // otherwise, when you open the saved page in Chrome again, download |
| 447 // file manager will treat it as downloadable resource, and download it | 447 // file manager will treat it as downloadable resource, and download it |
| 448 // instead of opening it as HTML. | 448 // instead of opening it as HTML. |
| 449 bool need_html_ext = | 449 bool need_html_ext = |
| 450 info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_DOM; | 450 info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_DOM; |
| 451 if (!GenerateFilename(info->content_disposition, | 451 if (!GenerateFileName(info->content_disposition, |
| 452 GURL(info->url), | 452 GURL(info->url), |
| 453 need_html_ext, | 453 need_html_ext, |
| 454 &generated_name)) { | 454 &generated_name)) { |
| 455 // We can not generate file name for this SaveItem, so we cancel the | 455 // We can not generate file name for this SaveItem, so we cancel the |
| 456 // saving page job if the save source is from serialized DOM data. | 456 // saving page job if the save source is from serialized DOM data. |
| 457 // Otherwise, it means this SaveItem is sub-resource type, we treat it | 457 // Otherwise, it means this SaveItem is sub-resource type, we treat it |
| 458 // as an error happened on saving. We can ignore this type error for | 458 // as an error happened on saving. We can ignore this type error for |
| 459 // sub-resource links which will be resolved as absolute links instead | 459 // sub-resource links which will be resolved as absolute links instead |
| 460 // of local links in final saved contents. | 460 // of local links in final saved contents. |
| 461 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) | 461 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) |
| 462 Cancel(true); | 462 Cancel(true); |
| 463 else | 463 else |
| 464 SaveFinished(save_item->save_id(), 0, false); | 464 SaveFinished(save_item->save_id(), 0, false); |
| 465 return; | 465 return; |
| 466 } | 466 } |
| 467 | 467 |
| 468 // When saving page as only-HTML, we only have a SaveItem whose url | 468 // When saving page as only-HTML, we only have a SaveItem whose url |
| 469 // must be page_url_. | 469 // must be page_url_. |
| 470 DCHECK(save_type_ == SAVE_AS_COMPLETE_HTML); | 470 DCHECK(save_type_ == SAVE_AS_COMPLETE_HTML); |
| 471 DCHECK(!saved_main_directory_path_.empty()); | 471 DCHECK(!saved_main_directory_path_.empty()); |
| 472 | 472 |
| 473 // Now we get final name retrieved from GenerateFilename, we will use it | 473 // Now we get final name retrieved from GenerateFileName, we will use it |
| 474 // rename the SaveItem. | 474 // rename the SaveItem. |
| 475 FilePath final_name = saved_main_directory_path_.Append(generated_name); | 475 FilePath final_name = saved_main_directory_path_.Append(generated_name); |
| 476 save_item->Rename(final_name); | 476 save_item->Rename(final_name); |
| 477 } else { | 477 } else { |
| 478 // It is the main HTML file, use the name chosen by the user. | 478 // It is the main HTML file, use the name chosen by the user. |
| 479 save_item->Rename(saved_main_file_path_); | 479 save_item->Rename(saved_main_file_path_); |
| 480 } | 480 } |
| 481 | 481 |
| 482 // If the save source is from file system, inform SaveFileManager to copy | 482 // If the save source is from file system, inform SaveFileManager to copy |
| 483 // corresponding file to the file path which this SaveItem specifies. | 483 // corresponding file to the file path which this SaveItem specifies. |
| (...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1165 } | 1165 } |
| 1166 | 1166 |
| 1167 // Called after the save file dialog box returns. | 1167 // Called after the save file dialog box returns. |
| 1168 void SavePackage::ContinueSave(SavePackageParam* param, | 1168 void SavePackage::ContinueSave(SavePackageParam* param, |
| 1169 const FilePath& final_name, | 1169 const FilePath& final_name, |
| 1170 int index) { | 1170 int index) { |
| 1171 // Ensure the filename is safe. | 1171 // Ensure the filename is safe. |
| 1172 param->saved_main_file_path = final_name; | 1172 param->saved_main_file_path = final_name; |
| 1173 DownloadManager* dlm = tab_contents_->profile()->GetDownloadManager(); | 1173 DownloadManager* dlm = tab_contents_->profile()->GetDownloadManager(); |
| 1174 DCHECK(dlm); | 1174 DCHECK(dlm); |
| 1175 dlm->GenerateSafeFilename(param->current_tab_mime_type, | 1175 dlm->GenerateSafeFileName(param->current_tab_mime_type, |
| 1176 ¶m->saved_main_file_path); | 1176 ¶m->saved_main_file_path); |
| 1177 | 1177 |
| 1178 // The option index is not zero-based. | 1178 // The option index is not zero-based. |
| 1179 DCHECK(index > 0 && index < 3); | 1179 DCHECK(index > 0 && index < 3); |
| 1180 param->dir = param->saved_main_file_path.DirName(); | 1180 param->dir = param->saved_main_file_path.DirName(); |
| 1181 | 1181 |
| 1182 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | 1182 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| 1183 StringPrefMember save_file_path; | 1183 StringPrefMember save_file_path; |
| 1184 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); | 1184 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); |
| 1185 // If user change the default saving directory, we will remember it just | 1185 // If user change the default saving directory, we will remember it just |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1264 int index, void* params) { | 1264 int index, void* params) { |
| 1265 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1265 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1266 ContinueSave(save_params, path, index); | 1266 ContinueSave(save_params, path, index); |
| 1267 delete save_params; | 1267 delete save_params; |
| 1268 } | 1268 } |
| 1269 | 1269 |
| 1270 void SavePackage::FileSelectionCanceled(void* params) { | 1270 void SavePackage::FileSelectionCanceled(void* params) { |
| 1271 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1271 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1272 delete save_params; | 1272 delete save_params; |
| 1273 } | 1273 } |
| OLD | NEW |