Allow supervised users to create bookmark apps.
[chromium-blink-merge.git] / base / files / important_file_writer.cc
blob814fc7b9add0eb8e30d951b5784e8ec817c64055
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
3 // found in the LICENSE file.
5 #include "base/files/important_file_writer.h"
7 #include <stdio.h>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/critical_closure.h"
13 #include "base/debug/alias.h"
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/logging.h"
18 #include "base/metrics/histogram.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
21 #include "base/task_runner.h"
22 #include "base/task_runner_util.h"
23 #include "base/threading/thread.h"
24 #include "base/time/time.h"
26 namespace base {
28 namespace {
30 const int kDefaultCommitIntervalMs = 10000;
32 // This enum is used to define the buckets for an enumerated UMA histogram.
33 // Hence,
34 // (a) existing enumerated constants should never be deleted or reordered, and
35 // (b) new constants should only be appended at the end of the enumeration.
36 enum TempFileFailure {
37 FAILED_CREATING,
38 FAILED_OPENING,
39 FAILED_CLOSING, // Unused.
40 FAILED_WRITING,
41 FAILED_RENAMING,
42 FAILED_FLUSHING,
43 TEMP_FILE_FAILURE_MAX
46 void LogFailure(const FilePath& path, TempFileFailure failure_code,
47 const std::string& message) {
48 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code,
49 TEMP_FILE_FAILURE_MAX);
50 DPLOG(WARNING) << "temp file failure: " << path.value().c_str()
51 << " : " << message;
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);
60 } // namespace
62 // static
63 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
64 const std::string& data) {
65 #if defined(OS_CHROMEOS)
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.
68 // Include some info to the report for investigation. crbug.com/418627
69 // TODO(hashimoto): Remove this.
70 struct {
71 size_t data_size;
72 char path[128];
73 } file_info;
74 file_info.data_size = data.size();
75 base::strlcpy(file_info.path, path.value().c_str(),
76 arraysize(file_info.path));
77 base::debug::Alias(&file_info);
78 #endif
79 // Write the data to a temp file then rename to avoid data loss if we crash
80 // while writing the file. Ensure that the temp file is on the same volume
81 // as target file, so it can be moved in one step, and that the temp file
82 // is securely created.
83 FilePath tmp_file_path;
84 if (!base::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
85 LogFailure(path, FAILED_CREATING, "could not create temporary file");
86 return false;
89 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE);
90 if (!tmp_file.IsValid()) {
91 LogFailure(path, FAILED_OPENING, "could not open temporary file");
92 return false;
95 // If this happens in the wild something really bad is going on.
96 CHECK_LE(data.length(), static_cast<size_t>(kint32max));
97 int bytes_written = tmp_file.Write(0, data.data(),
98 static_cast<int>(data.length()));
99 bool flush_success = tmp_file.Flush();
100 tmp_file.Close();
102 if (bytes_written < static_cast<int>(data.length())) {
103 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" +
104 IntToString(bytes_written));
105 base::DeleteFile(tmp_file_path, false);
106 return false;
109 if (!flush_success) {
110 LogFailure(path, FAILED_FLUSHING, "error flushing");
111 base::DeleteFile(tmp_file_path, false);
112 return false;
115 if (!base::ReplaceFile(tmp_file_path, path, NULL)) {
116 LogFailure(path, FAILED_RENAMING, "could not rename temporary file");
117 base::DeleteFile(tmp_file_path, false);
118 return false;
121 return true;
124 ImportantFileWriter::ImportantFileWriter(
125 const FilePath& path,
126 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
127 : path_(path),
128 task_runner_(task_runner),
129 serializer_(NULL),
130 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)),
131 weak_factory_(this) {
132 DCHECK(CalledOnValidThread());
133 DCHECK(task_runner_);
136 ImportantFileWriter::~ImportantFileWriter() {
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
139 // being destructed.
140 DCHECK(!HasPendingWrite());
143 bool ImportantFileWriter::HasPendingWrite() const {
144 DCHECK(CalledOnValidThread());
145 return timer_.IsRunning();
148 void ImportantFileWriter::WriteNow(scoped_ptr<std::string> data) {
149 DCHECK(CalledOnValidThread());
150 if (data->length() > static_cast<size_t>(kint32max)) {
151 NOTREACHED();
152 return;
155 if (HasPendingWrite())
156 timer_.Stop();
158 auto task = Bind(&WriteScopedStringToFileAtomically, path_, Passed(&data));
159 if (!PostWriteTask(task)) {
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
162 // on the current thread.
163 NOTREACHED();
165 task.Run();
169 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) {
170 DCHECK(CalledOnValidThread());
172 DCHECK(serializer);
173 serializer_ = serializer;
175 if (!timer_.IsRunning()) {
176 timer_.Start(FROM_HERE, commit_interval_, this,
177 &ImportantFileWriter::DoScheduledWrite);
181 void ImportantFileWriter::DoScheduledWrite() {
182 DCHECK(serializer_);
183 scoped_ptr<std::string> data(new std::string);
184 if (serializer_->SerializeData(data.get())) {
185 WriteNow(data.Pass());
186 } else {
187 DLOG(WARNING) << "failed to serialize data to be saved in "
188 << path_.value().c_str();
190 serializer_ = NULL;
193 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback(
194 const base::Closure& on_next_successful_write) {
195 DCHECK(on_next_successful_write_.is_null());
196 on_next_successful_write_ = on_next_successful_write;
199 bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) {
200 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let
201 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but
202 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and
203 // suppressing all of those is unrealistic hence we avoid most of them by
204 // using PostTask() in the typical scenario below.
205 if (!on_next_successful_write_.is_null()) {
206 return base::PostTaskAndReplyWithResult(
207 task_runner_.get(),
208 FROM_HERE,
209 MakeCriticalClosure(task),
210 Bind(&ImportantFileWriter::ForwardSuccessfulWrite,
211 weak_factory_.GetWeakPtr()));
213 return task_runner_->PostTask(
214 FROM_HERE,
215 MakeCriticalClosure(base::Bind(IgnoreResult(task))));
218 void ImportantFileWriter::ForwardSuccessfulWrite(bool result) {
219 DCHECK(CalledOnValidThread());
220 if (result && !on_next_successful_write_.is_null()) {
221 on_next_successful_write_.Run();
222 on_next_successful_write_.Reset();
226 } // namespace base