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" |
akalin
2012/10/19 02:00:51
sequenced_task_runner
| |
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 56 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, base::SequencedTaskRunner* task_runner) |
93 : path_(path), | 93 : path_(path), |
94 file_message_loop_proxy_(file_message_loop_proxy), | 94 task_runner_(task_runner), |
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(task_runner_.get()); |
100 } | 100 } |
101 | 101 |
102 ImportantFileWriter::~ImportantFileWriter() { | 102 ImportantFileWriter::~ImportantFileWriter() { |
103 // 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 |
104 // 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 |
105 // being destructed. | 105 // being destructed. |
106 DCHECK(!HasPendingWrite()); | 106 DCHECK(!HasPendingWrite()); |
107 } | 107 } |
108 | 108 |
109 bool ImportantFileWriter::HasPendingWrite() const { | 109 bool ImportantFileWriter::HasPendingWrite() const { |
110 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
111 return timer_.IsRunning(); | 111 return timer_.IsRunning(); |
112 } | 112 } |
113 | 113 |
114 void ImportantFileWriter::WriteNow(const std::string& data) { | 114 void ImportantFileWriter::WriteNow(const std::string& data) { |
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 (!task_runner_->PostTask( |
125 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { | 125 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { |
akalin
2012/10/19 02:00:51
fix indent here
zel
2012/10/19 18:45:07
Done.
| |
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 } |
OLD | NEW |