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 can be set at once. |
| 95 void RegisterOnNextSuccessfulWriteCallback( |
| 96 const base::Closure& on_next_successful_write); |
| 97 |
92 TimeDelta commit_interval() const { | 98 TimeDelta commit_interval() const { |
93 return commit_interval_; | 99 return commit_interval_; |
94 } | 100 } |
95 | 101 |
96 void set_commit_interval(const TimeDelta& interval) { | 102 void set_commit_interval(const TimeDelta& interval) { |
97 commit_interval_ = interval; | 103 commit_interval_ = interval; |
98 } | 104 } |
99 | 105 |
100 private: | 106 private: |
| 107 // Helper method for WriteNow(). |
| 108 bool PostWriteTask(const std::string& data); |
| 109 |
| 110 // If |result| is true and |on_next_successful_write_| is set, invokes |
| 111 // |on_successful_write_| and then resets it; no-ops otherwise. |
| 112 void ForwardSuccessfulWrite(bool result); |
| 113 |
| 114 // Invoked once and then reset on the next successful write event. |
| 115 base::Closure on_next_successful_write_; |
| 116 |
101 // Path being written to. | 117 // Path being written to. |
102 const FilePath path_; | 118 const FilePath path_; |
103 | 119 |
104 // TaskRunner for the thread on which file I/O can be done. | 120 // TaskRunner for the thread on which file I/O can be done. |
105 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 121 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
106 | 122 |
107 // Timer used to schedule commit after ScheduleWrite. | 123 // Timer used to schedule commit after ScheduleWrite. |
108 OneShotTimer<ImportantFileWriter> timer_; | 124 OneShotTimer<ImportantFileWriter> timer_; |
109 | 125 |
110 // Serializer which will provide the data to be saved. | 126 // Serializer which will provide the data to be saved. |
111 DataSerializer* serializer_; | 127 DataSerializer* serializer_; |
112 | 128 |
113 // Time delta after which scheduled data will be written to disk. | 129 // Time delta after which scheduled data will be written to disk. |
114 TimeDelta commit_interval_; | 130 TimeDelta commit_interval_; |
115 | 131 |
| 132 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
| 133 |
116 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 134 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
117 }; | 135 }; |
118 | 136 |
119 } // namespace base | 137 } // namespace base |
120 | 138 |
121 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 139 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |