| 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 #include "chrome/browser/bookmarks/bookmark_storage.h" | 5 #include "chrome/browser/bookmarks/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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
| 12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/sequenced_task_runner.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_codec.h" | 16 #include "chrome/browser/bookmarks/bookmark_codec.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_index.h" | 17 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_model.h" | 18 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 18 #include "components/bookmarks/core/common/bookmark_constants.h" | 19 #include "components/bookmarks/core/common/bookmark_constants.h" |
| 19 #include "components/startup_metric_utils/startup_metric_utils.h" | 20 #include "components/startup_metric_utils/startup_metric_utils.h" |
| 20 #include "content/public/browser/browser_context.h" | |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 | 21 |
| 23 using base::TimeTicks; | 22 using base::TimeTicks; |
| 24 using content::BrowserThread; | |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| 27 | 25 |
| 28 // Extension used for backup files (copy of main file created during startup). | 26 // Extension used for backup files (copy of main file created during startup). |
| 29 const base::FilePath::CharType kBackupExtension[] = FILE_PATH_LITERAL("bak"); | 27 const base::FilePath::CharType kBackupExtension[] = FILE_PATH_LITERAL("bak"); |
| 30 | 28 |
| 31 // How often we save. | 29 // How often we save. |
| 32 const int kSaveDelayMS = 2500; | 30 const int kSaveDelayMS = 2500; |
| 33 | 31 |
| 34 void BackupCallback(const base::FilePath& path) { | 32 void BackupCallback(const base::FilePath& path) { |
| 35 base::FilePath backup_path = path.ReplaceExtension(kBackupExtension); | 33 base::FilePath backup_path = path.ReplaceExtension(kBackupExtension); |
| 36 base::CopyFile(path, backup_path); | 34 base::CopyFile(path, backup_path); |
| 37 } | 35 } |
| 38 | 36 |
| 39 // Adds node to the model's index, recursing through all children as well. | 37 // Adds node to the model's index, recursing through all children as well. |
| 40 void AddBookmarksToIndex(BookmarkLoadDetails* details, | 38 void AddBookmarksToIndex(BookmarkLoadDetails* details, |
| 41 BookmarkNode* node) { | 39 BookmarkNode* node) { |
| 42 if (node->is_url()) { | 40 if (node->is_url()) { |
| 43 if (node->url().is_valid()) | 41 if (node->url().is_valid()) |
| 44 details->index()->Add(node); | 42 details->index()->Add(node); |
| 45 } else { | 43 } else { |
| 46 for (int i = 0; i < node->child_count(); ++i) | 44 for (int i = 0; i < node->child_count(); ++i) |
| 47 AddBookmarksToIndex(details, node->GetChild(i)); | 45 AddBookmarksToIndex(details, node->GetChild(i)); |
| 48 } | 46 } |
| 49 } | 47 } |
| 50 | 48 |
| 51 void LoadCallback(const base::FilePath& path, | 49 void LoadCallback(const base::FilePath& path, |
| 52 BookmarkStorage* storage, | 50 BookmarkStorage* storage, |
| 53 BookmarkLoadDetails* details) { | 51 BookmarkLoadDetails* details, |
| 52 base::SequencedTaskRunner* task_runner) { |
| 54 startup_metric_utils::ScopedSlowStartupUMA | 53 startup_metric_utils::ScopedSlowStartupUMA |
| 55 scoped_timer("Startup.SlowStartupBookmarksLoad"); | 54 scoped_timer("Startup.SlowStartupBookmarksLoad"); |
| 56 bool bookmark_file_exists = base::PathExists(path); | 55 bool bookmark_file_exists = base::PathExists(path); |
| 57 if (bookmark_file_exists) { | 56 if (bookmark_file_exists) { |
| 58 JSONFileValueSerializer serializer(path); | 57 JSONFileValueSerializer serializer(path); |
| 59 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 58 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 60 | 59 |
| 61 if (root.get()) { | 60 if (root.get()) { |
| 62 // Building the index can take a while, so we do it on the background | 61 // Building the index can take a while, so we do it on the background |
| 63 // thread. | 62 // thread. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 | 77 |
| 79 start_time = TimeTicks::Now(); | 78 start_time = TimeTicks::Now(); |
| 80 AddBookmarksToIndex(details, details->bb_node()); | 79 AddBookmarksToIndex(details, details->bb_node()); |
| 81 AddBookmarksToIndex(details, details->other_folder_node()); | 80 AddBookmarksToIndex(details, details->other_folder_node()); |
| 82 AddBookmarksToIndex(details, details->mobile_folder_node()); | 81 AddBookmarksToIndex(details, details->mobile_folder_node()); |
| 83 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", | 82 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", |
| 84 TimeTicks::Now() - start_time); | 83 TimeTicks::Now() - start_time); |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 BrowserThread::PostTask( | 87 task_runner->PostTask(FROM_HERE, |
| 89 BrowserThread::UI, FROM_HERE, | 88 base::Bind(&BookmarkStorage::OnLoadFinished, storage)); |
| 90 base::Bind(&BookmarkStorage::OnLoadFinished, storage)); | |
| 91 } | 89 } |
| 92 | 90 |
| 93 } // namespace | 91 } // namespace |
| 94 | 92 |
| 95 // BookmarkLoadDetails --------------------------------------------------------- | 93 // BookmarkLoadDetails --------------------------------------------------------- |
| 96 | 94 |
| 97 BookmarkLoadDetails::BookmarkLoadDetails( | 95 BookmarkLoadDetails::BookmarkLoadDetails( |
| 98 BookmarkPermanentNode* bb_node, | 96 BookmarkPermanentNode* bb_node, |
| 99 BookmarkPermanentNode* other_folder_node, | 97 BookmarkPermanentNode* other_folder_node, |
| 100 BookmarkPermanentNode* mobile_folder_node, | 98 BookmarkPermanentNode* mobile_folder_node, |
| 101 BookmarkIndex* index, | 99 BookmarkIndex* index, |
| 102 int64 max_id) | 100 int64 max_id) |
| 103 : bb_node_(bb_node), | 101 : bb_node_(bb_node), |
| 104 other_folder_node_(other_folder_node), | 102 other_folder_node_(other_folder_node), |
| 105 mobile_folder_node_(mobile_folder_node), | 103 mobile_folder_node_(mobile_folder_node), |
| 106 index_(index), | 104 index_(index), |
| 107 model_sync_transaction_version_( | 105 model_sync_transaction_version_( |
| 108 BookmarkNode::kInvalidSyncTransactionVersion), | 106 BookmarkNode::kInvalidSyncTransactionVersion), |
| 109 max_id_(max_id), | 107 max_id_(max_id), |
| 110 ids_reassigned_(false) { | 108 ids_reassigned_(false) { |
| 111 } | 109 } |
| 112 | 110 |
| 113 BookmarkLoadDetails::~BookmarkLoadDetails() { | 111 BookmarkLoadDetails::~BookmarkLoadDetails() { |
| 114 } | 112 } |
| 115 | 113 |
| 116 // BookmarkStorage ------------------------------------------------------------- | 114 // BookmarkStorage ------------------------------------------------------------- |
| 117 | 115 |
| 118 BookmarkStorage::BookmarkStorage( | 116 BookmarkStorage::BookmarkStorage( |
| 119 content::BrowserContext* context, | |
| 120 BookmarkModel* model, | 117 BookmarkModel* model, |
| 118 const base::FilePath& profile_path, |
| 121 base::SequencedTaskRunner* sequenced_task_runner) | 119 base::SequencedTaskRunner* sequenced_task_runner) |
| 122 : model_(model), | 120 : model_(model), |
| 123 writer_(context->GetPath().Append(bookmarks::kBookmarksFileName), | 121 writer_(profile_path.Append(bookmarks::kBookmarksFileName), |
| 124 sequenced_task_runner) { | 122 sequenced_task_runner) { |
| 125 sequenced_task_runner_ = sequenced_task_runner; | 123 sequenced_task_runner_ = sequenced_task_runner; |
| 126 writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 124 writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
| 127 sequenced_task_runner_->PostTask(FROM_HERE, | 125 sequenced_task_runner_->PostTask(FROM_HERE, |
| 128 base::Bind(&BackupCallback, writer_.path())); | 126 base::Bind(&BackupCallback, writer_.path())); |
| 129 } | 127 } |
| 130 | 128 |
| 131 BookmarkStorage::~BookmarkStorage() { | 129 BookmarkStorage::~BookmarkStorage() { |
| 132 if (writer_.HasPendingWrite()) | 130 if (writer_.HasPendingWrite()) |
| 133 writer_.DoScheduledWrite(); | 131 writer_.DoScheduledWrite(); |
| 134 } | 132 } |
| 135 | 133 |
| 136 void BookmarkStorage::LoadBookmarks(BookmarkLoadDetails* details) { | 134 void BookmarkStorage::LoadBookmarks( |
| 135 BookmarkLoadDetails* details, |
| 136 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 137 DCHECK(!details_.get()); | 137 DCHECK(!details_.get()); |
| 138 DCHECK(details); | 138 DCHECK(details); |
| 139 details_.reset(details); | 139 details_.reset(details); |
| 140 sequenced_task_runner_->PostTask( | 140 sequenced_task_runner_->PostTask(FROM_HERE, |
| 141 FROM_HERE, | 141 base::Bind(&LoadCallback, |
| 142 base::Bind(&LoadCallback, writer_.path(), make_scoped_refptr(this), | 142 writer_.path(), |
| 143 details_.get())); | 143 make_scoped_refptr(this), |
| 144 details_.get(), |
| 145 task_runner)); |
| 144 } | 146 } |
| 145 | 147 |
| 146 void BookmarkStorage::ScheduleSave() { | 148 void BookmarkStorage::ScheduleSave() { |
| 147 writer_.ScheduleWrite(this); | 149 writer_.ScheduleWrite(this); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void BookmarkStorage::BookmarkModelDeleted() { | 152 void BookmarkStorage::BookmarkModelDeleted() { |
| 151 // We need to save now as otherwise by the time SaveNow is invoked | 153 // We need to save now as otherwise by the time SaveNow is invoked |
| 152 // the model is gone. | 154 // the model is gone. |
| 153 if (writer_.HasPendingWrite()) | 155 if (writer_.HasPendingWrite()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 177 NOTREACHED(); | 179 NOTREACHED(); |
| 178 return false; | 180 return false; |
| 179 } | 181 } |
| 180 | 182 |
| 181 std::string data; | 183 std::string data; |
| 182 if (!SerializeData(&data)) | 184 if (!SerializeData(&data)) |
| 183 return false; | 185 return false; |
| 184 writer_.WriteNow(data); | 186 writer_.WriteNow(data); |
| 185 return true; | 187 return true; |
| 186 } | 188 } |
| OLD | NEW |