| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 TEMP_FILE_FAILURE_MAX | 48 TEMP_FILE_FAILURE_MAX |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 void LogFailure(const FilePath& path, TempFileFailure failure_code, | 51 void LogFailure(const FilePath& path, TempFileFailure failure_code, |
| 52 const std::string& message) { | 52 const std::string& 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 scoped_ptr<std::string>. | 58 // Helper function to call WriteFileAtomically() with a |
| 59 // std::unique_ptr<std::string>. |
| 59 bool WriteScopedStringToFileAtomically(const FilePath& path, | 60 bool WriteScopedStringToFileAtomically(const FilePath& path, |
| 60 scoped_ptr<std::string> data) { | 61 std::unique_ptr<std::string> data) { |
| 61 return ImportantFileWriter::WriteFileAtomically(path, *data); | 62 return ImportantFileWriter::WriteFileAtomically(path, *data); |
| 62 } | 63 } |
| 63 | 64 |
| 64 } // namespace | 65 } // namespace |
| 65 | 66 |
| 66 // static | 67 // static |
| 67 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, | 68 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, |
| 68 const std::string& data) { | 69 const std::string& data) { |
| 69 #if defined(OS_CHROMEOS) | 70 #if defined(OS_CHROMEOS) |
| 70 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, | 71 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // to be our serializer. It may not be safe to call back to the parent object | 151 // to be our serializer. It may not be safe to call back to the parent object |
| 151 // being destructed. | 152 // being destructed. |
| 152 DCHECK(!HasPendingWrite()); | 153 DCHECK(!HasPendingWrite()); |
| 153 } | 154 } |
| 154 | 155 |
| 155 bool ImportantFileWriter::HasPendingWrite() const { | 156 bool ImportantFileWriter::HasPendingWrite() const { |
| 156 DCHECK(CalledOnValidThread()); | 157 DCHECK(CalledOnValidThread()); |
| 157 return timer_.IsRunning(); | 158 return timer_.IsRunning(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void ImportantFileWriter::WriteNow(scoped_ptr<std::string> data) { | 161 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { |
| 161 DCHECK(CalledOnValidThread()); | 162 DCHECK(CalledOnValidThread()); |
| 162 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { | 163 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { |
| 163 NOTREACHED(); | 164 NOTREACHED(); |
| 164 return; | 165 return; |
| 165 } | 166 } |
| 166 | 167 |
| 167 if (HasPendingWrite()) | 168 if (HasPendingWrite()) |
| 168 timer_.Stop(); | 169 timer_.Stop(); |
| 169 | 170 |
| 170 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); | 171 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 185 serializer_ = serializer; | 186 serializer_ = serializer; |
| 186 | 187 |
| 187 if (!timer_.IsRunning()) { | 188 if (!timer_.IsRunning()) { |
| 188 timer_.Start(FROM_HERE, commit_interval_, this, | 189 timer_.Start(FROM_HERE, commit_interval_, this, |
| 189 &ImportantFileWriter::DoScheduledWrite); | 190 &ImportantFileWriter::DoScheduledWrite); |
| 190 } | 191 } |
| 191 } | 192 } |
| 192 | 193 |
| 193 void ImportantFileWriter::DoScheduledWrite() { | 194 void ImportantFileWriter::DoScheduledWrite() { |
| 194 DCHECK(serializer_); | 195 DCHECK(serializer_); |
| 195 scoped_ptr<std::string> data(new std::string); | 196 std::unique_ptr<std::string> data(new std::string); |
| 196 if (serializer_->SerializeData(data.get())) { | 197 if (serializer_->SerializeData(data.get())) { |
| 197 WriteNow(std::move(data)); | 198 WriteNow(std::move(data)); |
| 198 } else { | 199 } else { |
| 199 DLOG(WARNING) << "failed to serialize data to be saved in " | 200 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 200 << path_.value(); | 201 << path_.value(); |
| 201 } | 202 } |
| 202 serializer_ = nullptr; | 203 serializer_ = nullptr; |
| 203 } | 204 } |
| 204 | 205 |
| 205 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( | 206 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 229 | 230 |
| 230 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { | 231 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { |
| 231 DCHECK(CalledOnValidThread()); | 232 DCHECK(CalledOnValidThread()); |
| 232 if (result && !on_next_successful_write_.is_null()) { | 233 if (result && !on_next_successful_write_.is_null()) { |
| 233 on_next_successful_write_.Run(); | 234 on_next_successful_write_.Run(); |
| 234 on_next_successful_write_.Reset(); | 235 on_next_successful_write_.Reset(); |
| 235 } | 236 } |
| 236 } | 237 } |
| 237 | 238 |
| 238 } // namespace base | 239 } // namespace base |
| OLD | NEW |