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