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/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 class MessageLoopProxy; | 20 class SequencedTaskRunner; |
21 class Thread; | 21 class Thread; |
22 | 22 |
23 // Helper to ensure that a file won't be corrupted by the write (for example on | 23 // Helper to ensure that a file won't be corrupted by the write (for example on |
24 // application crash). Consider a naive way to save an important file F: | 24 // application crash). Consider a naive way to save an important file F: |
25 // | 25 // |
26 // 1. Open F for writing, truncating it. | 26 // 1. Open F for writing, truncating it. |
27 // 2. Write new data to F. | 27 // 2. Write new data to F. |
28 // | 28 // |
29 // It's good when it works, but it gets very bad if step 2. doesn't complete. | 29 // It's good when it works, but it gets very bad if step 2. doesn't complete. |
30 // It can be caused by a crash, a computer hang, or a weird I/O error. And you | 30 // It can be caused by a crash, a computer hang, or a weird I/O error. And you |
(...skipping 15 matching lines...) Expand all Loading... |
46 // serialization. Will be called on the same thread on which | 46 // serialization. Will be called on the same thread on which |
47 // ImportantFileWriter has been created. | 47 // ImportantFileWriter has been created. |
48 virtual bool SerializeData(std::string* data) = 0; | 48 virtual bool SerializeData(std::string* data) = 0; |
49 | 49 |
50 protected: | 50 protected: |
51 virtual ~DataSerializer() {} | 51 virtual ~DataSerializer() {} |
52 }; | 52 }; |
53 | 53 |
54 // Initialize the writer. | 54 // Initialize the writer. |
55 // |path| is the name of file to write. | 55 // |path| is the name of file to write. |
56 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which | 56 // |task_runner| is the SequencedTaskRunner instance where on which we will |
57 // file I/O can be done. | 57 // execute file I/O operations. |
58 // All non-const methods, ctor and dtor must be called on the same thread. | 58 // All non-const methods, ctor and dtor must be called on the same thread. |
59 ImportantFileWriter(const FilePath& path, | 59 ImportantFileWriter(const FilePath& path, |
60 MessageLoopProxy* file_message_loop_proxy); | 60 base::SequencedTaskRunner* task_runner); |
61 | 61 |
62 // You have to ensure that there are no pending writes at the moment | 62 // You have to ensure that there are no pending writes at the moment |
63 // of destruction. | 63 // of destruction. |
64 ~ImportantFileWriter(); | 64 ~ImportantFileWriter(); |
65 | 65 |
66 const FilePath& path() const { return path_; } | 66 const FilePath& path() const { return path_; } |
67 | 67 |
68 // Returns true if there is a scheduled write pending which has not yet | 68 // Returns true if there is a scheduled write pending which has not yet |
69 // been started. | 69 // been started. |
70 bool HasPendingWrite() const; | 70 bool HasPendingWrite() const; |
(...skipping 18 matching lines...) Expand all Loading... |
89 } | 89 } |
90 | 90 |
91 void set_commit_interval(const TimeDelta& interval) { | 91 void set_commit_interval(const TimeDelta& interval) { |
92 commit_interval_ = interval; | 92 commit_interval_ = interval; |
93 } | 93 } |
94 | 94 |
95 private: | 95 private: |
96 // Path being written to. | 96 // Path being written to. |
97 const FilePath path_; | 97 const FilePath path_; |
98 | 98 |
99 // MessageLoopProxy for the thread on which file I/O can be done. | 99 // TaskRunner for the thread on which file I/O can be done. |
100 scoped_refptr<MessageLoopProxy> file_message_loop_proxy_; | 100 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
101 | 101 |
102 // Timer used to schedule commit after ScheduleWrite. | 102 // Timer used to schedule commit after ScheduleWrite. |
103 OneShotTimer<ImportantFileWriter> timer_; | 103 OneShotTimer<ImportantFileWriter> timer_; |
104 | 104 |
105 // Serializer which will provide the data to be saved. | 105 // Serializer which will provide the data to be saved. |
106 DataSerializer* serializer_; | 106 DataSerializer* serializer_; |
107 | 107 |
108 // Time delta after which scheduled data will be written to disk. | 108 // Time delta after which scheduled data will be written to disk. |
109 TimeDelta commit_interval_; | 109 TimeDelta commit_interval_; |
110 | 110 |
111 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 111 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
112 }; | 112 }; |
113 | 113 |
114 } // namespace base | 114 } // namespace base |
115 | 115 |
116 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 116 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |