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

Side by Side Diff: base/files/important_file_writer.cc

Issue 2204943002: Integrate registry_hash_store_contents with the rest of tracked prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unit and browser tests. GetType => GetUMASuffix Created 4 years, 4 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/files/important_file_writer.h" 5 #include "base/files/important_file_writer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <string> 10 #include <string>
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 void LogFailure(const FilePath& path, TempFileFailure failure_code, 51 void LogFailure(const FilePath& path, TempFileFailure failure_code,
52 StringPiece message) { 52 StringPiece message) {
53 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, 53 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code,
54 TEMP_FILE_FAILURE_MAX); 54 TEMP_FILE_FAILURE_MAX);
55 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message; 55 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message;
56 } 56 }
57 57
58 // Helper function to call WriteFileAtomically() with a 58 // Helper function to call WriteFileAtomically() with a
59 // std::unique_ptr<std::string>. 59 // std::unique_ptr<std::string>.
60 bool WriteScopedStringToFileAtomically(const FilePath& path, 60 bool WriteScopedStringToFileAtomically(const FilePath& path,
61 std::unique_ptr<std::string> data) { 61 std::unique_ptr<std::string> data,
62 return ImportantFileWriter::WriteFileAtomically(path, *data); 62 Callback<void(bool success)> callback) {
63 bool result = ImportantFileWriter::WriteFileAtomically(path, *data);
64
65 if (!callback.is_null())
66 callback.Run(result);
67
68 return result;
63 } 69 }
64 70
65 } // namespace 71 } // namespace
66 72
67 // static 73 // static
68 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, 74 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
69 StringPiece data) { 75 StringPiece data) {
70 #if defined(OS_CHROMEOS) 76 #if defined(OS_CHROMEOS)
71 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, 77 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly,
72 // and this function seems to be one of the slowest shutdown steps. 78 // and this function seems to be one of the slowest shutdown steps.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { 167 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) {
162 DCHECK(CalledOnValidThread()); 168 DCHECK(CalledOnValidThread());
163 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { 169 if (!IsValueInRangeForNumericType<int32_t>(data->length())) {
164 NOTREACHED(); 170 NOTREACHED();
165 return; 171 return;
166 } 172 }
167 173
168 if (HasPendingWrite()) 174 if (HasPendingWrite())
169 timer_.Stop(); 175 timer_.Stop();
170 176
171 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); 177 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data),
178 on_next_write_callback_);
179 on_next_write_callback_.Reset();
180
172 if (!PostWriteTask(task)) { 181 if (!PostWriteTask(task)) {
173 // Posting the task to background message loop is not expected 182 // Posting the task to background message loop is not expected
174 // to fail, but if it does, avoid losing data and just hit the disk 183 // to fail, but if it does, avoid losing data and just hit the disk
175 // on the current thread. 184 // on the current thread.
176 NOTREACHED(); 185 NOTREACHED();
177 186
178 task.Run(); 187 task.Run();
179 } 188 }
180 } 189 }
181 190
(...skipping 14 matching lines...) Expand all
196 std::unique_ptr<std::string> data(new std::string); 205 std::unique_ptr<std::string> data(new std::string);
197 if (serializer_->SerializeData(data.get())) { 206 if (serializer_->SerializeData(data.get())) {
198 WriteNow(std::move(data)); 207 WriteNow(std::move(data));
199 } else { 208 } else {
200 DLOG(WARNING) << "failed to serialize data to be saved in " 209 DLOG(WARNING) << "failed to serialize data to be saved in "
201 << path_.value(); 210 << path_.value();
202 } 211 }
203 serializer_ = nullptr; 212 serializer_ = nullptr;
204 } 213 }
205 214
206 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( 215 void ImportantFileWriter::RegisterOnNextSuccessfulWriteReply(
207 const Closure& on_next_successful_write) { 216 const Closure& on_next_successful_write_reply) {
208 DCHECK(on_next_successful_write_.is_null()); 217 DCHECK(on_next_successful_write_reply_.is_null());
209 on_next_successful_write_ = on_next_successful_write; 218 on_next_successful_write_reply_ = on_next_successful_write_reply;
219 }
220
221 void ImportantFileWriter::RegisterOnNextWriteSynchronousCallback(
222 const Callback<void(bool success)>& on_next_write_callback) {
223 DCHECK(on_next_write_callback_.is_null());
224 on_next_write_callback_ = on_next_write_callback;
210 } 225 }
211 226
212 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) { 227 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) {
213 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let 228 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let
214 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but 229 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but
215 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and 230 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and
216 // suppressing all of those is unrealistic hence we avoid most of them by 231 // suppressing all of those is unrealistic hence we avoid most of them by
217 // using PostTask() in the typical scenario below. 232 // using PostTask() in the typical scenario below.
218 if (!on_next_successful_write_.is_null()) { 233 if (!on_next_successful_write_reply_.is_null()) {
219 return PostTaskAndReplyWithResult( 234 return PostTaskAndReplyWithResult(
220 task_runner_.get(), 235 task_runner_.get(),
221 FROM_HERE, 236 FROM_HERE,
222 MakeCriticalClosure(task), 237 MakeCriticalClosure(task),
223 Bind(&ImportantFileWriter::ForwardSuccessfulWrite, 238 Bind(&ImportantFileWriter::ForwardSuccessfulWrite,
224 weak_factory_.GetWeakPtr())); 239 weak_factory_.GetWeakPtr()));
225 } 240 }
226 return task_runner_->PostTask( 241 return task_runner_->PostTask(
227 FROM_HERE, 242 FROM_HERE,
228 MakeCriticalClosure(Bind(IgnoreResult(task)))); 243 MakeCriticalClosure(Bind(IgnoreResult(task))));
229 } 244 }
230 245
231 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { 246 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) {
232 DCHECK(CalledOnValidThread()); 247 DCHECK(CalledOnValidThread());
233 if (result && !on_next_successful_write_.is_null()) { 248 if (result && !on_next_successful_write_reply_.is_null()) {
234 on_next_successful_write_.Run(); 249 on_next_successful_write_reply_.Run();
235 on_next_successful_write_.Reset(); 250 on_next_successful_write_reply_.Reset();
236 } 251 }
237 } 252 }
238 253
239 } // namespace base 254 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698