| 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, base::MessageLoopProxy* file_message_loop_proxy) | 92 const FilePath& path, |
| 93 : path_(path), | 93 base::SequencedTaskRunner* blocking_task_runner) |
| 94 file_message_loop_proxy_(file_message_loop_proxy), | 94 : path_(path), |
| 95 serializer_(NULL), | 95 blocking_task_runner_(blocking_task_runner), |
| 96 commit_interval_(TimeDelta::FromMilliseconds( | 96 serializer_(NULL), |
| 97 kDefaultCommitIntervalMs)) { | 97 commit_interval_(TimeDelta::FromMilliseconds( |
| 98 kDefaultCommitIntervalMs)) { |
| 98 DCHECK(CalledOnValidThread()); | 99 DCHECK(CalledOnValidThread()); |
| 99 DCHECK(file_message_loop_proxy_.get()); | 100 DCHECK(blocking_task_runner_.get()); |
| 100 } | 101 } |
| 101 | 102 |
| 102 ImportantFileWriter::~ImportantFileWriter() { | 103 ImportantFileWriter::~ImportantFileWriter() { |
| 103 // 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 |
| 104 // 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 |
| 105 // being destructed. | 106 // being destructed. |
| 106 DCHECK(!HasPendingWrite()); | 107 DCHECK(!HasPendingWrite()); |
| 107 } | 108 } |
| 108 | 109 |
| 109 bool ImportantFileWriter::HasPendingWrite() const { | 110 bool ImportantFileWriter::HasPendingWrite() const { |
| 110 DCHECK(CalledOnValidThread()); | 111 DCHECK(CalledOnValidThread()); |
| 111 return timer_.IsRunning(); | 112 return timer_.IsRunning(); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void ImportantFileWriter::WriteNow(const std::string& data) { | 115 void ImportantFileWriter::WriteNow(const std::string& data) { |
| 115 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 116 if (data.length() > static_cast<size_t>(kint32max)) { | 117 if (data.length() > static_cast<size_t>(kint32max)) { |
| 117 NOTREACHED(); | 118 NOTREACHED(); |
| 118 return; | 119 return; |
| 119 } | 120 } |
| 120 | 121 |
| 121 if (HasPendingWrite()) | 122 if (HasPendingWrite()) |
| 122 timer_.Stop(); | 123 timer_.Stop(); |
| 123 | 124 |
| 124 if (!file_message_loop_proxy_->PostTask( | 125 if (!blocking_task_runner_->PostTask( |
| 125 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { | 126 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { |
| 126 // Posting the task to background message loop is not expected | 127 // 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 | 128 // to fail, but if it does, avoid losing data and just hit the disk |
| 128 // on the current thread. | 129 // on the current thread. |
| 129 NOTREACHED(); | 130 NOTREACHED(); |
| 130 | 131 |
| 131 WriteToDiskTask(path_, data); | 132 WriteToDiskTask(path_, data); |
| 132 } | 133 } |
| 133 } | 134 } |
| 134 | 135 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 DCHECK(serializer_); | 149 DCHECK(serializer_); |
| 149 std::string data; | 150 std::string data; |
| 150 if (serializer_->SerializeData(&data)) { | 151 if (serializer_->SerializeData(&data)) { |
| 151 WriteNow(data); | 152 WriteNow(data); |
| 152 } else { | 153 } else { |
| 153 DLOG(WARNING) << "failed to serialize data to be saved in " | 154 DLOG(WARNING) << "failed to serialize data to be saved in " |
| 154 << path_.value(); | 155 << path_.value(); |
| 155 } | 156 } |
| 156 serializer_ = NULL; | 157 serializer_ = NULL; |
| 157 } | 158 } |
| 159 |
| OLD | NEW |