[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 5 | #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| 6 | #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 7 | |
Greg Thompson | dfc7d7f4 | 2020-05-14 19:30:50 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 15 | #include "base/sequence_checker.h" |
bcwhite | be133b5 | 2016-04-11 20:03:38 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 17 | #include "base/time/time.h" |
| 18 | #include "base/timer/timer.h" |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 19 | #include "third_party/abseil-cpp/absl/types/variant.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 20 | |
| 21 | namespace base { |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 22 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 23 | class SequencedTaskRunner; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 24 | |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 25 | // Helper for atomically writing a file to ensure that it won't be corrupted by |
| 26 | // *application* crash during write (implemented as create, flush, rename). |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 27 | // |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 28 | // As an added benefit, ImportantFileWriter makes it less likely that the file |
| 29 | // is corrupted by *system* crash, though even if the ImportantFileWriter call |
| 30 | // has already returned at the time of the crash it is not specified which |
| 31 | // version of the file (old or new) is preserved. And depending on system |
| 32 | // configuration (hardware and software) a significant likelihood of file |
| 33 | // corruption may remain, thus using ImportantFileWriter is not a valid |
| 34 | // substitute for file integrity checks and recovery codepaths for malformed |
| 35 | // files. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 36 | // |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 37 | // Also note that ImportantFileWriter can be *really* slow (cf. File::Flush() |
| 38 | // for details) and thus please don't block shutdown on ImportantFileWriter. |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 39 | class BASE_EXPORT ImportantFileWriter { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 40 | public: |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 41 | // Promise-like callback that returns (via output parameter) the serialized |
| 42 | // data to be written. This callback is invoked on the sequence where I/O |
| 43 | // operations are executed. Returning false indicates an error. |
| 44 | using BackgroundDataProducerCallback = base::OnceCallback<bool(std::string*)>; |
| 45 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 46 | // Used by ScheduleSave to lazily provide the data to be saved. Allows us |
| 47 | // to also batch data serializations. |
[email protected] | f59f33e | 2012-11-01 12:05:27 | [diff] [blame] | 48 | class BASE_EXPORT DataSerializer { |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 49 | public: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 50 | // Should put serialized string in |data| and return true on successful |
| 51 | // serialization. Will be called on the same thread on which |
| 52 | // ImportantFileWriter has been created. |
| 53 | virtual bool SerializeData(std::string* data) = 0; |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 54 | |
| 55 | protected: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 56 | virtual ~DataSerializer() = default; |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 57 | }; |
| 58 | |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 59 | // Same as DataSerializer but allows the caller to move some of the |
| 60 | // serialization logic to the sequence where I/O operations are executed. |
| 61 | class BASE_EXPORT BackgroundDataSerializer { |
| 62 | public: |
| 63 | // Returns a promise-like callback that, when invoked, will produce the |
| 64 | // serialized string. This getter itself will be called on the same thread |
| 65 | // on which ImportantFileWriter has been created, but the callback will be |
| 66 | // invoked from the sequence where I/O operations are executed. |
| 67 | virtual BackgroundDataProducerCallback |
| 68 | GetSerializedDataProducerForBackgroundSequence() = 0; |
| 69 | |
| 70 | protected: |
| 71 | virtual ~BackgroundDataSerializer() = default; |
| 72 | }; |
| 73 | |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 74 | // Save |data| to |path| in an atomic manner. Blocks and writes data on the |
| 75 | // current thread. Does not guarantee file integrity across system crash (see |
| 76 | // the class comment above). |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 77 | static bool WriteFileAtomically(const FilePath& path, |
| 78 | StringPiece data, |
| 79 | StringPiece histogram_suffix = StringPiece()); |
[email protected] | 95b42e2 | 2012-11-29 14:00:12 | [diff] [blame] | 80 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 81 | // Initialize the writer. |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 82 | // |path| is the name of file to write. |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 83 | // |task_runner| is the SequencedTaskRunner instance where on which we will |
| 84 | // execute file I/O operations. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 85 | // All non-const methods, ctor and dtor must be called on the same thread. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 86 | ImportantFileWriter(const FilePath& path, |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 87 | scoped_refptr<SequencedTaskRunner> task_runner, |
Dominic Battre | 81f1908c | 2020-12-02 22:27:37 | [diff] [blame] | 88 | StringPiece histogram_suffix = StringPiece()); |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 89 | |
| 90 | // Same as above, but with a custom commit interval. |
| 91 | ImportantFileWriter(const FilePath& path, |
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 92 | scoped_refptr<SequencedTaskRunner> task_runner, |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 93 | TimeDelta interval, |
Dominic Battre | 81f1908c | 2020-12-02 22:27:37 | [diff] [blame] | 94 | StringPiece histogram_suffix = StringPiece()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 95 | |
David Bienvenu | e56b996a | 2020-12-18 04:44:19 | [diff] [blame] | 96 | ImportantFileWriter(const ImportantFileWriter&) = delete; |
| 97 | ImportantFileWriter& operator=(const ImportantFileWriter&) = delete; |
| 98 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 99 | // You have to ensure that there are no pending writes at the moment |
| 100 | // of destruction. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 101 | ~ImportantFileWriter(); |
| 102 | |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 103 | const FilePath& path() const { return path_; } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 104 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 105 | // Returns true if there is a scheduled write pending which has not yet |
| 106 | // been started. |
| 107 | bool HasPendingWrite() const; |
| 108 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 109 | // Save |data| to target filename. Does not block. If there is a pending write |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 110 | // scheduled by ScheduleWrite(), it is cancelled. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 111 | void WriteNow(std::unique_ptr<std::string> data); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 112 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 113 | // Schedule a save to target filename. Data will be serialized and saved |
| 114 | // to disk after the commit interval. If another ScheduleWrite is issued |
| 115 | // before that, only one serialization and write to disk will happen, and |
| 116 | // the most recent |serializer| will be used. This operation does not block. |
| 117 | // |serializer| should remain valid through the lifetime of |
| 118 | // ImportantFileWriter. |
| 119 | void ScheduleWrite(DataSerializer* serializer); |
| 120 | |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 121 | // Same as above but uses the BackgroundDataSerializer API. |
| 122 | void ScheduleWriteWithBackgroundDataSerializer( |
| 123 | BackgroundDataSerializer* serializer); |
| 124 | |
| 125 | // Serialize data pending to be saved and execute write on background thread. |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 126 | void DoScheduledWrite(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 127 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 128 | // Registers |before_next_write_callback| and |after_next_write_callback| to |
| 129 | // be synchronously invoked from WriteFileAtomically() before its next write |
| 130 | // and after its next write, respectively. The boolean passed to |
| 131 | // |after_next_write_callback| indicates whether the write was successful. |
| 132 | // Both callbacks must be thread safe as they will be called on |task_runner_| |
| 133 | // and may be called during Chrome shutdown. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 134 | // If called more than once before a write is scheduled on |task_runner|, the |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 135 | // latest callbacks clobber the others. |
| 136 | void RegisterOnNextWriteCallbacks( |
Christian Dullweber | ee89d67 | 2018-11-29 15:12:28 | [diff] [blame] | 137 | OnceClosure before_next_write_callback, |
| 138 | OnceCallback<void(bool success)> after_next_write_callback); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 139 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 140 | TimeDelta commit_interval() const { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 141 | return commit_interval_; |
| 142 | } |
| 143 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 144 | // Overrides the timer to use for scheduling writes with |timer_override|. |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 145 | void SetTimerForTesting(OneShotTimer* timer_override); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 146 | |
Dominic Battre | 0a41c4a | 2020-11-26 15:08:10 | [diff] [blame] | 147 | #if defined(UNIT_TEST) |
| 148 | size_t previous_data_size() const { return previous_data_size_; } |
| 149 | #endif |
Dominic Battre | c7911fe | 2020-11-26 21:13:18 | [diff] [blame] | 150 | void set_previous_data_size(size_t previous_data_size) { |
| 151 | previous_data_size_ = previous_data_size; |
| 152 | } |
Dominic Battre | 0a41c4a | 2020-11-26 15:08:10 | [diff] [blame] | 153 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 154 | private: |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 155 | const OneShotTimer& timer() const { |
| 156 | return timer_override_ ? *timer_override_ : timer_; |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 157 | } |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 158 | OneShotTimer& timer() { return timer_override_ ? *timer_override_ : timer_; } |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 159 | |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 160 | // Same as WriteNow() but it uses a promise-like signature that allows running |
| 161 | // custom logic in the background sequence. |
| 162 | void WriteNowWithBackgroundDataProducer( |
| 163 | BackgroundDataProducerCallback background_producer); |
| 164 | |
| 165 | // Helper function to call WriteFileAtomically() with a promise-like callback |
| 166 | // producing a std::string. |
| 167 | static void ProduceAndWriteStringToFileAtomically( |
Greg Thompson | dfc7d7f4 | 2020-05-14 19:30:50 | [diff] [blame] | 168 | const FilePath& path, |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 169 | BackgroundDataProducerCallback data_producer_for_background_sequence, |
Greg Thompson | dfc7d7f4 | 2020-05-14 19:30:50 | [diff] [blame] | 170 | OnceClosure before_write_callback, |
| 171 | OnceCallback<void(bool success)> after_write_callback, |
| 172 | const std::string& histogram_suffix); |
| 173 | |
| 174 | // Writes |data| to |path|, recording histograms with an optional |
| 175 | // |histogram_suffix|. |from_instance| indicates whether the call originates |
| 176 | // from an instance of ImportantFileWriter or a direct call to |
| 177 | // WriteFileAtomically. When false, the directory containing |path| is added |
| 178 | // to the set cleaned by the ImportantFileWriterCleaner (Windows only). |
| 179 | static bool WriteFileAtomicallyImpl(const FilePath& path, |
| 180 | StringPiece data, |
| 181 | StringPiece histogram_suffix, |
| 182 | bool from_instance); |
| 183 | |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 184 | void ClearPendingWrite(); |
| 185 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 186 | // Invoked synchronously on the next write event. |
Christian Dullweber | ee89d67 | 2018-11-29 15:12:28 | [diff] [blame] | 187 | OnceClosure before_next_write_callback_; |
| 188 | OnceCallback<void(bool success)> after_next_write_callback_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 189 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 190 | // Path being written to. |
| 191 | const FilePath path_; |
| 192 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 193 | // TaskRunner for the thread on which file I/O can be done. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 194 | const scoped_refptr<SequencedTaskRunner> task_runner_; |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 195 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 196 | // Timer used to schedule commit after ScheduleWrite. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 197 | OneShotTimer timer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 198 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 199 | // An override for |timer_| used for testing. |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 200 | OneShotTimer* timer_override_ = nullptr; |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 201 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 202 | // Serializer which will provide the data to be saved. |
Mikel Astiz | b6108d6 | 2021-03-18 17:34:49 | [diff] [blame] | 203 | absl::variant<absl::monostate, DataSerializer*, BackgroundDataSerializer*> |
| 204 | serializer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 205 | |
| 206 | // Time delta after which scheduled data will be written to disk. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 207 | const TimeDelta commit_interval_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 208 | |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 209 | // Custom histogram suffix. |
| 210 | const std::string histogram_suffix_; |
| 211 | |
Dominic Battre | 0a41c4a | 2020-11-26 15:08:10 | [diff] [blame] | 212 | // Memorizes the amount of data written on the previous write. This helps |
| 213 | // preallocating memory for the data serialization. It is only used for |
| 214 | // scheduled writes. |
| 215 | size_t previous_data_size_ = 0; |
| 216 | |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 217 | SEQUENCE_CHECKER(sequence_checker_); |
| 218 | |
Jeremy Roman | 577d8849 | 2019-07-05 14:30:23 | [diff] [blame] | 219 | WeakPtrFactory<ImportantFileWriter> weak_factory_{this}; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 220 | }; |
| 221 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 222 | } // namespace base |
| 223 | |
| 224 | #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |