| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/histogram.h" | 9 #include "base/histogram.h" |
| 10 #include "base/json_writer.h" | 10 #include "base/json_writer.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 DISALLOW_COPY_AND_ASSIGN(FileDeleteTask); | 61 DISALLOW_COPY_AND_ASSIGN(FileDeleteTask); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 class BookmarkStorage::LoadTask : public Task { | 66 class BookmarkStorage::LoadTask : public Task { |
| 67 public: | 67 public: |
| 68 LoadTask(const FilePath& path, | 68 LoadTask(const FilePath& path, |
| 69 MessageLoop* loop, | 69 MessageLoop* loop, |
| 70 BookmarkStorage* storage, | 70 BookmarkStorage* storage, |
| 71 LoadDetails* details, | 71 LoadDetails* details) |
| 72 bool persist_ids) | |
| 73 : path_(path), | 72 : path_(path), |
| 74 loop_(loop), | 73 loop_(loop), |
| 75 storage_(storage), | 74 storage_(storage), |
| 76 details_(details), | 75 details_(details) { |
| 77 persist_ids_(persist_ids) { | |
| 78 } | 76 } |
| 79 | 77 |
| 80 virtual void Run() { | 78 virtual void Run() { |
| 81 bool bookmark_file_exists = file_util::PathExists(path_); | 79 bool bookmark_file_exists = file_util::PathExists(path_); |
| 82 if (bookmark_file_exists) { | 80 if (bookmark_file_exists) { |
| 83 JSONFileValueSerializer serializer(path_); | 81 JSONFileValueSerializer serializer(path_); |
| 84 scoped_ptr<Value> root(serializer.Deserialize(NULL)); | 82 scoped_ptr<Value> root(serializer.Deserialize(NULL)); |
| 85 | 83 |
| 86 if (root.get()) { | 84 if (root.get()) { |
| 87 // Building the index cane take a while, so we do it on the background | 85 // Building the index cane take a while, so we do it on the background |
| 88 // thread. | 86 // thread. |
| 89 int max_node_id = 0; | 87 int64 max_node_id = 0; |
| 90 BookmarkCodec codec(persist_ids_); | 88 BookmarkCodec codec; |
| 91 TimeTicks start_time = TimeTicks::Now(); | 89 TimeTicks start_time = TimeTicks::Now(); |
| 92 codec.Decode(details_->bb_node(), details_->other_folder_node(), | 90 codec.Decode(details_->bb_node(), details_->other_folder_node(), |
| 93 &max_node_id, *root.get()); | 91 &max_node_id, *root.get()); |
| 94 details_->set_max_id(std::max(max_node_id, details_->max_id())); | 92 details_->set_max_id(std::max(max_node_id, details_->max_id())); |
| 95 details_->set_computed_checksum(codec.computed_checksum()); | 93 details_->set_computed_checksum(codec.computed_checksum()); |
| 96 details_->set_stored_checksum(codec.stored_checksum()); | 94 details_->set_stored_checksum(codec.stored_checksum()); |
| 95 details_->set_ids_reassigned(codec.ids_reassigned()); |
| 97 UMA_HISTOGRAM_TIMES("Bookmarks.DecodeTime", | 96 UMA_HISTOGRAM_TIMES("Bookmarks.DecodeTime", |
| 98 TimeTicks::Now() - start_time); | 97 TimeTicks::Now() - start_time); |
| 99 | 98 |
| 100 start_time = TimeTicks::Now(); | 99 start_time = TimeTicks::Now(); |
| 101 AddBookmarksToIndex(details_->bb_node()); | 100 AddBookmarksToIndex(details_->bb_node()); |
| 102 AddBookmarksToIndex(details_->other_folder_node()); | 101 AddBookmarksToIndex(details_->other_folder_node()); |
| 103 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", | 102 UMA_HISTOGRAM_TIMES("Bookmarks.CreateBookmarkIndexTime", |
| 104 TimeTicks::Now() - start_time); | 103 TimeTicks::Now() - start_time); |
| 105 } | 104 } |
| 106 } | 105 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 } else { | 121 } else { |
| 123 for (int i = 0; i < node->GetChildCount(); ++i) | 122 for (int i = 0; i < node->GetChildCount(); ++i) |
| 124 AddBookmarksToIndex(node->GetChild(i)); | 123 AddBookmarksToIndex(node->GetChild(i)); |
| 125 } | 124 } |
| 126 } | 125 } |
| 127 | 126 |
| 128 const FilePath path_; | 127 const FilePath path_; |
| 129 MessageLoop* loop_; | 128 MessageLoop* loop_; |
| 130 scoped_refptr<BookmarkStorage> storage_; | 129 scoped_refptr<BookmarkStorage> storage_; |
| 131 LoadDetails* details_; | 130 LoadDetails* details_; |
| 132 bool persist_ids_; | |
| 133 | 131 |
| 134 DISALLOW_COPY_AND_ASSIGN(LoadTask); | 132 DISALLOW_COPY_AND_ASSIGN(LoadTask); |
| 135 }; | 133 }; |
| 136 | 134 |
| 137 // BookmarkStorage ------------------------------------------------------------- | 135 // BookmarkStorage ------------------------------------------------------------- |
| 138 | 136 |
| 139 BookmarkStorage::BookmarkStorage(Profile* profile, BookmarkModel* model) | 137 BookmarkStorage::BookmarkStorage(Profile* profile, BookmarkModel* model) |
| 140 : profile_(profile), | 138 : profile_(profile), |
| 141 model_(model), | 139 model_(model), |
| 142 backend_thread_(g_browser_process->file_thread()), | 140 backend_thread_(g_browser_process->file_thread()), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 DCHECK(!details_.get()); | 155 DCHECK(!details_.get()); |
| 158 DCHECK(details); | 156 DCHECK(details); |
| 159 details_.reset(details); | 157 details_.reset(details); |
| 160 DoLoadBookmarks(writer_.path()); | 158 DoLoadBookmarks(writer_.path()); |
| 161 } | 159 } |
| 162 | 160 |
| 163 void BookmarkStorage::DoLoadBookmarks(const FilePath& path) { | 161 void BookmarkStorage::DoLoadBookmarks(const FilePath& path) { |
| 164 Task* task = new LoadTask(path, | 162 Task* task = new LoadTask(path, |
| 165 backend_thread() ? MessageLoop::current() : NULL, | 163 backend_thread() ? MessageLoop::current() : NULL, |
| 166 this, | 164 this, |
| 167 details_.get(), | 165 details_.get()); |
| 168 model_->PersistIDs()); | |
| 169 RunTaskOnBackendThread(task); | 166 RunTaskOnBackendThread(task); |
| 170 } | 167 } |
| 171 | 168 |
| 172 void BookmarkStorage::MigrateFromHistory() { | 169 void BookmarkStorage::MigrateFromHistory() { |
| 173 // We need to wait until history has finished loading before reading | 170 // We need to wait until history has finished loading before reading |
| 174 // from generated bookmarks file. | 171 // from generated bookmarks file. |
| 175 HistoryService* history = | 172 HistoryService* history = |
| 176 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 173 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 177 if (!history) { | 174 if (!history) { |
| 178 // This happens in unit tests. | 175 // This happens in unit tests. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 204 | 201 |
| 205 void BookmarkStorage::BookmarkModelDeleted() { | 202 void BookmarkStorage::BookmarkModelDeleted() { |
| 206 // We need to save now as otherwise by the time SaveNow is invoked | 203 // We need to save now as otherwise by the time SaveNow is invoked |
| 207 // the model is gone. | 204 // the model is gone. |
| 208 if (writer_.HasPendingWrite()) | 205 if (writer_.HasPendingWrite()) |
| 209 SaveNow(); | 206 SaveNow(); |
| 210 model_ = NULL; | 207 model_ = NULL; |
| 211 } | 208 } |
| 212 | 209 |
| 213 bool BookmarkStorage::SerializeData(std::string* output) { | 210 bool BookmarkStorage::SerializeData(std::string* output) { |
| 214 BookmarkCodec codec(model_->PersistIDs()); | 211 BookmarkCodec codec; |
| 215 scoped_ptr<Value> value(codec.Encode(model_)); | 212 scoped_ptr<Value> value(codec.Encode(model_)); |
| 216 JSONStringValueSerializer serializer(output); | 213 JSONStringValueSerializer serializer(output); |
| 217 serializer.set_pretty_print(true); | 214 serializer.set_pretty_print(true); |
| 218 return serializer.Serialize(*(value.get())); | 215 return serializer.Serialize(*(value.get())); |
| 219 } | 216 } |
| 220 | 217 |
| 221 void BookmarkStorage::OnLoadFinished(bool file_exists, const FilePath& path) { | 218 void BookmarkStorage::OnLoadFinished(bool file_exists, const FilePath& path) { |
| 222 if (path == writer_.path() && !file_exists) { | 219 if (path == writer_.path() && !file_exists) { |
| 223 // The file doesn't exist. This means one of two things: | 220 // The file doesn't exist. This means one of two things: |
| 224 // 1. A clean profile. | 221 // 1. A clean profile. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 } | 272 } |
| 276 | 273 |
| 277 void BookmarkStorage::RunTaskOnBackendThread(Task* task) const { | 274 void BookmarkStorage::RunTaskOnBackendThread(Task* task) const { |
| 278 if (backend_thread()) { | 275 if (backend_thread()) { |
| 279 backend_thread()->message_loop()->PostTask(FROM_HERE, task); | 276 backend_thread()->message_loop()->PostTask(FROM_HERE, task); |
| 280 } else { | 277 } else { |
| 281 task->Run(); | 278 task->Run(); |
| 282 delete task; | 279 delete task; |
| 283 } | 280 } |
| 284 } | 281 } |
| OLD | NEW |