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