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 <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 }; | 44 }; |
45 | 45 |
46 void LogFailure(const FilePath& path, TempFileFailure failure_code, | 46 void LogFailure(const FilePath& path, TempFileFailure failure_code, |
47 const std::string& message) { | 47 const std::string& message) { |
48 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, | 48 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, |
49 TEMP_FILE_FAILURE_MAX); | 49 TEMP_FILE_FAILURE_MAX); |
50 DPLOG(WARNING) << "temp file failure: " << path.value().c_str() | 50 DPLOG(WARNING) << "temp file failure: " << path.value().c_str() |
51 << " : " << message; | 51 << " : " << message; |
52 } | 52 } |
53 | 53 |
| 54 // Helper function to call WriteFileAtomically() with a scoped_ptr<std::string>. |
| 55 bool WriteScopedStringToFileAtomically(const FilePath& path, |
| 56 scoped_ptr<std::string> data) { |
| 57 return ImportantFileWriter::WriteFileAtomically(path, *data); |
| 58 } |
| 59 |
54 } // namespace | 60 } // namespace |
55 | 61 |
56 // static | 62 // static |
57 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, | 63 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, |
58 const std::string& data) { | 64 const std::string& data) { |
59 #if defined(OS_CHROMEOS) | 65 #if defined(OS_CHROMEOS) |
60 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, | 66 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, |
61 // and this function seems to be one of the slowest shutdown steps. | 67 // and this function seems to be one of the slowest shutdown steps. |
62 // Include some info to the report for investigation. crbug.com/418627 | 68 // Include some info to the report for investigation. crbug.com/418627 |
63 // TODO(hashimoto): Remove this. | 69 // TODO(hashimoto): Remove this. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 123 |
118 ImportantFileWriter::ImportantFileWriter( | 124 ImportantFileWriter::ImportantFileWriter( |
119 const FilePath& path, | 125 const FilePath& path, |
120 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 126 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
121 : path_(path), | 127 : path_(path), |
122 task_runner_(task_runner), | 128 task_runner_(task_runner), |
123 serializer_(NULL), | 129 serializer_(NULL), |
124 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)), | 130 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)), |
125 weak_factory_(this) { | 131 weak_factory_(this) { |
126 DCHECK(CalledOnValidThread()); | 132 DCHECK(CalledOnValidThread()); |
127 DCHECK(task_runner_.get()); | 133 DCHECK(task_runner_); |
128 } | 134 } |
129 | 135 |
130 ImportantFileWriter::~ImportantFileWriter() { | 136 ImportantFileWriter::~ImportantFileWriter() { |
131 // We're usually a member variable of some other object, which also tends | 137 // We're usually a member variable of some other object, which also tends |
132 // to be our serializer. It may not be safe to call back to the parent object | 138 // to be our serializer. It may not be safe to call back to the parent object |
133 // being destructed. | 139 // being destructed. |
134 DCHECK(!HasPendingWrite()); | 140 DCHECK(!HasPendingWrite()); |
135 } | 141 } |
136 | 142 |
137 bool ImportantFileWriter::HasPendingWrite() const { | 143 bool ImportantFileWriter::HasPendingWrite() const { |
138 DCHECK(CalledOnValidThread()); | 144 DCHECK(CalledOnValidThread()); |
139 return timer_.IsRunning(); | 145 return timer_.IsRunning(); |
140 } | 146 } |
141 | 147 |
142 void ImportantFileWriter::WriteNow(const std::string& data) { | 148 void ImportantFileWriter::WriteNow(scoped_ptr<std::string> data) { |
143 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
144 if (data.length() > static_cast<size_t>(kint32max)) { | 150 if (data->length() > static_cast<size_t>(kint32max)) { |
145 NOTREACHED(); | 151 NOTREACHED(); |
146 return; | 152 return; |
147 } | 153 } |
148 | 154 |
149 if (HasPendingWrite()) | 155 if (HasPendingWrite()) |
150 timer_.Stop(); | 156 timer_.Stop(); |
151 | 157 |
152 if (!PostWriteTask(data)) { | 158 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); |
| 159 if (!PostWriteTask(task)) { |
153 // Posting the task to background message loop is not expected | 160 // Posting the task to background message loop is not expected |
154 // to fail, but if it does, avoid losing data and just hit the disk | 161 // to fail, but if it does, avoid losing data and just hit the disk |
155 // on the current thread. | 162 // on the current thread. |
156 NOTREACHED(); | 163 NOTREACHED(); |
157 | 164 |
158 WriteFileAtomically(path_, data); | 165 task.Run(); |
159 } | 166 } |
160 } | 167 } |
161 | 168 |
162 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 169 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
163 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
164 | 171 |
165 DCHECK(serializer); | 172 DCHECK(serializer); |
166 serializer_ = serializer; | 173 serializer_ = serializer; |
167 | 174 |
168 if (!timer_.IsRunning()) { | 175 if (!timer_.IsRunning()) { |
169 timer_.Start(FROM_HERE, commit_interval_, this, | 176 timer_.Start(FROM_HERE, commit_interval_, this, |
170 &ImportantFileWriter::DoScheduledWrite); | 177 &ImportantFileWriter::DoScheduledWrite); |
171 } | 178 } |
172 } | 179 } |
173 | 180 |
174 void ImportantFileWriter::DoScheduledWrite() { | 181 void ImportantFileWriter::DoScheduledWrite() { |
175 DCHECK(serializer_); | 182 DCHECK(serializer_); |
176 std::string data; | 183 scoped_ptr<std::string> data(new std::string); |
177 if (serializer_->SerializeData(&data)) { | 184 if (serializer_->SerializeData(data.get())) { |
178 WriteNow(data); | 185 WriteNow(data.Pass()); |
179 } else { | 186 } else { |
180 DLOG(WARNING) << "failed to serialize data to be saved in " | 187 DLOG(WARNING) << "failed to serialize data to be saved in " |
181 << path_.value().c_str(); | 188 << path_.value().c_str(); |
182 } | 189 } |
183 serializer_ = NULL; | 190 serializer_ = NULL; |
184 } | 191 } |
185 | 192 |
186 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( | 193 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( |
187 const base::Closure& on_next_successful_write) { | 194 const base::Closure& on_next_successful_write) { |
188 DCHECK(on_next_successful_write_.is_null()); | 195 DCHECK(on_next_successful_write_.is_null()); |
189 on_next_successful_write_ = on_next_successful_write; | 196 on_next_successful_write_ = on_next_successful_write; |
190 } | 197 } |
191 | 198 |
192 bool ImportantFileWriter::PostWriteTask(const std::string& data) { | 199 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) { |
193 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let | 200 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let |
194 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but | 201 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but |
195 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and | 202 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and |
196 // suppressing all of those is unrealistic hence we avoid most of them by | 203 // suppressing all of those is unrealistic hence we avoid most of them by |
197 // using PostTask() in the typical scenario below. | 204 // using PostTask() in the typical scenario below. |
198 if (!on_next_successful_write_.is_null()) { | 205 if (!on_next_successful_write_.is_null()) { |
199 return base::PostTaskAndReplyWithResult( | 206 return base::PostTaskAndReplyWithResult( |
200 task_runner_.get(), | 207 task_runner_.get(), |
201 FROM_HERE, | 208 FROM_HERE, |
202 MakeCriticalClosure( | 209 MakeCriticalClosure(task), |
203 Bind(&ImportantFileWriter::WriteFileAtomically, path_, data)), | |
204 Bind(&ImportantFileWriter::ForwardSuccessfulWrite, | 210 Bind(&ImportantFileWriter::ForwardSuccessfulWrite, |
205 weak_factory_.GetWeakPtr())); | 211 weak_factory_.GetWeakPtr())); |
206 } | 212 } |
207 return task_runner_->PostTask( | 213 return task_runner_->PostTask( |
208 FROM_HERE, | 214 FROM_HERE, |
209 MakeCriticalClosure( | 215 MakeCriticalClosure(base::Bind(IgnoreResult(task)))); |
210 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically), | |
211 path_, data))); | |
212 } | 216 } |
213 | 217 |
214 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { | 218 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { |
215 DCHECK(CalledOnValidThread()); | 219 DCHECK(CalledOnValidThread()); |
216 if (result && !on_next_successful_write_.is_null()) { | 220 if (result && !on_next_successful_write_.is_null()) { |
217 on_next_successful_write_.Run(); | 221 on_next_successful_write_.Run(); |
218 on_next_successful_write_.Reset(); | 222 on_next_successful_write_.Reset(); |
219 } | 223 } |
220 } | 224 } |
221 | 225 |
222 } // namespace base | 226 } // namespace base |
OLD | NEW |