| 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" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // to disk after the commit interval. If another ScheduleWrite is issued | 88 // to disk after the commit interval. If another ScheduleWrite is issued |
| 89 // before that, only one serialization and write to disk will happen, and | 89 // before that, only one serialization and write to disk will happen, and |
| 90 // the most recent |serializer| will be used. This operation does not block. | 90 // the most recent |serializer| will be used. This operation does not block. |
| 91 // |serializer| should remain valid through the lifetime of | 91 // |serializer| should remain valid through the lifetime of |
| 92 // ImportantFileWriter. | 92 // ImportantFileWriter. |
| 93 void ScheduleWrite(DataSerializer* serializer); | 93 void ScheduleWrite(DataSerializer* serializer); |
| 94 | 94 |
| 95 // Serialize data pending to be saved and execute write on backend thread. | 95 // Serialize data pending to be saved and execute write on backend thread. |
| 96 void DoScheduledWrite(); | 96 void DoScheduledWrite(); |
| 97 | 97 |
| 98 // Registers |on_next_successful_write| to be called once, on the next | 98 // Registers |on_next_successful_write_reply| to be invoked on the sequence |
| 99 // successful write event. Only one callback can be set at once. | 99 // owning this ImportantFileWriter in reply to the next successful write. |
| 100 void RegisterOnNextSuccessfulWriteCallback( | 100 // Only one such callback can be set at once. |
| 101 const Closure& on_next_successful_write); | 101 void RegisterOnNextSuccessfulWriteReply( |
| 102 const Closure& on_next_successful_write_reply); |
| 103 |
| 104 // Registers |on_next_write_callback| to be synchronously invoked from |
| 105 // WriteFileAtomically() on its next write (i.e. from |task_runner_|), with |
| 106 // |success| indicating whether it succeeded or not. |
| 107 // Only one such callback can be set at once. |
| 108 void RegisterOnNextWriteSynchronousCallback( |
| 109 const Callback<void(bool success)>& on_next_write_callback); |
| 102 | 110 |
| 103 TimeDelta commit_interval() const { | 111 TimeDelta commit_interval() const { |
| 104 return commit_interval_; | 112 return commit_interval_; |
| 105 } | 113 } |
| 106 | 114 |
| 107 private: | 115 private: |
| 108 // Helper method for WriteNow(). | 116 // Helper method for WriteNow(). |
| 109 bool PostWriteTask(const Callback<bool()>& task); | 117 bool PostWriteTask(const Callback<bool()>& task); |
| 110 | 118 |
| 111 // If |result| is true and |on_next_successful_write_| is set, invokes | 119 // If |result| is true and |on_next_successful_write_| is set, invokes |
| 112 // |on_successful_write_| and then resets it; no-ops otherwise. | 120 // |on_successful_write_| and then resets it; no-ops otherwise. |
| 113 void ForwardSuccessfulWrite(bool result); | 121 void ForwardSuccessfulWrite(bool result); |
| 114 | 122 |
| 115 // Invoked once and then reset on the next successful write event. | 123 // Invoked once and then reset on the next successful write event. |
| 116 Closure on_next_successful_write_; | 124 Closure on_next_successful_write_reply_; |
| 125 |
| 126 // Invoked synchronously on the next write event. |
| 127 Callback<void(bool success)> on_next_write_callback_; |
| 117 | 128 |
| 118 // Path being written to. | 129 // Path being written to. |
| 119 const FilePath path_; | 130 const FilePath path_; |
| 120 | 131 |
| 121 // TaskRunner for the thread on which file I/O can be done. | 132 // TaskRunner for the thread on which file I/O can be done. |
| 122 const scoped_refptr<SequencedTaskRunner> task_runner_; | 133 const scoped_refptr<SequencedTaskRunner> task_runner_; |
| 123 | 134 |
| 124 // Timer used to schedule commit after ScheduleWrite. | 135 // Timer used to schedule commit after ScheduleWrite. |
| 125 OneShotTimer timer_; | 136 OneShotTimer timer_; |
| 126 | 137 |
| 127 // Serializer which will provide the data to be saved. | 138 // Serializer which will provide the data to be saved. |
| 128 DataSerializer* serializer_; | 139 DataSerializer* serializer_; |
| 129 | 140 |
| 130 // Time delta after which scheduled data will be written to disk. | 141 // Time delta after which scheduled data will be written to disk. |
| 131 const TimeDelta commit_interval_; | 142 const TimeDelta commit_interval_; |
| 132 | 143 |
| 133 WeakPtrFactory<ImportantFileWriter> weak_factory_; | 144 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
| 134 | 145 |
| 135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 146 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 136 }; | 147 }; |
| 137 | 148 |
| 138 } // namespace base | 149 } // namespace base |
| 139 | 150 |
| 140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 151 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| OLD | NEW |