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

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

Issue 2579343002: [Offline Pages] Make new archives have random file names. (Closed)
Patch Set: Remove unnecessary includes Created 3 years, 11 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/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_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
18 #include "chrome/browser/ssl/security_state_tab_helper.h" 16 #include "chrome/browser/ssl/security_state_tab_helper.h"
19 #include "components/security_state/core/security_state.h" 17 #include "components/security_state/core/security_state.h"
20 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/mhtml_generation_params.h" 20 #include "content/public/common/mhtml_generation_params.h"
23 #include "net/base/filename_util.h" 21 #include "net/base/filename_util.h"
24 22
25 namespace offline_pages { 23 namespace offline_pages {
26 namespace { 24 namespace {
27 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); 25 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 26
36 void DeleteFileOnFileThread(const base::FilePath& file_path, 27 void DeleteFileOnFileThread(const base::FilePath& file_path,
37 const base::Closure& callback) { 28 const base::Closure& callback) {
38 content::BrowserThread::PostTaskAndReply( 29 content::BrowserThread::PostTaskAndReply(
39 content::BrowserThread::FILE, FROM_HERE, 30 content::BrowserThread::FILE, FROM_HERE,
40 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, 31 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path,
41 false /* recursive */), 32 false /* recursive */),
42 callback); 33 callback);
43 } 34 }
44 } // namespace 35 } // namespace
45 36
46 // static 37 // 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( 38 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver(
76 content::WebContents* web_contents) 39 content::WebContents* web_contents)
77 : web_contents_(web_contents), 40 : web_contents_(web_contents),
78 weak_ptr_factory_(this) { 41 weak_ptr_factory_(this) {
79 DCHECK(web_contents_); 42 DCHECK(web_contents_);
80 } 43 }
81 44
82 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver() 45 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver()
83 : web_contents_(nullptr), 46 : web_contents_(nullptr),
84 weak_ptr_factory_(this) { 47 weak_ptr_factory_(this) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); 85 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE);
123 return; 86 return;
124 } 87 }
125 88
126 // TODO(fgorski): Figure out if the actual URL can be different at 89 // 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 90 // the end of MHTML generation. Perhaps we should pull it out after the MHTML
128 // is generated. 91 // is generated.
129 GURL url(web_contents_->GetLastCommittedURL()); 92 GURL url(web_contents_->GetLastCommittedURL());
130 base::string16 title(web_contents_->GetTitle()); 93 base::string16 title(web_contents_->GetTitle());
131 base::FilePath file_path( 94 base::FilePath file_path(
132 archives_dir.Append( 95 archives_dir.Append(base::GenerateGUID()).AddExtension(kMHTMLExtension));
133 GenerateFileName(url, base::UTF16ToUTF8(title), archive_id)));
134
135 content::MHTMLGenerationParams params(file_path); 96 content::MHTMLGenerationParams params(file_path);
136 params.use_binary_encoding = true; 97 params.use_binary_encoding = true;
137 98
138 web_contents_->GenerateMHTML( 99 web_contents_->GenerateMHTML(
139 params, 100 params,
140 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, 101 base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone,
141 weak_ptr_factory_.GetWeakPtr(), url, file_path, title)); 102 weak_ptr_factory_.GetWeakPtr(), url, file_path, title));
142 } 103 }
143 104
144 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( 105 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(
(...skipping 26 matching lines...) Expand all
171 } 132 }
172 133
173 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { 134 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) {
174 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); 135 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED);
175 base::ThreadTaskRunnerHandle::Get()->PostTask( 136 base::ThreadTaskRunnerHandle::Get()->PostTask(
176 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(), 137 FROM_HERE, base::Bind(callback_, this, result, GURL(), base::FilePath(),
177 base::string16(), 0)); 138 base::string16(), 0));
178 } 139 }
179 140
180 } // namespace offline_pages 141 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698