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