| 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 if (node->is_url()) { | 41 if (node->is_url()) { |
| 42 if (node->url().is_valid()) | 42 if (node->url().is_valid()) |
| 43 details->index()->Add(node); | 43 details->index()->Add(node); |
| 44 } else { | 44 } else { |
| 45 for (int i = 0; i < node->child_count(); ++i) | 45 for (int i = 0; i < node->child_count(); ++i) |
| 46 AddBookmarksToIndex(details, node->GetChild(i)); | 46 AddBookmarksToIndex(details, node->GetChild(i)); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 void LoadCallback(const base::FilePath& path, | 50 void LoadCallback(const base::FilePath& path, |
| 51 BookmarkStorage* storage, | 51 const base::WeakPtr<BookmarkStorage>& storage, |
| 52 BookmarkLoadDetails* details, | 52 BookmarkLoadDetails* details, |
| 53 base::SequencedTaskRunner* task_runner) { | 53 base::SequencedTaskRunner* task_runner) { |
| 54 startup_metric_utils::ScopedSlowStartupUMA | 54 startup_metric_utils::ScopedSlowStartupUMA |
| 55 scoped_timer("Startup.SlowStartupBookmarksLoad"); | 55 scoped_timer("Startup.SlowStartupBookmarksLoad"); |
| 56 bool load_index = false; | 56 bool load_index = false; |
| 57 bool bookmark_file_exists = base::PathExists(path); | 57 bool bookmark_file_exists = base::PathExists(path); |
| 58 if (bookmark_file_exists) { | 58 if (bookmark_file_exists) { |
| 59 JSONFileValueSerializer serializer(path); | 59 JSONFileValueSerializer serializer(path); |
| 60 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 60 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 61 | 61 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } | 139 } |
| 140 | 140 |
| 141 // BookmarkStorage ------------------------------------------------------------- | 141 // BookmarkStorage ------------------------------------------------------------- |
| 142 | 142 |
| 143 BookmarkStorage::BookmarkStorage( | 143 BookmarkStorage::BookmarkStorage( |
| 144 BookmarkModel* model, | 144 BookmarkModel* model, |
| 145 const base::FilePath& profile_path, | 145 const base::FilePath& profile_path, |
| 146 base::SequencedTaskRunner* sequenced_task_runner) | 146 base::SequencedTaskRunner* sequenced_task_runner) |
| 147 : model_(model), | 147 : model_(model), |
| 148 writer_(profile_path.Append(bookmarks::kBookmarksFileName), | 148 writer_(profile_path.Append(bookmarks::kBookmarksFileName), |
| 149 sequenced_task_runner) { | 149 sequenced_task_runner), |
| 150 weak_factory_(this) { |
| 150 sequenced_task_runner_ = sequenced_task_runner; | 151 sequenced_task_runner_ = sequenced_task_runner; |
| 151 writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 152 writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
| 152 sequenced_task_runner_->PostTask(FROM_HERE, | 153 sequenced_task_runner_->PostTask(FROM_HERE, |
| 153 base::Bind(&BackupCallback, writer_.path())); | 154 base::Bind(&BackupCallback, writer_.path())); |
| 154 } | 155 } |
| 155 | 156 |
| 156 BookmarkStorage::~BookmarkStorage() { | 157 BookmarkStorage::~BookmarkStorage() { |
| 157 if (writer_.HasPendingWrite()) | 158 if (writer_.HasPendingWrite()) |
| 158 writer_.DoScheduledWrite(); | 159 writer_.DoScheduledWrite(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 void BookmarkStorage::LoadBookmarks( | 162 void BookmarkStorage::LoadBookmarks( |
| 162 scoped_ptr<BookmarkLoadDetails> details, | 163 scoped_ptr<BookmarkLoadDetails> details, |
| 163 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 164 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 164 DCHECK(!details_.get()); | 165 DCHECK(!details_.get()); |
| 165 DCHECK(details); | 166 DCHECK(details); |
| 166 details_ = details.Pass(); | 167 details_ = details.Pass(); |
| 167 sequenced_task_runner_->PostTask(FROM_HERE, | 168 sequenced_task_runner_->PostTask(FROM_HERE, |
| 168 base::Bind(&LoadCallback, | 169 base::Bind(&LoadCallback, |
| 169 writer_.path(), | 170 writer_.path(), |
| 170 make_scoped_refptr(this), | 171 weak_factory_.GetWeakPtr(), |
| 171 details_.get(), | 172 details_.get(), |
| 172 task_runner)); | 173 task_runner)); |
| 173 } | 174 } |
| 174 | 175 |
| 175 void BookmarkStorage::ScheduleSave() { | 176 void BookmarkStorage::ScheduleSave() { |
| 176 writer_.ScheduleWrite(this); | 177 writer_.ScheduleWrite(this); |
| 177 } | 178 } |
| 178 | 179 |
| 179 void BookmarkStorage::BookmarkModelDeleted() { | 180 void BookmarkStorage::BookmarkModelDeleted() { |
| 180 // We need to save now as otherwise by the time SaveNow is invoked | 181 // We need to save now as otherwise by the time SaveNow is invoked |
| (...skipping 27 matching lines...) Expand all Loading... |
| 208 } | 209 } |
| 209 | 210 |
| 210 std::string data; | 211 std::string data; |
| 211 if (!SerializeData(&data)) | 212 if (!SerializeData(&data)) |
| 212 return false; | 213 return false; |
| 213 writer_.WriteNow(data); | 214 writer_.WriteNow(data); |
| 214 return true; | 215 return true; |
| 215 } | 216 } |
| 216 | 217 |
| 217 } // namespace bookmarks | 218 } // namespace bookmarks |
| OLD | NEW |