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

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: 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 scoped_refptr<base::SequencedWorkerPool> worker_pool(
39 content::BrowserThread::GetBlockingPool());
Dmitry Titov 2016/06/02 00:03:55 Isn't it what BrowserThread::FILE is for?
dewittj 2016/06/02 18:05:37 Done.
40 // Blocks shutdown because otherwise there would be leftover junk files.
41 scoped_refptr<base::SequencedTaskRunner> task_runner(
42 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
43 worker_pool->GetSequenceToken(),
44 base::SequencedWorkerPool::BLOCK_SHUTDOWN));
45 task_runner->PostTaskAndReply(
46 FROM_HERE, base::Bind(base::IgnoreResult(&base::DeleteFile), file_path,
47 false /* recursive */),
48 callback);
49 }
32 } // namespace 50 } // namespace
33 51
34 // static 52 // static
35 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() { 53 std::string OfflinePageMHTMLArchiver::GetFileNameExtension() {
36 return kMHTMLFileNameExtension; 54 return kMHTMLFileNameExtension;
37 } 55 }
38 56
39 // static 57 // static
40 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName( 58 base::FilePath OfflinePageMHTMLArchiver::GenerateFileName(
41 const GURL& url, 59 const GURL& url,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 web_contents_->GenerateMHTML( 146 web_contents_->GenerateMHTML(
129 params, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, 147 params, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone,
130 weak_ptr_factory_.GetWeakPtr(), url, file_path)); 148 weak_ptr_factory_.GetWeakPtr(), url, file_path));
131 } 149 }
132 150
133 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone( 151 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(
134 const GURL& url, 152 const GURL& url,
135 const base::FilePath& file_path, 153 const base::FilePath& file_path,
136 int64_t file_size) { 154 int64_t file_size) {
137 if (file_size < 0) { 155 if (file_size < 0) {
138 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); 156 DeleteFileOnFileThread(
157 file_path, base::Bind(&OfflinePageMHTMLArchiver::ReportFailure,
158 weak_ptr_factory_.GetWeakPtr(),
159 ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED));
139 } else { 160 } else {
140 base::ThreadTaskRunnerHandle::Get()->PostTask( 161 base::ThreadTaskRunnerHandle::Get()->PostTask(
141 FROM_HERE, 162 FROM_HERE,
142 base::Bind(callback_, this, ArchiverResult::SUCCESSFULLY_CREATED, url, 163 base::Bind(callback_, this, ArchiverResult::SUCCESSFULLY_CREATED, url,
143 file_path, file_size)); 164 file_path, file_size));
144 } 165 }
145 } 166 }
146 167
147 bool OfflinePageMHTMLArchiver::HasConnectionSecurityError() { 168 bool OfflinePageMHTMLArchiver::HasConnectionSecurityError() {
148 ChromeSecurityStateModelClient::CreateForWebContents(web_contents_); 169 ChromeSecurityStateModelClient::CreateForWebContents(web_contents_);
149 ChromeSecurityStateModelClient* model_client = 170 ChromeSecurityStateModelClient* model_client =
150 ChromeSecurityStateModelClient::FromWebContents(web_contents_); 171 ChromeSecurityStateModelClient::FromWebContents(web_contents_);
151 DCHECK(model_client); 172 DCHECK(model_client);
152 return security_state::SecurityStateModel::SecurityLevel::SECURITY_ERROR == 173 return security_state::SecurityStateModel::SecurityLevel::SECURITY_ERROR ==
153 model_client->GetSecurityInfo().security_level; 174 model_client->GetSecurityInfo().security_level;
154 } 175 }
155 176
156 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { 177 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) {
157 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); 178 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED);
158 base::ThreadTaskRunnerHandle::Get()->PostTask( 179 base::ThreadTaskRunnerHandle::Get()->PostTask(
159 FROM_HERE, 180 FROM_HERE,
160 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0)); 181 base::Bind(callback_, this, result, GURL(), base::FilePath(), 0));
161 } 182 }
162 183
163 } // namespace offline_pages 184 } // 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