| 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" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/values.h" |
| 15 #include "components/bookmarks/browser/bookmark_codec.h" | 16 #include "components/bookmarks/browser/bookmark_codec.h" |
| 16 #include "components/bookmarks/browser/bookmark_index.h" | 17 #include "components/bookmarks/browser/bookmark_index.h" |
| 17 #include "components/bookmarks/browser/bookmark_model.h" | 18 #include "components/bookmarks/browser/bookmark_model.h" |
| 19 #include "components/bookmarks/browser/managed_bookmarks_tracker.h" |
| 18 #include "components/bookmarks/common/bookmark_constants.h" | 20 #include "components/bookmarks/common/bookmark_constants.h" |
| 19 #include "components/startup_metric_utils/startup_metric_utils.h" | 21 #include "components/startup_metric_utils/startup_metric_utils.h" |
| 20 | 22 |
| 21 using base::TimeTicks; | 23 using base::TimeTicks; |
| 22 | 24 |
| 23 namespace bookmarks { | 25 namespace bookmarks { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 // Extension used for backup files (copy of main file created during startup). | 29 // Extension used for backup files (copy of main file created during startup). |
| (...skipping 15 matching lines...) Expand all Loading... |
| 43 details->index()->Add(node); | 45 details->index()->Add(node); |
| 44 } else { | 46 } else { |
| 45 for (int i = 0; i < node->child_count(); ++i) | 47 for (int i = 0; i < node->child_count(); ++i) |
| 46 AddBookmarksToIndex(details, node->GetChild(i)); | 48 AddBookmarksToIndex(details, node->GetChild(i)); |
| 47 } | 49 } |
| 48 } | 50 } |
| 49 | 51 |
| 50 void LoadCallback(const base::FilePath& path, | 52 void LoadCallback(const base::FilePath& path, |
| 51 BookmarkStorage* storage, | 53 BookmarkStorage* storage, |
| 52 BookmarkLoadDetails* details, | 54 BookmarkLoadDetails* details, |
| 55 scoped_ptr<base::ListValue> initial_managed_bookmarks, |
| 53 base::SequencedTaskRunner* task_runner) { | 56 base::SequencedTaskRunner* task_runner) { |
| 54 startup_metric_utils::ScopedSlowStartupUMA | 57 startup_metric_utils::ScopedSlowStartupUMA |
| 55 scoped_timer("Startup.SlowStartupBookmarksLoad"); | 58 scoped_timer("Startup.SlowStartupBookmarksLoad"); |
| 59 bool load_index = false; |
| 56 bool bookmark_file_exists = base::PathExists(path); | 60 bool bookmark_file_exists = base::PathExists(path); |
| 57 if (bookmark_file_exists) { | 61 if (bookmark_file_exists) { |
| 58 JSONFileValueSerializer serializer(path); | 62 JSONFileValueSerializer serializer(path); |
| 59 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 63 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 60 | 64 |
| 61 if (root.get()) { | 65 if (root.get()) { |
| 62 // Building the index can take a while, so we do it on the background | 66 // Building the index can take a while, so we do it on the background |
| 63 // thread. | 67 // thread. |
| 64 int64 max_node_id = 0; | 68 int64 max_node_id = 0; |
| 65 BookmarkCodec codec; | 69 BookmarkCodec codec; |
| 66 TimeTicks start_time = TimeTicks::Now(); | 70 TimeTicks start_time = TimeTicks::Now(); |
| 67 codec.Decode(details->bb_node(), details->other_folder_node(), | 71 codec.Decode(details->bb_node(), details->other_folder_node(), |
| 68 details->mobile_folder_node(), &max_node_id, *root.get()); | 72 details->mobile_folder_node(), &max_node_id, *root.get()); |
| 69 details->set_max_id(std::max(max_node_id, details->max_id())); | 73 details->set_max_id(std::max(max_node_id, details->max_id())); |
| 70 details->set_computed_checksum(codec.computed_checksum()); | 74 details->set_computed_checksum(codec.computed_checksum()); |
| 71 details->set_stored_checksum(codec.stored_checksum()); | 75 details->set_stored_checksum(codec.stored_checksum()); |
| 72 details->set_ids_reassigned(codec.ids_reassigned()); | 76 details->set_ids_reassigned(codec.ids_reassigned()); |
| 73 details->set_model_meta_info_map(codec.model_meta_info_map()); | 77 details->set_model_meta_info_map(codec.model_meta_info_map()); |
| 74 details->set_model_sync_transaction_version( | 78 details->set_model_sync_transaction_version( |
| 75 codec.model_sync_transaction_version()); | 79 codec.model_sync_transaction_version()); |
| 76 UMA_HISTOGRAM_TIMES("Bookmarks.DecodeTime", | 80 UMA_HISTOGRAM_TIMES("Bookmarks.DecodeTime", |
| 77 TimeTicks::Now() - start_time); | 81 TimeTicks::Now() - start_time); |
| 78 | 82 |
| 79 start_time = TimeTicks::Now(); | 83 load_index = true; |
| 80 AddBookmarksToIndex(details, details->bb_node()); | |
| 81 AddBookmarksToIndex(details, details->other_folder_node()); | |
| 82 AddBookmarksToIndex(details, details->mobile_folder_node()); | |
| 83 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", | |
| 84 TimeTicks::Now() - start_time); | |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 87 // Load the managed bookmarks. |
| 88 if (!initial_managed_bookmarks->empty()) { |
| 89 int64 max_node_id = ManagedBookmarksTracker::LoadInitial( |
| 90 details->managed_node(), |
| 91 initial_managed_bookmarks.get(), |
| 92 details->max_id()); |
| 93 details->managed_node()->set_visible(!details->managed_node()->empty()); |
| 94 details->set_max_id(max_node_id); |
| 95 load_index = true; |
| 96 } |
| 97 |
| 98 if (load_index) { |
| 99 TimeTicks start_time = TimeTicks::Now(); |
| 100 AddBookmarksToIndex(details, details->bb_node()); |
| 101 AddBookmarksToIndex(details, details->other_folder_node()); |
| 102 AddBookmarksToIndex(details, details->mobile_folder_node()); |
| 103 AddBookmarksToIndex(details, details->managed_node()); |
| 104 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", |
| 105 TimeTicks::Now() - start_time); |
| 106 } |
| 107 |
| 88 task_runner->PostTask(FROM_HERE, | 108 task_runner->PostTask(FROM_HERE, |
| 89 base::Bind(&BookmarkStorage::OnLoadFinished, storage)); | 109 base::Bind(&BookmarkStorage::OnLoadFinished, storage)); |
| 90 } | 110 } |
| 91 | 111 |
| 92 } // namespace | 112 } // namespace |
| 93 | 113 |
| 94 // BookmarkLoadDetails --------------------------------------------------------- | 114 // BookmarkLoadDetails --------------------------------------------------------- |
| 95 | 115 |
| 96 BookmarkLoadDetails::BookmarkLoadDetails( | 116 BookmarkLoadDetails::BookmarkLoadDetails( |
| 97 BookmarkPermanentNode* bb_node, | 117 BookmarkPermanentNode* bb_node, |
| 98 BookmarkPermanentNode* other_folder_node, | 118 BookmarkPermanentNode* other_folder_node, |
| 99 BookmarkPermanentNode* mobile_folder_node, | 119 BookmarkPermanentNode* mobile_folder_node, |
| 120 BookmarkPermanentNode* managed_node, |
| 100 BookmarkIndex* index, | 121 BookmarkIndex* index, |
| 101 int64 max_id) | 122 int64 max_id) |
| 102 : bb_node_(bb_node), | 123 : bb_node_(bb_node), |
| 103 other_folder_node_(other_folder_node), | 124 other_folder_node_(other_folder_node), |
| 104 mobile_folder_node_(mobile_folder_node), | 125 mobile_folder_node_(mobile_folder_node), |
| 126 managed_node_(managed_node), |
| 105 index_(index), | 127 index_(index), |
| 106 model_sync_transaction_version_( | 128 model_sync_transaction_version_( |
| 107 BookmarkNode::kInvalidSyncTransactionVersion), | 129 BookmarkNode::kInvalidSyncTransactionVersion), |
| 108 max_id_(max_id), | 130 max_id_(max_id), |
| 109 ids_reassigned_(false) { | 131 ids_reassigned_(false) { |
| 110 } | 132 } |
| 111 | 133 |
| 112 BookmarkLoadDetails::~BookmarkLoadDetails() { | 134 BookmarkLoadDetails::~BookmarkLoadDetails() { |
| 113 } | 135 } |
| 114 | 136 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 127 base::Bind(&BackupCallback, writer_.path())); | 149 base::Bind(&BackupCallback, writer_.path())); |
| 128 } | 150 } |
| 129 | 151 |
| 130 BookmarkStorage::~BookmarkStorage() { | 152 BookmarkStorage::~BookmarkStorage() { |
| 131 if (writer_.HasPendingWrite()) | 153 if (writer_.HasPendingWrite()) |
| 132 writer_.DoScheduledWrite(); | 154 writer_.DoScheduledWrite(); |
| 133 } | 155 } |
| 134 | 156 |
| 135 void BookmarkStorage::LoadBookmarks( | 157 void BookmarkStorage::LoadBookmarks( |
| 136 scoped_ptr<BookmarkLoadDetails> details, | 158 scoped_ptr<BookmarkLoadDetails> details, |
| 159 scoped_ptr<base::ListValue> initial_managed_bookmarks, |
| 137 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 160 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 138 DCHECK(!details_.get()); | 161 DCHECK(!details_.get()); |
| 139 DCHECK(details); | 162 DCHECK(details); |
| 140 details_ = details.Pass(); | 163 details_ = details.Pass(); |
| 141 sequenced_task_runner_->PostTask(FROM_HERE, | 164 sequenced_task_runner_->PostTask( |
| 142 base::Bind(&LoadCallback, | 165 FROM_HERE, |
| 143 writer_.path(), | 166 base::Bind(&LoadCallback, |
| 144 make_scoped_refptr(this), | 167 writer_.path(), |
| 145 details_.get(), | 168 make_scoped_refptr(this), |
| 146 task_runner)); | 169 details_.get(), |
| 170 base::Passed(&initial_managed_bookmarks), |
| 171 task_runner)); |
| 147 } | 172 } |
| 148 | 173 |
| 149 void BookmarkStorage::ScheduleSave() { | 174 void BookmarkStorage::ScheduleSave() { |
| 150 writer_.ScheduleWrite(this); | 175 writer_.ScheduleWrite(this); |
| 151 } | 176 } |
| 152 | 177 |
| 153 void BookmarkStorage::BookmarkModelDeleted() { | 178 void BookmarkStorage::BookmarkModelDeleted() { |
| 154 // We need to save now as otherwise by the time SaveNow is invoked | 179 // We need to save now as otherwise by the time SaveNow is invoked |
| 155 // the model is gone. | 180 // the model is gone. |
| 156 if (writer_.HasPendingWrite()) | 181 if (writer_.HasPendingWrite()) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 182 } | 207 } |
| 183 | 208 |
| 184 std::string data; | 209 std::string data; |
| 185 if (!SerializeData(&data)) | 210 if (!SerializeData(&data)) |
| 186 return false; | 211 return false; |
| 187 writer_.WriteNow(data); | 212 writer_.WriteNow(data); |
| 188 return true; | 213 return true; |
| 189 } | 214 } |
| 190 | 215 |
| 191 } // namespace bookmarks | 216 } // namespace bookmarks |
| OLD | NEW |