Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: base/files/important_file_writer.cc

Issue 1127963002: Implement lossy pref behavior for JsonPrefStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@prefs-fix-flags
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
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>. 54 // Helper function to call WriteFileAtomically() with a scoped_ptr<std::string>.
55 bool WriteScopedStringToFileAtomically(const FilePath& path, 55 bool WriteScopedStringToFileAtomically(const FilePath& path,
56 scoped_ptr<std::string> data) { 56 scoped_ptr<std::string> data) {
57 return ImportantFileWriter::WriteFileAtomically(path, *data); 57 return ImportantFileWriterImpl::WriteFileAtomically(path, *data);
58 } 58 }
59 59
60 } // namespace 60 } // namespace
61 61
62 // static 62 // static
63 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, 63 bool ImportantFileWriterImpl::WriteFileAtomically(const FilePath& path,
64 const std::string& data) { 64 const std::string& data) {
65 #if defined(OS_CHROMEOS) 65 #if defined(OS_CHROMEOS)
66 // 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,
67 // 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.
68 // Include some info to the report for investigation. crbug.com/418627 68 // Include some info to the report for investigation. crbug.com/418627
69 // TODO(hashimoto): Remove this. 69 // TODO(hashimoto): Remove this.
70 struct { 70 struct {
71 size_t data_size; 71 size_t data_size;
72 char path[128]; 72 char path[128];
73 } file_info; 73 } file_info;
74 file_info.data_size = data.size(); 74 file_info.data_size = data.size();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 114
115 if (!base::ReplaceFile(tmp_file_path, path, NULL)) { 115 if (!base::ReplaceFile(tmp_file_path, path, NULL)) {
116 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); 116 LogFailure(path, FAILED_RENAMING, "could not rename temporary file");
117 base::DeleteFile(tmp_file_path, false); 117 base::DeleteFile(tmp_file_path, false);
118 return false; 118 return false;
119 } 119 }
120 120
121 return true; 121 return true;
122 } 122 }
123 123
124 ImportantFileWriter::ImportantFileWriter( 124 ImportantFileWriterImpl::ImportantFileWriterImpl(
125 const FilePath& path, 125 const FilePath& path,
126 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 126 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
127 : path_(path), 127 : path_(path),
128 task_runner_(task_runner), 128 task_runner_(task_runner),
129 serializer_(NULL), 129 serializer_(NULL),
130 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)), 130 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)),
131 weak_factory_(this) { 131 weak_factory_(this) {
132 DCHECK(CalledOnValidThread()); 132 DCHECK(CalledOnValidThread());
133 DCHECK(task_runner_); 133 DCHECK(task_runner_);
134 } 134 }
135 135
136 ImportantFileWriter::~ImportantFileWriter() { 136 ImportantFileWriterImpl::~ImportantFileWriterImpl() {
137 // 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
138 // 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
139 // being destructed. 139 // being destructed.
140 DCHECK(!HasPendingWrite()); 140 DCHECK(!HasPendingWrite());
141 } 141 }
142 142
143 bool ImportantFileWriter::HasPendingWrite() const { 143 bool ImportantFileWriterImpl::HasPendingWrite() const {
144 DCHECK(CalledOnValidThread()); 144 DCHECK(CalledOnValidThread());
145 return timer_.IsRunning(); 145 return timer_.IsRunning();
146 } 146 }
147 147
148 void ImportantFileWriter::WriteNow(scoped_ptr<std::string> data) { 148 void ImportantFileWriterImpl::WriteNow(scoped_ptr<std::string> data) {
149 DCHECK(CalledOnValidThread()); 149 DCHECK(CalledOnValidThread());
150 if (data->length() > static_cast<size_t>(kint32max)) { 150 if (data->length() > static_cast<size_t>(kint32max)) {
151 NOTREACHED(); 151 NOTREACHED();
152 return; 152 return;
153 } 153 }
154 154
155 if (HasPendingWrite()) 155 if (HasPendingWrite())
156 timer_.Stop(); 156 timer_.Stop();
157 157
158 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data)); 158 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data));
159 if (!PostWriteTask(task)) { 159 if (!PostWriteTask(task)) {
160 // Posting the task to background message loop is not expected 160 // Posting the task to background message loop is not expected
161 // 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
162 // on the current thread. 162 // on the current thread.
163 NOTREACHED(); 163 NOTREACHED();
164 164
165 task.Run(); 165 task.Run();
166 } 166 }
167 } 167 }
168 168
169 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { 169 void ImportantFileWriterImpl::ScheduleWrite(DataSerializer* serializer) {
170 DCHECK(CalledOnValidThread()); 170 DCHECK(CalledOnValidThread());
171 171
172 DCHECK(serializer); 172 DCHECK(serializer);
173 serializer_ = serializer; 173 serializer_ = serializer;
174 174
175 if (!timer_.IsRunning()) { 175 if (!timer_.IsRunning()) {
176 timer_.Start(FROM_HERE, commit_interval_, this, 176 timer_.Start(FROM_HERE, commit_interval_, this,
177 &ImportantFileWriter::DoScheduledWrite); 177 &ImportantFileWriterImpl::DoScheduledWrite);
178 } 178 }
179 } 179 }
180 180
181 void ImportantFileWriter::DoScheduledWrite() { 181 void ImportantFileWriterImpl::DoScheduledWrite() {
182 DCHECK(serializer_); 182 DCHECK(serializer_);
183 scoped_ptr<std::string> data(new std::string); 183 scoped_ptr<std::string> data(new std::string);
184 if (serializer_->SerializeData(data.get())) { 184 if (serializer_->SerializeData(data.get())) {
185 WriteNow(data.Pass()); 185 WriteNow(data.Pass());
186 } else { 186 } else {
187 DLOG(WARNING) << "failed to serialize data to be saved in " 187 DLOG(WARNING) << "failed to serialize data to be saved in "
188 << path_.value().c_str(); 188 << path_.value().c_str();
189 } 189 }
190 serializer_ = NULL; 190 serializer_ = NULL;
191 } 191 }
192 192
193 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback( 193 void ImportantFileWriterImpl::RegisterOnNextSuccessfulWriteCallback(
194 const base::Closure& on_next_successful_write) { 194 const base::Closure& on_next_successful_write) {
195 DCHECK(on_next_successful_write_.is_null()); 195 DCHECK(on_next_successful_write_.is_null());
196 on_next_successful_write_ = on_next_successful_write; 196 on_next_successful_write_ = on_next_successful_write;
197 } 197 }
198 198
199 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) { 199 TimeDelta ImportantFileWriterImpl::CommitInterval() const {
200 return commit_interval_;
201 }
202
203 bool ImportantFileWriterImpl::PostWriteTask(const Callback<bool()>& task) {
200 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let 204 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let
201 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but 205 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but
202 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and 206 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and
203 // suppressing all of those is unrealistic hence we avoid most of them by 207 // suppressing all of those is unrealistic hence we avoid most of them by
204 // using PostTask() in the typical scenario below. 208 // using PostTask() in the typical scenario below.
205 if (!on_next_successful_write_.is_null()) { 209 if (!on_next_successful_write_.is_null()) {
206 return base::PostTaskAndReplyWithResult( 210 return base::PostTaskAndReplyWithResult(
207 task_runner_.get(), 211 task_runner_.get(), FROM_HERE, MakeCriticalClosure(task),
208 FROM_HERE, 212 Bind(&ImportantFileWriterImpl::ForwardSuccessfulWrite,
209 MakeCriticalClosure(task),
210 Bind(&ImportantFileWriter::ForwardSuccessfulWrite,
211 weak_factory_.GetWeakPtr())); 213 weak_factory_.GetWeakPtr()));
212 } 214 }
213 return task_runner_->PostTask( 215 return task_runner_->PostTask(
214 FROM_HERE, 216 FROM_HERE,
215 MakeCriticalClosure(base::Bind(IgnoreResult(task)))); 217 MakeCriticalClosure(base::Bind(IgnoreResult(task))));
216 } 218 }
217 219
218 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) { 220 void ImportantFileWriterImpl::ForwardSuccessfulWrite(bool result) {
219 DCHECK(CalledOnValidThread()); 221 DCHECK(CalledOnValidThread());
220 if (result && !on_next_successful_write_.is_null()) { 222 if (result && !on_next_successful_write_.is_null()) {
221 on_next_successful_write_.Run(); 223 on_next_successful_write_.Run();
222 on_next_successful_write_.Reset(); 224 on_next_successful_write_.Reset();
223 } 225 }
224 } 226 }
225 227
226 } // namespace base 228 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698