| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 5 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| 6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | |
| 13 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 15 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 16 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 | 19 |
| 21 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 22 class Thread; | 21 class Thread; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // to disk after the commit interval. If another ScheduleWrite is issued | 82 // to disk after the commit interval. If another ScheduleWrite is issued |
| 84 // before that, only one serialization and write to disk will happen, and | 83 // before that, only one serialization and write to disk will happen, and |
| 85 // the most recent |serializer| will be used. This operation does not block. | 84 // the most recent |serializer| will be used. This operation does not block. |
| 86 // |serializer| should remain valid through the lifetime of | 85 // |serializer| should remain valid through the lifetime of |
| 87 // ImportantFileWriter. | 86 // ImportantFileWriter. |
| 88 void ScheduleWrite(DataSerializer* serializer); | 87 void ScheduleWrite(DataSerializer* serializer); |
| 89 | 88 |
| 90 // Serialize data pending to be saved and execute write on backend thread. | 89 // Serialize data pending to be saved and execute write on backend thread. |
| 91 void DoScheduledWrite(); | 90 void DoScheduledWrite(); |
| 92 | 91 |
| 93 // Registers |on_next_successful_write| to be called once, on the next | |
| 94 // successful write event. Only one callback can be set at once. | |
| 95 void RegisterOnNextSuccessfulWriteCallback( | |
| 96 const base::Closure& on_next_successful_write); | |
| 97 | |
| 98 TimeDelta commit_interval() const { | 92 TimeDelta commit_interval() const { |
| 99 return commit_interval_; | 93 return commit_interval_; |
| 100 } | 94 } |
| 101 | 95 |
| 102 void set_commit_interval(const TimeDelta& interval) { | 96 void set_commit_interval(const TimeDelta& interval) { |
| 103 commit_interval_ = interval; | 97 commit_interval_ = interval; |
| 104 } | 98 } |
| 105 | 99 |
| 106 private: | 100 private: |
| 107 // If |result| is true and |on_next_successful_write_| is set, invokes | |
| 108 // |on_successful_write_| and then resets it; no-ops otherwise. | |
| 109 void ForwardSuccessfulWrite(bool result); | |
| 110 | |
| 111 // Invoked once and then reset on the next successful write event. | |
| 112 base::Closure on_next_successful_write_; | |
| 113 | |
| 114 // Path being written to. | 101 // Path being written to. |
| 115 const FilePath path_; | 102 const FilePath path_; |
| 116 | 103 |
| 117 // TaskRunner for the thread on which file I/O can be done. | 104 // TaskRunner for the thread on which file I/O can be done. |
| 118 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 105 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 119 | 106 |
| 120 // Timer used to schedule commit after ScheduleWrite. | 107 // Timer used to schedule commit after ScheduleWrite. |
| 121 OneShotTimer<ImportantFileWriter> timer_; | 108 OneShotTimer<ImportantFileWriter> timer_; |
| 122 | 109 |
| 123 // Serializer which will provide the data to be saved. | 110 // Serializer which will provide the data to be saved. |
| 124 DataSerializer* serializer_; | 111 DataSerializer* serializer_; |
| 125 | 112 |
| 126 // Time delta after which scheduled data will be written to disk. | 113 // Time delta after which scheduled data will be written to disk. |
| 127 TimeDelta commit_interval_; | 114 TimeDelta commit_interval_; |
| 128 | 115 |
| 129 WeakPtrFactory<ImportantFileWriter> weak_factory_; | |
| 130 | |
| 131 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 116 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 132 }; | 117 }; |
| 133 | 118 |
| 134 } // namespace base | 119 } // namespace base |
| 135 | 120 |
| 136 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 121 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| OLD | NEW |