OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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" |
(...skipping 30 matching lines...) Expand all Loading... |
41 public: | 41 public: |
42 virtual ~DataSerializer() {} | 42 virtual ~DataSerializer() {} |
43 | 43 |
44 // Should put serialized string in |data| and return true on successful | 44 // Should put serialized string in |data| and return true on successful |
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 | 49 |
50 // Initialize the writer. | 50 // Initialize the writer. |
51 // |path| is the name of file to write. Disk operations will be executed on | 51 // |path| is the name of file to write. |
52 // |backend_thread|, or current thread if |backend_thread| is NULL. | |
53 // | |
54 // All non-const methods, ctor and dtor must be called on the same thread. | 52 // All non-const methods, ctor and dtor must be called on the same thread. |
55 ImportantFileWriter(const FilePath& path, const base::Thread* backend_thread); | 53 ImportantFileWriter(const FilePath& path); |
56 | 54 |
57 // You have to ensure that there are no pending writes at the moment | 55 // You have to ensure that there are no pending writes at the moment |
58 // of destruction. | 56 // of destruction. |
59 ~ImportantFileWriter(); | 57 ~ImportantFileWriter(); |
60 | 58 |
61 FilePath path() const { return path_; } | 59 FilePath path() const { return path_; } |
62 | 60 |
63 // Returns true if there is a scheduled write pending which has not yet | 61 // Returns true if there is a scheduled write pending which has not yet |
64 // been started. | 62 // been started. |
65 bool HasPendingWrite() const; | 63 bool HasPendingWrite() const; |
(...skipping 18 matching lines...) Expand all Loading... |
84 } | 82 } |
85 | 83 |
86 void set_commit_interval(const base::TimeDelta& interval) { | 84 void set_commit_interval(const base::TimeDelta& interval) { |
87 commit_interval_ = interval; | 85 commit_interval_ = interval; |
88 } | 86 } |
89 | 87 |
90 private: | 88 private: |
91 // Path being written to. | 89 // Path being written to. |
92 const FilePath path_; | 90 const FilePath path_; |
93 | 91 |
94 // Thread on which disk operation run. NULL means no separate thread is used. | |
95 const base::Thread* backend_thread_; | |
96 | |
97 // Timer used to schedule commit after ScheduleWrite. | 92 // Timer used to schedule commit after ScheduleWrite. |
98 base::OneShotTimer<ImportantFileWriter> timer_; | 93 base::OneShotTimer<ImportantFileWriter> timer_; |
99 | 94 |
100 // Serializer which will provide the data to be saved. | 95 // Serializer which will provide the data to be saved. |
101 DataSerializer* serializer_; | 96 DataSerializer* serializer_; |
102 | 97 |
103 // Time delta after which scheduled data will be written to disk. | 98 // Time delta after which scheduled data will be written to disk. |
104 base::TimeDelta commit_interval_; | 99 base::TimeDelta commit_interval_; |
105 | 100 |
106 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 101 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
107 }; | 102 }; |
108 | 103 |
109 #endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ | 104 #endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |