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