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

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

Issue 273243002: Revert 269415 "Introduce a new framework for back-and-forth trac..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: 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"
27 #include "base/threading/thread.h" 26 #include "base/threading/thread.h"
28 #include "base/time/time.h" 27 #include "base/time/time.h"
29 28
30 namespace base { 29 namespace base {
31 30
32 namespace { 31 namespace {
33 32
34 const int kDefaultCommitIntervalMs = 10000; 33 const int kDefaultCommitIntervalMs = 10000;
35 34
36 enum TempFileFailure { 35 enum TempFileFailure {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 86
88 if (!base::ReplaceFile(tmp_file_path, path, NULL)) { 87 if (!base::ReplaceFile(tmp_file_path, path, NULL)) {
89 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); 88 LogFailure(path, FAILED_RENAMING, "could not rename temporary file");
90 base::DeleteFile(tmp_file_path, false); 89 base::DeleteFile(tmp_file_path, false);
91 return false; 90 return false;
92 } 91 }
93 92
94 return true; 93 return true;
95 } 94 }
96 95
97 ImportantFileWriter::ImportantFileWriter(const FilePath& path, 96 ImportantFileWriter::ImportantFileWriter(
98 base::SequencedTaskRunner* task_runner) 97 const FilePath& path, base::SequencedTaskRunner* task_runner)
99 : path_(path), 98 : path_(path),
100 task_runner_(task_runner), 99 task_runner_(task_runner),
101 serializer_(NULL), 100 serializer_(NULL),
102 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)), 101 commit_interval_(TimeDelta::FromMilliseconds(
103 weak_factory_(this) { 102 kDefaultCommitIntervalMs)) {
104 DCHECK(CalledOnValidThread()); 103 DCHECK(CalledOnValidThread());
105 DCHECK(task_runner_.get()); 104 DCHECK(task_runner_.get());
106 } 105 }
107 106
108 ImportantFileWriter::~ImportantFileWriter() { 107 ImportantFileWriter::~ImportantFileWriter() {
109 // We're usually a member variable of some other object, which also tends 108 // We're usually a member variable of some other object, which also tends
110 // to be our serializer. It may not be safe to call back to the parent object 109 // to be our serializer. It may not be safe to call back to the parent object
111 // being destructed. 110 // being destructed.
112 DCHECK(!HasPendingWrite()); 111 DCHECK(!HasPendingWrite());
113 } 112 }
114 113
115 bool ImportantFileWriter::HasPendingWrite() const { 114 bool ImportantFileWriter::HasPendingWrite() const {
116 DCHECK(CalledOnValidThread()); 115 DCHECK(CalledOnValidThread());
117 return timer_.IsRunning(); 116 return timer_.IsRunning();
118 } 117 }
119 118
120 void ImportantFileWriter::WriteNow(const std::string& data) { 119 void ImportantFileWriter::WriteNow(const std::string& data) {
121 DCHECK(CalledOnValidThread()); 120 DCHECK(CalledOnValidThread());
122 if (data.length() > static_cast<size_t>(kint32max)) { 121 if (data.length() > static_cast<size_t>(kint32max)) {
123 NOTREACHED(); 122 NOTREACHED();
124 return; 123 return;
125 } 124 }
126 125
127 if (HasPendingWrite()) 126 if (HasPendingWrite())
128 timer_.Stop(); 127 timer_.Stop();
129 128
130 if (!base::PostTaskAndReplyWithResult( 129 if (!task_runner_->PostTask(
131 task_runner_,
132 FROM_HERE, 130 FROM_HERE,
133 MakeCriticalClosure( 131 MakeCriticalClosure(
134 Bind(&ImportantFileWriter::WriteFileAtomically, path_, data)), 132 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically),
135 Bind(&ImportantFileWriter::ForwardSuccessfulWrite, 133 path_, data)))) {
136 weak_factory_.GetWeakPtr()))) {
137 // Posting the task to background message loop is not expected 134 // Posting the task to background message loop is not expected
138 // to fail, but if it does, avoid losing data and just hit the disk 135 // to fail, but if it does, avoid losing data and just hit the disk
139 // on the current thread. 136 // on the current thread.
140 NOTREACHED(); 137 NOTREACHED();
141 138
142 WriteFileAtomically(path_, data); 139 WriteFileAtomically(path_, data);
143 } 140 }
144 } 141 }
145 142
146 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { 143 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) {
(...skipping 13 matching lines...) Expand all
160 std::string data; 157 std::string data;
161 if (serializer_->SerializeData(&data)) { 158 if (serializer_->SerializeData(&data)) {
162 WriteNow(data); 159 WriteNow(data);
163 } else { 160 } else {
164 DLOG(WARNING) << "failed to serialize data to be saved in " 161 DLOG(WARNING) << "failed to serialize data to be saved in "
165 << path_.value().c_str(); 162 << path_.value().c_str();
166 } 163 }
167 serializer_ = NULL; 164 serializer_ = NULL;
168 } 165 }
169 166
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
184 } // namespace base 167 } // namespace base
OLDNEW
« no previous file with comments | « trunk/src/base/files/important_file_writer.h ('k') | trunk/src/base/files/important_file_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698