Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_mhtml_archiver.cc

Issue 1843893002: Support resaving offline page right after it is being deleted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "base/bind.h" 7 #include "base/bind.h"
9 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
10 #include "base/logging.h" 9 #include "base/logging.h"
11 #include "base/sha1.h"
12 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
15 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
16 #include "net/base/filename_util.h" 15 #include "net/base/filename_util.h"
17 16
18 namespace offline_pages { 17 namespace offline_pages {
19 namespace { 18 namespace {
20 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); 19 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml");
21 const base::FilePath::CharType kDefaultFileName[] = 20 const base::FilePath::CharType kDefaultFileName[] =
22 FILE_PATH_LITERAL("offline_page"); 21 FILE_PATH_LITERAL("offline_page");
23 const int kTitleLengthMax = 80; 22 const int kTitleLengthMax = 80;
24 const char kMHTMLFileNameExtension[] = ".mhtml"; 23 const char kMHTMLFileNameExtension[] = ".mhtml";
25 const char kFileNameComponentsSeparator[] = "-"; 24 const char kFileNameComponentsSeparator[] = "-";
26 const char kReplaceChars[] = " "; 25 const char kReplaceChars[] = " ";
27 const char kReplaceWith[] = "_"; 26 const char kReplaceWith[] = "_";
28 } // namespace 27 } // namespace
29 28
30 // static 29 // static
31 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { 30 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() {
32 return kMHTMLFileNameExtension; 31 return kMHTMLFileNameExtension;
33 } 32 }
34 33
35 // static 34 // static
36 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( 35 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName(
37 const GURL& url, 36 const GURL& url,
38 const std::string& title) { 37 const std::string& title,
38 int64_t archive_id) {
39 std::string title_part(title.substr(0, kTitleLengthMax)); 39 std::string title_part(title.substr(0, kTitleLengthMax));
40 std::string url_hash(base::SHA1HashString(url.spec())); 40 std::string suggested_name(
41 base::Base64Encode(url_hash, &url_hash); 41 url.host() + kFileNameComponentsSeparator +
42 std::string suggested_name(url.host() + kFileNameComponentsSeparator + 42 title_part + kFileNameComponentsSeparator +
43 title_part + kFileNameComponentsSeparator + 43 base::Int64ToString(archive_id));
44 url_hash);
45 44
46 // Substitute spaces out from title. 45 // Substitute spaces out from title.
47 base::ReplaceChars(suggested_name, kReplaceChars, kReplaceWith, 46 base::ReplaceChars(suggested_name, kReplaceChars, kReplaceWith,
48 &suggested_name); 47 &suggested_name);
49 48
50 return net::GenerateFileName(url, 49 return net::GenerateFileName(url,
51 std::string(), // content disposition 50 std::string(), // content disposition
52 std::string(), // charset 51 std::string(), // charset
53 suggested_name, 52 suggested_name,
54 std::string(), // mime-type 53 std::string(), // mime-type
(...skipping 11 matching lines...) Expand all
66 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() 65 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver()
67 : web_contents_(nullptr), 66 : web_contents_(nullptr),
68 weak_ptr_factory_(this) { 67 weak_ptr_factory_(this) {
69 } 68 }
70 69
71 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { 70 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() {
72 } 71 }
73 72
74 void OfflinePageMHTMLArchiver::CreateArchive( 73 void OfflinePageMHTMLArchiver::CreateArchive(
75 const base::FilePath& archives_dir, 74 const base::FilePath& archives_dir,
75 int64_t archive_id,
76 const CreateArchiveCallback& callback) { 76 const CreateArchiveCallback& callback) {
77 DCHECK(callback_.is_null()); 77 DCHECK(callback_.is_null());
78 DCHECK(!callback.is_null()); 78 DCHECK(!callback.is_null());
79 callback_ = callback; 79 callback_ = callback;
80 80
81 GenerateMHTML(archives_dir); 81 GenerateMHTML(archives_dir, archive_id);
82 } 82 }
83 83
84 void OfflinePageMHTMLArchiver::GenerateMHTML( 84 void OfflinePageMHTMLArchiver::GenerateMHTML(
85 const base::FilePath& archives_dir) { 85 const base::FilePath& archives_dir, int64_t archive_id) {
86 if (archives_dir.empty()) { 86 if (archives_dir.empty()) {
87 DVLOG(1) << "Archive path was empty. Can't create archive."; 87 DVLOG(1) << "Archive path was empty. Can't create archive.";
88 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); 88 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED);
89 return; 89 return;
90 } 90 }
91 91
92 if (!web_contents_) { 92 if (!web_contents_) {
93 DVLOG(1) << "WebContents is missing. Can't create archive."; 93 DVLOG(1) << "WebContents is missing. Can't create archive.";
94 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); 94 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE);
95 return; 95 return;
96 } 96 }
97 97
98 if (!web_contents_->GetRenderViewHost()) { 98 if (!web_contents_->GetRenderViewHost()) {
99 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive."; 99 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive.";
100 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); 100 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE);
101 return; 101 return;
102 } 102 }
103 103
104 // TODO(fgorski): Figure out if the actual URL can be different at 104 // 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 105 // the end of MHTML generation. Perhaps we should pull it out after the MHTML
106 // is generated. 106 // is generated.
107 GURL url(web_contents_->GetLastCommittedURL()); 107 GURL url(web_contents_->GetLastCommittedURL());
108 base::string16 title(web_contents_->GetTitle()); 108 base::string16 title(web_contents_->GetTitle());
109 base::FilePath file_path( 109 base::FilePath file_path(
110 archives_dir.Append(GenerateFileName(url, base::UTF16ToUTF8(title)))); 110 archives_dir.Append(
111 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id)));
111 112
112 web_contents_->GenerateMHTML( 113 web_contents_->GenerateMHTML(
113 file_path, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, 114 file_path, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone,
114 weak_ptr_factory_.GetWeakPtr(), url, file_path)); 115 weak_ptr_factory_.GetWeakPtr(), url, file_path));
115 } 116 }
116 117
117 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( 118 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(
118 const GURL& url, 119 const GURL& url,
119 const base::FilePath& file_path, 120 const base::FilePath& file_path,
120 int64_t file_size) { 121 int64_t file_size) {
121 if (file_size < 0) { 122 if (file_size < 0) {
122 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); 123 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED);
123 } else { 124 } else {
124 callback_.Run(this, ArchiverResult::SUCCESSFULLY_CREATED, url, file_path, 125 callback_.Run(this, ArchiverResult::SUCCESSFULLY_CREATED, url, file_path,
125 file_size); 126 file_size);
126 } 127 }
127 } 128 }
128 129
129 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { 130 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) {
130 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); 131 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED);
131 callback_.Run(this, result, GURL(), base::FilePath(), 0); 132 callback_.Run(this, result, GURL(), base::FilePath(), 0);
132 } 133 }
133 134
134 } // namespace offline_pages 135 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698