| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <ostream> | 9 #include <ostream> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 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/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/task.h" | 16 #include "base/task.h" |
| 17 #include "base/thread.h" | 17 #include "base/thread.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "chrome/browser/chrome_thread.h" |
| 19 | 20 |
| 20 using base::TimeDelta; | 21 using base::TimeDelta; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const int kDefaultCommitIntervalMs = 10000; | 25 const int kDefaultCommitIntervalMs = 10000; |
| 25 | 26 |
| 26 class WriteToDiskTask : public Task { | 27 class WriteToDiskTask : public Task { |
| 27 public: | 28 public: |
| 28 WriteToDiskTask(const FilePath& path, const std::string& data) | 29 WriteToDiskTask(const FilePath& path, const std::string& data) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } | 76 } |
| 76 | 77 |
| 77 const FilePath path_; | 78 const FilePath path_; |
| 78 const std::string data_; | 79 const std::string data_; |
| 79 | 80 |
| 80 DISALLOW_COPY_AND_ASSIGN(WriteToDiskTask); | 81 DISALLOW_COPY_AND_ASSIGN(WriteToDiskTask); |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 } // namespace | 84 } // namespace |
| 84 | 85 |
| 85 ImportantFileWriter::ImportantFileWriter(const FilePath& path, | 86 ImportantFileWriter::ImportantFileWriter(const FilePath& path) |
| 86 const base::Thread* backend_thread) | |
| 87 : path_(path), | 87 : path_(path), |
| 88 backend_thread_(backend_thread), | |
| 89 serializer_(NULL), | 88 serializer_(NULL), |
| 90 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) { | 89 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) { |
| 91 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
| 92 } | 91 } |
| 93 | 92 |
| 94 ImportantFileWriter::~ImportantFileWriter() { | 93 ImportantFileWriter::~ImportantFileWriter() { |
| 95 // We're usually a member variable of some other object, which also tends | 94 // We're usually a member variable of some other object, which also tends |
| 96 // to be our serializer. It may not be safe to call back to the parent object | 95 // to be our serializer. It may not be safe to call back to the parent object |
| 97 // being destructed. | 96 // being destructed. |
| 98 DCHECK(!HasPendingWrite()); | 97 DCHECK(!HasPendingWrite()); |
| 99 } | 98 } |
| 100 | 99 |
| 101 bool ImportantFileWriter::HasPendingWrite() const { | 100 bool ImportantFileWriter::HasPendingWrite() const { |
| 102 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 103 return timer_.IsRunning(); | 102 return timer_.IsRunning(); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void ImportantFileWriter::WriteNow(const std::string& data) { | 105 void ImportantFileWriter::WriteNow(const std::string& data) { |
| 107 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
| 108 | 107 |
| 109 if (HasPendingWrite()) | 108 if (HasPendingWrite()) |
| 110 timer_.Stop(); | 109 timer_.Stop(); |
| 111 | 110 |
| 112 Task* task = new WriteToDiskTask(path_, data); | 111 ChromeThread::PostTask( |
| 113 if (backend_thread_) { | 112 ChromeThread::FILE, FROM_HERE, new WriteToDiskTask(path_, data)); |
| 114 backend_thread_->message_loop()->PostTask(FROM_HERE, task); | |
| 115 } else { | |
| 116 task->Run(); | |
| 117 delete task; | |
| 118 } | |
| 119 } | 113 } |
| 120 | 114 |
| 121 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | 115 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { |
| 122 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 123 | 117 |
| 124 DCHECK(serializer); | 118 DCHECK(serializer); |
| 125 serializer_ = serializer; | 119 serializer_ = serializer; |
| 126 | 120 |
| 127 if (!MessageLoop::current()) { | 121 if (!MessageLoop::current()) { |
| 128 // Happens in unit tests. | 122 // Happens in unit tests. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 140 DCHECK(serializer_); | 134 DCHECK(serializer_); |
| 141 std::string data; | 135 std::string data; |
| 142 if (serializer_->SerializeData(&data)) { | 136 if (serializer_->SerializeData(&data)) { |
| 143 WriteNow(data); | 137 WriteNow(data); |
| 144 } else { | 138 } else { |
| 145 LOG(WARNING) << "failed to serialize data to be saved in " | 139 LOG(WARNING) << "failed to serialize data to be saved in " |
| 146 << path_.value(); | 140 << path_.value(); |
| 147 } | 141 } |
| 148 serializer_ = NULL; | 142 serializer_ = NULL; |
| 149 } | 143 } |
| OLD | NEW |