| 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 void WriteScopedStringToFileAtomically(const FilePath& path, | 60 void WriteScopedStringToFileAtomically( |
| 61 std::unique_ptr<std::string> data, | 61 const FilePath& path, |
| 62 Callback<void(bool success)> callback) { | 62 std::unique_ptr<std::string> data, |
| 63 Closure before_write_callback, |
| 64 Callback<void(bool success)> after_write_callback) { |
| 65 if (!before_write_callback.is_null()) |
| 66 before_write_callback.Run(); |
| 67 |
| 63 bool result = ImportantFileWriter::WriteFileAtomically(path, *data); | 68 bool result = ImportantFileWriter::WriteFileAtomically(path, *data); |
| 64 | 69 |
| 65 if (!callback.is_null()) | 70 if (!after_write_callback.is_null()) |
| 66 callback.Run(result); | 71 after_write_callback.Run(result); |
| 67 } | 72 } |
| 68 | 73 |
| 69 } // namespace | 74 } // namespace |
| 70 | 75 |
| 71 // static | 76 // static |
| 72 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, | 77 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, |
| 73 StringPiece data) { | 78 StringPiece data) { |
| 74 #if defined(OS_CHROMEOS) | 79 #if defined(OS_CHROMEOS) |
| 75 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, | 80 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, |
| 76 // and this function seems to be one of the slowest shutdown steps. | 81 // and this function seems to be one of the slowest shutdown steps. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 DCHECK(CalledOnValidThread()); | 171 DCHECK(CalledOnValidThread()); |
| 167 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { | 172 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { |
| 168 NOTREACHED(); | 173 NOTREACHED(); |
| 169 return; | 174 return; |
| 170 } | 175 } |
| 171 | 176 |
| 172 if (HasPendingWrite()) | 177 if (HasPendingWrite()) |
| 173 timer_.Stop(); | 178 timer_.Stop(); |
| 174 | 179 |
| 175 Closure task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data), | 180 Closure task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data), |
| 176 Passed(&on_next_write_callback_)); | 181 Passed(&before_next_write_callback_), |
| 182 Passed(&after_next_write_callback_)); |
| 177 | 183 |
| 178 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { | 184 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { |
| 179 // Posting the task to background message loop is not expected | 185 // Posting the task to background message loop is not expected |
| 180 // to fail, but if it does, avoid losing data and just hit the disk | 186 // to fail, but if it does, avoid losing data and just hit the disk |
| 181 // on the current thread. | 187 // on the current thread. |
| 182 NOTREACHED(); | 188 NOTREACHED(); |
| 183 | 189 |
| 184 task.Run(); | 190 task.Run(); |
| 185 } | 191 } |
| 186 } | 192 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 202 std::unique_ptr<std::string> data(new std::string); | 208 std::unique_ptr<std::string> data(new std::string); |
| 203 if (serializer_->SerializeData(data.get())) { | 209 if (serializer_->SerializeData(data.get())) { |
| 204 WriteNow(std::move(data)); | 210 WriteNow(std::move(data)); |
| 205 } else { | 211 } else { |
| 206 DLOG(WARNING) << "failed to serialize data to be saved in " | 212 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 207 << path_.value(); | 213 << path_.value(); |
| 208 } | 214 } |
| 209 serializer_ = nullptr; | 215 serializer_ = nullptr; |
| 210 } | 216 } |
| 211 | 217 |
| 212 void ImportantFileWriter::RegisterOnNextWriteCallback( | 218 void ImportantFileWriter::RegisterOnNextWriteCallbacks( |
| 213 const Callback<void(bool success)>& on_next_write_callback) { | 219 const Closure& before_next_write_callback, |
| 214 on_next_write_callback_ = on_next_write_callback; | 220 const Callback<void(bool success)>& after_next_write_callback) { |
| 221 before_next_write_callback_ = before_next_write_callback; |
| 222 after_next_write_callback_ = after_next_write_callback; |
| 215 } | 223 } |
| 216 | 224 |
| 217 } // namespace base | 225 } // namespace base |
| OLD | NEW |