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