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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 base::Closure callback) { |
63 bool result = ImportantFileWriter::WriteFileAtomically(path, *data); | |
64 | |
65 if (result && !callback.is_null()) | |
66 callback.Run(); | |
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 Loading... | |
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 blocking_on_next_successful_write_); | |
179 blocking_on_next_successful_write_.Reset(); | |
gab
2016/08/03 18:19:34
This won't respect the contract (it won't stay aro
proberge
2016/08/04 00:13:45
Right, unlike the migration case we care about a s
| |
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 20 matching lines...) Expand all Loading... | |
202 } | 211 } |
203 serializer_ = nullptr; | 212 serializer_ = nullptr; |
204 } | 213 } |
205 | 214 |
206 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( | 215 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( |
207 const Closure& on_next_successful_write) { | 216 const Closure& on_next_successful_write) { |
208 DCHECK(on_next_successful_write_.is_null()); | 217 DCHECK(on_next_successful_write_.is_null()); |
209 on_next_successful_write_ = on_next_successful_write; | 218 on_next_successful_write_ = on_next_successful_write; |
210 } | 219 } |
211 | 220 |
221 void ImportantFileWriter::RegisterOnNextSuccessfulWriteBlockingCallback( | |
222 const Closure& on_next_successful_write) { | |
223 DCHECK(blocking_on_next_successful_write_.is_null()); | |
224 blocking_on_next_successful_write_ = on_next_successful_write; | |
225 } | |
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_.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_.is_null()) { |
234 on_next_successful_write_.Run(); | 249 on_next_successful_write_.Run(); |
235 on_next_successful_write_.Reset(); | 250 on_next_successful_write_.Reset(); |
236 } | 251 } |
237 } | 252 } |
238 | 253 |
239 } // namespace base | 254 } // namespace base |
OLD | NEW |