OLD | NEW |
---|---|
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" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
16 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 | 20 |
20 class SequencedTaskRunner; | 21 class SequencedTaskRunner; |
21 class Thread; | 22 class Thread; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 // to disk after the commit interval. If another ScheduleWrite is issued | 83 // to disk after the commit interval. If another ScheduleWrite is issued |
83 // before that, only one serialization and write to disk will happen, and | 84 // before that, only one serialization and write to disk will happen, and |
84 // the most recent |serializer| will be used. This operation does not block. | 85 // the most recent |serializer| will be used. This operation does not block. |
85 // |serializer| should remain valid through the lifetime of | 86 // |serializer| should remain valid through the lifetime of |
86 // ImportantFileWriter. | 87 // ImportantFileWriter. |
87 void ScheduleWrite(DataSerializer* serializer); | 88 void ScheduleWrite(DataSerializer* serializer); |
88 | 89 |
89 // Serialize data pending to be saved and execute write on backend thread. | 90 // Serialize data pending to be saved and execute write on backend thread. |
90 void DoScheduledWrite(); | 91 void DoScheduledWrite(); |
91 | 92 |
93 // Registers |on_next_successful_write| to be called once, on the next | |
94 // successful write event. Only one callback should be set at once, if this | |
Nico
2014/05/06 21:14:50
"…can be set at once." (remove comment after that)
gab
2014/05/07 03:59:09
Done.
| |
95 // isn't sufficient in the future, this chould be expanded into an observer | |
96 // API. | |
97 void RegisterOnNextSuccessfulWriteCallback( | |
98 const base::Closure& on_next_successful_write); | |
99 | |
92 TimeDelta commit_interval() const { | 100 TimeDelta commit_interval() const { |
93 return commit_interval_; | 101 return commit_interval_; |
94 } | 102 } |
95 | 103 |
96 void set_commit_interval(const TimeDelta& interval) { | 104 void set_commit_interval(const TimeDelta& interval) { |
97 commit_interval_ = interval; | 105 commit_interval_ = interval; |
98 } | 106 } |
99 | 107 |
100 private: | 108 private: |
109 // If |result| is true and |on_next_successful_write_| is set, invokes | |
110 // |on_successful_write_| and then resets it; no-ops otherwise. | |
111 void ForwardSuccessfulWrite(bool result); | |
112 | |
113 // Invoked once and then reset on the next successful write event. | |
114 base::Closure on_next_successful_write_; | |
115 | |
101 // Path being written to. | 116 // Path being written to. |
102 const FilePath path_; | 117 const FilePath path_; |
103 | 118 |
104 // TaskRunner for the thread on which file I/O can be done. | 119 // TaskRunner for the thread on which file I/O can be done. |
105 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 120 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
106 | 121 |
107 // Timer used to schedule commit after ScheduleWrite. | 122 // Timer used to schedule commit after ScheduleWrite. |
108 OneShotTimer<ImportantFileWriter> timer_; | 123 OneShotTimer<ImportantFileWriter> timer_; |
109 | 124 |
110 // Serializer which will provide the data to be saved. | 125 // Serializer which will provide the data to be saved. |
111 DataSerializer* serializer_; | 126 DataSerializer* serializer_; |
112 | 127 |
113 // Time delta after which scheduled data will be written to disk. | 128 // Time delta after which scheduled data will be written to disk. |
114 TimeDelta commit_interval_; | 129 TimeDelta commit_interval_; |
115 | 130 |
131 WeakPtrFactory<ImportantFileWriter> weak_factory_; | |
132 | |
116 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 133 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
117 }; | 134 }; |
118 | 135 |
119 } // namespace base | 136 } // namespace base |
120 | 137 |
121 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 138 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |