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