| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 ImportantFileWriter::ImportantFileWriter( | 146 ImportantFileWriter::ImportantFileWriter( |
| 147 const FilePath& path, | 147 const FilePath& path, |
| 148 scoped_refptr<SequencedTaskRunner> task_runner, | 148 scoped_refptr<SequencedTaskRunner> task_runner, |
| 149 TimeDelta interval) | 149 TimeDelta interval) |
| 150 : path_(path), | 150 : path_(path), |
| 151 task_runner_(std::move(task_runner)), | 151 task_runner_(std::move(task_runner)), |
| 152 serializer_(nullptr), | 152 serializer_(nullptr), |
| 153 commit_interval_(interval), | 153 commit_interval_(interval), |
| 154 weak_factory_(this) { | 154 weak_factory_(this) { |
| 155 DCHECK(CalledOnValidThread()); | |
| 156 DCHECK(task_runner_); | 155 DCHECK(task_runner_); |
| 157 } | 156 } |
| 158 | 157 |
| 159 ImportantFileWriter::~ImportantFileWriter() { | 158 ImportantFileWriter::~ImportantFileWriter() { |
| 159 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 160 // We're usually a member variable of some other object, which also tends | 160 // We're usually a member variable of some other object, which also tends |
| 161 // to be our serializer. It may not be safe to call back to the parent object | 161 // to be our serializer. It may not be safe to call back to the parent object |
| 162 // being destructed. | 162 // being destructed. |
| 163 DCHECK(!HasPendingWrite()); | 163 DCHECK(!HasPendingWrite()); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool ImportantFileWriter::HasPendingWrite() const { | 166 bool ImportantFileWriter::HasPendingWrite() const { |
| 167 DCHECK(CalledOnValidThread()); | 167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 168 return timer().IsRunning(); | 168 return timer().IsRunning(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { | 171 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { |
| 172 DCHECK(CalledOnValidThread()); | 172 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 173 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { | 173 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { |
| 174 NOTREACHED(); | 174 NOTREACHED(); |
| 175 return; | 175 return; |
| 176 } | 176 } |
| 177 | 177 |
| 178 Closure task = AdaptCallbackForRepeating( | 178 Closure task = AdaptCallbackForRepeating( |
| 179 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data), | 179 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data), |
| 180 std::move(before_next_write_callback_), | 180 std::move(before_next_write_callback_), |
| 181 std::move(after_next_write_callback_))); | 181 std::move(after_next_write_callback_))); |
| 182 | 182 |
| 183 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { | 183 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { |
| 184 // Posting the task to background message loop is not expected | 184 // Posting the task to background message loop is not expected |
| 185 // to fail, but if it does, avoid losing data and just hit the disk | 185 // to fail, but if it does, avoid losing data and just hit the disk |
| 186 // on the current thread. | 186 // on the current thread. |
| 187 NOTREACHED(); | 187 NOTREACHED(); |
| 188 | 188 |
| 189 task.Run(); | 189 task.Run(); |
| 190 } | 190 } |
| 191 ClearPendingWrite(); | 191 ClearPendingWrite(); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 194 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
| 195 DCHECK(CalledOnValidThread()); | 195 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 196 | 196 |
| 197 DCHECK(serializer); | 197 DCHECK(serializer); |
| 198 serializer_ = serializer; | 198 serializer_ = serializer; |
| 199 | 199 |
| 200 if (!timer().IsRunning()) { | 200 if (!timer().IsRunning()) { |
| 201 timer().Start( | 201 timer().Start( |
| 202 FROM_HERE, commit_interval_, | 202 FROM_HERE, commit_interval_, |
| 203 Bind(&ImportantFileWriter::DoScheduledWrite, Unretained(this))); | 203 Bind(&ImportantFileWriter::DoScheduledWrite, Unretained(this))); |
| 204 } | 204 } |
| 205 } | 205 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 226 void ImportantFileWriter::ClearPendingWrite() { | 226 void ImportantFileWriter::ClearPendingWrite() { |
| 227 timer().Stop(); | 227 timer().Stop(); |
| 228 serializer_ = nullptr; | 228 serializer_ = nullptr; |
| 229 } | 229 } |
| 230 | 230 |
| 231 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) { | 231 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) { |
| 232 timer_override_ = timer_override; | 232 timer_override_ = timer_override; |
| 233 } | 233 } |
| 234 | 234 |
| 235 } // namespace base | 235 } // namespace base |
| OLD | NEW |