[email protected] | 6524385ef | 2010-08-18 06:34:13 | [diff] [blame] | 1 | // Copyright (c) 2010 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] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
[email protected] | 8891d14 | 2010-01-25 20:38:53 | [diff] [blame] | 8 | #include "app/l10n_util_collator.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 10 | #include "base/scoped_vector.h" |
[email protected] | fc3fc45 | 2009-02-10 03:25:40 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 12 | #include "chrome/browser/bookmarks/bookmark_index.h" |
[email protected] | 9333f18 | 2008-12-09 17:34:17 | [diff] [blame] | 13 | #include "chrome/browser/bookmarks/bookmark_utils.h" |
[email protected] | 68de8b7 | 2008-09-09 23:08:13 | [diff] [blame] | 14 | #include "chrome/browser/bookmarks/bookmark_storage.h" |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 16 | #include "chrome/browser/history/history_notifications.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 17 | #include "chrome/browser/pref_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "chrome/browser/profile.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 19 | #include "chrome/common/notification_service.h" |
[email protected] | 538ddce9 | 2010-03-18 04:28:06 | [diff] [blame] | 20 | #include "gfx/codec/png_codec.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 21 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 23 | using base::Time; |
| 24 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Helper to get a mutable bookmark node. |
| 28 | static BookmarkNode* AsMutable(const BookmarkNode* node) { |
| 29 | return const_cast<BookmarkNode*>(node); |
| 30 | } |
| 31 | |
| 32 | } // anonymous namespace |
| 33 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 34 | // BookmarkNode --------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 36 | BookmarkNode::BookmarkNode(const GURL& url) |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 37 | : url_(url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 38 | Initialize(0); |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 41 | BookmarkNode::BookmarkNode(int64 id, const GURL& url) |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 42 | : url_(url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 43 | Initialize(id); |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 46 | void BookmarkNode::Initialize(int64 id) { |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 47 | id_ = id; |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 48 | loaded_favicon_ = false; |
| 49 | favicon_load_handle_ = 0; |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 50 | type_ = !url_.is_empty() ? URL : BOOKMARK_BAR; |
[email protected] | 814a2d3 | 2009-04-30 23:09:01 | [diff] [blame] | 51 | date_added_ = Time::Now(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 54 | void BookmarkNode::Reset(const history::StarredEntry& entry) { |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 55 | DCHECK(entry.type != history::StarredEntry::URL || entry.url == url_); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 56 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | favicon_ = SkBitmap(); |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 58 | switch (entry.type) { |
| 59 | case history::StarredEntry::URL: |
| 60 | type_ = BookmarkNode::URL; |
| 61 | break; |
| 62 | case history::StarredEntry::USER_GROUP: |
| 63 | type_ = BookmarkNode::FOLDER; |
| 64 | break; |
| 65 | case history::StarredEntry::BOOKMARK_BAR: |
| 66 | type_ = BookmarkNode::BOOKMARK_BAR; |
| 67 | break; |
| 68 | case history::StarredEntry::OTHER: |
| 69 | type_ = BookmarkNode::OTHER_NODE; |
| 70 | break; |
| 71 | default: |
| 72 | NOTREACHED(); |
| 73 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | date_added_ = entry.date_added; |
| 75 | date_group_modified_ = entry.date_group_modified; |
| 76 | SetTitle(entry.title); |
| 77 | } |
| 78 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 79 | // BookmarkModel -------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 81 | namespace { |
| 82 | |
| 83 | // Comparator used when sorting bookmarks. Folders are sorted first, then |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 84 | // bookmarks. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 85 | class SortComparator : public std::binary_function<const BookmarkNode*, |
| 86 | const BookmarkNode*, |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 87 | bool> { |
| 88 | public: |
[email protected] | b5b2385a | 2009-08-18 05:12:29 | [diff] [blame] | 89 | explicit SortComparator(icu::Collator* collator) : collator_(collator) { } |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 90 | |
| 91 | // Returns true if lhs preceeds rhs. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 92 | bool operator() (const BookmarkNode* n1, const BookmarkNode* n2) { |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 93 | if (n1->type() == n2->type()) { |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 94 | // Types are the same, compare the names. |
| 95 | if (!collator_) |
| 96 | return n1->GetTitle() < n2->GetTitle(); |
| 97 | return l10n_util::CompareStringWithCollator(collator_, n1->GetTitle(), |
| 98 | n2->GetTitle()) == UCOL_LESS; |
| 99 | } |
| 100 | // Types differ, sort such that folders come first. |
| 101 | return n1->is_folder(); |
| 102 | } |
| 103 | |
| 104 | private: |
[email protected] | b5b2385a | 2009-08-18 05:12:29 | [diff] [blame] | 105 | icu::Collator* collator_; |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace |
| 109 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 110 | BookmarkModel::BookmarkModel(Profile* profile) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | : profile_(profile), |
| 112 | loaded_(false), |
[email protected] | fc7c36a2 | 2009-05-28 20:23:33 | [diff] [blame] | 113 | file_changed_(false), |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 114 | root_(GURL()), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | bookmark_bar_node_(NULL), |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 116 | other_node_(NULL), |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 117 | next_node_id_(1), |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 118 | observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 119 | loaded_signal_(TRUE, FALSE) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 120 | if (!profile_) { |
| 121 | // Profile is null during testing. |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 122 | DoneLoading(CreateLoadDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 126 | BookmarkModel::~BookmarkModel() { |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 127 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
[email protected] | 3de6fd34 | 2008-09-05 02:44:51 | [diff] [blame] | 128 | BookmarkModelBeingDeleted(this)); |
| 129 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 130 | if (store_) { |
| 131 | // The store maintains a reference back to us. We need to tell it we're gone |
| 132 | // so that it doesn't try and invoke a method back on us again. |
| 133 | store_->BookmarkModelDeleted(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 137 | void BookmarkModel::Load() { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 138 | if (store_.get()) { |
| 139 | // If the store is non-null, it means Load was already invoked. Load should |
| 140 | // only be invoked once. |
| 141 | NOTREACHED(); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // Listen for changes to favicons so that we can update the favicon of the |
| 146 | // node appropriately. |
[email protected] | 40e0486b | 2009-05-22 01:47:27 | [diff] [blame] | 147 | registrar_.Add(this, NotificationType::FAVICON_CHANGED, |
| 148 | Source<Profile>(profile_)); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 149 | |
| 150 | // Load the bookmarks. BookmarkStorage notifies us when done. |
| 151 | store_ = new BookmarkStorage(profile_, this); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 152 | store_->LoadBookmarks(CreateLoadDetails()); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 155 | const BookmarkNode* BookmarkModel::GetParentForNewNodes() { |
| 156 | std::vector<const BookmarkNode*> nodes = |
[email protected] | 9333f18 | 2008-12-09 17:34:17 | [diff] [blame] | 157 | bookmark_utils::GetMostRecentlyModifiedGroups(this, 1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | return nodes.empty() ? bookmark_bar_node_ : nodes[0]; |
| 159 | } |
| 160 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 161 | void BookmarkModel::Remove(const BookmarkNode* parent, int index) { |
| 162 | if (!loaded_ || !IsValidIndex(parent, index, false) || is_root(parent)) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 163 | NOTREACHED(); |
| 164 | return; |
| 165 | } |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 166 | RemoveAndDeleteNode(AsMutable(parent->GetChild(index))); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 169 | void BookmarkModel::Move(const BookmarkNode* node, |
| 170 | const BookmarkNode* new_parent, |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 171 | int index) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 172 | if (!loaded_ || !node || !IsValidIndex(new_parent, index, true) || |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 173 | is_root(new_parent) || is_permanent_node(node)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | NOTREACHED(); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | if (new_parent->HasAncestor(node)) { |
| 179 | // Can't make an ancestor of the node be a child of the node. |
| 180 | NOTREACHED(); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | SetDateGroupModified(new_parent, Time::Now()); |
| 185 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 186 | const BookmarkNode* old_parent = node->GetParent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | int old_index = old_parent->IndexOfChild(node); |
| 188 | |
| 189 | if (old_parent == new_parent && |
| 190 | (index == old_index || index == old_index + 1)) { |
| 191 | // Node is already in this position, nothing to do. |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (old_parent == new_parent && index > old_index) |
| 196 | index--; |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 197 | BookmarkNode* mutable_new_parent = AsMutable(new_parent); |
| 198 | mutable_new_parent->Add(index, AsMutable(node)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 200 | if (store_.get()) |
| 201 | store_->ScheduleSave(); |
| 202 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 203 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | BookmarkNodeMoved(this, old_parent, old_index, |
| 205 | new_parent, index)); |
| 206 | } |
| 207 | |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 208 | void BookmarkModel::Copy(const BookmarkNode* node, |
| 209 | const BookmarkNode* new_parent, |
| 210 | int index) { |
| 211 | if (!loaded_ || !node || !IsValidIndex(new_parent, index, true) || |
| 212 | is_root(new_parent) || is_permanent_node(node)) { |
| 213 | NOTREACHED(); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (new_parent->HasAncestor(node)) { |
| 218 | // Can't make an ancestor of the node be a child of the node. |
| 219 | NOTREACHED(); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | SetDateGroupModified(new_parent, Time::Now()); |
| 224 | BookmarkDragData drag_data_(node); |
| 225 | std::vector<BookmarkDragData::Element> elements(drag_data_.elements); |
[email protected] | 1a566fae | 2010-04-16 01:05:06 | [diff] [blame] | 226 | // CloneDragData will use BookmarkModel methods to do the job, so we |
| 227 | // don't need to send notifications here. |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 228 | bookmark_utils::CloneDragData(this, elements, new_parent, index); |
| 229 | |
| 230 | if (store_.get()) |
| 231 | store_->ScheduleSave(); |
[email protected] | 4e187ef65 | 2010-03-11 05:21:35 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 234 | const SkBitmap& BookmarkModel::GetFavIcon(const BookmarkNode* node) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 235 | DCHECK(node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 236 | if (!node->is_favicon_loaded()) { |
| 237 | BookmarkNode* mutable_node = AsMutable(node); |
| 238 | mutable_node->set_favicon_loaded(true); |
| 239 | LoadFavIcon(mutable_node); |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 240 | } |
| 241 | return node->favicon(); |
| 242 | } |
| 243 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 244 | void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 245 | if (!node) { |
| 246 | NOTREACHED(); |
| 247 | return; |
| 248 | } |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 249 | if (node->GetTitleAsString16() == title) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | return; |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 251 | |
[email protected] | baf4f92 | 2009-10-19 16:44:07 | [diff] [blame] | 252 | if (node == bookmark_bar_node_ || node == other_node_) { |
| 253 | NOTREACHED(); |
| 254 | return; |
| 255 | } |
| 256 | |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 257 | // The title index doesn't support changing the title, instead we remove then |
| 258 | // add it back. |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 259 | index_->Remove(node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 260 | AsMutable(node)->SetTitle(title); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 261 | index_->Add(node); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 262 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 263 | if (store_.get()) |
| 264 | store_->ScheduleSave(); |
| 265 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 266 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | BookmarkNodeChanged(this, node)); |
| 268 | } |
| 269 | |
[email protected] | e548660 | 2010-02-09 21:27:55 | [diff] [blame] | 270 | void BookmarkModel::SetURL(const BookmarkNode* node, const GURL& url) { |
| 271 | if (!node) { |
| 272 | NOTREACHED(); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // We cannot change the URL of a folder. |
| 277 | if (node->is_folder()) { |
| 278 | NOTREACHED(); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if (url == node->GetURL()) |
| 283 | return; |
| 284 | |
| 285 | AsMutable(node)->InvalidateFavicon(); |
| 286 | CancelPendingFavIconLoadRequests(AsMutable(node)); |
| 287 | |
| 288 | { |
| 289 | AutoLock url_lock(url_lock_); |
| 290 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find( |
| 291 | AsMutable(node)); |
| 292 | DCHECK(i != nodes_ordered_by_url_set_.end()); |
| 293 | // i points to the first node with the URL, advance until we find the |
| 294 | // node we're removing. |
| 295 | while (*i != node) |
| 296 | ++i; |
| 297 | nodes_ordered_by_url_set_.erase(i); |
| 298 | |
| 299 | AsMutable(node)->SetURL(url); |
| 300 | nodes_ordered_by_url_set_.insert(AsMutable(node)); |
| 301 | } |
| 302 | |
| 303 | if (store_.get()) |
| 304 | store_->ScheduleSave(); |
| 305 | |
| 306 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 307 | BookmarkNodeChanged(this, node)); |
| 308 | } |
| 309 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 310 | void BookmarkModel::GetNodesByURL(const GURL& url, |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 311 | std::vector<const BookmarkNode*>* nodes) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 312 | AutoLock url_lock(url_lock_); |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 313 | BookmarkNode tmp_node(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find(&tmp_node); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 315 | while (i != nodes_ordered_by_url_set_.end() && (*i)->GetURL() == url) { |
| 316 | nodes->push_back(*i); |
| 317 | ++i; |
| 318 | } |
| 319 | } |
| 320 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 321 | const BookmarkNode* BookmarkModel::GetMostRecentlyAddedNodeForURL( |
| 322 | const GURL& url) { |
| 323 | std::vector<const BookmarkNode*> nodes; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 324 | GetNodesByURL(url, &nodes); |
| 325 | if (nodes.empty()) |
| 326 | return NULL; |
| 327 | |
[email protected] | 9333f18 | 2008-12-09 17:34:17 | [diff] [blame] | 328 | std::sort(nodes.begin(), nodes.end(), &bookmark_utils::MoreRecentlyAdded); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 329 | return nodes.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 332 | void BookmarkModel::GetBookmarks(std::vector<GURL>* urls) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 333 | AutoLock url_lock(url_lock_); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 334 | const GURL* last_url = NULL; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 335 | for (NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.begin(); |
| 336 | i != nodes_ordered_by_url_set_.end(); ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 337 | const GURL* url = &((*i)->GetURL()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 338 | // Only add unique URLs. |
| 339 | if (!last_url || *url != *last_url) |
| 340 | urls->push_back(*url); |
| 341 | last_url = url; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 345 | bool BookmarkModel::IsBookmarked(const GURL& url) { |
| 346 | AutoLock url_lock(url_lock_); |
[email protected] | dddc1b4 | 2008-10-09 20:56:59 | [diff] [blame] | 347 | return IsBookmarkedNoLock(url); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 350 | const BookmarkNode* BookmarkModel::GetNodeByID(int64 id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 352 | return GetNodeByID(&root_, id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 355 | const BookmarkNode* BookmarkModel::AddGroup(const BookmarkNode* parent, |
| 356 | int index, |
| 357 | const string16& title) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 358 | if (!loaded_ || parent == &root_ || !IsValidIndex(parent, index, true)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | // Can't add to the root. |
| 360 | NOTREACHED(); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 364 | BookmarkNode* new_node = new BookmarkNode(generate_next_node_id(), |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 365 | GURL()); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 366 | new_node->set_date_group_modified(Time::Now()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | new_node->SetTitle(title); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 368 | new_node->set_type(BookmarkNode::FOLDER); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 370 | return AddNode(AsMutable(parent), index, new_node, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 373 | const BookmarkNode* BookmarkModel::AddURL(const BookmarkNode* parent, |
| 374 | int index, |
| 375 | const string16& title, |
| 376 | const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | return AddURLWithCreationTime(parent, index, title, url, Time::Now()); |
| 378 | } |
| 379 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 380 | const BookmarkNode* BookmarkModel::AddURLWithCreationTime( |
| 381 | const BookmarkNode* parent, |
| 382 | int index, |
| 383 | const string16& title, |
| 384 | const GURL& url, |
| 385 | const Time& creation_time) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 386 | if (!loaded_ || !url.is_valid() || is_root(parent) || |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 387 | !IsValidIndex(parent, index, true)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | NOTREACHED(); |
| 389 | return NULL; |
| 390 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 391 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 392 | bool was_bookmarked = IsBookmarked(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | |
| 394 | SetDateGroupModified(parent, creation_time); |
| 395 | |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 396 | BookmarkNode* new_node = new BookmarkNode(generate_next_node_id(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | new_node->SetTitle(title); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 398 | new_node->set_date_added(creation_time); |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 399 | new_node->set_type(BookmarkNode::URL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 401 | { |
| 402 | // Only hold the lock for the duration of the insert. |
| 403 | AutoLock url_lock(url_lock_); |
| 404 | nodes_ordered_by_url_set_.insert(new_node); |
| 405 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 406 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 407 | return AddNode(AsMutable(parent), index, new_node, was_bookmarked); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 410 | void BookmarkModel::SortChildren(const BookmarkNode* parent) { |
| 411 | if (!parent || !parent->is_folder() || is_root(parent) || |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 412 | parent->GetChildCount() <= 1) { |
| 413 | return; |
| 414 | } |
| 415 | |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 416 | UErrorCode error = U_ZERO_ERROR; |
[email protected] | b5b2385a | 2009-08-18 05:12:29 | [diff] [blame] | 417 | scoped_ptr<icu::Collator> collator( |
| 418 | icu::Collator::createInstance( |
| 419 | icu::Locale(g_browser_process->GetApplicationLocale().c_str()), |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 420 | error)); |
| 421 | if (U_FAILURE(error)) |
| 422 | collator.reset(NULL); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 423 | BookmarkNode* mutable_parent = AsMutable(parent); |
| 424 | std::sort(mutable_parent->children().begin(), |
| 425 | mutable_parent->children().end(), |
[email protected] | ef76264 | 2009-03-05 16:30:25 | [diff] [blame] | 426 | SortComparator(collator.get())); |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 427 | |
[email protected] | 997a036 | 2009-03-12 03:10:51 | [diff] [blame] | 428 | if (store_.get()) |
| 429 | store_->ScheduleSave(); |
| 430 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 431 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 432 | BookmarkNodeChildrenReordered(this, parent)); |
| 433 | } |
| 434 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 435 | void BookmarkModel::SetURLStarred(const GURL& url, |
| 436 | const string16& title, |
| 437 | bool is_starred) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 438 | std::vector<const BookmarkNode*> bookmarks; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 439 | GetNodesByURL(url, &bookmarks); |
| 440 | bool bookmarks_exist = !bookmarks.empty(); |
| 441 | if (is_starred == bookmarks_exist) |
| 442 | return; // Nothing to do, state already matches. |
| 443 | |
| 444 | if (is_starred) { |
| 445 | // Create a bookmark. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 446 | const BookmarkNode* parent = GetParentForNewNodes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | AddURL(parent, parent->GetChildCount(), title, url); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 448 | } else { |
| 449 | // Remove all the bookmarks. |
| 450 | for (size_t i = 0; i < bookmarks.size(); ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 451 | const BookmarkNode* node = bookmarks[i]; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 452 | Remove(node->GetParent(), node->GetParent()->IndexOfChild(node)); |
| 453 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
[email protected] | eea8fd553 | 2009-12-16 00:08:10 | [diff] [blame] | 457 | void BookmarkModel::SetDateGroupModified(const BookmarkNode* parent, |
| 458 | const Time time) { |
| 459 | DCHECK(parent); |
| 460 | AsMutable(parent)->set_date_group_modified(time); |
| 461 | |
| 462 | if (store_.get()) |
| 463 | store_->ScheduleSave(); |
| 464 | } |
| 465 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 466 | void BookmarkModel::ResetDateGroupModified(const BookmarkNode* node) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | SetDateGroupModified(node, Time()); |
| 468 | } |
| 469 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 470 | void BookmarkModel::GetBookmarksWithTitlesMatching( |
| 471 | const string16& text, |
| 472 | size_t max_count, |
| 473 | std::vector<bookmark_utils::TitleMatch>* matches) { |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 474 | if (!loaded_) |
| 475 | return; |
| 476 | |
[email protected] | 8141522 | 2010-08-18 16:17:20 | [diff] [blame] | 477 | index_->GetBookmarksWithTitlesMatching(UTF16ToWideHack(text), max_count, |
| 478 | matches); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 479 | } |
| 480 | |
[email protected] | 9876bb1c | 2008-12-16 20:42:25 | [diff] [blame] | 481 | void BookmarkModel::ClearStore() { |
[email protected] | 40e0486b | 2009-05-22 01:47:27 | [diff] [blame] | 482 | registrar_.RemoveAll(); |
[email protected] | 9876bb1c | 2008-12-16 20:42:25 | [diff] [blame] | 483 | store_ = NULL; |
| 484 | } |
| 485 | |
[email protected] | dddc1b4 | 2008-10-09 20:56:59 | [diff] [blame] | 486 | bool BookmarkModel::IsBookmarkedNoLock(const GURL& url) { |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 487 | BookmarkNode tmp_node(url); |
[email protected] | dddc1b4 | 2008-10-09 20:56:59 | [diff] [blame] | 488 | return (nodes_ordered_by_url_set_.find(&tmp_node) != |
| 489 | nodes_ordered_by_url_set_.end()); |
| 490 | } |
| 491 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 492 | void BookmarkModel::FavIconLoaded(const BookmarkNode* node) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 493 | // Send out notification to the observer. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 494 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | BookmarkNodeFavIconLoaded(this, node)); |
| 496 | } |
| 497 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 498 | void BookmarkModel::RemoveNode(BookmarkNode* node, |
| 499 | std::set<GURL>* removed_urls) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 500 | if (!loaded_ || !node || is_permanent_node(node)) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 501 | NOTREACHED(); |
| 502 | return; |
| 503 | } |
| 504 | |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 505 | if (node->type() == BookmarkNode::URL) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 506 | // NOTE: this is called in such a way that url_lock_ is already held. As |
| 507 | // such, this doesn't explicitly grab the lock. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 508 | NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.find(node); |
| 509 | DCHECK(i != nodes_ordered_by_url_set_.end()); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 510 | // i points to the first node with the URL, advance until we find the |
| 511 | // node we're removing. |
| 512 | while (*i != node) |
| 513 | ++i; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | nodes_ordered_by_url_set_.erase(i); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 515 | removed_urls->insert(node->GetURL()); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 516 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 517 | index_->Remove(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | CancelPendingFavIconLoadRequests(node); |
| 521 | |
| 522 | // Recurse through children. |
| 523 | for (int i = node->GetChildCount() - 1; i >= 0; --i) |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 524 | RemoveNode(node->GetChild(i), removed_urls); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | } |
| 526 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 527 | void BookmarkModel::DoneLoading( |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 528 | BookmarkLoadDetails* details_delete_me) { |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 529 | DCHECK(details_delete_me); |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 530 | scoped_ptr<BookmarkLoadDetails> details(details_delete_me); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 531 | if (loaded_) { |
| 532 | // We should only ever be loaded once. |
| 533 | NOTREACHED(); |
| 534 | return; |
| 535 | } |
| 536 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 537 | next_node_id_ = details->max_id(); |
[email protected] | fc7c36a2 | 2009-05-28 20:23:33 | [diff] [blame] | 538 | if (details->computed_checksum() != details->stored_checksum()) |
| 539 | SetFileChanged(); |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 540 | if (details->computed_checksum() != details->stored_checksum() || |
| 541 | details->ids_reassigned()) { |
| 542 | // If bookmarks file changed externally, the IDs may have changed |
| 543 | // externally. In that case, the decoder may have reassigned IDs to make |
| 544 | // them unique. So when the file has changed externally, we should save the |
| 545 | // bookmarks file to persist new IDs. |
| 546 | if (store_.get()) |
| 547 | store_->ScheduleSave(); |
| 548 | } |
[email protected] | d22d873 | 2010-05-04 19:24:42 | [diff] [blame] | 549 | bookmark_bar_node_ = details->release_bb_node(); |
| 550 | other_node_ = details->release_other_folder_node(); |
| 551 | index_.reset(details->release_index()); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 552 | |
| 553 | // WARNING: order is important here, various places assume bookmark bar then |
| 554 | // other node. |
| 555 | root_.Add(0, bookmark_bar_node_); |
| 556 | root_.Add(1, other_node_); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 557 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 558 | { |
| 559 | AutoLock url_lock(url_lock_); |
| 560 | // Update nodes_ordered_by_url_set_ from the nodes. |
| 561 | PopulateNodesByURL(&root_); |
| 562 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 563 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 564 | loaded_ = true; |
| 565 | |
[email protected] | cbcd641 | 2009-03-09 22:31:39 | [diff] [blame] | 566 | loaded_signal_.Signal(); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 567 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 568 | // Notify our direct observers. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 569 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, Loaded(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 570 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 571 | // And generic notification. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 572 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 573 | NotificationType::BOOKMARK_MODEL_LOADED, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | Source<Profile>(profile_), |
| 575 | NotificationService::NoDetails()); |
| 576 | } |
| 577 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 578 | void BookmarkModel::RemoveAndDeleteNode(BookmarkNode* delete_me) { |
| 579 | scoped_ptr<BookmarkNode> node(delete_me); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 580 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 581 | BookmarkNode* parent = AsMutable(node->GetParent()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | DCHECK(parent); |
| 583 | int index = parent->IndexOfChild(node.get()); |
| 584 | parent->Remove(index); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 585 | history::URLsStarredDetails details(false); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 586 | { |
| 587 | AutoLock url_lock(url_lock_); |
| 588 | RemoveNode(node.get(), &details.changed_urls); |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 589 | |
| 590 | // RemoveNode adds an entry to changed_urls for each node of type URL. As we |
| 591 | // allow duplicates we need to remove any entries that are still bookmarked. |
| 592 | for (std::set<GURL>::iterator i = details.changed_urls.begin(); |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 593 | i != details.changed_urls.end(); ) { |
[email protected] | fc3fc45 | 2009-02-10 03:25:40 | [diff] [blame] | 594 | if (IsBookmarkedNoLock(*i)) { |
| 595 | // When we erase the iterator pointing at the erasee is |
| 596 | // invalidated, so using i++ here within the "erase" call is |
| 597 | // important as it advances the iterator before passing the |
| 598 | // old value through to erase. |
| 599 | details.changed_urls.erase(i++); |
| 600 | } else { |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 601 | ++i; |
[email protected] | fc3fc45 | 2009-02-10 03:25:40 | [diff] [blame] | 602 | } |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 603 | } |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 604 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 605 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 606 | if (store_.get()) |
| 607 | store_->ScheduleSave(); |
| 608 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 609 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 610 | BookmarkNodeRemoved(this, parent, index, node.get())); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 611 | |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 612 | if (details.changed_urls.empty()) { |
| 613 | // No point in sending out notification if the starred state didn't change. |
| 614 | return; |
| 615 | } |
| 616 | |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 617 | if (profile_) { |
| 618 | HistoryService* history = |
| 619 | profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 620 | if (history) |
| 621 | history->URLsNoLongerBookmarked(details.changed_urls); |
| 622 | } |
| 623 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 624 | NotificationService::current()->Notify( |
| 625 | NotificationType::URLS_STARRED, |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 626 | Source<Profile>(profile_), |
| 627 | Details<history::URLsStarredDetails>(&details)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | cb6cf79 | 2010-01-28 00:04:56 | [diff] [blame] | 630 | void BookmarkModel::BeginImportMode() { |
| 631 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 632 | BookmarkImportBeginning(this)); |
| 633 | } |
| 634 | |
| 635 | void BookmarkModel::EndImportMode() { |
| 636 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 637 | BookmarkImportEnding(this)); |
| 638 | } |
| 639 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 640 | BookmarkNode* BookmarkModel::AddNode(BookmarkNode* parent, |
| 641 | int index, |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 642 | BookmarkNode* node, |
| 643 | bool was_bookmarked) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 644 | parent->Add(index, node); |
| 645 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 646 | if (store_.get()) |
| 647 | store_->ScheduleSave(); |
| 648 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 649 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 650 | BookmarkNodeAdded(this, parent, index)); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 651 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 652 | index_->Add(node); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 653 | |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 654 | if (node->type() == BookmarkNode::URL && !was_bookmarked) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 655 | history::URLsStarredDetails details(true); |
| 656 | details.changed_urls.insert(node->GetURL()); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 657 | NotificationService::current()->Notify( |
| 658 | NotificationType::URLS_STARRED, |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 659 | Source<Profile>(profile_), |
| 660 | Details<history::URLsStarredDetails>(&details)); |
| 661 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 662 | return node; |
| 663 | } |
| 664 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 665 | void BookmarkModel::BlockTillLoaded() { |
[email protected] | cbcd641 | 2009-03-09 22:31:39 | [diff] [blame] | 666 | loaded_signal_.Wait(); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 667 | } |
| 668 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 669 | const BookmarkNode* BookmarkModel::GetNodeByID(const BookmarkNode* node, |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 670 | int64 id) { |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 671 | if (node->id() == id) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 672 | return node; |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 673 | |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 674 | for (int i = 0, child_count = node->GetChildCount(); i < child_count; ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 675 | const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 676 | if (result) |
| 677 | return result; |
| 678 | } |
| 679 | return NULL; |
| 680 | } |
| 681 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 682 | bool BookmarkModel::IsValidIndex(const BookmarkNode* parent, |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 683 | int index, |
| 684 | bool allow_end) { |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 685 | return (parent && parent->is_folder() && |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 686 | (index >= 0 && (index < parent->GetChildCount() || |
| 687 | (allow_end && index == parent->GetChildCount())))); |
[email protected] | bd1b9670 | 2009-07-08 21:54:14 | [diff] [blame] | 688 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 689 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 690 | BookmarkNode* BookmarkModel::CreateBookmarkNode() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | history::StarredEntry entry; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 692 | entry.type = history::StarredEntry::BOOKMARK_BAR; |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 693 | return CreateRootNodeFromStarredEntry(entry); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 694 | } |
| 695 | |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 696 | BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 697 | history::StarredEntry entry; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | entry.type = history::StarredEntry::OTHER; |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 699 | return CreateRootNodeFromStarredEntry(entry); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 700 | } |
| 701 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 702 | BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 703 | const history::StarredEntry& entry) { |
| 704 | DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || |
| 705 | entry.type == history::StarredEntry::OTHER); |
[email protected] | ea2e5aa5 | 2009-05-20 18:01:28 | [diff] [blame] | 706 | BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 707 | node->Reset(entry); |
[email protected] | 7caca8a2 | 2010-08-21 18:25:31 | [diff] [blame^] | 708 | if (entry.type == history::StarredEntry::BOOKMARK_BAR) { |
| 709 | node->SetTitle(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); |
| 710 | } else { |
| 711 | node->SetTitle( |
| 712 | l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); |
| 713 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 714 | return node; |
| 715 | } |
| 716 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 717 | void BookmarkModel::OnFavIconDataAvailable( |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 718 | FaviconService::Handle handle, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 719 | bool know_favicon, |
[email protected] | 790879f7 | 2010-03-17 20:19:57 | [diff] [blame] | 720 | scoped_refptr<RefCountedMemory> data, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 721 | bool expired, |
[email protected] | 3e377c5 | 2009-08-06 07:46:37 | [diff] [blame] | 722 | GURL icon_url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 723 | SkBitmap fav_icon; |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 724 | BookmarkNode* node = |
[email protected] | 4167c3a | 2008-08-21 18:12:20 | [diff] [blame] | 725 | load_consumer_.GetClientData( |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 726 | profile_->GetFaviconService(Profile::EXPLICIT_ACCESS), handle); |
[email protected] | 4167c3a | 2008-08-21 18:12:20 | [diff] [blame] | 727 | DCHECK(node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 728 | node->set_favicon_load_handle(0); |
[email protected] | 7dc7f03 | 2009-10-27 06:09:33 | [diff] [blame] | 729 | if (know_favicon && data.get() && data->size() && |
[email protected] | 52d70a7c | 2009-10-22 17:36:17 | [diff] [blame] | 730 | gfx::PNGCodec::Decode(data->front(), data->size(), &fav_icon)) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 731 | node->set_favicon(fav_icon); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 732 | FavIconLoaded(node); |
| 733 | } |
| 734 | } |
| 735 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 736 | void BookmarkModel::LoadFavIcon(BookmarkNode* node) { |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 737 | if (node->type() != BookmarkNode::URL) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 738 | return; |
| 739 | |
| 740 | DCHECK(node->GetURL().is_valid()); |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 741 | FaviconService* favicon_service = |
| 742 | profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 743 | if (!favicon_service) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 744 | return; |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 745 | FaviconService::Handle handle = favicon_service->GetFaviconForURL( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 746 | node->GetURL(), &load_consumer_, |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 747 | NewCallback(this, &BookmarkModel::OnFavIconDataAvailable)); |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 748 | load_consumer_.SetClientData(favicon_service, handle, node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 749 | node->set_favicon_load_handle(handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 752 | void BookmarkModel::CancelPendingFavIconLoadRequests(BookmarkNode* node) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 753 | if (node->favicon_load_handle()) { |
[email protected] | 0189bc72 | 2009-08-28 21:56:48 | [diff] [blame] | 754 | FaviconService* favicon_service = |
| 755 | profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 756 | if (favicon_service) |
| 757 | favicon_service->CancelRequest(node->favicon_load_handle()); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 758 | node->set_favicon_load_handle(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 762 | void BookmarkModel::Observe(NotificationType type, |
| 763 | const NotificationSource& source, |
| 764 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 765 | switch (type.value) { |
| 766 | case NotificationType::FAVICON_CHANGED: { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 767 | // Prevent the observers from getting confused for multiple favicon loads. |
| 768 | Details<history::FavIconChangeDetails> favicon_details(details); |
| 769 | for (std::set<GURL>::const_iterator i = favicon_details->urls.begin(); |
| 770 | i != favicon_details->urls.end(); ++i) { |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 771 | std::vector<const BookmarkNode*> nodes; |
[email protected] | 848cd05e | 2008-09-19 18:33:48 | [diff] [blame] | 772 | GetNodesByURL(*i, &nodes); |
| 773 | for (size_t i = 0; i < nodes.size(); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 774 | // Got an updated favicon, for a URL, do a new request. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 775 | BookmarkNode* node = AsMutable(nodes[i]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 776 | node->InvalidateFavicon(); |
| 777 | CancelPendingFavIconLoadRequests(node); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 778 | FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 779 | BookmarkNodeChanged(this, node)); |
| 780 | } |
| 781 | } |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | default: |
| 786 | NOTREACHED(); |
| 787 | break; |
| 788 | } |
| 789 | } |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 790 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 791 | void BookmarkModel::PopulateNodesByURL(BookmarkNode* node) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 792 | // NOTE: this is called with url_lock_ already held. As such, this doesn't |
| 793 | // explicitly grab the lock. |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 794 | if (node->is_url()) |
| 795 | nodes_ordered_by_url_set_.insert(node); |
| 796 | for (int i = 0; i < node->GetChildCount(); ++i) |
| 797 | PopulateNodesByURL(node->GetChild(i)); |
| 798 | } |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 799 | |
[email protected] | 367d707 | 2009-07-13 23:27:13 | [diff] [blame] | 800 | int64 BookmarkModel::generate_next_node_id() { |
[email protected] | 4d89f38 | 2009-05-12 06:56:49 | [diff] [blame] | 801 | return next_node_id_++; |
| 802 | } |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 803 | |
[email protected] | fc7c36a2 | 2009-05-28 20:23:33 | [diff] [blame] | 804 | void BookmarkModel::SetFileChanged() { |
| 805 | file_changed_ = true; |
[email protected] | fc7c36a2 | 2009-05-28 20:23:33 | [diff] [blame] | 806 | } |
| 807 | |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 808 | BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 809 | BookmarkNode* bb_node = CreateBookmarkNode(); |
| 810 | BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 811 | return new BookmarkLoadDetails( |
[email protected] | 2c685cc2 | 2009-08-28 00:17:44 | [diff] [blame] | 812 | bb_node, other_folder_node, new BookmarkIndex(profile()), next_node_id_); |
[email protected] | 01eec88 | 2009-05-22 18:13:28 | [diff] [blame] | 813 | } |