OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/bookmarks/browser/bookmark_storage.h" | 5 #include "components/bookmarks/browser/bookmark_storage.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 extra_nodes_ = load_extra_callback_.Run(&max_id_); | 139 extra_nodes_ = load_extra_callback_.Run(&max_id_); |
140 } | 140 } |
141 | 141 |
142 // BookmarkStorage ------------------------------------------------------------- | 142 // BookmarkStorage ------------------------------------------------------------- |
143 | 143 |
144 BookmarkStorage::BookmarkStorage( | 144 BookmarkStorage::BookmarkStorage( |
145 BookmarkModel* model, | 145 BookmarkModel* model, |
146 const base::FilePath& profile_path, | 146 const base::FilePath& profile_path, |
147 base::SequencedTaskRunner* sequenced_task_runner) | 147 base::SequencedTaskRunner* sequenced_task_runner) |
148 : model_(model), | 148 : model_(model), |
149 writer_(profile_path.Append(kBookmarksFileName), sequenced_task_runner), | 149 writer_(new base::ImportantFileWriterImpl( |
| 150 profile_path.Append(kBookmarksFileName), |
| 151 sequenced_task_runner)), |
150 weak_factory_(this) { | 152 weak_factory_(this) { |
151 sequenced_task_runner_ = sequenced_task_runner; | 153 sequenced_task_runner_ = sequenced_task_runner; |
152 writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 154 writer_->set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
153 sequenced_task_runner_->PostTask(FROM_HERE, | 155 sequenced_task_runner_->PostTask( |
154 base::Bind(&BackupCallback, writer_.path())); | 156 FROM_HERE, base::Bind(&BackupCallback, writer_->path())); |
155 } | 157 } |
156 | 158 |
157 BookmarkStorage::~BookmarkStorage() { | 159 BookmarkStorage::~BookmarkStorage() { |
158 if (writer_.HasPendingWrite()) | 160 if (writer_->HasPendingWrite()) |
159 writer_.DoScheduledWrite(); | 161 writer_->DoScheduledWrite(); |
160 } | 162 } |
161 | 163 |
162 void BookmarkStorage::LoadBookmarks( | 164 void BookmarkStorage::LoadBookmarks( |
163 scoped_ptr<BookmarkLoadDetails> details, | 165 scoped_ptr<BookmarkLoadDetails> details, |
164 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 166 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
165 sequenced_task_runner_->PostTask(FROM_HERE, | 167 sequenced_task_runner_->PostTask( |
166 base::Bind(&LoadCallback, | 168 FROM_HERE, |
167 writer_.path(), | 169 base::Bind(&LoadCallback, writer_->path(), weak_factory_.GetWeakPtr(), |
168 weak_factory_.GetWeakPtr(), | 170 base::Passed(&details), task_runner)); |
169 base::Passed(&details), | |
170 task_runner)); | |
171 } | 171 } |
172 | 172 |
173 void BookmarkStorage::ScheduleSave() { | 173 void BookmarkStorage::ScheduleSave() { |
174 writer_.ScheduleWrite(this); | 174 writer_->ScheduleWrite(this); |
175 } | 175 } |
176 | 176 |
177 void BookmarkStorage::BookmarkModelDeleted() { | 177 void BookmarkStorage::BookmarkModelDeleted() { |
178 // We need to save now as otherwise by the time SaveNow is invoked | 178 // We need to save now as otherwise by the time SaveNow is invoked |
179 // the model is gone. | 179 // the model is gone. |
180 if (writer_.HasPendingWrite()) | 180 if (writer_->HasPendingWrite()) |
181 SaveNow(); | 181 SaveNow(); |
182 model_ = NULL; | 182 model_ = NULL; |
183 } | 183 } |
184 | 184 |
185 bool BookmarkStorage::SerializeData(std::string* output) { | 185 bool BookmarkStorage::SerializeData(std::string* output) { |
186 BookmarkCodec codec; | 186 BookmarkCodec codec; |
187 scoped_ptr<base::Value> value(codec.Encode(model_)); | 187 scoped_ptr<base::Value> value(codec.Encode(model_)); |
188 JSONStringValueSerializer serializer(output); | 188 JSONStringValueSerializer serializer(output); |
189 serializer.set_pretty_print(true); | 189 serializer.set_pretty_print(true); |
190 return serializer.Serialize(*(value.get())); | 190 return serializer.Serialize(*(value.get())); |
(...skipping 10 matching lines...) Expand all Loading... |
201 if (!model_ || !model_->loaded()) { | 201 if (!model_ || !model_->loaded()) { |
202 // We should only get here if we have a valid model and it's finished | 202 // We should only get here if we have a valid model and it's finished |
203 // loading. | 203 // loading. |
204 NOTREACHED(); | 204 NOTREACHED(); |
205 return false; | 205 return false; |
206 } | 206 } |
207 | 207 |
208 scoped_ptr<std::string> data(new std::string); | 208 scoped_ptr<std::string> data(new std::string); |
209 if (!SerializeData(data.get())) | 209 if (!SerializeData(data.get())) |
210 return false; | 210 return false; |
211 writer_.WriteNow(data.Pass()); | 211 writer_->WriteNow(data.Pass()); |
212 return true; | 212 return true; |
213 } | 213 } |
214 | 214 |
215 } // namespace bookmarks | 215 } // namespace bookmarks |
OLD | NEW |