| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 5 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| 6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 ~ImportantFileWriter(); | 71 ~ImportantFileWriter(); |
| 72 | 72 |
| 73 const FilePath& path() const { return path_; } | 73 const FilePath& path() const { return path_; } |
| 74 | 74 |
| 75 // Returns true if there is a scheduled write pending which has not yet | 75 // Returns true if there is a scheduled write pending which has not yet |
| 76 // been started. | 76 // been started. |
| 77 bool HasPendingWrite() const; | 77 bool HasPendingWrite() const; |
| 78 | 78 |
| 79 // Save |data| to target filename. Does not block. If there is a pending write | 79 // Save |data| to target filename. Does not block. If there is a pending write |
| 80 // scheduled by ScheduleWrite, it is cancelled. | 80 // scheduled by ScheduleWrite, it is cancelled. |
| 81 void WriteNow(const std::string& data); | 81 void WriteNow(scoped_ptr<std::string> data); |
| 82 | 82 |
| 83 // Schedule a save to target filename. Data will be serialized and saved | 83 // Schedule a save to target filename. Data will be serialized and saved |
| 84 // to disk after the commit interval. If another ScheduleWrite is issued | 84 // to disk after the commit interval. If another ScheduleWrite is issued |
| 85 // before that, only one serialization and write to disk will happen, and | 85 // before that, only one serialization and write to disk will happen, and |
| 86 // the most recent |serializer| will be used. This operation does not block. | 86 // the most recent |serializer| will be used. This operation does not block. |
| 87 // |serializer| should remain valid through the lifetime of | 87 // |serializer| should remain valid through the lifetime of |
| 88 // ImportantFileWriter. | 88 // ImportantFileWriter. |
| 89 void ScheduleWrite(DataSerializer* serializer); | 89 void ScheduleWrite(DataSerializer* serializer); |
| 90 | 90 |
| 91 // Serialize data pending to be saved and execute write on backend thread. | 91 // Serialize data pending to be saved and execute write on backend thread. |
| 92 void DoScheduledWrite(); | 92 void DoScheduledWrite(); |
| 93 | 93 |
| 94 // Registers |on_next_successful_write| to be called once, on the next | 94 // Registers |on_next_successful_write| to be called once, on the next |
| 95 // successful write event. Only one callback can be set at once. | 95 // successful write event. Only one callback can be set at once. |
| 96 void RegisterOnNextSuccessfulWriteCallback( | 96 void RegisterOnNextSuccessfulWriteCallback( |
| 97 const base::Closure& on_next_successful_write); | 97 const base::Closure& on_next_successful_write); |
| 98 | 98 |
| 99 TimeDelta commit_interval() const { | 99 TimeDelta commit_interval() const { |
| 100 return commit_interval_; | 100 return commit_interval_; |
| 101 } | 101 } |
| 102 | 102 |
| 103 void set_commit_interval(const TimeDelta& interval) { | 103 void set_commit_interval(const TimeDelta& interval) { |
| 104 commit_interval_ = interval; | 104 commit_interval_ = interval; |
| 105 } | 105 } |
| 106 | 106 |
| 107 private: | 107 private: |
| 108 // Helper method for WriteNow(). | 108 // Helper method for WriteNow(). |
| 109 bool PostWriteTask(const std::string& data); | 109 bool PostWriteTask(const Callback<bool()>& task); |
| 110 | 110 |
| 111 // If |result| is true and |on_next_successful_write_| is set, invokes | 111 // If |result| is true and |on_next_successful_write_| is set, invokes |
| 112 // |on_successful_write_| and then resets it; no-ops otherwise. | 112 // |on_successful_write_| and then resets it; no-ops otherwise. |
| 113 void ForwardSuccessfulWrite(bool result); | 113 void ForwardSuccessfulWrite(bool result); |
| 114 | 114 |
| 115 // Invoked once and then reset on the next successful write event. | 115 // Invoked once and then reset on the next successful write event. |
| 116 base::Closure on_next_successful_write_; | 116 base::Closure on_next_successful_write_; |
| 117 | 117 |
| 118 // Path being written to. | 118 // Path being written to. |
| 119 const FilePath path_; | 119 const FilePath path_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 131 TimeDelta commit_interval_; | 131 TimeDelta commit_interval_; |
| 132 | 132 |
| 133 WeakPtrFactory<ImportantFileWriter> weak_factory_; | 133 WeakPtrFactory<ImportantFileWriter> weak_factory_; |
| 134 | 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); | 135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 } // namespace base | 138 } // namespace base |
| 139 | 139 |
| 140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ | 140 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_ |
| OLD | NEW |