| OLD | NEW |
| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/critical_closure.h" | 12 #include "base/critical_closure.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/message_loop_proxy.h" | 16 #include "base/task_runner.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/time.h" | 20 #include "base/time.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kDefaultCommitIntervalMs = 10000; | 26 const int kDefaultCommitIntervalMs = 10000; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 if (!file_util::ReplaceFile(tmp_file_path, path)) { | 83 if (!file_util::ReplaceFile(tmp_file_path, path)) { |
| 84 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); | 84 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); |
| 85 file_util::Delete(tmp_file_path, false); | 85 file_util::Delete(tmp_file_path, false); |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 } // namespace | 90 } // namespace |
| 91 | 91 |
| 92 ImportantFileWriter::ImportantFileWriter( | 92 ImportantFileWriter::ImportantFileWriter( |
| 93 const FilePath& path, MessageLoopProxy* file_message_loop_proxy) | 93 const FilePath& path, base::SequencedTaskRunner* task_runner) |
| 94 : path_(path), | 94 : path_(path), |
| 95 file_message_loop_proxy_(file_message_loop_proxy), | 95 task_runner_(task_runner), |
| 96 serializer_(NULL), | 96 serializer_(NULL), |
| 97 commit_interval_(TimeDelta::FromMilliseconds( | 97 commit_interval_(TimeDelta::FromMilliseconds( |
| 98 kDefaultCommitIntervalMs)) { | 98 kDefaultCommitIntervalMs)) { |
| 99 DCHECK(CalledOnValidThread()); | 99 DCHECK(CalledOnValidThread()); |
| 100 DCHECK(file_message_loop_proxy_.get()); | 100 DCHECK(task_runner_.get()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 ImportantFileWriter::~ImportantFileWriter() { | 103 ImportantFileWriter::~ImportantFileWriter() { |
| 104 // We're usually a member variable of some other object, which also tends | 104 // We're usually a member variable of some other object, which also tends |
| 105 // to be our serializer. It may not be safe to call back to the parent object | 105 // to be our serializer. It may not be safe to call back to the parent object |
| 106 // being destructed. | 106 // being destructed. |
| 107 DCHECK(!HasPendingWrite()); | 107 DCHECK(!HasPendingWrite()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool ImportantFileWriter::HasPendingWrite() const { | 110 bool ImportantFileWriter::HasPendingWrite() const { |
| 111 DCHECK(CalledOnValidThread()); | 111 DCHECK(CalledOnValidThread()); |
| 112 return timer_.IsRunning(); | 112 return timer_.IsRunning(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void ImportantFileWriter::WriteNow(const std::string& data) { | 115 void ImportantFileWriter::WriteNow(const std::string& data) { |
| 116 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 117 if (data.length() > static_cast<size_t>(kint32max)) { | 117 if (data.length() > static_cast<size_t>(kint32max)) { |
| 118 NOTREACHED(); | 118 NOTREACHED(); |
| 119 return; | 119 return; |
| 120 } | 120 } |
| 121 | 121 |
| 122 if (HasPendingWrite()) | 122 if (HasPendingWrite()) |
| 123 timer_.Stop(); | 123 timer_.Stop(); |
| 124 | 124 |
| 125 if (!file_message_loop_proxy_->PostTask( | 125 if (!task_runner_->PostTask(FROM_HERE, |
| 126 FROM_HERE, MakeCriticalClosure(Bind(&WriteToDiskTask, path_, data)))) { | 126 MakeCriticalClosure(Bind(&WriteToDiskTask, path_, data)))) { |
| 127 // Posting the task to background message loop is not expected | 127 // Posting the task to background message loop is not expected |
| 128 // to fail, but if it does, avoid losing data and just hit the disk | 128 // to fail, but if it does, avoid losing data and just hit the disk |
| 129 // on the current thread. | 129 // on the current thread. |
| 130 NOTREACHED(); | 130 NOTREACHED(); |
| 131 | 131 |
| 132 WriteToDiskTask(path_, data); | 132 WriteToDiskTask(path_, data); |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 136 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 151 if (serializer_->SerializeData(&data)) { | 151 if (serializer_->SerializeData(&data)) { |
| 152 WriteNow(data); | 152 WriteNow(data); |
| 153 } else { | 153 } else { |
| 154 DLOG(WARNING) << "failed to serialize data to be saved in " | 154 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 155 << path_.value().c_str(); | 155 << path_.value().c_str(); |
| 156 } | 156 } |
| 157 serializer_ = NULL; | 157 serializer_ = NULL; |
| 158 } | 158 } |
| 159 | 159 |
| 160 } // namespace base | 160 } // namespace base |
| OLD | NEW |