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/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequence_checker.h" |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 #include "base/threading/non_thread_safe.h" | |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 class SequencedTaskRunner; | 22 class SequencedTaskRunner; |
23 | 23 |
24 // Helper for atomically writing a file to ensure that it won't be corrupted by | 24 // Helper for atomically writing a file to ensure that it won't be corrupted by |
25 // *application* crash during write (implemented as create, flush, rename). | 25 // *application* crash during write (implemented as create, flush, rename). |
26 // | 26 // |
27 // As an added benefit, ImportantFileWriter makes it less likely that the file | 27 // As an added benefit, ImportantFileWriter makes it less likely that the file |
28 // is corrupted by *system* crash, though even if the ImportantFileWriter call | 28 // is corrupted by *system* crash, though even if the ImportantFileWriter call |
29 // has already returned at the time of the crash it is not specified which | 29 // has already returned at the time of the crash it is not specified which |
30 // version of the file (old or new) is preserved. And depending on system | 30 // version of the file (old or new) is preserved. And depending on system |
31 // configuration (hardware and software) a significant likelihood of file | 31 // configuration (hardware and software) a significant likelihood of file |
32 // corruption may remain, thus using ImportantFileWriter is not a valid | 32 // corruption may remain, thus using ImportantFileWriter is not a valid |
33 // substitute for file integrity checks and recovery codepaths for malformed | 33 // substitute for file integrity checks and recovery codepaths for malformed |
34 // files. | 34 // files. |
35 // | 35 // |
36 // Also note that ImportantFileWriter can be *really* slow (cf. File::Flush() | 36 // Also note that ImportantFileWriter can be *really* slow (cf. File::Flush() |
37 // for details) and thus please don't block shutdown on ImportantFileWriter. | 37 // for details) and thus please don't block shutdown on ImportantFileWriter. |
38 class BASE_EXPORT ImportantFileWriter : public NonThreadSafe { | 38 class BASE_EXPORT ImportantFileWriter { |
39 public: | 39 public: |
40 // Used by ScheduleSave to lazily provide the data to be saved. Allows us | 40 // Used by ScheduleSave to lazily provide the data to be saved. Allows us |
41 // to also batch data serializations. | 41 // to also batch data serializations. |
42 class BASE_EXPORT DataSerializer { | 42 class BASE_EXPORT DataSerializer { |
43 public: | 43 public: |
44 // Should put serialized string in |data| and return true on successful | 44 // Should put serialized string in |data| and return true on successful |
45 // serialization. Will be called on the same thread on which | 45 // serialization. Will be called on the same thread on which |
46 // ImportantFileWriter has been created. | 46 // ImportantFileWriter has been created. |
47 virtual bool SerializeData(std::string* data) = 0; | 47 virtual bool SerializeData(std::string* data) = 0; |
48 | 48 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 136 |
137 // An override for |timer_| used for testing. | 137 // An override for |timer_| used for testing. |
138 Timer* timer_override_ = nullptr; | 138 Timer* timer_override_ = nullptr; |
139 | 139 |
140 // Serializer which will provide the data to be saved. | 140 // Serializer which will provide the data to be saved. |
141 DataSerializer* serializer_; | 141 DataSerializer* serializer_; |
142 | 142 |
143 // Time delta after which scheduled data will be written to disk. | 143 // Time delta after which scheduled data will be written to disk. |
144 const TimeDelta commit_interval_; | 144 const TimeDelta commit_interval_; |
145 | 145 |
| 146 SEQUENCE_CHECKER(sequence_checker_); |
| 147 |
146 WeakPtrFactory<ImportantFileWriter> weak_factory_; | 148 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
147 | 149 |
148 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 150 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
149 }; | 151 }; |
150 | 152 |
151 } // namespace base | 153 } // namespace base |
152 | 154 |
153 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 155 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |