blob: 3d04eecbc6fd0bafa21d04b10495f3d5cd9afaff [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"
gab7d2fae42017-06-01 14:02:5515#include "base/sequence_checker.h"
bcwhitebe133b52016-04-11 20:03:3816#include "base/strings/string_piece.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.
gab7d2fae42017-06-01 14:02:5538class BASE_EXPORT ImportantFileWriter {
[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).
xaerox4ae8d172017-06-20 10:20:1256 static bool WriteFileAtomically(const FilePath& path,
57 StringPiece data,
58 StringPiece histogram_suffix = StringPiece());
[email protected]95b42e22012-11-29 14:00:1259
[email protected]6faa0e0d2009-04-28 06:50:3660 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3761 // |path| is the name of file to write.
[email protected]0de615a2012-11-08 04:40:5962 // |task_runner| is the SequencedTaskRunner instance where on which we will
63 // execute file I/O operations.
[email protected]6faa0e0d2009-04-28 06:50:3664 // All non-const methods, ctor and dtor must be called on the same thread.
thestig1433edb2015-08-06 21:45:2765 ImportantFileWriter(const FilePath& path,
xaerox4ae8d172017-06-20 10:20:1266 scoped_refptr<SequencedTaskRunner> task_runner,
67 const char* histogram_suffix = nullptr);
thestig1433edb2015-08-06 21:45:2768
69 // Same as above, but with a custom commit interval.
70 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2871 scoped_refptr<SequencedTaskRunner> task_runner,
xaerox4ae8d172017-06-20 10:20:1272 TimeDelta interval,
73 const char* histogram_suffix = nullptr);
[email protected]6faa0e0d2009-04-28 06:50:3674
[email protected]6c1164042009-05-08 14:41:0875 // You have to ensure that there are no pending writes at the moment
76 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3677 ~ImportantFileWriter();
78
[email protected]a83d42292010-08-17 22:51:1079 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3680
[email protected]6c1164042009-05-08 14:41:0881 // Returns true if there is a scheduled write pending which has not yet
82 // been started.
83 bool HasPendingWrite() const;
84
[email protected]6faa0e0d2009-04-28 06:50:3685 // Save |data| to target filename. Does not block. If there is a pending write
thestig1433edb2015-08-06 21:45:2786 // scheduled by ScheduleWrite(), it is cancelled.
dcheng093de9b2016-04-04 21:25:5187 void WriteNow(std::unique_ptr<std::string> data);
[email protected]6faa0e0d2009-04-28 06:50:3688
[email protected]6c1164042009-05-08 14:41:0889 // Schedule a save to target filename. Data will be serialized and saved
90 // to disk after the commit interval. If another ScheduleWrite is issued
91 // before that, only one serialization and write to disk will happen, and
92 // the most recent |serializer| will be used. This operation does not block.
93 // |serializer| should remain valid through the lifetime of
94 // ImportantFileWriter.
95 void ScheduleWrite(DataSerializer* serializer);
96
97 // Serialize data pending to be saved and execute write on backend thread.
98 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3699
probergec503d692016-09-28 19:51:05100 // Registers |before_next_write_callback| and |after_next_write_callback| to
101 // be synchronously invoked from WriteFileAtomically() before its next write
102 // and after its next write, respectively. The boolean passed to
103 // |after_next_write_callback| indicates whether the write was successful.
104 // Both callbacks must be thread safe as they will be called on |task_runner_|
105 // and may be called during Chrome shutdown.
probergefc46ac12016-09-21 18:03:00106 // If called more than once before a write is scheduled on |task_runner|, the
probergec503d692016-09-28 19:51:05107 // latest callbacks clobber the others.
108 void RegisterOnNextWriteCallbacks(
109 const Closure& before_next_write_callback,
110 const Callback<void(bool success)>& after_next_write_callback);
[email protected]e33c9512014-05-12 02:24:13111
[email protected]43486252012-10-24 16:33:36112 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:36113 return commit_interval_;
114 }
115
Sam McNally365428e2017-05-22 05:25:22116 // Overrides the timer to use for scheduling writes with |timer_override|.
117 void SetTimerForTesting(Timer* timer_override);
118
[email protected]6faa0e0d2009-04-28 06:50:36119 private:
Sam McNally365428e2017-05-22 05:25:22120 const Timer& timer() const {
121 return timer_override_ ? const_cast<const Timer&>(*timer_override_)
122 : timer_;
123 }
124 Timer& timer() { return timer_override_ ? *timer_override_ : timer_; }
125
Sam McNally8f50faa2017-05-19 05:08:00126 void ClearPendingWrite();
127
probergefc46ac12016-09-21 18:03:00128 // Invoked synchronously on the next write event.
probergec503d692016-09-28 19:51:05129 Closure before_next_write_callback_;
130 Callback<void(bool success)> after_next_write_callback_;
[email protected]e33c9512014-05-12 02:24:13131
[email protected]6faa0e0d2009-04-28 06:50:36132 // Path being written to.
133 const FilePath path_;
134
[email protected]0de615a2012-11-08 04:40:59135 // TaskRunner for the thread on which file I/O can be done.
thestig1433edb2015-08-06 21:45:27136 const scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29137
[email protected]6faa0e0d2009-04-28 06:50:36138 // Timer used to schedule commit after ScheduleWrite.
danakj8c3eb802015-09-24 07:53:00139 OneShotTimer timer_;
[email protected]6faa0e0d2009-04-28 06:50:36140
Sam McNally365428e2017-05-22 05:25:22141 // An override for |timer_| used for testing.
142 Timer* timer_override_ = nullptr;
143
[email protected]6c1164042009-05-08 14:41:08144 // Serializer which will provide the data to be saved.
145 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36146
147 // Time delta after which scheduled data will be written to disk.
thestig1433edb2015-08-06 21:45:27148 const TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36149
xaerox4ae8d172017-06-20 10:20:12150 // Custom histogram suffix.
151 const std::string histogram_suffix_;
152
gab7d2fae42017-06-01 14:02:55153 SEQUENCE_CHECKER(sequence_checker_);
154
[email protected]e33c9512014-05-12 02:24:13155 WeakPtrFactory<ImportantFileWriter> weak_factory_;
156
[email protected]6faa0e0d2009-04-28 06:50:36157 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
158};
159
[email protected]43486252012-10-24 16:33:36160} // namespace base
161
162#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_