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