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

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: run cleanup directly if destination store wasn't altered (i.e. migration completed in previous run,… 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 93
93 return true; 94 return true;
94 } 95 }
95 96
96 ImportantFileWriter::ImportantFileWriter( 97 ImportantFileWriter::ImportantFileWriter(
97 const FilePath& path, base::SequencedTaskRunner* task_runner) 98 const FilePath& path, 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(
102 kDefaultCommitIntervalMs)) { 103 kDefaultCommitIntervalMs)),
104 weak_factory_(this) {
103 DCHECK(CalledOnValidThread()); 105 DCHECK(CalledOnValidThread());
104 DCHECK(task_runner_.get()); 106 DCHECK(task_runner_.get());
105 } 107 }
106 108
107 ImportantFileWriter::~ImportantFileWriter() { 109 ImportantFileWriter::~ImportantFileWriter() {
108 // We're usually a member variable of some other object, which also tends 110 // 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 111 // to be our serializer. It may not be safe to call back to the parent object
110 // being destructed. 112 // being destructed.
111 DCHECK(!HasPendingWrite()); 113 DCHECK(!HasPendingWrite());
112 } 114 }
113 115
114 bool ImportantFileWriter::HasPendingWrite() const { 116 bool ImportantFileWriter::HasPendingWrite() const {
115 DCHECK(CalledOnValidThread()); 117 DCHECK(CalledOnValidThread());
116 return timer_.IsRunning(); 118 return timer_.IsRunning();
117 } 119 }
118 120
119 void ImportantFileWriter::WriteNow(const std::string& data) { 121 void ImportantFileWriter::WriteNow(const std::string& data) {
120 DCHECK(CalledOnValidThread()); 122 DCHECK(CalledOnValidThread());
121 if (data.length() > static_cast<size_t>(kint32max)) { 123 if (data.length() > static_cast<size_t>(kint32max)) {
122 NOTREACHED(); 124 NOTREACHED();
123 return; 125 return;
124 } 126 }
125 127
126 if (HasPendingWrite()) 128 if (HasPendingWrite())
127 timer_.Stop(); 129 timer_.Stop();
128 130
129 if (!task_runner_->PostTask( 131 if (!base::PostTaskAndReplyWithResult(
132 task_runner_,
130 FROM_HERE, 133 FROM_HERE,
131 MakeCriticalClosure( 134 MakeCriticalClosure(
132 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically), 135 Bind(&ImportantFileWriter::WriteFileAtomically,
133 path_, data)))) { 136 path_, data)),
137 Bind(&ImportantFileWriter::ForwardSuccessfulWrite,
138 weak_factory_.GetWeakPtr()))) {
134 // Posting the task to background message loop is not expected 139 // 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 140 // to fail, but if it does, avoid losing data and just hit the disk
136 // on the current thread. 141 // on the current thread.
137 NOTREACHED(); 142 NOTREACHED();
138 143
139 WriteFileAtomically(path_, data); 144 WriteFileAtomically(path_, data);
140 } 145 }
141 } 146 }
142 147
143 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { 148 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) {
(...skipping 13 matching lines...) Expand all
157 std::string data; 162 std::string data;
158 if (serializer_->SerializeData(&data)) { 163 if (serializer_->SerializeData(&data)) {
159 WriteNow(data); 164 WriteNow(data);
160 } else { 165 } else {
161 DLOG(WARNING) << "failed to serialize data to be saved in " 166 DLOG(WARNING) << "failed to serialize data to be saved in "
162 << path_.value().c_str(); 167 << path_.value().c_str();
163 } 168 }
164 serializer_ = NULL; 169 serializer_ = NULL;
165 } 170 }
166 171
172 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback(
173 const base::Closure& on_next_successful_write) {
174 DCHECK(on_next_successful_write_.is_null());
175 on_next_successful_write_ = on_next_successful_write;
176 }
177
178 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) {
179 DCHECK(CalledOnValidThread());
180 if (result && !on_next_successful_write_.is_null()) {
181 on_next_successful_write_.Run();
182 on_next_successful_write_.Reset();
183 }
184 }
185
167 } // namespace base 186 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698