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