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