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