[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 5 | #include "chrome/browser/bookmarks/bookmark_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 4e425be4 | 2011-01-15 06:56:09 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <functional> |
| 9 | |
[email protected] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 12 | #include "base/json/json_string_value_serializer.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 13 | #include "base/sequenced_task_runner.h" |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | fc3fc45 | 2009-02-10 03:25:40 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 17 | #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 18 | #include "chrome/browser/bookmarks/bookmark_index.h" |
[email protected] | 125b23418 | 2011-07-08 19:54:41 | [diff] [blame] | 19 | #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
[email protected] | 68de8b7 | 2008-09-09 23:08:13 | [diff] [blame] | 20 | #include "chrome/browser/bookmarks/bookmark_storage.h" |
[email protected] | 368f3a7 | 2011-03-08 17:17:48 | [diff] [blame] | 21 | #include "chrome/browser/bookmarks/bookmark_utils.h" |
[email protected] | dfad18be7 | 2012-12-21 00:46:34 | [diff] [blame] | 22 | #include "chrome/browser/favicon/favicon_service.h" |
[email protected] | f3d2b31 | 2012-08-23 22:27:59 | [diff] [blame] | 23 | #include "chrome/browser/favicon/favicon_service_factory.h" |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 24 | #include "chrome/browser/history/history_notifications.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 25 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9aac6686 | 2012-06-19 19:44:31 | [diff] [blame] | 26 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 27 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 28 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | dae9f5b | 2013-02-27 15:29:23 | [diff] [blame] | 29 | #include "content/public/browser/notification_details.h" |
| 30 | #include "content/public/browser/notification_source.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 31 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 32 | #include "ui/base/l10n/l10n_util.h" |
| 33 | #include "ui/base/l10n/l10n_util_collator.h" |
[email protected] | 65baa22 | 2012-08-30 15:43:51 | [diff] [blame] | 34 | #include "ui/gfx/favicon_size.h" |
[email protected] | 6a4e5a0 | 2012-06-26 19:47:48 | [diff] [blame] | 35 | #include "ui/gfx/image/image_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 37 | using base::Time; |
| 38 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | // Helper to get a mutable bookmark node. |
[email protected] | 7b084b0 | 2011-07-08 16:06:48 | [diff] [blame] | 42 | BookmarkNode* AsMutable(const BookmarkNode* node) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 43 | return const_cast<BookmarkNode*>(node); |
| 44 | } |
| 45 | |
[email protected] | f280db8 | 2012-10-20 04:21:08 | [diff] [blame] | 46 | // Whitespace characters to strip from bookmark titles. |
| 47 | const char16 kInvalidChars[] = { |
| 48 | '\n', '\r', '\t', |
| 49 | 0x2028, // Line separator |
| 50 | 0x2029, // Paragraph separator |
| 51 | 0 |
| 52 | }; |
| 53 | |
[email protected] | e1f76c6 | 2011-06-30 20:15:39 | [diff] [blame] | 54 | } // namespace |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 55 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 56 | // BookmarkNode --------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 58 | BookmarkNode::BookmarkNode(const GURL& url) |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 59 | : url_(url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 60 | Initialize(0); |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 63 | BookmarkNode::BookmarkNode(int64 id, const GURL& url) |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 64 | : url_(url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 65 | Initialize(id); |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | a0835ac | 2010-09-13 19:40:08 | [diff] [blame] | 68 | BookmarkNode::~BookmarkNode() { |
| 69 | } |
| 70 | |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 71 | void BookmarkNode::SetTitle(const string16& title) { |
[email protected] | f280db8 | 2012-10-20 04:21:08 | [diff] [blame] | 72 | // Replace newlines and other problematic whitespace characters in |
| 73 | // folder/bookmark names with spaces. |
| 74 | string16 trimmed_title; |
| 75 | ReplaceChars(title, kInvalidChars, ASCIIToUTF16(" "), &trimmed_title); |
| 76 | ui::TreeNode<BookmarkNode>::SetTitle(trimmed_title); |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 79 | bool BookmarkNode::IsVisible() const { |
| 80 | return true; |
| 81 | } |
| 82 | |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 83 | bool BookmarkNode::GetMetaInfo(const std::string& key, |
| 84 | std::string* value) const { |
| 85 | if (meta_info_str_.empty()) |
| 86 | return false; |
| 87 | |
| 88 | JSONStringValueSerializer serializer(meta_info_str_); |
| 89 | scoped_ptr<DictionaryValue> meta_dict( |
| 90 | static_cast<DictionaryValue*>(serializer.Deserialize(NULL, NULL))); |
| 91 | return meta_dict.get() ? meta_dict->GetString(key, value) : false; |
| 92 | } |
| 93 | |
| 94 | bool BookmarkNode::SetMetaInfo(const std::string& key, |
| 95 | const std::string& value) { |
| 96 | JSONStringValueSerializer serializer(&meta_info_str_); |
| 97 | scoped_ptr<DictionaryValue> meta_dict; |
| 98 | if (!meta_info_str_.empty()) { |
| 99 | meta_dict.reset( |
| 100 | static_cast<DictionaryValue*>(serializer.Deserialize(NULL, NULL))); |
| 101 | } |
| 102 | if (!meta_dict.get()) { |
| 103 | meta_dict.reset(new DictionaryValue); |
| 104 | } else { |
| 105 | std::string old_value; |
| 106 | if (meta_dict->GetString(key, &old_value) && old_value == value) |
| 107 | return false; |
| 108 | } |
| 109 | meta_dict->SetString(key, value); |
| 110 | serializer.Serialize(*meta_dict); |
| 111 | std::string(meta_info_str_.data(), meta_info_str_.size()).swap( |
| 112 | meta_info_str_); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool BookmarkNode::DeleteMetaInfo(const std::string& key) { |
| 117 | if (meta_info_str_.empty()) |
| 118 | return false; |
| 119 | |
| 120 | JSONStringValueSerializer serializer(&meta_info_str_); |
| 121 | scoped_ptr<DictionaryValue> meta_dict( |
| 122 | static_cast<DictionaryValue*>(serializer.Deserialize(NULL, NULL))); |
| 123 | if (meta_dict.get() && meta_dict->HasKey(key)) { |
| 124 | meta_dict->Remove(key, NULL); |
| 125 | if (meta_dict->empty()) { |
| 126 | meta_info_str_.clear(); |
| 127 | } else { |
| 128 | serializer.Serialize(*meta_dict); |
| 129 | std::string(meta_info_str_.data(), meta_info_str_.size()).swap( |
| 130 | meta_info_str_); |
| 131 | } |
| 132 | return true; |
| 133 | } else { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
[email protected] | 7b084b0 | 2011-07-08 16:06:48 | [diff] [blame] | 138 | void BookmarkNode::Initialize(int64 id) { |
| 139 | id_ = id; |
| 140 | type_ = url_.is_empty() ? FOLDER : URL; |
| 141 | date_added_ = Time::Now(); |
[email protected] | bcc3861 | 2012-10-23 01:10:27 | [diff] [blame] | 142 | favicon_state_ = INVALID_FAVICON; |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 143 | favicon_load_task_id_ = CancelableTaskTracker::kBadTaskId; |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 144 | meta_info_str_.clear(); |
[email protected] | 7b084b0 | 2011-07-08 16:06:48 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 147 | void BookmarkNode::InvalidateFavicon() { |
[email protected] | 2ad0e87 | 2012-11-30 01:24:46 | [diff] [blame] | 148 | icon_url_ = GURL(); |
[email protected] | 6a4e5a0 | 2012-06-26 19:47:48 | [diff] [blame] | 149 | favicon_ = gfx::Image(); |
[email protected] | bcc3861 | 2012-10-23 01:10:27 | [diff] [blame] | 150 | favicon_state_ = INVALID_FAVICON; |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 153 | namespace { |
| 154 | |
| 155 | // Comparator used when sorting bookmarks. Folders are sorted first, then |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 156 | // bookmarks. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 157 | class SortComparator : public std::binary_function<const BookmarkNode*, |
| 158 | const BookmarkNode*, |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 159 | bool> { |
| 160 | public: |
[email protected] | a48f87d | 2012-10-09 18:06:33 | [diff] [blame] | 161 | explicit SortComparator(icu::Collator* collator) : collator_(collator) {} |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 162 | |
[email protected] | a48f87d | 2012-10-09 18:06:33 | [diff] [blame] | 163 | // Returns true if |n1| preceeds |n2|. |
| 164 | bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) { |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 165 | if (n1->type() == n2->type()) { |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 166 | // Types are the same, compare the names. |
| 167 | if (!collator_) |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 168 | return n1->GetTitle() < n2->GetTitle(); |
[email protected] | 3abebda | 2011-01-07 20:17:15 | [diff] [blame] | 169 | return l10n_util::CompareString16WithCollator( |
| 170 | collator_, n1->GetTitle(), n2->GetTitle()) == UCOL_LESS; |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 171 | } |
| 172 | // Types differ, sort such that folders come first. |
| 173 | return n1->is_folder(); |
| 174 | } |
| 175 | |
| 176 | private: |
[email protected] | b5b2385a | 2009-08-18 05:12:29 | [diff] [blame] | 177 | icu::Collator* collator_; |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 178 | }; |
| 179 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 180 | } // namespace |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 181 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 182 | // BookmarkPermanentNode ------------------------------------------------------- |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 183 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 184 | BookmarkPermanentNode::BookmarkPermanentNode(int64 id) |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 185 | : BookmarkNode(id, GURL()), |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 186 | visible_(true) { |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 189 | BookmarkPermanentNode::~BookmarkPermanentNode() { |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 192 | bool BookmarkPermanentNode::IsVisible() const { |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 193 | return visible_ || !empty(); |
| 194 | } |
| 195 | |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 196 | // BookmarkModel -------------------------------------------------------------- |
| 197 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 198 | BookmarkModel::BookmarkModel(Profile* profile) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | : profile_(profile), |
| 200 | loaded_(false), |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 201 | root_(GURL()), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | bookmark_bar_node_(NULL), |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 203 | other_node_(NULL), |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 204 | mobile_node_(NULL), |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 205 | next_node_id_(1), |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 206 | observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 207 | loaded_signal_(true, false), |
| 208 | extensive_changes_(0) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 209 | if (!profile_) { |
| 210 | // Profile is null during testing. |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 211 | DoneLoading(CreateLoadDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 215 | BookmarkModel::~BookmarkModel() { |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 216 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
[email protected] | 3de6fd34 | 2008-09-05 02:44:51 | [diff] [blame] | 217 | BookmarkModelBeingDeleted(this)); |
| 218 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 219 | if (store_) { |
| 220 | // The store maintains a reference back to us. We need to tell it we're gone |
| 221 | // so that it doesn't try and invoke a method back on us again. |
| 222 | store_->BookmarkModelDeleted(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 226 | void BookmarkModel::Shutdown() { |
[email protected] | 88e6a23 | 2011-09-14 14:53:20 | [diff] [blame] | 227 | if (loaded_) |
| 228 | return; |
| 229 | |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 230 | // See comment in HistoryService::ShutdownOnUIThread where this is invoked for |
| 231 | // details. It is also called when the BookmarkModel is deleted. |
[email protected] | 88e6a23 | 2011-09-14 14:53:20 | [diff] [blame] | 232 | loaded_signal_.Signal(); |
| 233 | } |
| 234 | |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame^] | 235 | void BookmarkModel::Load( |
| 236 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 237 | if (store_.get()) { |
| 238 | // If the store is non-null, it means Load was already invoked. Load should |
| 239 | // only be invoked once. |
| 240 | NOTREACHED(); |
| 241 | return; |
| 242 | } |
| 243 | |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 244 | expanded_state_tracker_.reset(new BookmarkExpandedStateTracker( |
[email protected] | 25ee8530 | 2012-10-12 17:27:41 | [diff] [blame] | 245 | profile_, this)); |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 246 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 247 | // Listen for changes to favicons so that we can update the favicon of the |
| 248 | // node appropriately. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 249 | registrar_.Add(this, chrome::NOTIFICATION_FAVICON_CHANGED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 250 | content::Source<Profile>(profile_)); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 251 | |
| 252 | // Load the bookmarks. BookmarkStorage notifies us when done. |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame^] | 253 | store_ = new BookmarkStorage(profile_, this, task_runner); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 254 | store_->LoadBookmarks(CreateLoadDetails()); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 61b8c78 | 2011-07-25 18:11:16 | [diff] [blame] | 257 | bool BookmarkModel::IsLoaded() const { |
| 258 | return loaded_; |
| 259 | } |
| 260 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 261 | const BookmarkNode* BookmarkModel::GetParentForNewNodes() { |
| 262 | std::vector<const BookmarkNode*> nodes = |
[email protected] | 9fcaee7 | 2011-03-21 21:53:44 | [diff] [blame] | 263 | bookmark_utils::GetMostRecentlyModifiedFolders(this, 1); |
[email protected] | 82f4655a | 2011-10-18 13:05:43 | [diff] [blame] | 264 | DCHECK(!nodes.empty()); // This list is always padded with default folders. |
| 265 | return nodes[0]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 125b23418 | 2011-07-08 19:54:41 | [diff] [blame] | 268 | void BookmarkModel::AddObserver(BookmarkModelObserver* observer) { |
| 269 | observers_.AddObserver(observer); |
| 270 | } |
| 271 | |
| 272 | void BookmarkModel::RemoveObserver(BookmarkModelObserver* observer) { |
| 273 | observers_.RemoveObserver(observer); |
| 274 | } |
| 275 | |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 276 | void BookmarkModel::BeginExtensiveChanges() { |
| 277 | if (++extensive_changes_ == 1) { |
| 278 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 279 | ExtensiveBookmarkChangesBeginning(this)); |
| 280 | } |
[email protected] | 125b23418 | 2011-07-08 19:54:41 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | b68a817 | 2012-02-17 00:25:18 | [diff] [blame] | 283 | void BookmarkModel::EndExtensiveChanges() { |
| 284 | --extensive_changes_; |
| 285 | DCHECK_GE(extensive_changes_, 0); |
| 286 | if (extensive_changes_ == 0) { |
| 287 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 288 | ExtensiveBookmarkChangesEnded(this)); |
| 289 | } |
[email protected] | 125b23418 | 2011-07-08 19:54:41 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 292 | void BookmarkModel::Remove(const BookmarkNode* parent, int index) { |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 293 | if (!loaded_ || !IsValidIndex(parent, index, false) || is_root_node(parent)) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 294 | NOTREACHED(); |
| 295 | return; |
| 296 | } |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 297 | RemoveAndDeleteNode(AsMutable(parent->GetChild(index))); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 300 | void BookmarkModel::RemoveAll() { |
| 301 | std::set<GURL> changed_urls; |
| 302 | ScopedVector<BookmarkNode> removed_nodes; |
| 303 | BeginExtensiveChanges(); |
| 304 | // Skip deleting permanent nodes. Permanent bookmark nodes are the root and |
| 305 | // its immediate children. For removing all non permanent nodes just remove |
| 306 | // all children of non-root permanent nodes. |
| 307 | { |
| 308 | base::AutoLock url_lock(url_lock_); |
| 309 | for (int i = 0; i < root_.child_count(); ++i) { |
| 310 | const BookmarkNode* permanent_node = root_.GetChild(i); |
| 311 | for (int j = permanent_node->child_count() - 1; j >= 0; --j) { |
| 312 | BookmarkNode* child_node = AsMutable(permanent_node->GetChild(j)); |
| 313 | removed_nodes.push_back(child_node); |
| 314 | RemoveNodeAndGetRemovedUrls(child_node, &changed_urls); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | EndExtensiveChanges(); |
| 319 | if (store_.get()) |
| 320 | store_->ScheduleSave(); |
| 321 | |
| 322 | NotifyHistoryAboutRemovedBookmarks(changed_urls); |
| 323 | |
| 324 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 325 | BookmarkAllNodesRemoved(this)); |
| 326 | } |
| 327 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 328 | void BookmarkModel::Move(const BookmarkNode* node, |
| 329 | const BookmarkNode* new_parent, |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 330 | int index) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 331 | if (!loaded_ || !node || !IsValidIndex(new_parent, index, true) || |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 332 | is_root_node(new_parent) || is_permanent_node(node)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | NOTREACHED(); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | if (new_parent->HasAncestor(node)) { |
| 338 | // Can't make an ancestor of the node be a child of the node. |
| 339 | NOTREACHED(); |
| 340 | return; |
| 341 | } |
| 342 | |
[email protected] | 2d48ee84 | 2011-03-08 23:27:29 | [diff] [blame] | 343 | const BookmarkNode* old_parent = node->parent(); |
[email protected] | 368f3a7 | 2011-03-08 17:17:48 | [diff] [blame] | 344 | int old_index = old_parent->GetIndexOf(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | |
| 346 | if (old_parent == new_parent && |
| 347 | (index == old_index || index == old_index + 1)) { |
| 348 | // Node is already in this position, nothing to do. |
| 349 | return; |
| 350 | } |
| 351 | |
[email protected] | 9e58364 | 2012-12-05 02:48:32 | [diff] [blame] | 352 | SetDateFolderModified(new_parent, Time::Now()); |
| 353 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | if (old_parent == new_parent && index > old_index) |
| 355 | index--; |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 356 | BookmarkNode* mutable_new_parent = AsMutable(new_parent); |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 357 | mutable_new_parent->Add(AsMutable(node), index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 359 | if (store_.get()) |
| 360 | store_->ScheduleSave(); |
| 361 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 362 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | BookmarkNodeMoved(this, old_parent, old_index, |
| 364 | new_parent, index)); |
| 365 | } |
| 366 | |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 367 | void BookmarkModel::Copy(const BookmarkNode* node, |
| 368 | const BookmarkNode* new_parent, |
| 369 | int index) { |
| 370 | if (!loaded_ || !node || !IsValidIndex(new_parent, index, true) || |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 371 | is_root_node(new_parent) || is_permanent_node(node)) { |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 372 | NOTREACHED(); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | if (new_parent->HasAncestor(node)) { |
| 377 | // Can't make an ancestor of the node be a child of the node. |
| 378 | NOTREACHED(); |
| 379 | return; |
| 380 | } |
| 381 | |
[email protected] | c6a7a3d | 2011-03-12 01:04:30 | [diff] [blame] | 382 | SetDateFolderModified(new_parent, Time::Now()); |
[email protected] | 14eb15af | 2010-11-20 01:03:26 | [diff] [blame] | 383 | BookmarkNodeData drag_data_(node); |
| 384 | std::vector<BookmarkNodeData::Element> elements(drag_data_.elements); |
[email protected] | b186450 | 2010-11-13 00:55:51 | [diff] [blame] | 385 | // CloneBookmarkNode will use BookmarkModel methods to do the job, so we |
[email protected] | 1a566fae | 2010-04-16 01:05:06 | [diff] [blame] | 386 | // don't need to send notifications here. |
[email protected] | b186450 | 2010-11-13 00:55:51 | [diff] [blame] | 387 | bookmark_utils::CloneBookmarkNode(this, elements, new_parent, index); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 388 | |
| 389 | if (store_.get()) |
| 390 | store_->ScheduleSave(); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 391 | } |
| 392 | |
[email protected] | 6a4e5a0 | 2012-06-26 19:47:48 | [diff] [blame] | 393 | const gfx::Image& BookmarkModel::GetFavicon(const BookmarkNode* node) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 394 | DCHECK(node); |
[email protected] | bcc3861 | 2012-10-23 01:10:27 | [diff] [blame] | 395 | if (node->favicon_state() == BookmarkNode::INVALID_FAVICON) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 396 | BookmarkNode* mutable_node = AsMutable(node); |
[email protected] | bcc3861 | 2012-10-23 01:10:27 | [diff] [blame] | 397 | mutable_node->set_favicon_state(BookmarkNode::LOADING_FAVICON); |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 398 | LoadFavicon(mutable_node); |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 399 | } |
| 400 | return node->favicon(); |
| 401 | } |
| 402 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 403 | void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 404 | if (!node) { |
| 405 | NOTREACHED(); |
| 406 | return; |
| 407 | } |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 408 | if (node->GetTitle() == title) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | return; |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 410 | |
[email protected] | 6892e2e | 2011-05-26 22:07:17 | [diff] [blame] | 411 | if (is_permanent_node(node)) { |
[email protected] | baf4f92 | 2009-10-19 16:44:07 | [diff] [blame] | 412 | NOTREACHED(); |
| 413 | return; |
| 414 | } |
| 415 | |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 416 | // The title index doesn't support changing the title, instead we remove then |
| 417 | // add it back. |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 418 | index_->Remove(node); |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 419 | AsMutable(node)->SetTitle(title); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 420 | index_->Add(node); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 421 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 422 | if (store_.get()) |
| 423 | store_->ScheduleSave(); |
| 424 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 425 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | BookmarkNodeChanged(this, node)); |
| 427 | } |
| 428 | |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 429 | void BookmarkModel::SetURL(const BookmarkNode* node, const GURL& url) { |
| 430 | if (!node) { |
| 431 | NOTREACHED(); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | // We cannot change the URL of a folder. |
| 436 | if (node->is_folder()) { |
| 437 | NOTREACHED(); |
| 438 | return; |
| 439 | } |
| 440 | |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 441 | if (node->url() == url) |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 442 | return; |
| 443 | |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 444 | BookmarkNode* mutable_node = AsMutable(node); |
| 445 | mutable_node->InvalidateFavicon(); |
| 446 | CancelPendingFaviconLoadRequests(mutable_node); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 447 | |
| 448 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 449 | base::AutoLock url_lock(url_lock_); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 450 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find( |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 451 | mutable_node); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 452 | DCHECK(i != nodes_ordered_by_url_set_.end()); |
| 453 | // i points to the first node with the URL, advance until we find the |
| 454 | // node we're removing. |
| 455 | while (*i != node) |
| 456 | ++i; |
| 457 | nodes_ordered_by_url_set_.erase(i); |
| 458 | |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 459 | mutable_node->set_url(url); |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 460 | nodes_ordered_by_url_set_.insert(mutable_node); |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (store_.get()) |
| 464 | store_->ScheduleSave(); |
| 465 | |
| 466 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 467 | BookmarkNodeChanged(this, node)); |
| 468 | } |
| 469 | |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 470 | void BookmarkModel::SetNodeMetaInfo(const BookmarkNode* node, |
| 471 | const std::string& key, |
| 472 | const std::string& value) { |
| 473 | if (AsMutable(node)->SetMetaInfo(key, value) && store_.get()) |
| 474 | store_->ScheduleSave(); |
| 475 | } |
| 476 | |
| 477 | void BookmarkModel::DeleteNodeMetaInfo(const BookmarkNode* node, |
| 478 | const std::string& key) { |
| 479 | if (AsMutable(node)->DeleteMetaInfo(key) && store_.get()) |
| 480 | store_->ScheduleSave(); |
| 481 | } |
| 482 | |
[email protected] | b61445c | 2012-10-27 00:11:42 | [diff] [blame] | 483 | void BookmarkModel::SetDateAdded(const BookmarkNode* node, |
| 484 | base::Time date_added) { |
| 485 | if (!node) { |
| 486 | NOTREACHED(); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (node->date_added() == date_added) |
| 491 | return; |
| 492 | |
| 493 | if (is_permanent_node(node)) { |
| 494 | NOTREACHED(); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | AsMutable(node)->set_date_added(date_added); |
| 499 | |
| 500 | // Syncing might result in dates newer than the folder's last modified date. |
| 501 | if (date_added > node->parent()->date_folder_modified()) { |
| 502 | // Will trigger store_->ScheduleSave(). |
| 503 | SetDateFolderModified(node->parent(), date_added); |
| 504 | } else if (store_.get()) { |
| 505 | store_->ScheduleSave(); |
| 506 | } |
| 507 | } |
| 508 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 509 | void BookmarkModel::GetNodesByURL(const GURL& url, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 510 | std::vector<const BookmarkNode*>* nodes) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 511 | base::AutoLock url_lock(url_lock_); |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 512 | BookmarkNode tmp_node(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find(&tmp_node); |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 514 | while (i != nodes_ordered_by_url_set_.end() && (*i)->url() == url) { |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 515 | nodes->push_back(*i); |
| 516 | ++i; |
| 517 | } |
| 518 | } |
| 519 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 520 | const BookmarkNode* BookmarkModel::GetMostRecentlyAddedNodeForURL( |
| 521 | const GURL& url) { |
| 522 | std::vector<const BookmarkNode*> nodes; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 523 | GetNodesByURL(url, &nodes); |
| 524 | if (nodes.empty()) |
| 525 | return NULL; |
| 526 | |
[email protected] | 9333f18 | 2008-12-09 17:34:17 | [diff] [blame] | 527 | std::sort(nodes.begin(), nodes.end(), &bookmark_utils::MoreRecentlyAdded); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 528 | return nodes.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | } |
| 530 | |
[email protected] | cf8e817 | 2011-07-23 00:46:24 | [diff] [blame] | 531 | bool BookmarkModel::HasBookmarks() { |
| 532 | base::AutoLock url_lock(url_lock_); |
| 533 | return !nodes_ordered_by_url_set_.empty(); |
| 534 | } |
| 535 | |
| 536 | bool BookmarkModel::IsBookmarked(const GURL& url) { |
| 537 | base::AutoLock url_lock(url_lock_); |
| 538 | return IsBookmarkedNoLock(url); |
| 539 | } |
| 540 | |
[email protected] | 0f7bee5 | 2012-08-06 20:04:17 | [diff] [blame] | 541 | void BookmarkModel::GetBookmarks( |
| 542 | std::vector<BookmarkService::URLAndTitle>* bookmarks) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 543 | base::AutoLock url_lock(url_lock_); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 544 | const GURL* last_url = NULL; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 545 | for (NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.begin(); |
| 546 | i != nodes_ordered_by_url_set_.end(); ++i) { |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 547 | const GURL* url = &((*i)->url()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 548 | // Only add unique URLs. |
[email protected] | 0f7bee5 | 2012-08-06 20:04:17 | [diff] [blame] | 549 | if (!last_url || *url != *last_url) { |
| 550 | BookmarkService::URLAndTitle bookmark; |
| 551 | bookmark.url = *url; |
| 552 | bookmark.title = (*i)->GetTitle(); |
| 553 | bookmarks->push_back(bookmark); |
| 554 | } |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 555 | last_url = url; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
[email protected] | cf8e817 | 2011-07-23 00:46:24 | [diff] [blame] | 559 | void BookmarkModel::BlockTillLoaded() { |
| 560 | loaded_signal_.Wait(); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 561 | } |
| 562 | |
[email protected] | 66bb7dc | 2011-10-27 15:29:23 | [diff] [blame] | 563 | const BookmarkNode* BookmarkModel::GetNodeByID(int64 id) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 564 | // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 565 | return GetNodeByID(&root_, id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | 3970329 | 2011-03-18 17:03:40 | [diff] [blame] | 568 | const BookmarkNode* BookmarkModel::AddFolder(const BookmarkNode* parent, |
| 569 | int index, |
| 570 | const string16& title) { |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 571 | if (!loaded_ || is_root_node(parent) || !IsValidIndex(parent, index, true)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 572 | // Can't add to the root. |
| 573 | NOTREACHED(); |
| 574 | return NULL; |
| 575 | } |
| 576 | |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 577 | BookmarkNode* new_node = new BookmarkNode(generate_next_node_id(), GURL()); |
[email protected] | edb63cc | 2011-03-11 02:00:41 | [diff] [blame] | 578 | new_node->set_date_folder_modified(Time::Now()); |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 579 | // Folders shouldn't have line breaks in their titles. |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 580 | new_node->SetTitle(title); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 581 | new_node->set_type(BookmarkNode::FOLDER); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | |
[email protected] | a42ec64a | 2012-12-20 17:04:24 | [diff] [blame] | 583 | return AddNode(AsMutable(parent), index, new_node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 584 | } |
| 585 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 586 | const BookmarkNode* BookmarkModel::AddURL(const BookmarkNode* parent, |
| 587 | int index, |
| 588 | const string16& title, |
| 589 | const GURL& url) { |
[email protected] | aee23654 | 2011-12-01 04:34:03 | [diff] [blame] | 590 | return AddURLWithCreationTime(parent, index, |
| 591 | CollapseWhitespace(title, false), |
| 592 | url, Time::Now()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 593 | } |
| 594 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 595 | const BookmarkNode* BookmarkModel::AddURLWithCreationTime( |
| 596 | const BookmarkNode* parent, |
| 597 | int index, |
| 598 | const string16& title, |
| 599 | const GURL& url, |
| 600 | const Time& creation_time) { |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 601 | if (!loaded_ || !url.is_valid() || is_root_node(parent) || |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 602 | !IsValidIndex(parent, index, true)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 603 | NOTREACHED(); |
| 604 | return NULL; |
| 605 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | |
[email protected] | b61445c | 2012-10-27 00:11:42 | [diff] [blame] | 607 | // Syncing may result in dates newer than the last modified date. |
[email protected] | 8ad613b | 2012-10-12 21:28:45 | [diff] [blame] | 608 | if (creation_time > parent->date_folder_modified()) |
| 609 | SetDateFolderModified(parent, creation_time); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 610 | |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 611 | BookmarkNode* new_node = new BookmarkNode(generate_next_node_id(), url); |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 612 | new_node->SetTitle(title); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 613 | new_node->set_date_added(creation_time); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 614 | new_node->set_type(BookmarkNode::URL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 615 | |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 616 | { |
| 617 | // Only hold the lock for the duration of the insert. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 618 | base::AutoLock url_lock(url_lock_); |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 619 | nodes_ordered_by_url_set_.insert(new_node); |
| 620 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | |
[email protected] | a42ec64a | 2012-12-20 17:04:24 | [diff] [blame] | 622 | return AddNode(AsMutable(parent), index, new_node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 623 | } |
| 624 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 625 | void BookmarkModel::SortChildren(const BookmarkNode* parent) { |
[email protected] | 6b4d64c | 2011-07-29 21:33:24 | [diff] [blame] | 626 | if (!parent || !parent->is_folder() || is_root_node(parent) || |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 627 | parent->child_count() <= 1) { |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 631 | UErrorCode error = U_ZERO_ERROR; |
[email protected] | 94d91452 | 2013-04-02 19:49:47 | [diff] [blame] | 632 | scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error)); |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 633 | if (U_FAILURE(error)) |
| 634 | collator.reset(NULL); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 635 | BookmarkNode* mutable_parent = AsMutable(parent); |
| 636 | std::sort(mutable_parent->children().begin(), |
| 637 | mutable_parent->children().end(), |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 638 | SortComparator(collator.get())); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 639 | |
[email protected] | 997a036 | 2009-03-12 03:10:51 | [diff] [blame] | 640 | if (store_.get()) |
| 641 | store_->ScheduleSave(); |
| 642 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 643 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 644 | BookmarkNodeChildrenReordered(this, parent)); |
| 645 | } |
| 646 | |
[email protected] | c6a7a3d | 2011-03-12 01:04:30 | [diff] [blame] | 647 | void BookmarkModel::SetDateFolderModified(const BookmarkNode* parent, |
| 648 | const Time time) { |
[email protected] | eea8fd553 | 2009-12-16 00:08:10 | [diff] [blame] | 649 | DCHECK(parent); |
[email protected] | edb63cc | 2011-03-11 02:00:41 | [diff] [blame] | 650 | AsMutable(parent)->set_date_folder_modified(time); |
[email protected] | eea8fd553 | 2009-12-16 00:08:10 | [diff] [blame] | 651 | |
| 652 | if (store_.get()) |
| 653 | store_->ScheduleSave(); |
| 654 | } |
| 655 | |
[email protected] | c6a7a3d | 2011-03-12 01:04:30 | [diff] [blame] | 656 | void BookmarkModel::ResetDateFolderModified(const BookmarkNode* node) { |
| 657 | SetDateFolderModified(node, Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 658 | } |
| 659 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 660 | void BookmarkModel::GetBookmarksWithTitlesMatching( |
| 661 | const string16& text, |
| 662 | size_t max_count, |
| 663 | std::vector<bookmark_utils::TitleMatch>* matches) { |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 664 | if (!loaded_) |
| 665 | return; |
| 666 | |
[email protected] | d0195a6 | 2010-08-22 04:40:20 | [diff] [blame] | 667 | index_->GetBookmarksWithTitlesMatching(text, max_count, matches); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | 9876bb1c | 2008-12-16 20:42:25 | [diff] [blame] | 670 | void BookmarkModel::ClearStore() { |
[email protected] | 40e0486b | 2009-05-22 01:47:27 | [diff] [blame] | 671 | registrar_.RemoveAll(); |
[email protected] | 9876bb1c | 2008-12-16 20:42:25 | [diff] [blame] | 672 | store_ = NULL; |
| 673 | } |
| 674 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 675 | void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, |
| 676 | bool value) { |
[email protected] | 1da5f71 | 2011-12-06 05:52:26 | [diff] [blame] | 677 | DCHECK(loaded_); |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 678 | switch (type) { |
| 679 | case BookmarkNode::BOOKMARK_BAR: |
| 680 | bookmark_bar_node_->set_visible(value); |
| 681 | break; |
| 682 | case BookmarkNode::OTHER_NODE: |
| 683 | other_node_->set_visible(value); |
| 684 | break; |
| 685 | case BookmarkNode::MOBILE: |
| 686 | mobile_node_->set_visible(value); |
| 687 | break; |
| 688 | default: |
| 689 | NOTREACHED(); |
| 690 | } |
[email protected] | 1da5f71 | 2011-12-06 05:52:26 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | dddc1b4 | 2008-10-09 20:56:59 | [diff] [blame] | 693 | bool BookmarkModel::IsBookmarkedNoLock(const GURL& url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 694 | BookmarkNode tmp_node(url); |
[email protected] | dddc1b4 | 2008-10-09 20:56:59 | [diff] [blame] | 695 | return (nodes_ordered_by_url_set_.find(&tmp_node) != |
| 696 | nodes_ordered_by_url_set_.end()); |
| 697 | } |
| 698 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 699 | void BookmarkModel::RemoveNode(BookmarkNode* node, |
| 700 | std::set<GURL>* removed_urls) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 701 | if (!loaded_ || !node || is_permanent_node(node)) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 702 | NOTREACHED(); |
| 703 | return; |
| 704 | } |
| 705 | |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 706 | url_lock_.AssertAcquired(); |
[email protected] | 0890e60e | 2011-06-27 14:55:21 | [diff] [blame] | 707 | if (node->is_url()) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 708 | // NOTE: this is called in such a way that url_lock_ is already held. As |
| 709 | // such, this doesn't explicitly grab the lock. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 710 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find(node); |
| 711 | DCHECK(i != nodes_ordered_by_url_set_.end()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 712 | // i points to the first node with the URL, advance until we find the |
| 713 | // node we're removing. |
| 714 | while (*i != node) |
| 715 | ++i; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 716 | nodes_ordered_by_url_set_.erase(i); |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 717 | removed_urls->insert(node->url()); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 718 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 719 | index_->Remove(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 720 | } |
| 721 | |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 722 | CancelPendingFaviconLoadRequests(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 723 | |
| 724 | // Recurse through children. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 725 | for (int i = node->child_count() - 1; i >= 0; --i) |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 726 | RemoveNode(node->GetChild(i), removed_urls); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 727 | } |
| 728 | |
[email protected] | a48f87d | 2012-10-09 18:06:33 | [diff] [blame] | 729 | void BookmarkModel::DoneLoading(BookmarkLoadDetails* details_delete_me) { |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 730 | DCHECK(details_delete_me); |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 731 | scoped_ptr<BookmarkLoadDetails> details(details_delete_me); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 732 | if (loaded_) { |
| 733 | // We should only ever be loaded once. |
| 734 | NOTREACHED(); |
| 735 | return; |
| 736 | } |
| 737 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 738 | next_node_id_ = details->max_id(); |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 739 | if (details->computed_checksum() != details->stored_checksum() || |
| 740 | details->ids_reassigned()) { |
| 741 | // If bookmarks file changed externally, the IDs may have changed |
| 742 | // externally. In that case, the decoder may have reassigned IDs to make |
| 743 | // them unique. So when the file has changed externally, we should save the |
| 744 | // bookmarks file to persist new IDs. |
| 745 | if (store_.get()) |
| 746 | store_->ScheduleSave(); |
| 747 | } |
[email protected] | d22d873 | 2010-05-04 19:24:42 | [diff] [blame] | 748 | bookmark_bar_node_ = details->release_bb_node(); |
| 749 | other_node_ = details->release_other_folder_node(); |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 750 | mobile_node_ = details->release_mobile_folder_node(); |
[email protected] | d22d873 | 2010-05-04 19:24:42 | [diff] [blame] | 751 | index_.reset(details->release_index()); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 752 | |
[email protected] | 82f4655a | 2011-10-18 13:05:43 | [diff] [blame] | 753 | // WARNING: order is important here, various places assume the order is |
| 754 | // constant. |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 755 | root_.Add(bookmark_bar_node_, 0); |
| 756 | root_.Add(other_node_, 1); |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 757 | root_.Add(mobile_node_, 2); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 758 | |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 759 | root_.set_meta_info_str(details->model_meta_info()); |
| 760 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 761 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 762 | base::AutoLock url_lock(url_lock_); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 763 | // Update nodes_ordered_by_url_set_ from the nodes. |
| 764 | PopulateNodesByURL(&root_); |
| 765 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 766 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 767 | loaded_ = true; |
| 768 | |
[email protected] | cbcd641 | 2009-03-09 22:31:39 | [diff] [blame] | 769 | loaded_signal_.Signal(); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 770 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 771 | // Notify our direct observers. |
[email protected] | c58c5ea | 2011-07-13 21:43:16 | [diff] [blame] | 772 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 773 | Loaded(this, details->ids_reassigned())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 774 | } |
| 775 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 776 | void BookmarkModel::RemoveAndDeleteNode(BookmarkNode* delete_me) { |
| 777 | scoped_ptr<BookmarkNode> node(delete_me); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 779 | const BookmarkNode* parent = node->parent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 780 | DCHECK(parent); |
[email protected] | 368f3a7 | 2011-03-08 17:17:48 | [diff] [blame] | 781 | int index = parent->GetIndexOf(node.get()); |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 782 | |
[email protected] | 8633531 | 2012-12-09 21:16:40 | [diff] [blame] | 783 | std::set<GURL> changed_urls; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 784 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 785 | base::AutoLock url_lock(url_lock_); |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 786 | RemoveNodeAndGetRemovedUrls(node.get(), &changed_urls); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 787 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 788 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 789 | if (store_.get()) |
| 790 | store_->ScheduleSave(); |
| 791 | |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 792 | NotifyHistoryAboutRemovedBookmarks(changed_urls); |
| 793 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 794 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 795 | BookmarkNodeRemoved(this, parent, index, node.get())); |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 796 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 797 | |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 798 | void BookmarkModel::RemoveNodeAndGetRemovedUrls(BookmarkNode* node, |
| 799 | std::set<GURL>* removed_urls) { |
| 800 | // NOTE: this method should be always called with |url_lock_| held. |
| 801 | // This method does not explicitly acquires a lock. |
| 802 | url_lock_.AssertAcquired(); |
| 803 | DCHECK(removed_urls); |
| 804 | BookmarkNode* parent = AsMutable(node->parent()); |
| 805 | DCHECK(parent); |
| 806 | parent->Remove(node); |
| 807 | RemoveNode(node, removed_urls); |
| 808 | // RemoveNode adds an entry to removed_urls for each node of type URL. As we |
| 809 | // allow duplicates we need to remove any entries that are still bookmarked. |
| 810 | for (std::set<GURL>::iterator i = removed_urls->begin(); |
| 811 | i != removed_urls->end();) { |
| 812 | if (IsBookmarkedNoLock(*i)) { |
| 813 | // When we erase the iterator pointing at the erasee is |
| 814 | // invalidated, so using i++ here within the "erase" call is |
| 815 | // important as it advances the iterator before passing the |
| 816 | // old value through to erase. |
| 817 | removed_urls->erase(i++); |
| 818 | } else { |
| 819 | ++i; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | void BookmarkModel::NotifyHistoryAboutRemovedBookmarks( |
| 825 | const std::set<GURL>& removed_bookmark_urls) const { |
| 826 | if (removed_bookmark_urls.empty()) { |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 827 | // No point in sending out notification if the starred state didn't change. |
| 828 | return; |
| 829 | } |
| 830 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 831 | if (profile_) { |
| 832 | HistoryService* history = |
[email protected] | 9aac6686 | 2012-06-19 19:44:31 | [diff] [blame] | 833 | HistoryServiceFactory::GetForProfile(profile_, |
| 834 | Profile::EXPLICIT_ACCESS); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 835 | if (history) |
[email protected] | 323dbf7 | 2013-03-30 17:08:33 | [diff] [blame] | 836 | history->URLsNoLongerBookmarked(removed_bookmark_urls); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 837 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 838 | } |
| 839 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 840 | BookmarkNode* BookmarkModel::AddNode(BookmarkNode* parent, |
| 841 | int index, |
[email protected] | a42ec64a | 2012-12-20 17:04:24 | [diff] [blame] | 842 | BookmarkNode* node) { |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 843 | parent->Add(node, index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 844 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 845 | if (store_.get()) |
| 846 | store_->ScheduleSave(); |
| 847 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 848 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 849 | BookmarkNodeAdded(this, parent, index)); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 850 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 851 | index_->Add(node); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 852 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 853 | return node; |
| 854 | } |
| 855 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 856 | const BookmarkNode* BookmarkModel::GetNodeByID(const BookmarkNode* node, |
[email protected] | 66bb7dc | 2011-10-27 15:29:23 | [diff] [blame] | 857 | int64 id) const { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 858 | if (node->id() == id) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 859 | return node; |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 860 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 861 | for (int i = 0, child_count = node->child_count(); i < child_count; ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 862 | const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | if (result) |
| 864 | return result; |
| 865 | } |
| 866 | return NULL; |
| 867 | } |
| 868 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 869 | bool BookmarkModel::IsValidIndex(const BookmarkNode* parent, |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 870 | int index, |
| 871 | bool allow_end) { |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 872 | return (parent && parent->is_folder() && |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 873 | (index >= 0 && (index < parent->child_count() || |
| 874 | (allow_end && index == parent->child_count())))); |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 875 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 876 | |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 877 | BookmarkPermanentNode* BookmarkModel::CreatePermanentNode( |
| 878 | BookmarkNode::Type type) { |
[email protected] | e1f76c6 | 2011-06-30 20:15:39 | [diff] [blame] | 879 | DCHECK(type == BookmarkNode::BOOKMARK_BAR || |
| 880 | type == BookmarkNode::OTHER_NODE || |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 881 | type == BookmarkNode::MOBILE); |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 882 | BookmarkPermanentNode* node = |
| 883 | new BookmarkPermanentNode(generate_next_node_id()); |
| 884 | if (type == BookmarkNode::MOBILE) |
| 885 | node->set_visible(false); // Mobile node is initially hidden. |
| 886 | |
| 887 | int title_id; |
| 888 | switch (type) { |
| 889 | case BookmarkNode::BOOKMARK_BAR: |
| 890 | title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; |
| 891 | break; |
| 892 | case BookmarkNode::OTHER_NODE: |
| 893 | title_id = IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME; |
| 894 | break; |
| 895 | case BookmarkNode::MOBILE: |
| 896 | title_id = IDS_BOOKMARK_BAR_MOBILE_FOLDER_NAME; |
| 897 | break; |
| 898 | default: |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 899 | NOTREACHED(); |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 900 | title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; |
| 901 | break; |
[email protected] | 7caca8a2 | 2010-08-21 18:25:31 | [diff] [blame] | 902 | } |
[email protected] | 0491ff7 | 2011-12-30 00:45:59 | [diff] [blame] | 903 | node->SetTitle(l10n_util::GetStringUTF16(title_id)); |
[email protected] | 97fdd16 | 2011-12-03 20:50:12 | [diff] [blame] | 904 | node->set_type(type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 905 | return node; |
| 906 | } |
| 907 | |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 908 | void BookmarkModel::OnFaviconDataAvailable( |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 909 | BookmarkNode* node, |
[email protected] | 65baa22 | 2012-08-30 15:43:51 | [diff] [blame] | 910 | const history::FaviconImageResult& image_result) { |
[email protected] | 4167c3a | 2008-08-21 18:12:20 | [diff] [blame] | 911 | DCHECK(node); |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 912 | node->set_favicon_load_task_id(CancelableTaskTracker::kBadTaskId); |
[email protected] | bcc3861 | 2012-10-23 01:10:27 | [diff] [blame] | 913 | node->set_favicon_state(BookmarkNode::LOADED_FAVICON); |
[email protected] | 65baa22 | 2012-08-30 15:43:51 | [diff] [blame] | 914 | if (!image_result.image.IsEmpty()) { |
| 915 | node->set_favicon(image_result.image); |
[email protected] | 2ad0e87 | 2012-11-30 01:24:46 | [diff] [blame] | 916 | node->set_icon_url(image_result.icon_url); |
[email protected] | 65baa22 | 2012-08-30 15:43:51 | [diff] [blame] | 917 | FaviconLoaded(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 921 | void BookmarkModel::LoadFavicon(BookmarkNode* node) { |
[email protected] | 0890e60e | 2011-06-27 14:55:21 | [diff] [blame] | 922 | if (node->is_folder()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 923 | return; |
| 924 | |
[email protected] | 5d407754 | 2011-07-21 20:24:07 | [diff] [blame] | 925 | DCHECK(node->url().is_valid()); |
[email protected] | f3d2b31 | 2012-08-23 22:27:59 | [diff] [blame] | 926 | FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
| 927 | profile_, Profile::EXPLICIT_ACCESS); |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 928 | if (!favicon_service) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 929 | return; |
[email protected] | 65baa22 | 2012-08-30 15:43:51 | [diff] [blame] | 930 | FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 931 | FaviconService::FaviconForURLParams(profile_, |
| 932 | node->url(), |
| 933 | history::FAVICON, |
| 934 | gfx::kFaviconSize), |
[email protected] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 935 | base::Bind(&BookmarkModel::OnFaviconDataAvailable, |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 936 | base::Unretained(this), node), |
| 937 | &cancelable_task_tracker_); |
| 938 | node->set_favicon_load_task_id(handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 939 | } |
| 940 | |
[email protected] | 5b5c9b7f3 | 2011-07-21 01:07:18 | [diff] [blame] | 941 | void BookmarkModel::FaviconLoaded(const BookmarkNode* node) { |
| 942 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 943 | BookmarkNodeFaviconChanged(this, node)); |
| 944 | } |
| 945 | |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 946 | void BookmarkModel::CancelPendingFaviconLoadRequests(BookmarkNode* node) { |
[email protected] | 0ea3db5 | 2012-12-07 01:32:01 | [diff] [blame] | 947 | if (node->favicon_load_task_id() != CancelableTaskTracker::kBadTaskId) { |
| 948 | cancelable_task_tracker_.TryCancel(node->favicon_load_task_id()); |
| 949 | node->set_favicon_load_task_id(CancelableTaskTracker::kBadTaskId); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 953 | void BookmarkModel::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 954 | const content::NotificationSource& source, |
| 955 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 956 | switch (type) { |
| 957 | case chrome::NOTIFICATION_FAVICON_CHANGED: { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 958 | // Prevent the observers from getting confused for multiple favicon loads. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 959 | content::Details<history::FaviconChangeDetails> favicon_details(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 960 | for (std::set<GURL>::const_iterator i = favicon_details->urls.begin(); |
| 961 | i != favicon_details->urls.end(); ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 962 | std::vector<const BookmarkNode*> nodes; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 963 | GetNodesByURL(*i, &nodes); |
| 964 | for (size_t i = 0; i < nodes.size(); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 965 | // Got an updated favicon, for a URL, do a new request. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 966 | BookmarkNode* node = AsMutable(nodes[i]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 967 | node->InvalidateFavicon(); |
[email protected] | abc2f26 | 2011-03-15 21:15:44 | [diff] [blame] | 968 | CancelPendingFaviconLoadRequests(node); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 969 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
[email protected] | f12de830 | 2011-05-23 16:12:30 | [diff] [blame] | 970 | BookmarkNodeFaviconChanged(this, node)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | break; |
| 974 | } |
| 975 | |
| 976 | default: |
| 977 | NOTREACHED(); |
| 978 | break; |
| 979 | } |
| 980 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 981 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 982 | void BookmarkModel::PopulateNodesByURL(BookmarkNode* node) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 983 | // NOTE: this is called with url_lock_ already held. As such, this doesn't |
| 984 | // explicitly grab the lock. |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 985 | if (node->is_url()) |
| 986 | nodes_ordered_by_url_set_.insert(node); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 987 | for (int i = 0; i < node->child_count(); ++i) |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 988 | PopulateNodesByURL(node->GetChild(i)); |
| 989 | } |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 990 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 991 | int64 BookmarkModel::generate_next_node_id() { |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 992 | return next_node_id_++; |
| 993 | } |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 994 | |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 995 | BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
[email protected] | bc770a03 | 2011-12-12 17:35:30 | [diff] [blame] | 996 | BookmarkPermanentNode* bb_node = |
| 997 | CreatePermanentNode(BookmarkNode::BOOKMARK_BAR); |
| 998 | BookmarkPermanentNode* other_node = |
| 999 | CreatePermanentNode(BookmarkNode::OTHER_NODE); |
| 1000 | BookmarkPermanentNode* mobile_node = |
| 1001 | CreatePermanentNode(BookmarkNode::MOBILE); |
[email protected] | 37bc913 | 2011-12-01 22:29:29 | [diff] [blame] | 1002 | return new BookmarkLoadDetails(bb_node, other_node, mobile_node, |
[email protected] | 1858410f | 2012-10-26 05:06:45 | [diff] [blame] | 1003 | new BookmarkIndex(profile_), |
| 1004 | next_node_id_); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 1005 | } |