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

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

Issue 2028263003: Offline Pages: Remove leftover file if MHTML generation error occurred. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Format. Created 4 years, 6 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
9 #include "base/location.h" 11 #include "base/location.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
16 #include "chrome/browser/ssl/chrome_security_state_model_client.h" 18 #include "chrome/browser/ssl/chrome_security_state_model_client.h"
17 #include "components/security_state/security_state_model.h" 19 #include "components/security_state/security_state_model.h"
20 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/mhtml_generation_params.h" 22 #include "content/public/common/mhtml_generation_params.h"
20 #include "net/base/filename_util.h" 23 #include "net/base/filename_util.h"
21 24
22 namespace offline_pages { 25 namespace offline_pages {
23 namespace { 26 namespace {
24 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml"); 27 const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml");
25 const base::FilePath::CharType kDefaultFileName[] = 28 const base::FilePath::CharType kDefaultFileName[] =
26 FILE_PATH_LITERAL("offline_page"); 29 FILE_PATH_LITERAL("offline_page");
27 const int kTitleLengthMax = 80; 30 const int kTitleLengthMax = 80;
28 const char kMHTMLFileNameExtension[] = ".mhtml"; 31 const char kMHTMLFileNameExtension[] = ".mhtml";
29 const char kFileNameComponentsSeparator[] = "-"; 32 const char kFileNameComponentsSeparator[] = "-";
30 const char kReplaceChars[] = " "; 33 const char kReplaceChars[] = " ";
31 const char kReplaceWith[] = "_"; 34 const char kReplaceWith[] = "_";
35
36 void DeleteFileOnFileThread(const base::FilePath& file_path,
37 const base::Closure& callback) {
38 content::BrowserThread::PostTaskAndReply(
39 content::BrowserThread::FILE, FROM_HERE,
40 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path,
41 false /* recursive */),
42 callback);
43 }
32 } // namespace 44 } // namespace
33 45
34 // static 46 // static
35 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { 47 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() {
36 return kMHTMLFileNameExtension; 48 return kMHTMLFileNameExtension;
37 } 49 }
38 50
39 // static 51 // static
40 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( 52 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName(
41 const GURL& url, 53 const GURL& url,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 web_contents_->GenerateMHTML( 140 web_contents_->GenerateMHTML(
129 params, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, 141 params, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone,
130 weak_ptr_factory_.GetWeakPtr(), url, file_path)); 142 weak_ptr_factory_.GetWeakPtr(), url, file_path));
131 } 143 }
132 144
133 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( 145 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(
134 const GURL& url, 146 const GURL& url,
135 const base::FilePath& file_path, 147 const base::FilePath& file_path,
136 int64_t file_size) { 148 int64_t file_size) {
137 if (file_size < 0) { 149 if (file_size < 0) {
138 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); 150 DeleteFileOnFileThread(
151 file_path, base::Bind(&OfflinePageMHTMLArchiver::ReportFailure,
152 weak_ptr_factory_.GetWeakPtr(),
153 ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED));
139 } else { 154 } else {
140 base::ThreadTaskRunnerHandle::Get()->PostTask( 155 base::ThreadTaskRunnerHandle::Get()->PostTask(
141 FROM_HERE, 156 FROM_HERE,
142 base::Bind(callback_, this, ArchiverResult::SUCCESSFULLY_CREATED, url, 157 base::Bind(callback_, this, ArchiverResult::SUCCESSFULLY_CREATED, url,
143 file_path, file_size)); 158 file_path, file_size));
144 } 159 }
145 } 160 }
146 161
147 bool OfflinePageMHTMLArchiver::HasConnectionSecurityError() { 162 bool OfflinePageMHTMLArchiver::HasConnectionSecurityError() {
148 ChromeSecurityStateModelClient::CreateForWebContents(web_contents_); 163 ChromeSecurityStateModelClient::CreateForWebContents(web_contents_);
149 ChromeSecurityStateModelClient* model_client = 164 ChromeSecurityStateModelClient* model_client =
150 ChromeSecurityStateModelClient::FromWebContents(web_contents_); 165 ChromeSecurityStateModelClient::FromWebContents(web_contents_);
151 DCHECK(model_client); 166 DCHECK(model_client);
152 return security_state::SecurityStateModel::SecurityLevel::SECURITY_ERROR == 167 return security_state::SecurityStateModel::SecurityLevel::SECURITY_ERROR ==
153 model_client->GetSecurityInfo().security_level; 168 model_client->GetSecurityInfo().security_level;
154 } 169 }
155 170
156 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { 171 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) {
157 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); 172 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED);
158 base::ThreadTaskRunnerHandle::Get()->PostTask( 173 base::ThreadTaskRunnerHandle::Get()->PostTask(
159 FROM_HERE, 174 FROM_HERE,
160 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0)); 175 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0));
161 } 176 }
162 177
163 } // namespace offline_pages 178 } // namespace offline_pages
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698