Chromium Code Reviews| 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 "chrome/common/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/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 | 20 |
| 21 using base::TimeDelta; | |
| 22 | |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 const int kDefaultCommitIntervalMs = 10000; | 23 const int kDefaultCommitIntervalMs = 10000; |
| 26 | 24 |
| 27 enum TempFileFailure { | 25 enum TempFileFailure { |
| 28 FAILED_CREATING, | 26 FAILED_CREATING, |
| 29 FAILED_OPENING, | 27 FAILED_OPENING, |
| 30 FAILED_CLOSING, | 28 FAILED_CLOSING, |
| 31 FAILED_WRITING, | 29 FAILED_WRITING, |
| 32 FAILED_RENAMING, | 30 FAILED_RENAMING, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 79 |
| 82 if (!file_util::ReplaceFile(tmp_file_path, path)) { | 80 if (!file_util::ReplaceFile(tmp_file_path, path)) { |
| 83 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); | 81 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); |
| 84 file_util::Delete(tmp_file_path, false); | 82 file_util::Delete(tmp_file_path, false); |
| 85 return; | 83 return; |
| 86 } | 84 } |
| 87 } | 85 } |
| 88 | 86 |
| 89 } // namespace | 87 } // namespace |
| 90 | 88 |
| 89 namespace base { | |
|
brettw
2012/10/24 03:30:54
I usually put this around the whole thing (includi
Jói
2012/10/24 10:19:17
I did that as well, in patch set 4, doing it diffe
| |
| 90 | |
| 91 ImportantFileWriter::ImportantFileWriter( | 91 ImportantFileWriter::ImportantFileWriter( |
| 92 const FilePath& path, base::MessageLoopProxy* file_message_loop_proxy) | 92 const FilePath& path, MessageLoopProxy* file_message_loop_proxy) |
| 93 : path_(path), | 93 : path_(path), |
| 94 file_message_loop_proxy_(file_message_loop_proxy), | 94 file_message_loop_proxy_(file_message_loop_proxy), |
| 95 serializer_(NULL), | 95 serializer_(NULL), |
| 96 commit_interval_(TimeDelta::FromMilliseconds( | 96 commit_interval_(TimeDelta::FromMilliseconds( |
| 97 kDefaultCommitIntervalMs)) { | 97 kDefaultCommitIntervalMs)) { |
| 98 DCHECK(CalledOnValidThread()); | 98 DCHECK(CalledOnValidThread()); |
| 99 DCHECK(file_message_loop_proxy_.get()); | 99 DCHECK(file_message_loop_proxy_.get()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 ImportantFileWriter::~ImportantFileWriter() { | 102 ImportantFileWriter::~ImportantFileWriter() { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 115 DCHECK(CalledOnValidThread()); | 115 DCHECK(CalledOnValidThread()); |
| 116 if (data.length() > static_cast<size_t>(kint32max)) { | 116 if (data.length() > static_cast<size_t>(kint32max)) { |
| 117 NOTREACHED(); | 117 NOTREACHED(); |
| 118 return; | 118 return; |
| 119 } | 119 } |
| 120 | 120 |
| 121 if (HasPendingWrite()) | 121 if (HasPendingWrite()) |
| 122 timer_.Stop(); | 122 timer_.Stop(); |
| 123 | 123 |
| 124 if (!file_message_loop_proxy_->PostTask( | 124 if (!file_message_loop_proxy_->PostTask( |
| 125 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { | 125 FROM_HERE, Bind(&WriteToDiskTask, path_, data))) { |
| 126 // Posting the task to background message loop is not expected | 126 // Posting the task to background message loop is not expected |
| 127 // to fail, but if it does, avoid losing data and just hit the disk | 127 // to fail, but if it does, avoid losing data and just hit the disk |
| 128 // on the current thread. | 128 // on the current thread. |
| 129 NOTREACHED(); | 129 NOTREACHED(); |
| 130 | 130 |
| 131 WriteToDiskTask(path_, data); | 131 WriteToDiskTask(path_, data); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 135 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 148 DCHECK(serializer_); | 148 DCHECK(serializer_); |
| 149 std::string data; | 149 std::string data; |
| 150 if (serializer_->SerializeData(&data)) { | 150 if (serializer_->SerializeData(&data)) { |
| 151 WriteNow(data); | 151 WriteNow(data); |
| 152 } else { | 152 } else { |
| 153 DLOG(WARNING) << "failed to serialize data to be saved in " | 153 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 154 << path_.value(); | 154 << path_.value(); |
| 155 } | 155 } |
| 156 serializer_ = NULL; | 156 serializer_ = NULL; |
| 157 } | 157 } |
| 158 | |
| 159 } // namespace base | |
| OLD | NEW |