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

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

Issue 1265363002: Cleanup nits in ImportantFileWriter and JsonPrefStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 5 years, 4 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
« no previous file with comments | « no previous file | base/files/important_file_writer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // Save |data| to |path| in an atomic manner (see the class comment above). 55 // Save |data| to |path| in an atomic manner (see the class comment above).
56 // Blocks and writes data on the current thread. 56 // Blocks and writes data on the current thread.
57 static bool WriteFileAtomically(const FilePath& path, 57 static bool WriteFileAtomically(const FilePath& path,
58 const std::string& data); 58 const std::string& data);
59 59
60 // Initialize the writer. 60 // Initialize the writer.
61 // |path| is the name of file to write. 61 // |path| is the name of file to write.
62 // |task_runner| is the SequencedTaskRunner instance where on which we will 62 // |task_runner| is the SequencedTaskRunner instance where on which we will
63 // execute file I/O operations. 63 // execute file I/O operations.
64 // All non-const methods, ctor and dtor must be called on the same thread. 64 // All non-const methods, ctor and dtor must be called on the same thread.
65 ImportantFileWriter( 65 ImportantFileWriter(const FilePath& path,
66 const FilePath& path, 66 const scoped_refptr<SequencedTaskRunner>& task_runner);
67 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 67
68 // Same as above, but with a custom commit interval.
69 ImportantFileWriter(const FilePath& path,
70 const scoped_refptr<SequencedTaskRunner>& task_runner,
71 TimeDelta interval);
68 72
69 // You have to ensure that there are no pending writes at the moment 73 // You have to ensure that there are no pending writes at the moment
70 // of destruction. 74 // of destruction.
71 ~ImportantFileWriter(); 75 ~ImportantFileWriter();
72 76
73 const FilePath& path() const { return path_; } 77 const FilePath& path() const { return path_; }
74 78
75 // Returns true if there is a scheduled write pending which has not yet 79 // Returns true if there is a scheduled write pending which has not yet
76 // been started. 80 // been started.
77 bool HasPendingWrite() const; 81 bool HasPendingWrite() const;
78 82
79 // Save |data| to target filename. Does not block. If there is a pending write 83 // Save |data| to target filename. Does not block. If there is a pending write
80 // scheduled by ScheduleWrite, it is cancelled. 84 // scheduled by ScheduleWrite(), it is cancelled.
81 void WriteNow(scoped_ptr<std::string> data); 85 void WriteNow(scoped_ptr<std::string> data);
82 86
83 // Schedule a save to target filename. Data will be serialized and saved 87 // Schedule a save to target filename. Data will be serialized and saved
84 // to disk after the commit interval. If another ScheduleWrite is issued 88 // to disk after the commit interval. If another ScheduleWrite is issued
85 // before that, only one serialization and write to disk will happen, and 89 // 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. 90 // the most recent |serializer| will be used. This operation does not block.
87 // |serializer| should remain valid through the lifetime of 91 // |serializer| should remain valid through the lifetime of
88 // ImportantFileWriter. 92 // ImportantFileWriter.
89 void ScheduleWrite(DataSerializer* serializer); 93 void ScheduleWrite(DataSerializer* serializer);
90 94
91 // Serialize data pending to be saved and execute write on backend thread. 95 // Serialize data pending to be saved and execute write on backend thread.
92 void DoScheduledWrite(); 96 void DoScheduledWrite();
93 97
94 // Registers |on_next_successful_write| to be called once, on the next 98 // Registers |on_next_successful_write| to be called once, on the next
95 // successful write event. Only one callback can be set at once. 99 // successful write event. Only one callback can be set at once.
96 void RegisterOnNextSuccessfulWriteCallback( 100 void RegisterOnNextSuccessfulWriteCallback(
97 const base::Closure& on_next_successful_write); 101 const Closure& on_next_successful_write);
98 102
99 TimeDelta commit_interval() const { 103 TimeDelta commit_interval() const {
100 return commit_interval_; 104 return commit_interval_;
101 } 105 }
102 106
103 void set_commit_interval(const TimeDelta& interval) {
104 commit_interval_ = interval;
105 }
106
107 private: 107 private:
108 // Helper method for WriteNow(). 108 // Helper method for WriteNow().
109 bool PostWriteTask(const Callback<bool()>& task); 109 bool PostWriteTask(const Callback<bool()>& task);
110 110
111 // If |result| is true and |on_next_successful_write_| is set, invokes 111 // If |result| is true and |on_next_successful_write_| is set, invokes
112 // |on_successful_write_| and then resets it; no-ops otherwise. 112 // |on_successful_write_| and then resets it; no-ops otherwise.
113 void ForwardSuccessfulWrite(bool result); 113 void ForwardSuccessfulWrite(bool result);
114 114
115 // Invoked once and then reset on the next successful write event. 115 // Invoked once and then reset on the next successful write event.
116 base::Closure on_next_successful_write_; 116 Closure on_next_successful_write_;
117 117
118 // Path being written to. 118 // Path being written to.
119 const FilePath path_; 119 const FilePath path_;
120 120
121 // TaskRunner for the thread on which file I/O can be done. 121 // TaskRunner for the thread on which file I/O can be done.
122 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 122 const scoped_refptr<SequencedTaskRunner> task_runner_;
123 123
124 // Timer used to schedule commit after ScheduleWrite. 124 // Timer used to schedule commit after ScheduleWrite.
125 OneShotTimer<ImportantFileWriter> timer_; 125 OneShotTimer<ImportantFileWriter> timer_;
126 126
127 // Serializer which will provide the data to be saved. 127 // Serializer which will provide the data to be saved.
128 DataSerializer* serializer_; 128 DataSerializer* serializer_;
129 129
130 // Time delta after which scheduled data will be written to disk. 130 // Time delta after which scheduled data will be written to disk.
131 TimeDelta commit_interval_; 131 const TimeDelta commit_interval_;
132 132
133 WeakPtrFactory<ImportantFileWriter> weak_factory_; 133 WeakPtrFactory<ImportantFileWriter> weak_factory_;
134 134
135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); 135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
136 }; 136 };
137 137
138 } // namespace base 138 } // namespace base
139 139
140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ 140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_
OLDNEW
« no previous file with comments | « no previous file | base/files/important_file_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698