[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 5 | #ifndef CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ |
| 6 | #define CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/file_path.h" |
| 12 | #include "base/non_thread_safe.h" |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 13 | #include "base/ref_counted.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 14 | #include "base/time.h" |
| 15 | #include "base/timer.h" |
| 16 | |
| 17 | namespace base { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 18 | class MessageLoopProxy; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 19 | class Thread; |
| 20 | } |
| 21 | |
| 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: |
| 24 | // |
| 25 | // 1. Open F for writing, truncating it. |
| 26 | // 2. Write new data to F. |
| 27 | // |
| 28 | // It's good when it works, but it gets very bad if step 2. doesn't complete. |
| 29 | // It can be caused by a crash, a computer hang, or a weird I/O error. And you |
| 30 | // end up with a broken file. |
| 31 | // |
| 32 | // To be safe, we don't start with writing directly to F. Instead, we write to |
| 33 | // to a temporary file. Only after that write is successful, we rename the |
| 34 | // temporary file to target filename. |
| 35 | // |
| 36 | // If you want to know more about this approach and ext3/ext4 fsync issues, see |
| 37 | // http://valhenson.livejournal.com/37921.html |
| 38 | class ImportantFileWriter : public NonThreadSafe { |
| 39 | public: |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 40 | // Used by ScheduleSave to lazily provide the data to be saved. Allows us |
| 41 | // to also batch data serializations. |
| 42 | class DataSerializer { |
| 43 | public: |
| 44 | virtual ~DataSerializer() {} |
| 45 | |
| 46 | // Should put serialized string in |data| and return true on successful |
| 47 | // serialization. Will be called on the same thread on which |
| 48 | // ImportantFileWriter has been created. |
| 49 | virtual bool SerializeData(std::string* data) = 0; |
| 50 | }; |
| 51 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 52 | // Initialize the writer. |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 53 | // |path| is the name of file to write. |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 54 | // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which |
| 55 | // file I/O can be done. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 56 | // All non-const methods, ctor and dtor must be called on the same thread. |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 57 | ImportantFileWriter(const FilePath& path, |
| 58 | base::MessageLoopProxy* file_message_loop_proxy); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 59 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 60 | // You have to ensure that there are no pending writes at the moment |
| 61 | // of destruction. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 62 | ~ImportantFileWriter(); |
| 63 | |
| 64 | FilePath path() const { return path_; } |
| 65 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 66 | // Returns true if there is a scheduled write pending which has not yet |
| 67 | // been started. |
| 68 | bool HasPendingWrite() const; |
| 69 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 70 | // Save |data| to target filename. Does not block. If there is a pending write |
| 71 | // scheduled by ScheduleWrite, it is cancelled. |
| 72 | void WriteNow(const std::string& data); |
| 73 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 74 | // Schedule a save to target filename. Data will be serialized and saved |
| 75 | // to disk after the commit interval. If another ScheduleWrite is issued |
| 76 | // before that, only one serialization and write to disk will happen, and |
| 77 | // the most recent |serializer| will be used. This operation does not block. |
| 78 | // |serializer| should remain valid through the lifetime of |
| 79 | // ImportantFileWriter. |
| 80 | void ScheduleWrite(DataSerializer* serializer); |
| 81 | |
| 82 | // Serialize data pending to be saved and execute write on backend thread. |
| 83 | void DoScheduledWrite(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 84 | |
| 85 | base::TimeDelta commit_interval() const { |
| 86 | return commit_interval_; |
| 87 | } |
| 88 | |
| 89 | void set_commit_interval(const base::TimeDelta& interval) { |
| 90 | commit_interval_ = interval; |
| 91 | } |
| 92 | |
| 93 | private: |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 94 | // Path being written to. |
| 95 | const FilePath path_; |
| 96 | |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 97 | // MessageLoopProxy for the thread on which file I/O can be done. |
| 98 | scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; |
| 99 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 100 | // Timer used to schedule commit after ScheduleWrite. |
| 101 | base::OneShotTimer<ImportantFileWriter> timer_; |
| 102 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 103 | // Serializer which will provide the data to be saved. |
| 104 | DataSerializer* serializer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 105 | |
| 106 | // Time delta after which scheduled data will be written to disk. |
| 107 | base::TimeDelta commit_interval_; |
| 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 110 | }; |
| 111 | |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame^] | 112 | #endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ |