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