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" |
11 #include "base/guid.h" | |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
carlosk
2016/12/30 00:11:13
It seems these 3 imports above aren't needed anymo
dewittj
2017/01/03 22:23:38
Done.
| |
17 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
18 #include "chrome/browser/ssl/security_state_tab_helper.h" | 19 #include "chrome/browser/ssl/security_state_tab_helper.h" |
19 #include "components/security_state/core/security_state.h" | 20 #include "components/security_state/core/security_state.h" |
20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
22 #include "content/public/common/mhtml_generation_params.h" | 23 #include "content/public/common/mhtml_generation_params.h" |
23 #include "net/base/filename_util.h" | 24 #include "net/base/filename_util.h" |
24 | 25 |
25 namespace offline_pages { | 26 namespace offline_pages { |
26 namespace { | 27 namespace { |
27 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); | 28 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); |
28 const base::FilePath::CharType kDefaultFileName[] = | |
29 FILE_PATH_LITERAL("offline_page"); | |
30 const int kTitleLengthMax = 80; | |
31 const char kMHTMLFileNameExtension[] = ".mhtml"; | |
32 const char kFileNameComponentsSeparator[] = "-"; | |
33 const char kReplaceChars[] = " "; | |
34 const char kReplaceWith[] = "_"; | |
35 | 29 |
36 void DeleteFileOnFileThread(const base::FilePath& file_path, | 30 void DeleteFileOnFileThread(const base::FilePath& file_path, |
37 const base::Closure& callback) { | 31 const base::Closure& callback) { |
38 content::BrowserThread::PostTaskAndReply( | 32 content::BrowserThread::PostTaskAndReply( |
39 content::BrowserThread::FILE, FROM_HERE, | 33 content::BrowserThread::FILE, FROM_HERE, |
40 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, | 34 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, |
41 false /* recursive */), | 35 false /* recursive */), |
42 callback); | 36 callback); |
43 } | 37 } |
44 } // namespace | 38 } // namespace |
45 | 39 |
46 // static | 40 // static |
47 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { | |
48 return kMHTMLFileNameExtension; | |
49 } | |
50 | |
51 // static | |
52 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( | |
53 const GURL& url, | |
54 const std::string& title, | |
55 int64_t archive_id) { | |
56 std::string title_part(title.substr(0, kTitleLengthMax)); | |
57 std::string suggested_name( | |
58 url.host() + kFileNameComponentsSeparator + | |
59 title_part + kFileNameComponentsSeparator + | |
60 base::Int64ToString(archive_id)); | |
61 | |
62 // Substitute spaces out from title. | |
63 base::ReplaceChars(suggested_name, kReplaceChars, kReplaceWith, | |
64 &suggested_name); | |
65 | |
66 return net::GenerateFileName(url, | |
67 std::string(), // content disposition | |
68 std::string(), // charset | |
69 suggested_name, | |
70 std::string(), // mime-type | |
71 kDefaultFileName) | |
72 .AddExtension(kMHTMLExtension); | |
73 } | |
74 | |
75 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver( | 41 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver( |
76 content::WebContents* web_contents) | 42 content::WebContents* web_contents) |
77 : web_contents_(web_contents), | 43 : web_contents_(web_contents), |
78 weak_ptr_factory_(this) { | 44 weak_ptr_factory_(this) { |
79 DCHECK(web_contents_); | 45 DCHECK(web_contents_); |
80 } | 46 } |
81 | 47 |
82 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() | 48 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() |
83 : web_contents_(nullptr), | 49 : web_contents_(nullptr), |
84 weak_ptr_factory_(this) { | 50 weak_ptr_factory_(this) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 88 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
123 return; | 89 return; |
124 } | 90 } |
125 | 91 |
126 // TODO(fgorski): Figure out if the actual URL can be different at | 92 // TODO(fgorski): Figure out if the actual URL can be different at |
127 // the end of MHTML generation. Perhaps we should pull it out after the MHTML | 93 // the end of MHTML generation. Perhaps we should pull it out after the MHTML |
128 // is generated. | 94 // is generated. |
129 GURL url(web_contents_->GetLastCommittedURL()); | 95 GURL url(web_contents_->GetLastCommittedURL()); |
130 base::string16 title(web_contents_->GetTitle()); | 96 base::string16 title(web_contents_->GetTitle()); |
131 base::FilePath file_path( | 97 base::FilePath file_path( |
132 archives_dir.Append( | 98 archives_dir.Append(base::GenerateGUID()).AddExtension(kMHTMLExtension)); |
Pete Williamson
2017/01/03 20:32:30
Drive by: This means that if we generate another p
dewittj
2017/01/03 22:23:38
We do currently exhibit this behavior when we mana
| |
133 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id))); | |
134 | |
135 content::MHTMLGenerationParams params(file_path); | 99 content::MHTMLGenerationParams params(file_path); |
136 params.use_binary_encoding = true; | 100 params.use_binary_encoding = true; |
137 | 101 |
138 web_contents_->GenerateMHTML( | 102 web_contents_->GenerateMHTML( |
139 params, | 103 params, |
140 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, | 104 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, |
141 weak_ptr_factory_.GetWeakPtr(), url, file_path, title)); | 105 weak_ptr_factory_.GetWeakPtr(), url, file_path, title)); |
142 } | 106 } |
143 | 107 |
144 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( | 108 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( |
(...skipping 26 matching lines...) Expand all Loading... | |
171 } | 135 } |
172 | 136 |
173 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { | 137 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { |
174 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); | 138 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); |
175 base::ThreadTaskRunnerHandle::Get()->PostTask( | 139 base::ThreadTaskRunnerHandle::Get()->PostTask( |
176 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(), | 140 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(), |
177 base::string16(), 0)); | 141 base::string16(), 0)); |
178 } | 142 } |
179 | 143 |
180 } // namespace offline_pages | 144 } // namespace offline_pages |
OLD | NEW |