| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 if (node->url().is_valid()) | 45 if (node->url().is_valid()) |
| 46 details->index()->Add(node); | 46 details->index()->Add(node); |
| 47 } else { | 47 } else { |
| 48 for (int i = 0; i < node->child_count(); ++i) | 48 for (int i = 0; i < node->child_count(); ++i) |
| 49 AddBookmarksToIndex(details, node->GetChild(i)); | 49 AddBookmarksToIndex(details, node->GetChild(i)); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 void LoadCallback(const base::FilePath& path, | 53 void LoadCallback(const base::FilePath& path, |
| 54 const base::WeakPtr<BookmarkStorage>& storage, | 54 const base::WeakPtr<BookmarkStorage>& storage, |
| 55 scoped_ptr<BookmarkLoadDetails> details, | 55 std::unique_ptr<BookmarkLoadDetails> details, |
| 56 base::SequencedTaskRunner* task_runner) { | 56 base::SequencedTaskRunner* task_runner) { |
| 57 bool load_index = false; | 57 bool load_index = false; |
| 58 bool bookmark_file_exists = base::PathExists(path); | 58 bool bookmark_file_exists = base::PathExists(path); |
| 59 if (bookmark_file_exists) { | 59 if (bookmark_file_exists) { |
| 60 JSONFileValueDeserializer deserializer(path); | 60 JSONFileValueDeserializer deserializer(path); |
| 61 scoped_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); | 61 std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); |
| 62 | 62 |
| 63 if (root.get()) { | 63 if (root.get()) { |
| 64 // Building the index can take a while, so we do it on the background | 64 // Building the index can take a while, so we do it on the background |
| 65 // thread. | 65 // thread. |
| 66 int64_t max_node_id = 0; | 66 int64_t max_node_id = 0; |
| 67 BookmarkCodec codec; | 67 BookmarkCodec codec; |
| 68 TimeTicks start_time = TimeTicks::Now(); | 68 TimeTicks start_time = TimeTicks::Now(); |
| 69 codec.Decode(details->bb_node(), details->other_folder_node(), | 69 codec.Decode(details->bb_node(), details->other_folder_node(), |
| 70 details->mobile_folder_node(), &max_node_id, *root.get()); | 70 details->mobile_folder_node(), &max_node_id, *root.get()); |
| 71 details->set_max_id(std::max(max_node_id, details->max_id())); | 71 details->set_max_id(std::max(max_node_id, details->max_id())); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 sequenced_task_runner_(sequenced_task_runner), | 153 sequenced_task_runner_(sequenced_task_runner), |
| 154 weak_factory_(this) { | 154 weak_factory_(this) { |
| 155 } | 155 } |
| 156 | 156 |
| 157 BookmarkStorage::~BookmarkStorage() { | 157 BookmarkStorage::~BookmarkStorage() { |
| 158 if (writer_.HasPendingWrite()) | 158 if (writer_.HasPendingWrite()) |
| 159 writer_.DoScheduledWrite(); | 159 writer_.DoScheduledWrite(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void BookmarkStorage::LoadBookmarks( | 162 void BookmarkStorage::LoadBookmarks( |
| 163 scoped_ptr<BookmarkLoadDetails> details, | 163 std::unique_ptr<BookmarkLoadDetails> details, |
| 164 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 164 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 165 sequenced_task_runner_->PostTask( | 165 sequenced_task_runner_->PostTask( |
| 166 FROM_HERE, | 166 FROM_HERE, |
| 167 base::Bind(&LoadCallback, writer_.path(), weak_factory_.GetWeakPtr(), | 167 base::Bind(&LoadCallback, writer_.path(), weak_factory_.GetWeakPtr(), |
| 168 base::Passed(&details), base::RetainedRef(task_runner))); | 168 base::Passed(&details), base::RetainedRef(task_runner))); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void BookmarkStorage::ScheduleSave() { | 171 void BookmarkStorage::ScheduleSave() { |
| 172 switch (backup_state_) { | 172 switch (backup_state_) { |
| 173 case BACKUP_NONE: | 173 case BACKUP_NONE: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 195 void BookmarkStorage::BookmarkModelDeleted() { | 195 void BookmarkStorage::BookmarkModelDeleted() { |
| 196 // We need to save now as otherwise by the time SaveNow is invoked | 196 // We need to save now as otherwise by the time SaveNow is invoked |
| 197 // the model is gone. | 197 // the model is gone. |
| 198 if (writer_.HasPendingWrite()) | 198 if (writer_.HasPendingWrite()) |
| 199 SaveNow(); | 199 SaveNow(); |
| 200 model_ = NULL; | 200 model_ = NULL; |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool BookmarkStorage::SerializeData(std::string* output) { | 203 bool BookmarkStorage::SerializeData(std::string* output) { |
| 204 BookmarkCodec codec; | 204 BookmarkCodec codec; |
| 205 scoped_ptr<base::Value> value(codec.Encode(model_)); | 205 std::unique_ptr<base::Value> value(codec.Encode(model_)); |
| 206 JSONStringValueSerializer serializer(output); | 206 JSONStringValueSerializer serializer(output); |
| 207 serializer.set_pretty_print(true); | 207 serializer.set_pretty_print(true); |
| 208 return serializer.Serialize(*(value.get())); | 208 return serializer.Serialize(*(value.get())); |
| 209 } | 209 } |
| 210 | 210 |
| 211 void BookmarkStorage::OnLoadFinished(scoped_ptr<BookmarkLoadDetails> details) { | 211 void BookmarkStorage::OnLoadFinished( |
| 212 std::unique_ptr<BookmarkLoadDetails> details) { |
| 212 if (!model_) | 213 if (!model_) |
| 213 return; | 214 return; |
| 214 | 215 |
| 215 model_->DoneLoading(std::move(details)); | 216 model_->DoneLoading(std::move(details)); |
| 216 } | 217 } |
| 217 | 218 |
| 218 bool BookmarkStorage::SaveNow() { | 219 bool BookmarkStorage::SaveNow() { |
| 219 if (!model_ || !model_->loaded()) { | 220 if (!model_ || !model_->loaded()) { |
| 220 // We should only get here if we have a valid model and it's finished | 221 // We should only get here if we have a valid model and it's finished |
| 221 // loading. | 222 // loading. |
| 222 NOTREACHED(); | 223 NOTREACHED(); |
| 223 return false; | 224 return false; |
| 224 } | 225 } |
| 225 | 226 |
| 226 scoped_ptr<std::string> data(new std::string); | 227 std::unique_ptr<std::string> data(new std::string); |
| 227 if (!SerializeData(data.get())) | 228 if (!SerializeData(data.get())) |
| 228 return false; | 229 return false; |
| 229 writer_.WriteNow(std::move(data)); | 230 writer_.WriteNow(std::move(data)); |
| 230 return true; | 231 return true; |
| 231 } | 232 } |
| 232 | 233 |
| 233 } // namespace bookmarks | 234 } // namespace bookmarks |
| OLD | NEW |