| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/android/offline_pages/offline_page_mhtml_archiver.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() | 45 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() |
| 46 : web_contents_(nullptr), | 46 : web_contents_(nullptr), |
| 47 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 48 } | 48 } |
| 49 | 49 |
| 50 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { | 50 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 void OfflinePageMHTMLArchiver::CreateArchive( | 53 void OfflinePageMHTMLArchiver::CreateArchive( |
| 54 const base::FilePath& archives_dir, | 54 const base::FilePath& archives_dir, |
| 55 int64_t archive_id, | 55 const CreateArchiveParams& create_archive_params, |
| 56 const CreateArchiveCallback& callback) { | 56 const CreateArchiveCallback& callback) { |
| 57 DCHECK(callback_.is_null()); | 57 DCHECK(callback_.is_null()); |
| 58 DCHECK(!callback.is_null()); | 58 DCHECK(!callback.is_null()); |
| 59 callback_ = callback; | 59 callback_ = callback; |
| 60 | 60 |
| 61 if (HasConnectionSecurityError()) { | 61 if (HasConnectionSecurityError()) { |
| 62 ReportFailure(ArchiverResult::ERROR_SECURITY_CERTIFICATE); | 62 ReportFailure(ArchiverResult::ERROR_SECURITY_CERTIFICATE); |
| 63 return; | 63 return; |
| 64 } | 64 } |
| 65 | 65 |
| 66 GenerateMHTML(archives_dir, archive_id); | 66 GenerateMHTML(archives_dir, create_archive_params); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void OfflinePageMHTMLArchiver::GenerateMHTML( | 69 void OfflinePageMHTMLArchiver::GenerateMHTML( |
| 70 const base::FilePath& archives_dir, int64_t archive_id) { | 70 const base::FilePath& archives_dir, |
| 71 const CreateArchiveParams& create_archive_params) { |
| 71 if (archives_dir.empty()) { | 72 if (archives_dir.empty()) { |
| 72 DVLOG(1) << "Archive path was empty. Can't create archive."; | 73 DVLOG(1) << "Archive path was empty. Can't create archive."; |
| 73 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 74 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 74 return; | 75 return; |
| 75 } | 76 } |
| 76 | 77 |
| 77 if (!web_contents_) { | 78 if (!web_contents_) { |
| 78 DVLOG(1) << "WebContents is missing. Can't create archive."; | 79 DVLOG(1) << "WebContents is missing. Can't create archive."; |
| 79 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 80 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 80 return; | 81 return; |
| 81 } | 82 } |
| 82 | 83 |
| 83 if (!web_contents_->GetRenderViewHost()) { | 84 if (!web_contents_->GetRenderViewHost()) { |
| 84 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive."; | 85 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive."; |
| 85 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 86 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 | 89 |
| 89 // TODO(fgorski): Figure out if the actual URL can be different at | 90 // TODO(fgorski): Figure out if the actual URL can be different at |
| 90 // the end of MHTML generation. Perhaps we should pull it out after the MHTML | 91 // the end of MHTML generation. Perhaps we should pull it out after the MHTML |
| 91 // is generated. | 92 // is generated. |
| 92 GURL url(web_contents_->GetLastCommittedURL()); | 93 GURL url(web_contents_->GetLastCommittedURL()); |
| 93 base::string16 title(web_contents_->GetTitle()); | 94 base::string16 title(web_contents_->GetTitle()); |
| 94 base::FilePath file_path( | 95 base::FilePath file_path( |
| 95 archives_dir.Append(base::GenerateGUID()).AddExtension(kMHTMLExtension)); | 96 archives_dir.Append(base::GenerateGUID()).AddExtension(kMHTMLExtension)); |
| 96 content::MHTMLGenerationParams params(file_path); | 97 content::MHTMLGenerationParams params(file_path); |
| 97 params.use_binary_encoding = true; | 98 params.use_binary_encoding = true; |
| 99 params.remove_popup_overlay = create_archive_params.remove_popup_overlay; |
| 98 | 100 |
| 99 web_contents_->GenerateMHTML( | 101 web_contents_->GenerateMHTML( |
| 100 params, | 102 params, |
| 101 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, | 103 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, |
| 102 weak_ptr_factory_.GetWeakPtr(), url, file_path, title)); | 104 weak_ptr_factory_.GetWeakPtr(), url, file_path, title)); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( | 107 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( |
| 106 const GURL& url, | 108 const GURL& url, |
| 107 const base::FilePath& file_path, | 109 const base::FilePath& file_path, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 132 } | 134 } |
| 133 | 135 |
| 134 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { | 136 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { |
| 135 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); | 137 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); |
| 136 base::ThreadTaskRunnerHandle::Get()->PostTask( | 138 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 137 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(), | 139 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(), |
| 138 base::string16(), 0)); | 140 base::string16(), 0)); |
| 139 } | 141 } |
| 140 | 142 |
| 141 } // namespace offline_pages | 143 } // namespace offline_pages |
| OLD | NEW |