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_write_callback| to be synchronously invoked from |
98 // successful write event. Only one callback can be set at once. | 98 // WriteFileAtomically() on its next write (i.e. from |task_runner_|), with |
99 void RegisterOnNextSuccessfulWriteCallback( | 99 // |success| indicating whether it succeeded or not. |
100 const Closure& on_next_successful_write); | 100 // |on_next_write_callback| must be thread safe, as it will be called on |
| 101 // |task_runner_| and may be called during Chrome shutdown. |
| 102 // If called more than once before a write is scheduled on |task_runner|, the |
| 103 // latest callback clobbers the others. |
| 104 void RegisterOnNextWriteCallback( |
| 105 const Callback<void(bool success)>& on_next_write_callback); |
101 | 106 |
102 TimeDelta commit_interval() const { | 107 TimeDelta commit_interval() const { |
103 return commit_interval_; | 108 return commit_interval_; |
104 } | 109 } |
105 | 110 |
106 private: | 111 private: |
107 // Helper method for WriteNow(). | 112 // Invoked synchronously on the next write event. |
108 bool PostWriteTask(const Callback<bool()>& task); | 113 Callback<void(bool success)> on_next_write_callback_; |
109 | |
110 // If |result| is true and |on_next_successful_write_| is set, invokes | |
111 // |on_successful_write_| and then resets it; no-ops otherwise. | |
112 void ForwardSuccessfulWrite(bool result); | |
113 | |
114 // Invoked once and then reset on the next successful write event. | |
115 Closure on_next_successful_write_; | |
116 | 114 |
117 // Path being written to. | 115 // Path being written to. |
118 const FilePath path_; | 116 const FilePath path_; |
119 | 117 |
120 // TaskRunner for the thread on which file I/O can be done. | 118 // TaskRunner for the thread on which file I/O can be done. |
121 const scoped_refptr<SequencedTaskRunner> task_runner_; | 119 const scoped_refptr<SequencedTaskRunner> task_runner_; |
122 | 120 |
123 // Timer used to schedule commit after ScheduleWrite. | 121 // Timer used to schedule commit after ScheduleWrite. |
124 OneShotTimer timer_; | 122 OneShotTimer timer_; |
125 | 123 |
126 // Serializer which will provide the data to be saved. | 124 // Serializer which will provide the data to be saved. |
127 DataSerializer* serializer_; | 125 DataSerializer* serializer_; |
128 | 126 |
129 // Time delta after which scheduled data will be written to disk. | 127 // Time delta after which scheduled data will be written to disk. |
130 const TimeDelta commit_interval_; | 128 const TimeDelta commit_interval_; |
131 | 129 |
132 WeakPtrFactory<ImportantFileWriter> weak_factory_; | 130 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
133 | 131 |
134 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 132 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
135 }; | 133 }; |
136 | 134 |
137 } // namespace base | 135 } // namespace base |
138 | 136 |
139 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 137 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |