Chromium Code Reviews| Index: content/browser/download/save_package.cc |
| diff --git a/content/browser/download/save_package.cc b/content/browser/download/save_package.cc |
| index 627215074055513a769f17aeb11642fdad45892c..b816dd4b14d6b5d462d9c34cf15b3d6a2a84c325 100644 |
| --- a/content/browser/download/save_package.cc |
| +++ b/content/browser/download/save_package.cc |
| @@ -141,8 +141,9 @@ SavePackage::SavePackage(WebContents* web_contents, |
| wrote_to_completed_file_(false), |
| wrote_to_failed_file_(false) { |
| DCHECK(page_url_.is_valid()); |
| - DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || |
| - save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); |
| + DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| + (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| + (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| DCHECK(!saved_main_file_path_.empty() && |
| saved_main_file_path_.value().length() <= kMaxFilePathLength); |
| DCHECK(!saved_main_directory_path_.empty() && |
| @@ -281,6 +282,20 @@ bool SavePackage::Init() { |
| return false; |
| } |
| + if (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) { |
| + // Force the extension to be mht. If the extension is left as html, then |
| + // chrome will render it as html when opened instead of mhtml. |
| + // Force the extension before creating the DownloadItem. |
| + // TODO(benjhayden): Figure out how to move this back into the file picker. |
| + // If the .html file already exists, then the user will be prompted to |
| + // replace it, even thought it won't be replaced. If the .mht already |
| + // exists, then the user will not be prompted to replace it, even though it |
| + // will be replaced. The user will only be prompted to replace the .mht if |
| + // they type out the filename including .mht. |
| + saved_main_file_path_ = saved_main_file_path_.ReplaceExtension( |
| + FILE_PATH_LITERAL("mht")); |
| + } |
| + |
| // The download manager keeps ownership but adds us as an observer. |
| download_ = download_manager_->CreateSavePackageDownloadItem( |
| saved_main_file_path_, page_url_, |
| @@ -291,6 +306,9 @@ bool SavePackage::Init() { |
| // Get directory |
| DCHECK(!saved_main_directory_path_.empty()); |
| GetAllSavableResourceLinksForCurrentPage(); |
| + } else if (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) { |
| + web_contents()->GenerateMHTML(saved_main_file_path_, base::Bind( |
| + &SavePackage::MHTMLGenerated, this)); |
| } else { |
| wait_state_ = NET_FILES; |
| SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
| @@ -311,6 +329,18 @@ bool SavePackage::Init() { |
| return true; |
| } |
| +void SavePackage::MHTMLGenerated(const FilePath& path, int64 size) { |
| + if (size <= 0) { |
| + Cancel(false); |
| + } else { |
| + download_->SetTotalBytes(size); |
| + download_->OnAllDataSaved(size, DownloadItem::kEmptyFileHash); |
| + download_->UpdateProgress(size, 0, ""); |
| + wrote_to_completed_file_ = true; |
| + Finish(); |
| + } |
| +} |
| + |
| // On POSIX, the length of |pure_file_name| + |file_name_ext| is further |
| // restricted by NAME_MAX. The maximum allowed path looks like: |
| // '/path/to/save_dir' + '/' + NAME_MAX. |
| @@ -706,8 +736,10 @@ void SavePackage::Finish() { |
| save_ids)); |
| if (download_) { |
| - download_->OnAllDataSaved(all_save_items_count_, |
| - DownloadItem::kEmptyFileHash); |
| + if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) { |
|
Randy Smith (Not in Mondays)
2012/04/16 17:45:47
I'm somewhat torn about this and related logic. I
benjhayden
2012/04/16 19:08:06
Done.
|
| + download_->OnAllDataSaved(all_save_items_count_, |
|
Randy Smith (Not in Mondays)
2012/04/16 17:45:47
nit: If there's an easy, not too ugly way to do it
benjhayden
2012/04/16 19:08:06
Done.
|
| + DownloadItem::kEmptyFileHash); |
| + } |
| download_->MarkAsComplete(); |
| FinalizeDownloadEntry(); |
| } |
| @@ -775,8 +807,9 @@ void SavePackage::SaveFailed(const GURL& save_url) { |
| if (download_) |
| download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); |
| - if (save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || |
| - save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { |
| + if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| + (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| + (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { |
| // We got error when saving page. Treat it as disk error. |
| Cancel(true); |
| } |
| @@ -882,9 +915,10 @@ void SavePackage::DoSavingProcess() { |
| DCHECK(wait_state_ == HTML_DATA); |
| } |
| } else { |
| - // Save as HTML only. |
| + // Save as HTML only or MHTML. |
| DCHECK(wait_state_ == NET_FILES); |
| - DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML); |
| + DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| + (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML)); |
| if (waiting_item_queue_.size()) { |
| DCHECK(all_save_items_count_ == waiting_item_queue_.size()); |
| SaveNextFile(false); |