| 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/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/ssl/chrome_security_state_model_client.h" | 16 #include "chrome/browser/ssl/chrome_security_state_model_client.h" |
| 17 #include "components/security_state/security_state_model.h" | 17 #include "components/security_state/security_state_model.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/common/mhtml_generation_params.h" |
| 19 #include "net/base/filename_util.h" | 20 #include "net/base/filename_util.h" |
| 20 | 21 |
| 21 namespace offline_pages { | 22 namespace offline_pages { |
| 22 namespace { | 23 namespace { |
| 23 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); | 24 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); |
| 24 const base::FilePath::CharType kDefaultFileName[] = | 25 const base::FilePath::CharType kDefaultFileName[] = |
| 25 FILE_PATH_LITERAL("offline_page"); | 26 FILE_PATH_LITERAL("offline_page"); |
| 26 const int kTitleLengthMax = 80; | 27 const int kTitleLengthMax = 80; |
| 27 const char kMHTMLFileNameExtension[] = ".mhtml"; | 28 const char kMHTMLFileNameExtension[] = ".mhtml"; |
| 28 const char kFileNameComponentsSeparator[] = "-"; | 29 const char kFileNameComponentsSeparator[] = "-"; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 113 |
| 113 // TODO(fgorski): Figure out if the actual URL can be different at | 114 // TODO(fgorski): Figure out if the actual URL can be different at |
| 114 // the end of MHTML generation. Perhaps we should pull it out after the MHTML | 115 // the end of MHTML generation. Perhaps we should pull it out after the MHTML |
| 115 // is generated. | 116 // is generated. |
| 116 GURL url(web_contents_->GetLastCommittedURL()); | 117 GURL url(web_contents_->GetLastCommittedURL()); |
| 117 base::string16 title(web_contents_->GetTitle()); | 118 base::string16 title(web_contents_->GetTitle()); |
| 118 base::FilePath file_path( | 119 base::FilePath file_path( |
| 119 archives_dir.Append( | 120 archives_dir.Append( |
| 120 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id))); | 121 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id))); |
| 121 | 122 |
| 123 content::MHTMLGenerationParams params(file_path); |
| 124 params.cache_control_policy = |
| 125 content::MHTMLCacheControlPolicy::FAIL_FOR_NO_STORE_MAIN_FRAME; |
| 126 params.use_binary_encoding = true; |
| 127 |
| 122 web_contents_->GenerateMHTML( | 128 web_contents_->GenerateMHTML( |
| 123 file_path, true /* use_binary_encoding */, | 129 params, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, |
| 124 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, | 130 weak_ptr_factory_.GetWeakPtr(), url, file_path)); |
| 125 weak_ptr_factory_.GetWeakPtr(), url, file_path)); | |
| 126 } | 131 } |
| 127 | 132 |
| 128 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( | 133 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( |
| 129 const GURL& url, | 134 const GURL& url, |
| 130 const base::FilePath& file_path, | 135 const base::FilePath& file_path, |
| 131 int64_t file_size) { | 136 int64_t file_size) { |
| 132 if (file_size < 0) { | 137 if (file_size < 0) { |
| 133 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 138 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 134 } else { | 139 } else { |
| 135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 140 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 149 } | 154 } |
| 150 | 155 |
| 151 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { | 156 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { |
| 152 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); | 157 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 158 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, | 159 FROM_HERE, |
| 155 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0)); | 160 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0)); |
| 156 } | 161 } |
| 157 | 162 |
| 158 } // namespace offline_pages | 163 } // namespace offline_pages |
| OLD | NEW |