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

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

Issue 257003007: Introduce a new framework for back-and-forth tracked/protected preferences migration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: +cleanup-only tests Created 6 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 | Annotate | Revision Log
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 #if defined _MSC_VER && _MSC_VER == 1800 5 #if defined _MSC_VER && _MSC_VER == 1800
6 // TODO(scottmg): Internal errors on VS2013 RC in LTCG. This should be removed 6 // TODO(scottmg): Internal errors on VS2013 RC in LTCG. This should be removed
7 // after RTM. http://crbug.com/288948 7 // after RTM. http://crbug.com/288948
8 #pragma optimize("", off) 8 #pragma optimize("", off)
9 #endif 9 #endif
10 10
11 #include "base/files/important_file_writer.h" 11 #include "base/files/important_file_writer.h"
12 12
13 #include <stdio.h> 13 #include <stdio.h>
14 14
15 #include <string> 15 #include <string>
16 16
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/critical_closure.h" 18 #include "base/critical_closure.h"
19 #include "base/file_util.h" 19 #include "base/file_util.h"
20 #include "base/files/file.h" 20 #include "base/files/file.h"
21 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
22 #include "base/logging.h" 22 #include "base/logging.h"
23 #include "base/metrics/histogram.h" 23 #include "base/metrics/histogram.h"
24 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
25 #include "base/task_runner.h" 25 #include "base/task_runner.h"
26 #include "base/task_runner_util.h"
26 #include "base/threading/thread.h" 27 #include "base/threading/thread.h"
27 #include "base/time/time.h" 28 #include "base/time/time.h"
28 29
29 namespace base { 30 namespace base {
30 31
31 namespace { 32 namespace {
32 33
33 const int kDefaultCommitIntervalMs = 10000; 34 const int kDefaultCommitIntervalMs = 10000;
34 35
35 enum TempFileFailure { 36 enum TempFileFailure {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 87
87 if (!base::ReplaceFile(tmp_file_path, path, NULL)) { 88 if (!base::ReplaceFile(tmp_file_path, path, NULL)) {
88 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); 89 LogFailure(path, FAILED_RENAMING, "could not rename temporary file");
89 base::DeleteFile(tmp_file_path, false); 90 base::DeleteFile(tmp_file_path, false);
90 return false; 91 return false;
91 } 92 }
92 93
93 return true; 94 return true;
94 } 95 }
95 96
96 ImportantFileWriter::ImportantFileWriter( 97 ImportantFileWriter::ImportantFileWriter(const FilePath& path,
97 const FilePath& path, base::SequencedTaskRunner* task_runner) 98 base::SequencedTaskRunner* task_runner)
98 : path_(path), 99 : path_(path),
99 task_runner_(task_runner), 100 task_runner_(task_runner),
100 serializer_(NULL), 101 serializer_(NULL),
101 commit_interval_(TimeDelta::FromMilliseconds( 102 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)),
102 kDefaultCommitIntervalMs)) { 103 weak_factory_(this) {
103 DCHECK(CalledOnValidThread()); 104 DCHECK(CalledOnValidThread());
104 DCHECK(task_runner_.get()); 105 DCHECK(task_runner_.get());
105 } 106 }
106 107
107 ImportantFileWriter::~ImportantFileWriter() { 108 ImportantFileWriter::~ImportantFileWriter() {
108 // We're usually a member variable of some other object, which also tends 109 // We're usually a member variable of some other object, which also tends
109 // to be our serializer. It may not be safe to call back to the parent object 110 // to be our serializer. It may not be safe to call back to the parent object
110 // being destructed. 111 // being destructed.
111 DCHECK(!HasPendingWrite()); 112 DCHECK(!HasPendingWrite());
112 } 113 }
113 114
114 bool ImportantFileWriter::HasPendingWrite() const { 115 bool ImportantFileWriter::HasPendingWrite() const {
115 DCHECK(CalledOnValidThread()); 116 DCHECK(CalledOnValidThread());
116 return timer_.IsRunning(); 117 return timer_.IsRunning();
117 } 118 }
118 119
119 void ImportantFileWriter::WriteNow(const std::string& data) { 120 void ImportantFileWriter::WriteNow(const std::string& data) {
120 DCHECK(CalledOnValidThread()); 121 DCHECK(CalledOnValidThread());
121 if (data.length() > static_cast<size_t>(kint32max)) { 122 if (data.length() > static_cast<size_t>(kint32max)) {
122 NOTREACHED(); 123 NOTREACHED();
123 return; 124 return;
124 } 125 }
125 126
126 if (HasPendingWrite()) 127 if (HasPendingWrite())
127 timer_.Stop(); 128 timer_.Stop();
128 129
129 if (!task_runner_->PostTask( 130 if (!base::PostTaskAndReplyWithResult(
131 task_runner_,
130 FROM_HERE, 132 FROM_HERE,
131 MakeCriticalClosure( 133 MakeCriticalClosure(
132 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically), 134 Bind(&ImportantFileWriter::WriteFileAtomically, path_, data)),
133 path_, data)))) { 135 Bind(&ImportantFileWriter::ForwardSuccessfulWrite,
136 weak_factory_.GetWeakPtr()))) {
134 // Posting the task to background message loop is not expected 137 // Posting the task to background message loop is not expected
135 // to fail, but if it does, avoid losing data and just hit the disk 138 // to fail, but if it does, avoid losing data and just hit the disk
136 // on the current thread. 139 // on the current thread.
137 NOTREACHED(); 140 NOTREACHED();
138 141
139 WriteFileAtomically(path_, data); 142 WriteFileAtomically(path_, data);
140 } 143 }
141 } 144 }
142 145
143 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { 146 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) {
(...skipping 13 matching lines...) Expand all
157 std::string data; 160 std::string data;
158 if (serializer_->SerializeData(&data)) { 161 if (serializer_->SerializeData(&data)) {
159 WriteNow(data); 162 WriteNow(data);
160 } else { 163 } else {
161 DLOG(WARNING) << "failed to serialize data to be saved in " 164 DLOG(WARNING) << "failed to serialize data to be saved in "
162 << path_.value().c_str(); 165 << path_.value().c_str();
163 } 166 }
164 serializer_ = NULL; 167 serializer_ = NULL;
165 } 168 }
166 169
170 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback(
171 const base::Closure& on_next_successful_write) {
172 DCHECK(on_next_successful_write_.is_null());
173 on_next_successful_write_ = on_next_successful_write;
174 }
175
176 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) {
177 DCHECK(CalledOnValidThread());
178 if (result && !on_next_successful_write_.is_null()) {
179 on_next_successful_write_.Run();
180 on_next_successful_write_.Reset();
181 }
182 }
183
167 } // namespace base 184 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698