[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" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 16 | #include "base/sequence_checker.h" |
bcwhite | be133b5 | 2016-04-11 20:03:38 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 18 | #include "base/time/time.h" |
| 19 | #include "base/timer/timer.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: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 41 | // Used by ScheduleSave to lazily provide the data to be saved. Allows us |
| 42 | // to also batch data serializations. |
[email protected] | f59f33e | 2012-11-01 12:05:27 | [diff] [blame] | 43 | class BASE_EXPORT DataSerializer { |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 44 | public: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 45 | // Should put serialized string in |data| and return true on successful |
| 46 | // serialization. Will be called on the same thread on which |
| 47 | // ImportantFileWriter has been created. |
| 48 | virtual bool SerializeData(std::string* data) = 0; |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 49 | |
| 50 | protected: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 51 | virtual ~DataSerializer() = default; |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 52 | }; |
| 53 | |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 54 | // Save |data| to |path| in an atomic manner. Blocks and writes data on the |
| 55 | // current thread. Does not guarantee file integrity across system crash (see |
| 56 | // the class comment above). |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 57 | static bool WriteFileAtomically(const FilePath& path, |
| 58 | StringPiece data, |
| 59 | StringPiece histogram_suffix = StringPiece()); |
[email protected] | 95b42e2 | 2012-11-29 14:00:12 | [diff] [blame] | 60 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 61 | // Initialize the writer. |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 62 | // |path| is the name of file to write. |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 63 | // |task_runner| is the SequencedTaskRunner instance where on which we will |
| 64 | // execute file I/O operations. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 65 | // All non-const methods, ctor and dtor must be called on the same thread. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 66 | ImportantFileWriter(const FilePath& path, |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 67 | scoped_refptr<SequencedTaskRunner> task_runner, |
| 68 | const char* histogram_suffix = nullptr); |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 69 | |
| 70 | // Same as above, but with a custom commit interval. |
| 71 | ImportantFileWriter(const FilePath& path, |
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 72 | scoped_refptr<SequencedTaskRunner> task_runner, |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 73 | TimeDelta interval, |
| 74 | const char* histogram_suffix = nullptr); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 75 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 76 | // You have to ensure that there are no pending writes at the moment |
| 77 | // of destruction. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 78 | ~ImportantFileWriter(); |
| 79 | |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 80 | const FilePath& path() const { return path_; } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 81 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 82 | // Returns true if there is a scheduled write pending which has not yet |
| 83 | // been started. |
| 84 | bool HasPendingWrite() const; |
| 85 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 86 | // Save |data| to target filename. Does not block. If there is a pending write |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 87 | // scheduled by ScheduleWrite(), it is cancelled. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 88 | void WriteNow(std::unique_ptr<std::string> data); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 89 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 90 | // Schedule a save to target filename. Data will be serialized and saved |
| 91 | // to disk after the commit interval. If another ScheduleWrite is issued |
| 92 | // before that, only one serialization and write to disk will happen, and |
| 93 | // the most recent |serializer| will be used. This operation does not block. |
| 94 | // |serializer| should remain valid through the lifetime of |
| 95 | // ImportantFileWriter. |
| 96 | void ScheduleWrite(DataSerializer* serializer); |
| 97 | |
| 98 | // Serialize data pending to be saved and execute write on backend thread. |
| 99 | void DoScheduledWrite(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 100 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 101 | // Registers |before_next_write_callback| and |after_next_write_callback| to |
| 102 | // be synchronously invoked from WriteFileAtomically() before its next write |
| 103 | // and after its next write, respectively. The boolean passed to |
| 104 | // |after_next_write_callback| indicates whether the write was successful. |
| 105 | // Both callbacks must be thread safe as they will be called on |task_runner_| |
| 106 | // and may be called during Chrome shutdown. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 107 | // If called more than once before a write is scheduled on |task_runner|, the |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 108 | // latest callbacks clobber the others. |
| 109 | void RegisterOnNextWriteCallbacks( |
Christian Dullweber | ee89d67 | 2018-11-29 15:12:28 | [diff] [blame] | 110 | OnceClosure before_next_write_callback, |
| 111 | OnceCallback<void(bool success)> after_next_write_callback); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 112 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 113 | TimeDelta commit_interval() const { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 114 | return commit_interval_; |
| 115 | } |
| 116 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 117 | // Overrides the timer to use for scheduling writes with |timer_override|. |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 118 | void SetTimerForTesting(OneShotTimer* timer_override); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 119 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 120 | private: |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 121 | const OneShotTimer& timer() const { |
| 122 | return timer_override_ ? *timer_override_ : timer_; |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 123 | } |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 124 | OneShotTimer& timer() { return timer_override_ ? *timer_override_ : timer_; } |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 125 | |
Greg Thompson | dfc7d7f4 | 2020-05-14 19:30:50 | [diff] [blame] | 126 | // Helper function to call WriteFileAtomically() with a |
| 127 | // std::unique_ptr<std::string>. |
| 128 | static void WriteScopedStringToFileAtomically( |
| 129 | const FilePath& path, |
| 130 | std::unique_ptr<std::string> data, |
| 131 | OnceClosure before_write_callback, |
| 132 | OnceCallback<void(bool success)> after_write_callback, |
| 133 | const std::string& histogram_suffix); |
| 134 | |
| 135 | // Writes |data| to |path|, recording histograms with an optional |
| 136 | // |histogram_suffix|. |from_instance| indicates whether the call originates |
| 137 | // from an instance of ImportantFileWriter or a direct call to |
| 138 | // WriteFileAtomically. When false, the directory containing |path| is added |
| 139 | // to the set cleaned by the ImportantFileWriterCleaner (Windows only). |
| 140 | static bool WriteFileAtomicallyImpl(const FilePath& path, |
| 141 | StringPiece data, |
| 142 | StringPiece histogram_suffix, |
| 143 | bool from_instance); |
| 144 | |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 145 | void ClearPendingWrite(); |
| 146 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 147 | // Invoked synchronously on the next write event. |
Christian Dullweber | ee89d67 | 2018-11-29 15:12:28 | [diff] [blame] | 148 | OnceClosure before_next_write_callback_; |
| 149 | OnceCallback<void(bool success)> after_next_write_callback_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 150 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 151 | // Path being written to. |
| 152 | const FilePath path_; |
| 153 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 154 | // TaskRunner for the thread on which file I/O can be done. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 155 | const scoped_refptr<SequencedTaskRunner> task_runner_; |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 156 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 157 | // Timer used to schedule commit after ScheduleWrite. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 158 | OneShotTimer timer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 159 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 160 | // An override for |timer_| used for testing. |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 161 | OneShotTimer* timer_override_ = nullptr; |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 162 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 163 | // Serializer which will provide the data to be saved. |
| 164 | DataSerializer* serializer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 165 | |
| 166 | // Time delta after which scheduled data will be written to disk. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 167 | const TimeDelta commit_interval_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 168 | |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 169 | // Custom histogram suffix. |
| 170 | const std::string histogram_suffix_; |
| 171 | |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 172 | SEQUENCE_CHECKER(sequence_checker_); |
| 173 | |
Jeremy Roman | 577d8849 | 2019-07-05 14:30:23 | [diff] [blame] | 174 | WeakPtrFactory<ImportantFileWriter> weak_factory_{this}; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 175 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 176 | DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 177 | }; |
| 178 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 179 | } // namespace base |
| 180 | |
| 181 | #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |