blob: 46efbaa145c2cf4c683c64b4f5fda6765da81f57 [file] [log] [blame]
[email protected]512d03f2012-06-26 01:06:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6faa0e0d2009-04-28 06:50:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]43486252012-10-24 16:33:365#ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_
6#define BASE_FILES_IMPORTANT_FILE_WRITER_H_
[email protected]6faa0e0d2009-04-28 06:50:367
8#include <string>
9
[email protected]43486252012-10-24 16:33:3610#include "base/base_export.h"
[email protected]e33c9512014-05-12 02:24:1311#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
avi543540e2015-12-24 05:15:3213#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
bcwhitebe133b52016-04-11 20:03:3815#include "base/strings/string_piece.h"
[email protected]c9177502011-01-01 04:48:4916#include "base/threading/non_thread_safe.h"
[email protected]99084f62013-06-28 00:49:0717#include "base/time/time.h"
18#include "base/timer/timer.h"
[email protected]6faa0e0d2009-04-28 06:50:3619
20namespace base {
[email protected]43486252012-10-24 16:33:3621
[email protected]0de615a2012-11-08 04:40:5922class SequencedTaskRunner;
[email protected]6faa0e0d2009-04-28 06:50:3623
tnagelc1d331a2016-08-16 21:39:5424// 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]6faa0e0d2009-04-28 06:50:3626//
tnagelc1d331a2016-08-16 21:39:5427// 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]6faa0e0d2009-04-28 06:50:3635//
tnagelc1d331a2016-08-16 21:39:5436// 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]43486252012-10-24 16:33:3638class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3639 public:
[email protected]6c1164042009-05-08 14:41:0840 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
41 // to also batch data serializations.
[email protected]f59f33e2012-11-01 12:05:2742 class BASE_EXPORT DataSerializer {
[email protected]6c1164042009-05-08 14:41:0843 public:
[email protected]6c1164042009-05-08 14:41:0844 // 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]512d03f2012-06-26 01:06:0648
49 protected:
50 virtual ~DataSerializer() {}
[email protected]6c1164042009-05-08 14:41:0851 };
52
tnagelc1d331a2016-08-16 21:39:5453 // 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).
bcwhitebe133b52016-04-11 20:03:3856 static bool WriteFileAtomically(const FilePath& path, StringPiece data);
[email protected]95b42e22012-11-29 14:00:1257
[email protected]6faa0e0d2009-04-28 06:50:3658 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3759 // |path| is the name of file to write.
[email protected]0de615a2012-11-08 04:40:5960 // |task_runner| is the SequencedTaskRunner instance where on which we will
61 // execute file I/O operations.
[email protected]6faa0e0d2009-04-28 06:50:3662 // All non-const methods, ctor and dtor must be called on the same thread.
thestig1433edb2015-08-06 21:45:2763 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2864 scoped_refptr<SequencedTaskRunner> task_runner);
thestig1433edb2015-08-06 21:45:2765
66 // Same as above, but with a custom commit interval.
67 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2868 scoped_refptr<SequencedTaskRunner> task_runner,
thestig1433edb2015-08-06 21:45:2769 TimeDelta interval);
[email protected]6faa0e0d2009-04-28 06:50:3670
[email protected]6c1164042009-05-08 14:41:0871 // You have to ensure that there are no pending writes at the moment
72 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3673 ~ImportantFileWriter();
74
[email protected]a83d42292010-08-17 22:51:1075 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3676
[email protected]6c1164042009-05-08 14:41:0877 // Returns true if there is a scheduled write pending which has not yet
78 // been started.
79 bool HasPendingWrite() const;
80
[email protected]6faa0e0d2009-04-28 06:50:3681 // Save |data| to target filename. Does not block. If there is a pending write
thestig1433edb2015-08-06 21:45:2782 // scheduled by ScheduleWrite(), it is cancelled.
dcheng093de9b2016-04-04 21:25:5183 void WriteNow(std::unique_ptr<std::string> data);
[email protected]6faa0e0d2009-04-28 06:50:3684
[email protected]6c1164042009-05-08 14:41:0885 // 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]6faa0e0d2009-04-28 06:50:3695
probergec503d692016-09-28 19:51:0596 // 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.
probergefc46ac12016-09-21 18:03:00102 // If called more than once before a write is scheduled on |task_runner|, the
probergec503d692016-09-28 19:51:05103 // 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]e33c9512014-05-12 02:24:13107
[email protected]43486252012-10-24 16:33:36108 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:36109 return commit_interval_;
110 }
111
Sam McNally365428e2017-05-22 05:25:22112 // Overrides the timer to use for scheduling writes with |timer_override|.
113 void SetTimerForTesting(Timer* timer_override);
114
[email protected]6faa0e0d2009-04-28 06:50:36115 private:
Sam McNally365428e2017-05-22 05:25:22116 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 McNally8f50faa2017-05-19 05:08:00122 void ClearPendingWrite();
123
probergefc46ac12016-09-21 18:03:00124 // Invoked synchronously on the next write event.
probergec503d692016-09-28 19:51:05125 Closure before_next_write_callback_;
126 Callback<void(bool success)> after_next_write_callback_;
[email protected]e33c9512014-05-12 02:24:13127
[email protected]6faa0e0d2009-04-28 06:50:36128 // Path being written to.
129 const FilePath path_;
130
[email protected]0de615a2012-11-08 04:40:59131 // TaskRunner for the thread on which file I/O can be done.
thestig1433edb2015-08-06 21:45:27132 const scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29133
[email protected]6faa0e0d2009-04-28 06:50:36134 // Timer used to schedule commit after ScheduleWrite.
danakj8c3eb802015-09-24 07:53:00135 OneShotTimer timer_;
[email protected]6faa0e0d2009-04-28 06:50:36136
Sam McNally365428e2017-05-22 05:25:22137 // An override for |timer_| used for testing.
138 Timer* timer_override_ = nullptr;
139
[email protected]6c1164042009-05-08 14:41:08140 // Serializer which will provide the data to be saved.
141 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36142
143 // Time delta after which scheduled data will be written to disk.
thestig1433edb2015-08-06 21:45:27144 const TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36145
[email protected]e33c9512014-05-12 02:24:13146 WeakPtrFactory<ImportantFileWriter> weak_factory_;
147
[email protected]6faa0e0d2009-04-28 06:50:36148 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
149};
150
[email protected]43486252012-10-24 16:33:36151} // namespace base
152
153#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_