blob: 34c60674c9504f516fdb3a070eeaa0f8f4c42fe7 [file] [log] [blame]
[email protected]6faa0e0d2009-04-28 06:50:361// 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]6658ca82010-05-20 18:20:295#ifndef CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
6#define CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]6faa0e0d2009-04-28 06:50:368
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/file_path.h"
[email protected]6658ca82010-05-20 18:20:2913#include "base/ref_counted.h"
[email protected]c9177502011-01-01 04:48:4914#include "base/threading/non_thread_safe.h"
[email protected]6faa0e0d2009-04-28 06:50:3615#include "base/time.h"
16#include "base/timer.h"
17
18namespace base {
[email protected]6658ca82010-05-20 18:20:2919class MessageLoopProxy;
[email protected]6faa0e0d2009-04-28 06:50:3620class Thread;
21}
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]c9177502011-01-01 04:48:4939class ImportantFileWriter : public base::NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3640 public:
[email protected]6c1164042009-05-08 14:41:0841 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
42 // to also batch data serializations.
43 class DataSerializer {
44 public:
45 virtual ~DataSerializer() {}
46
47 // Should put serialized string in |data| and return true on successful
48 // serialization. Will be called on the same thread on which
49 // ImportantFileWriter has been created.
50 virtual bool SerializeData(std::string* data) = 0;
51 };
52
[email protected]6faa0e0d2009-04-28 06:50:3653 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3754 // |path| is the name of file to write.
[email protected]6658ca82010-05-20 18:20:2955 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which
56 // file I/O can be done.
[email protected]6faa0e0d2009-04-28 06:50:3657 // All non-const methods, ctor and dtor must be called on the same thread.
[email protected]6658ca82010-05-20 18:20:2958 ImportantFileWriter(const FilePath& path,
59 base::MessageLoopProxy* file_message_loop_proxy);
[email protected]6faa0e0d2009-04-28 06:50:3660
[email protected]6c1164042009-05-08 14:41:0861 // You have to ensure that there are no pending writes at the moment
62 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3663 ~ImportantFileWriter();
64
[email protected]a83d42292010-08-17 22:51:1065 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3666
[email protected]6c1164042009-05-08 14:41:0867 // Returns true if there is a scheduled write pending which has not yet
68 // been started.
69 bool HasPendingWrite() const;
70
[email protected]6faa0e0d2009-04-28 06:50:3671 // Save |data| to target filename. Does not block. If there is a pending write
72 // scheduled by ScheduleWrite, it is cancelled.
73 void WriteNow(const std::string& data);
74
[email protected]6c1164042009-05-08 14:41:0875 // Schedule a save to target filename. Data will be serialized and saved
76 // to disk after the commit interval. If another ScheduleWrite is issued
77 // before that, only one serialization and write to disk will happen, and
78 // the most recent |serializer| will be used. This operation does not block.
79 // |serializer| should remain valid through the lifetime of
80 // ImportantFileWriter.
81 void ScheduleWrite(DataSerializer* serializer);
82
83 // Serialize data pending to be saved and execute write on backend thread.
84 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3685
86 base::TimeDelta commit_interval() const {
87 return commit_interval_;
88 }
89
90 void set_commit_interval(const base::TimeDelta& interval) {
91 commit_interval_ = interval;
92 }
93
94 private:
[email protected]6faa0e0d2009-04-28 06:50:3695 // Path being written to.
96 const FilePath path_;
97
[email protected]6658ca82010-05-20 18:20:2998 // MessageLoopProxy for the thread on which file I/O can be done.
99 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_;
100
[email protected]6faa0e0d2009-04-28 06:50:36101 // Timer used to schedule commit after ScheduleWrite.
102 base::OneShotTimer<ImportantFileWriter> timer_;
103
[email protected]6c1164042009-05-08 14:41:08104 // Serializer which will provide the data to be saved.
105 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36106
107 // Time delta after which scheduled data will be written to disk.
108 base::TimeDelta commit_interval_;
109
110 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
111};
112
[email protected]6658ca82010-05-20 18:20:29113#endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_