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