Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 | 50 |
| 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 void 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); | |
| 63 } | 67 } |
| 64 | 68 |
| 65 } // namespace | 69 } // namespace |
| 66 | 70 |
| 67 // static | 71 // static |
| 68 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, | 72 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, |
| 69 StringPiece data) { | 73 StringPiece data) { |
| 70 #if defined(OS_CHROMEOS) | 74 #if defined(OS_CHROMEOS) |
| 71 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, | 75 // 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. | 76 // and this function seems to be one of the slowest shutdown steps. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { | 165 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { |
| 162 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 163 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { | 167 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { |
| 164 NOTREACHED(); | 168 NOTREACHED(); |
| 165 return; | 169 return; |
| 166 } | 170 } |
| 167 | 171 |
| 168 if (HasPendingWrite()) | 172 if (HasPendingWrite()) |
| 169 timer_.Stop(); | 173 timer_.Stop(); |
| 170 | 174 |
| 171 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); | 175 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data), |
|
gab
2016/09/19 18:01:30
In the mindset of the comment below, I think s/aut
proberge
2016/09/19 21:24:25
Done.
| |
| 172 if (!PostWriteTask(task)) { | 176 Passed(&on_next_write_callback_)); |
| 177 | |
| 178 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(Bind(task)))) { | |
|
gab
2016/09/19 18:01:30
|task| is already a Closure, why do we need the ex
proberge
2016/09/19 21:24:25
Done.
| |
| 173 // Posting the task to background message loop is not expected | 179 // 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 | 180 // to fail, but if it does, avoid losing data and just hit the disk |
| 175 // on the current thread. | 181 // on the current thread. |
| 176 NOTREACHED(); | 182 NOTREACHED(); |
| 177 | 183 |
| 178 task.Run(); | 184 task.Run(); |
| 179 } | 185 } |
| 180 } | 186 } |
| 181 | 187 |
| 182 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 188 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 196 std::unique_ptr<std::string> data(new std::string); | 202 std::unique_ptr<std::string> data(new std::string); |
| 197 if (serializer_->SerializeData(data.get())) { | 203 if (serializer_->SerializeData(data.get())) { |
| 198 WriteNow(std::move(data)); | 204 WriteNow(std::move(data)); |
| 199 } else { | 205 } else { |
| 200 DLOG(WARNING) << "failed to serialize data to be saved in " | 206 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 201 << path_.value(); | 207 << path_.value(); |
| 202 } | 208 } |
| 203 serializer_ = nullptr; | 209 serializer_ = nullptr; |
| 204 } | 210 } |
| 205 | 211 |
| 206 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( | 212 void ImportantFileWriter::RegisterOnNextWriteCallback( |
| 207 const Closure& on_next_successful_write) { | 213 const Callback<void(bool success)>& on_next_write_callback) { |
| 208 DCHECK(on_next_successful_write_.is_null()); | 214 on_next_write_callback_ = on_next_write_callback; |
| 209 on_next_successful_write_ = on_next_successful_write; | |
| 210 } | |
| 211 | |
| 212 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) { | |
| 213 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let | |
| 214 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but | |
| 215 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and | |
| 216 // suppressing all of those is unrealistic hence we avoid most of them by | |
| 217 // using PostTask() in the typical scenario below. | |
| 218 if (!on_next_successful_write_.is_null()) { | |
| 219 return PostTaskAndReplyWithResult( | |
| 220 task_runner_.get(), | |
| 221 FROM_HERE, | |
| 222 MakeCriticalClosure(task), | |
| 223 Bind(&ImportantFileWriter::ForwardSuccessfulWrite, | |
| 224 weak_factory_.GetWeakPtr())); | |
| 225 } | |
| 226 return task_runner_->PostTask( | |
| 227 FROM_HERE, | |
| 228 MakeCriticalClosure(Bind(IgnoreResult(task)))); | |
| 229 } | |
| 230 | |
| 231 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { | |
| 232 DCHECK(CalledOnValidThread()); | |
| 233 if (result && !on_next_successful_write_.is_null()) { | |
| 234 on_next_successful_write_.Run(); | |
| 235 on_next_successful_write_.Reset(); | |
| 236 } | |
| 237 } | 215 } |
| 238 | 216 |
| 239 } // namespace base | 217 } // namespace base |
| OLD | NEW |