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