OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
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 class MessageLoopProxy; | 19 class SequencedTaskRunner; |
20 class Thread; | 20 class Thread; |
21 } | 21 } // namespace base |
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 |
31 // end up with a broken file. | 31 // end up with a broken file. |
(...skipping 17 matching lines...) Expand all Loading... |
49 // ImportantFileWriter has been created. | 49 // ImportantFileWriter has been created. |
50 virtual bool SerializeData(std::string* data) = 0; | 50 virtual bool SerializeData(std::string* data) = 0; |
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 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which |
56 // file I/O can be done. | 56 // file I/O can be done. |
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* blocking_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 // SequencedTaskRunner for blocking I/O operations. |
99 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; | 99 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
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 |