Chromium Code Reviews| 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/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sha1.h" | 11 #include "base/sha1.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 16 #include "net/base/filename_util.h" | 17 #include "net/base/filename_util.h" |
| 17 | 18 |
| 18 namespace offline_pages { | 19 namespace offline_pages { |
| 19 namespace { | 20 namespace { |
| 20 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); | 21 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); |
| 21 const base::FilePath::CharType kDefaultFileName[] = | 22 const base::FilePath::CharType kDefaultFileName[] = |
| 22 FILE_PATH_LITERAL("offline_page"); | 23 FILE_PATH_LITERAL("offline_page"); |
| 23 const int kTitleLengthMax = 80; | 24 const int kTitleLengthMax = 80; |
| 24 const char kMHTMLFileNameExtension[] = ".mhtml"; | 25 const char kMHTMLFileNameExtension[] = ".mhtml"; |
| 25 const char kFileNameComponentsSeparator[] = "-"; | 26 const char kFileNameComponentsSeparator[] = "-"; |
| 26 const char kReplaceChars[] = " "; | 27 const char kReplaceChars[] = " "; |
| 27 const char kReplaceWith[] = "_"; | 28 const char kReplaceWith[] = "_"; |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 // static | 31 // static |
| 31 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { | 32 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { |
| 32 return kMHTMLFileNameExtension; | 33 return kMHTMLFileNameExtension; |
| 33 } | 34 } |
| 34 | 35 |
| 35 // static | 36 // static |
| 36 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( | 37 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( |
| 37 const GURL& url, | 38 const GURL& url, |
| 38 const std::string& title) { | 39 const std::string& title, |
| 40 int64_t id) { | |
| 39 std::string title_part(title.substr(0, kTitleLengthMax)); | 41 std::string title_part(title.substr(0, kTitleLengthMax)); |
| 40 std::string url_hash(base::SHA1HashString(url.spec())); | 42 std::string url_hash(base::SHA1HashString(url.spec())); |
| 41 base::Base64Encode(url_hash, &url_hash); | 43 base::Base64Encode(url_hash, &url_hash); |
|
fgorski
2016/03/30 01:43:29
if we are adding offline_id here, do we still need
jianli
2016/03/31 21:02:42
Done.
| |
| 42 std::string suggested_name(url.host() + kFileNameComponentsSeparator + | 44 std::string suggested_name( |
| 43 title_part + kFileNameComponentsSeparator + | 45 url.host() + kFileNameComponentsSeparator + |
| 44 url_hash); | 46 title_part + kFileNameComponentsSeparator + |
| 47 base::Int64ToString(id) + kFileNameComponentsSeparator + | |
| 48 url_hash); | |
| 45 | 49 |
| 46 // Substitute spaces out from title. | 50 // Substitute spaces out from title. |
| 47 base::ReplaceChars(suggested_name, kReplaceChars, kReplaceWith, | 51 base::ReplaceChars(suggested_name, kReplaceChars, kReplaceWith, |
| 48 &suggested_name); | 52 &suggested_name); |
| 49 | 53 |
| 50 return net::GenerateFileName(url, | 54 return net::GenerateFileName(url, |
| 51 std::string(), // content disposition | 55 std::string(), // content disposition |
| 52 std::string(), // charset | 56 std::string(), // charset |
| 53 suggested_name, | 57 suggested_name, |
| 54 std::string(), // mime-type | 58 std::string(), // mime-type |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 66 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() | 70 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() |
| 67 : web_contents_(nullptr), | 71 : web_contents_(nullptr), |
| 68 weak_ptr_factory_(this) { | 72 weak_ptr_factory_(this) { |
| 69 } | 73 } |
| 70 | 74 |
| 71 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { | 75 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { |
| 72 } | 76 } |
| 73 | 77 |
| 74 void OfflinePageMHTMLArchiver::CreateArchive( | 78 void OfflinePageMHTMLArchiver::CreateArchive( |
| 75 const base::FilePath& archives_dir, | 79 const base::FilePath& archives_dir, |
| 80 int64_t archive_id, | |
| 76 const CreateArchiveCallback& callback) { | 81 const CreateArchiveCallback& callback) { |
| 77 DCHECK(callback_.is_null()); | 82 DCHECK(callback_.is_null()); |
| 78 DCHECK(!callback.is_null()); | 83 DCHECK(!callback.is_null()); |
| 79 callback_ = callback; | 84 callback_ = callback; |
| 80 | 85 |
| 81 GenerateMHTML(archives_dir); | 86 GenerateMHTML(archives_dir, archive_id); |
| 82 } | 87 } |
| 83 | 88 |
| 84 void OfflinePageMHTMLArchiver::GenerateMHTML( | 89 void OfflinePageMHTMLArchiver::GenerateMHTML( |
| 85 const base::FilePath& archives_dir) { | 90 const base::FilePath& archives_dir, int64_t archive_id) { |
| 86 if (archives_dir.empty()) { | 91 if (archives_dir.empty()) { |
| 87 DVLOG(1) << "Archive path was empty. Can't create archive."; | 92 DVLOG(1) << "Archive path was empty. Can't create archive."; |
| 88 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 93 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 89 return; | 94 return; |
| 90 } | 95 } |
| 91 | 96 |
| 92 if (!web_contents_) { | 97 if (!web_contents_) { |
| 93 DVLOG(1) << "WebContents is missing. Can't create archive."; | 98 DVLOG(1) << "WebContents is missing. Can't create archive."; |
| 94 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 99 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 95 return; | 100 return; |
| 96 } | 101 } |
| 97 | 102 |
| 98 if (!web_contents_->GetRenderViewHost()) { | 103 if (!web_contents_->GetRenderViewHost()) { |
| 99 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive."; | 104 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive."; |
| 100 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 105 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 101 return; | 106 return; |
| 102 } | 107 } |
| 103 | 108 |
| 104 // TODO(fgorski): Figure out if the actual URL can be different at | 109 // TODO(fgorski): Figure out if the actual URL can be different at |
| 105 // the end of MHTML generation. Perhaps we should pull it out after the MHTML | 110 // the end of MHTML generation. Perhaps we should pull it out after the MHTML |
| 106 // is generated. | 111 // is generated. |
| 107 GURL url(web_contents_->GetLastCommittedURL()); | 112 GURL url(web_contents_->GetLastCommittedURL()); |
| 108 base::string16 title(web_contents_->GetTitle()); | 113 base::string16 title(web_contents_->GetTitle()); |
| 109 base::FilePath file_path( | 114 base::FilePath file_path( |
| 110 archives_dir.Append(GenerateFileName(url, base::UTF16ToUTF8(title)))); | 115 archives_dir.Append( |
| 116 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id))); | |
| 111 | 117 |
| 112 web_contents_->GenerateMHTML( | 118 web_contents_->GenerateMHTML( |
| 113 file_path, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, | 119 file_path, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, |
| 114 weak_ptr_factory_.GetWeakPtr(), url, file_path)); | 120 weak_ptr_factory_.GetWeakPtr(), url, file_path)); |
| 115 } | 121 } |
| 116 | 122 |
| 117 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( | 123 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( |
| 118 const GURL& url, | 124 const GURL& url, |
| 119 const base::FilePath& file_path, | 125 const base::FilePath& file_path, |
| 120 int64_t file_size) { | 126 int64_t file_size) { |
| 121 if (file_size < 0) { | 127 if (file_size < 0) { |
| 122 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 128 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 123 } else { | 129 } else { |
| 124 callback_.Run(this, ArchiverResult::SUCCESSFULLY_CREATED, url, file_path, | 130 callback_.Run(this, ArchiverResult::SUCCESSFULLY_CREATED, url, file_path, |
| 125 file_size); | 131 file_size); |
| 126 } | 132 } |
| 127 } | 133 } |
| 128 | 134 |
| 129 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { | 135 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { |
| 130 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); | 136 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); |
| 131 callback_.Run(this, result, GURL(), base::FilePath(), 0); | 137 callback_.Run(this, result, GURL(), base::FilePath(), 0); |
| 132 } | 138 } |
| 133 | 139 |
| 134 } // namespace offline_pages | 140 } // namespace offline_pages |
| OLD | NEW |