[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 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
bcwhite | be133b5 | 2016-04-11 20:03:38 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 16 | #include "base/threading/non_thread_safe.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 17 | #include "base/time/time.h" |
| 18 | #include "base/timer/timer.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 19 | |
| 20 | namespace base { |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 21 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 22 | class SequencedTaskRunner; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 23 | |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 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). |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 26 | // |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 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 |
| 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 |
| 31 | // configuration (hardware and software) a significant likelihood of file |
| 32 | // corruption may remain, thus using ImportantFileWriter is not a valid |
| 33 | // substitute for file integrity checks and recovery codepaths for malformed |
| 34 | // files. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 35 | // |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 36 | // Also note that ImportantFileWriter can be *really* slow (cf. File::Flush() |
| 37 | // for details) and thus please don't block shutdown on ImportantFileWriter. |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 38 | class BASE_EXPORT ImportantFileWriter : public NonThreadSafe { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 39 | public: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 40 | // Used by ScheduleSave to lazily provide the data to be saved. Allows us |
| 41 | // to also batch data serializations. |
[email protected] | f59f33e | 2012-11-01 12:05:27 | [diff] [blame] | 42 | class BASE_EXPORT DataSerializer { |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 43 | public: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 44 | // Should put serialized string in |data| and return true on successful |
| 45 | // serialization. Will be called on the same thread on which |
| 46 | // ImportantFileWriter has been created. |
| 47 | virtual bool SerializeData(std::string* data) = 0; |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 48 | |
| 49 | protected: |
| 50 | virtual ~DataSerializer() {} |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 51 | }; |
| 52 | |
tnagel | c1d331a | 2016-08-16 21:39:54 | [diff] [blame] | 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 |
| 55 | // the class comment above). |
bcwhite | be133b5 | 2016-04-11 20:03:38 | [diff] [blame] | 56 | static bool WriteFileAtomically(const FilePath& path, StringPiece data); |
[email protected] | 95b42e2 | 2012-11-29 14:00:12 | [diff] [blame] | 57 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 58 | // Initialize the writer. |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 59 | // |path| is the name of file to write. |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 60 | // |task_runner| is the SequencedTaskRunner instance where on which we will |
| 61 | // execute file I/O operations. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 62 | // All non-const methods, ctor and dtor must be called on the same thread. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 63 | ImportantFileWriter(const FilePath& path, |
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 64 | scoped_refptr<SequencedTaskRunner> task_runner); |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 65 | |
| 66 | // Same as above, but with a custom commit interval. |
| 67 | ImportantFileWriter(const FilePath& path, |
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 68 | scoped_refptr<SequencedTaskRunner> task_runner, |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 69 | TimeDelta interval); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 70 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 71 | // You have to ensure that there are no pending writes at the moment |
| 72 | // of destruction. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 73 | ~ImportantFileWriter(); |
| 74 | |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 75 | const FilePath& path() const { return path_; } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 76 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 77 | // Returns true if there is a scheduled write pending which has not yet |
| 78 | // been started. |
| 79 | bool HasPendingWrite() const; |
| 80 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 81 | // Save |data| to target filename. Does not block. If there is a pending write |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 82 | // scheduled by ScheduleWrite(), it is cancelled. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 83 | void WriteNow(std::unique_ptr<std::string> data); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 84 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 85 | // Schedule a save to target filename. Data will be serialized and saved |
| 86 | // to disk after the commit interval. If another ScheduleWrite is issued |
| 87 | // before that, only one serialization and write to disk will happen, and |
| 88 | // the most recent |serializer| will be used. This operation does not block. |
| 89 | // |serializer| should remain valid through the lifetime of |
| 90 | // ImportantFileWriter. |
| 91 | void ScheduleWrite(DataSerializer* serializer); |
| 92 | |
| 93 | // Serialize data pending to be saved and execute write on backend thread. |
| 94 | void DoScheduledWrite(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 95 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 96 | // Registers |before_next_write_callback| and |after_next_write_callback| to |
| 97 | // be synchronously invoked from WriteFileAtomically() before its next write |
| 98 | // and after its next write, respectively. The boolean passed to |
| 99 | // |after_next_write_callback| indicates whether the write was successful. |
| 100 | // Both callbacks must be thread safe as they will be called on |task_runner_| |
| 101 | // and may be called during Chrome shutdown. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 102 | // If called more than once before a write is scheduled on |task_runner|, the |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 103 | // latest callbacks clobber the others. |
| 104 | void RegisterOnNextWriteCallbacks( |
| 105 | const Closure& before_next_write_callback, |
| 106 | const Callback<void(bool success)>& after_next_write_callback); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 107 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 108 | TimeDelta commit_interval() const { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 109 | return commit_interval_; |
| 110 | } |
| 111 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame^] | 112 | // Overrides the timer to use for scheduling writes with |timer_override|. |
| 113 | void SetTimerForTesting(Timer* timer_override); |
| 114 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 115 | private: |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame^] | 116 | const Timer& timer() const { |
| 117 | return timer_override_ ? const_cast<const Timer&>(*timer_override_) |
| 118 | : timer_; |
| 119 | } |
| 120 | Timer& timer() { return timer_override_ ? *timer_override_ : timer_; } |
| 121 | |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 122 | void ClearPendingWrite(); |
| 123 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 124 | // Invoked synchronously on the next write event. |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 125 | Closure before_next_write_callback_; |
| 126 | Callback<void(bool success)> after_next_write_callback_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 127 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 128 | // Path being written to. |
| 129 | const FilePath path_; |
| 130 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 131 | // TaskRunner for the thread on which file I/O can be done. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 132 | const scoped_refptr<SequencedTaskRunner> task_runner_; |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 133 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 134 | // Timer used to schedule commit after ScheduleWrite. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 135 | OneShotTimer timer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 136 | |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame^] | 137 | // An override for |timer_| used for testing. |
| 138 | Timer* timer_override_ = nullptr; |
| 139 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 140 | // Serializer which will provide the data to be saved. |
| 141 | DataSerializer* serializer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 142 | |
| 143 | // Time delta after which scheduled data will be written to disk. |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame] | 144 | const TimeDelta commit_interval_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 145 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 146 | WeakPtrFactory<ImportantFileWriter> weak_factory_; |
| 147 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 149 | }; |
| 150 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 151 | } // namespace base |
| 152 | |
| 153 | #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |