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