blob: cd724e326691a2d1e2516b2ffd1859c78bb86946 [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:3623class Thread;
[email protected]6faa0e0d2009-04-28 06:50:3624
tnagelc1d331a2016-08-16 21:39:5425// 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]6faa0e0d2009-04-28 06:50:3627//
tnagelc1d331a2016-08-16 21:39:5428// 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]6faa0e0d2009-04-28 06:50:3636//
tnagelc1d331a2016-08-16 21:39:5437// Also note that ImportantFileWriter can be *really* slow (cf. File::Flush()
38// for details) and thus please don't block shutdown on ImportantFileWriter.
[email protected]43486252012-10-24 16:33:3639class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3640 public:
[email protected]6c1164042009-05-08 14:41:0841 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
42 // to also batch data serializations.
[email protected]f59f33e2012-11-01 12:05:2743 class BASE_EXPORT DataSerializer {
[email protected]6c1164042009-05-08 14:41:0844 public:
[email protected]6c1164042009-05-08 14:41:0845 // 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]512d03f2012-06-26 01:06:0649
50 protected:
51 virtual ~DataSerializer() {}
[email protected]6c1164042009-05-08 14:41:0852 };
53
tnagelc1d331a2016-08-16 21:39:5454 // 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).
bcwhitebe133b52016-04-11 20:03:3857 static bool WriteFileAtomically(const FilePath& path, StringPiece data);
[email protected]95b42e22012-11-29 14:00:1258
[email protected]6faa0e0d2009-04-28 06:50:3659 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3760 // |path| is the name of file to write.
[email protected]0de615a2012-11-08 04:40:5961 // |task_runner| is the SequencedTaskRunner instance where on which we will
62 // execute file I/O operations.
[email protected]6faa0e0d2009-04-28 06:50:3663 // All non-const methods, ctor and dtor must be called on the same thread.
thestig1433edb2015-08-06 21:45:2764 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2865 scoped_refptr<SequencedTaskRunner> task_runner);
thestig1433edb2015-08-06 21:45:2766
67 // Same as above, but with a custom commit interval.
68 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2869 scoped_refptr<SequencedTaskRunner> task_runner,
thestig1433edb2015-08-06 21:45:2770 TimeDelta interval);
[email protected]6faa0e0d2009-04-28 06:50:3671
[email protected]6c1164042009-05-08 14:41:0872 // You have to ensure that there are no pending writes at the moment
73 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3674 ~ImportantFileWriter();
75
[email protected]a83d42292010-08-17 22:51:1076 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3677
[email protected]6c1164042009-05-08 14:41:0878 // Returns true if there is a scheduled write pending which has not yet
79 // been started.
80 bool HasPendingWrite() const;
81
[email protected]6faa0e0d2009-04-28 06:50:3682 // Save |data| to target filename. Does not block. If there is a pending write
thestig1433edb2015-08-06 21:45:2783 // scheduled by ScheduleWrite(), it is cancelled.
dcheng093de9b2016-04-04 21:25:5184 void WriteNow(std::unique_ptr<std::string> data);
[email protected]6faa0e0d2009-04-28 06:50:3685
[email protected]6c1164042009-05-08 14:41:0886 // Schedule a save to target filename. Data will be serialized and saved
87 // to disk after the commit interval. If another ScheduleWrite is issued
88 // before that, only one serialization and write to disk will happen, and
89 // the most recent |serializer| will be used. This operation does not block.
90 // |serializer| should remain valid through the lifetime of
91 // ImportantFileWriter.
92 void ScheduleWrite(DataSerializer* serializer);
93
94 // Serialize data pending to be saved and execute write on backend thread.
95 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3696
probergefc46ac12016-09-21 18:03:0097 // Registers |on_next_write_callback| to be synchronously invoked from
98 // WriteFileAtomically() on its next write (i.e. from |task_runner_|), with
99 // |success| indicating whether it succeeded or not.
100 // |on_next_write_callback| must be thread safe, as it will be called on
101 // |task_runner_| and may be called during Chrome shutdown.
102 // If called more than once before a write is scheduled on |task_runner|, the
103 // latest callback clobbers the others.
104 void RegisterOnNextWriteCallback(
105 const Callback<void(bool success)>& on_next_write_callback);
[email protected]e33c9512014-05-12 02:24:13106
[email protected]43486252012-10-24 16:33:36107 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:36108 return commit_interval_;
109 }
110
[email protected]6faa0e0d2009-04-28 06:50:36111 private:
probergefc46ac12016-09-21 18:03:00112 // Invoked synchronously on the next write event.
113 Callback<void(bool success)> on_next_write_callback_;
[email protected]e33c9512014-05-12 02:24:13114
[email protected]6faa0e0d2009-04-28 06:50:36115 // Path being written to.
116 const FilePath path_;
117
[email protected]0de615a2012-11-08 04:40:59118 // TaskRunner for the thread on which file I/O can be done.
thestig1433edb2015-08-06 21:45:27119 const scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29120
[email protected]6faa0e0d2009-04-28 06:50:36121 // Timer used to schedule commit after ScheduleWrite.
danakj8c3eb802015-09-24 07:53:00122 OneShotTimer timer_;
[email protected]6faa0e0d2009-04-28 06:50:36123
[email protected]6c1164042009-05-08 14:41:08124 // Serializer which will provide the data to be saved.
125 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36126
127 // Time delta after which scheduled data will be written to disk.
thestig1433edb2015-08-06 21:45:27128 const TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36129
[email protected]e33c9512014-05-12 02:24:13130 WeakPtrFactory<ImportantFileWriter> weak_factory_;
131
[email protected]6faa0e0d2009-04-28 06:50:36132 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
133};
134
[email protected]43486252012-10-24 16:33:36135} // namespace base
136
137#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_