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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
49 protected: | 49 protected: |
50 virtual ~DataSerializer() {} | 50 virtual ~DataSerializer() {} |
51 }; | 51 }; |
52 | 52 |
53 // Save |data| to |path| in an atomic manner. Blocks and writes data on the | 53 // Save |data| to |path| in an atomic manner. Blocks and writes data on the |
54 // current thread. Does not guarantee file integrity across system crash (see | 54 // current thread. Does not guarantee file integrity across system crash (see |
55 // the class comment above). | 55 // the class comment above). |
56 static bool WriteFileAtomically(const FilePath& path, StringPiece data); | 56 static bool WriteFileAtomically(const FilePath& path, |
| 57 StringPiece data, |
| 58 StringPiece histogram_suffix = StringPiece()); |
57 | 59 |
58 // Initialize the writer. | 60 // Initialize the writer. |
59 // |path| is the name of file to write. | 61 // |path| is the name of file to write. |
60 // |task_runner| is the SequencedTaskRunner instance where on which we will | 62 // |task_runner| is the SequencedTaskRunner instance where on which we will |
61 // execute file I/O operations. | 63 // execute file I/O operations. |
62 // All non-const methods, ctor and dtor must be called on the same thread. | 64 // All non-const methods, ctor and dtor must be called on the same thread. |
63 ImportantFileWriter(const FilePath& path, | 65 ImportantFileWriter(const FilePath& path, |
64 scoped_refptr<SequencedTaskRunner> task_runner); | 66 scoped_refptr<SequencedTaskRunner> task_runner, |
| 67 const char* histogram_suffix = nullptr); |
65 | 68 |
66 // Same as above, but with a custom commit interval. | 69 // Same as above, but with a custom commit interval. |
67 ImportantFileWriter(const FilePath& path, | 70 ImportantFileWriter(const FilePath& path, |
68 scoped_refptr<SequencedTaskRunner> task_runner, | 71 scoped_refptr<SequencedTaskRunner> task_runner, |
69 TimeDelta interval); | 72 TimeDelta interval, |
| 73 const char* histogram_suffix = nullptr); |
70 | 74 |
71 // You have to ensure that there are no pending writes at the moment | 75 // You have to ensure that there are no pending writes at the moment |
72 // of destruction. | 76 // of destruction. |
73 ~ImportantFileWriter(); | 77 ~ImportantFileWriter(); |
74 | 78 |
75 const FilePath& path() const { return path_; } | 79 const FilePath& path() const { return path_; } |
76 | 80 |
77 // Returns true if there is a scheduled write pending which has not yet | 81 // Returns true if there is a scheduled write pending which has not yet |
78 // been started. | 82 // been started. |
79 bool HasPendingWrite() const; | 83 bool HasPendingWrite() const; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 140 |
137 // An override for |timer_| used for testing. | 141 // An override for |timer_| used for testing. |
138 Timer* timer_override_ = nullptr; | 142 Timer* timer_override_ = nullptr; |
139 | 143 |
140 // Serializer which will provide the data to be saved. | 144 // Serializer which will provide the data to be saved. |
141 DataSerializer* serializer_; | 145 DataSerializer* serializer_; |
142 | 146 |
143 // Time delta after which scheduled data will be written to disk. | 147 // Time delta after which scheduled data will be written to disk. |
144 const TimeDelta commit_interval_; | 148 const TimeDelta commit_interval_; |
145 | 149 |
| 150 // Custom histogram suffix. |
| 151 const std::string histogram_suffix_; |
| 152 |
146 SEQUENCE_CHECKER(sequence_checker_); | 153 SEQUENCE_CHECKER(sequence_checker_); |
147 | 154 |
148 WeakPtrFactory<ImportantFileWriter> weak_factory_; | 155 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
149 | 156 |
150 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 157 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
151 }; | 158 }; |
152 | 159 |
153 } // namespace base | 160 } // namespace base |
154 | 161 |
155 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 162 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |