Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: base/files/important_file_writer.h

Issue 1127963002: Implement lossy pref behavior for JsonPrefStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@prefs-fix-flags
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_ 5 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_
6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ 6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
(...skipping 21 matching lines...) Expand all
32 // end up with a broken file. 32 // end up with a broken file.
33 // 33 //
34 // To be safe, we don't start with writing directly to F. Instead, we write to 34 // To be safe, we don't start with writing directly to F. Instead, we write to
35 // to a temporary file. Only after that write is successful, we rename the 35 // to a temporary file. Only after that write is successful, we rename the
36 // temporary file to target filename. 36 // temporary file to target filename.
37 // 37 //
38 // If you want to know more about this approach and ext3/ext4 fsync issues, see 38 // If you want to know more about this approach and ext3/ext4 fsync issues, see
39 // http://valhenson.livejournal.com/37921.html 39 // http://valhenson.livejournal.com/37921.html
40 class BASE_EXPORT ImportantFileWriter : public NonThreadSafe { 40 class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
41 public: 41 public:
42 // Used by ScheduleSave to lazily provide the data to be saved. Allows us 42 // Used to lazily provide the data to be saved. Allows us to also batch data
43 // to also batch data serializations. 43 // serializations.
44 class BASE_EXPORT DataSerializer { 44 class BASE_EXPORT DataSerializer {
45 public: 45 public:
46 // Should put serialized string in |data| and return true on successful 46 // Should put serialized string in |data| and return true on successful
47 // serialization. Will be called on the same thread on which 47 // serialization. Will be called on the same thread on which
48 // ImportantFileWriter has been created. 48 // ImportantFileWriter has been created.
49 virtual bool SerializeData(std::string* data) = 0; 49 virtual bool SerializeData(std::string* data) = 0;
50 50
51 protected: 51 protected:
52 virtual ~DataSerializer() {} 52 virtual ~DataSerializer() {}
53 }; 53 };
54 54
55 // Save |data| to |path| in an atomic manner (see the class comment above). 55 virtual ~ImportantFileWriter() {}
56 // Blocks and writes data on the current thread.
57 static bool WriteFileAtomically(const FilePath& path,
58 const std::string& data);
59
60 // Initialize the writer.
61 // |path| is the name of file to write.
62 // |task_runner| is the SequencedTaskRunner instance where on which we will
63 // execute file I/O operations.
64 // All non-const methods, ctor and dtor must be called on the same thread.
65 ImportantFileWriter(
66 const FilePath& path,
67 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
68
69 // You have to ensure that there are no pending writes at the moment
70 // of destruction.
71 ~ImportantFileWriter();
72
73 const FilePath& path() const { return path_; }
74 56
75 // Returns true if there is a scheduled write pending which has not yet 57 // Returns true if there is a scheduled write pending which has not yet
76 // been started. 58 // been started.
77 bool HasPendingWrite() const; 59 virtual bool HasPendingWrite() const = 0;
78 60
79 // Save |data| to target filename. Does not block. If there is a pending write 61 // Save |data| to target filename. Does not block. If there is a pending write
80 // scheduled by ScheduleWrite, it is cancelled. 62 // scheduled by ScheduleWrite, it is cancelled.
81 void WriteNow(scoped_ptr<std::string> data); 63 virtual void WriteNow(scoped_ptr<std::string> data) = 0;
82 64
83 // Schedule a save to target filename. Data will be serialized and saved 65 // Schedule a save to target filename. Data will be serialized and saved
84 // to disk after the commit interval. If another ScheduleWrite is issued 66 // to disk after the commit interval. If another ScheduleWrite is issued
85 // before that, only one serialization and write to disk will happen, and 67 // before that, only one serialization and write to disk will happen, and
86 // the most recent |serializer| will be used. This operation does not block. 68 // the most recent |serializer| will be used. This operation does not block.
87 // |serializer| should remain valid through the lifetime of 69 // |serializer| should remain valid through the lifetime of
88 // ImportantFileWriter. 70 // ImportantFileWriter.
89 void ScheduleWrite(DataSerializer* serializer); 71 virtual void ScheduleWrite(DataSerializer* serializer) = 0;
90 72
91 // Serialize data pending to be saved and execute write on backend thread. 73 // Serialize data pending to be saved and execute write on backend thread.
92 void DoScheduledWrite(); 74 virtual void DoScheduledWrite() = 0;
93 75
94 // Registers |on_next_successful_write| to be called once, on the next 76 // Registers |on_next_successful_write| to be called once, on the next
95 // successful write event. Only one callback can be set at once. 77 // successful write event. Only one callback can be set at once.
78 virtual void RegisterOnNextSuccessfulWriteCallback(
79 const base::Closure& on_next_successful_write) = 0;
80
81 // Returns the time delta after which scheduled data will be written to disk.
82 virtual TimeDelta CommitInterval() const = 0;
83 };
84
85 class BASE_EXPORT ImportantFileWriterImpl : public ImportantFileWriter {
86 public:
87 // Save |data| to |path| in an atomic manner (see the class comment above).
88 // Blocks and writes data on the current thread.
89 static bool WriteFileAtomically(const FilePath& path,
90 const std::string& data);
91
92 // Initialize the writer.
93 // |path| is the name of file to write.
94 // |task_runner| is the SequencedTaskRunner instance where on which we will
95 // execute file I/O operations.
96 // All non-const methods, ctor and dtor must be called on the same thread.
97 ImportantFileWriterImpl(
98 const FilePath& path,
99 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
100
101 // You have to ensure that there are no pending writes at the moment
102 // of destruction.
103 ~ImportantFileWriterImpl() override;
104
105 const FilePath& path() const { return path_; }
106
107 // ImportantFileWriter overrides.
108 bool HasPendingWrite() const override;
109 void WriteNow(scoped_ptr<std::string> data) override;
110 void ScheduleWrite(DataSerializer* serializer) override;
111 void DoScheduledWrite() override;
96 void RegisterOnNextSuccessfulWriteCallback( 112 void RegisterOnNextSuccessfulWriteCallback(
97 const base::Closure& on_next_successful_write); 113 const base::Closure& on_next_successful_write) override;
98 114 TimeDelta CommitInterval() const override;
99 TimeDelta commit_interval() const {
100 return commit_interval_;
101 }
102 115
103 void set_commit_interval(const TimeDelta& interval) { 116 void set_commit_interval(const TimeDelta& interval) {
104 commit_interval_ = interval; 117 commit_interval_ = interval;
105 } 118 }
106 119
107 private: 120 private:
108 // Helper method for WriteNow(). 121 // Helper method for WriteNow().
109 bool PostWriteTask(const Callback<bool()>& task); 122 bool PostWriteTask(const Callback<bool()>& task);
110 123
111 // If |result| is true and |on_next_successful_write_| is set, invokes 124 // If |result| is true and |on_next_successful_write_| is set, invokes
112 // |on_successful_write_| and then resets it; no-ops otherwise. 125 // |on_successful_write_| and then resets it; no-ops otherwise.
113 void ForwardSuccessfulWrite(bool result); 126 void ForwardSuccessfulWrite(bool result);
114 127
115 // Invoked once and then reset on the next successful write event. 128 // Invoked once and then reset on the next successful write event.
116 base::Closure on_next_successful_write_; 129 base::Closure on_next_successful_write_;
117 130
118 // Path being written to. 131 // Path being written to.
119 const FilePath path_; 132 const FilePath path_;
120 133
121 // TaskRunner for the thread on which file I/O can be done. 134 // TaskRunner for the thread on which file I/O can be done.
122 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 135 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
123 136
124 // Timer used to schedule commit after ScheduleWrite. 137 // Timer used to schedule commit after ScheduleWrite.
125 OneShotTimer<ImportantFileWriter> timer_; 138 OneShotTimer<ImportantFileWriterImpl> timer_;
126 139
127 // Serializer which will provide the data to be saved. 140 // Serializer which will provide the data to be saved.
128 DataSerializer* serializer_; 141 DataSerializer* serializer_;
129 142
130 // Time delta after which scheduled data will be written to disk. 143 // Time delta after which scheduled data will be written to disk.
131 TimeDelta commit_interval_; 144 TimeDelta commit_interval_;
132 145
133 WeakPtrFactory<ImportantFileWriter> weak_factory_; 146 WeakPtrFactory<ImportantFileWriterImpl> weak_factory_;
134 147
135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); 148 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriterImpl);
136 }; 149 };
137 150
138 } // namespace base 151 } // namespace base
139 152
140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ 153 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_
OLDNEW
« no previous file with comments | « no previous file | base/files/important_file_writer.cc » ('j') | base/prefs/json_pref_store_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698