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