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