| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/bookmarks/core/browser/bookmark_utils.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/i18n/case_conversion.h" | |
| 12 #include "base/i18n/string_search.h" | |
| 13 #include "base/metrics/user_metrics_action.h" | |
| 14 #include "base/prefs/pref_service.h" | |
| 15 #include "base/strings/string16.h" | |
| 16 #include "base/strings/utf_string_conversions.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "components/bookmarks/core/browser/bookmark_model.h" | |
| 19 #include "components/bookmarks/core/browser/scoped_group_bookmark_actions.h" | |
| 20 #include "components/bookmarks/core/common/bookmark_pref_names.h" | |
| 21 #include "components/query_parser/query_parser.h" | |
| 22 #include "components/user_prefs/pref_registry_syncable.h" | |
| 23 #include "net/base/net_util.h" | |
| 24 #include "ui/base/models/tree_node_iterator.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 using base::Time; | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // The maximum length of URL or title returned by the Cleanup functions. | |
| 32 const size_t kCleanedUpUrlMaxLength = 1024u; | |
| 33 const size_t kCleanedUpTitleMaxLength = 1024u; | |
| 34 | |
| 35 void CloneBookmarkNodeImpl(BookmarkModel* model, | |
| 36 const BookmarkNodeData::Element& element, | |
| 37 const BookmarkNode* parent, | |
| 38 int index_to_add_at, | |
| 39 bool reset_node_times) { | |
| 40 if (element.is_url) { | |
| 41 base::Time date_added = reset_node_times ? Time::Now() : element.date_added; | |
| 42 DCHECK(!date_added.is_null()); | |
| 43 | |
| 44 model->AddURLWithCreationTimeAndMetaInfo(parent, | |
| 45 index_to_add_at, | |
| 46 element.title, | |
| 47 element.url, | |
| 48 date_added, | |
| 49 &element.meta_info_map); | |
| 50 } else { | |
| 51 const BookmarkNode* cloned_node = model->AddFolderWithMetaInfo( | |
| 52 parent, index_to_add_at, element.title, &element.meta_info_map); | |
| 53 if (!reset_node_times) { | |
| 54 DCHECK(!element.date_folder_modified.is_null()); | |
| 55 model->SetDateFolderModified(cloned_node, element.date_folder_modified); | |
| 56 } | |
| 57 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | |
| 58 CloneBookmarkNodeImpl(model, element.children[i], cloned_node, i, | |
| 59 reset_node_times); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // Comparison function that compares based on date modified of the two nodes. | |
| 64 bool MoreRecentlyModified(const BookmarkNode* n1, const BookmarkNode* n2) { | |
| 65 return n1->date_folder_modified() > n2->date_folder_modified(); | |
| 66 } | |
| 67 | |
| 68 // Returns true if |text| contains each string in |words|. This is used when | |
| 69 // searching for bookmarks. | |
| 70 bool DoesBookmarkTextContainWords(const base::string16& text, | |
| 71 const std::vector<base::string16>& words) { | |
| 72 for (size_t i = 0; i < words.size(); ++i) { | |
| 73 if (!base::i18n::StringSearchIgnoringCaseAndAccents( | |
| 74 words[i], text, NULL, NULL)) { | |
| 75 return false; | |
| 76 } | |
| 77 } | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 // Returns true if |node|s title or url contains the strings in |words|. | |
| 82 // |languages| argument is user's accept-language setting to decode IDN. | |
| 83 bool DoesBookmarkContainWords(const BookmarkNode* node, | |
| 84 const std::vector<base::string16>& words, | |
| 85 const std::string& languages) { | |
| 86 return | |
| 87 DoesBookmarkTextContainWords(node->GetTitle(), words) || | |
| 88 DoesBookmarkTextContainWords( | |
| 89 base::UTF8ToUTF16(node->url().spec()), words) || | |
| 90 DoesBookmarkTextContainWords(net::FormatUrl( | |
| 91 node->url(), languages, net::kFormatUrlOmitNothing, | |
| 92 net::UnescapeRule::NORMAL, NULL, NULL, NULL), words); | |
| 93 } | |
| 94 | |
| 95 // This is used with a tree iterator to skip subtrees which are not visible. | |
| 96 bool PruneInvisibleFolders(const BookmarkNode* node) { | |
| 97 return !node->IsVisible(); | |
| 98 } | |
| 99 | |
| 100 // This traces parents up to root, determines if node is contained in a | |
| 101 // selected folder. | |
| 102 bool HasSelectedAncestor(BookmarkModel* model, | |
| 103 const std::vector<const BookmarkNode*>& selected_nodes, | |
| 104 const BookmarkNode* node) { | |
| 105 if (!node || model->is_permanent_node(node)) | |
| 106 return false; | |
| 107 | |
| 108 for (size_t i = 0; i < selected_nodes.size(); ++i) | |
| 109 if (node->id() == selected_nodes[i]->id()) | |
| 110 return true; | |
| 111 | |
| 112 return HasSelectedAncestor(model, selected_nodes, node->parent()); | |
| 113 } | |
| 114 | |
| 115 const BookmarkNode* GetNodeByID(const BookmarkNode* node, int64 id) { | |
| 116 if (node->id() == id) | |
| 117 return node; | |
| 118 | |
| 119 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) { | |
| 120 const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); | |
| 121 if (result) | |
| 122 return result; | |
| 123 } | |
| 124 return NULL; | |
| 125 } | |
| 126 | |
| 127 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL | |
| 128 // from being in the middle of an escape sequence) to no more than | |
| 129 // kCleanedUpUrlMaxLength characters, returning the result. | |
| 130 std::string TruncateUrl(const std::string& url) { | |
| 131 if (url.length() <= kCleanedUpUrlMaxLength) | |
| 132 return url; | |
| 133 | |
| 134 // If we're in the middle of an escape sequence, truncate just before it. | |
| 135 if (url[kCleanedUpUrlMaxLength - 1] == '%') | |
| 136 return url.substr(0, kCleanedUpUrlMaxLength - 1); | |
| 137 if (url[kCleanedUpUrlMaxLength - 2] == '%') | |
| 138 return url.substr(0, kCleanedUpUrlMaxLength - 2); | |
| 139 | |
| 140 return url.substr(0, kCleanedUpUrlMaxLength); | |
| 141 } | |
| 142 | |
| 143 } // namespace | |
| 144 | |
| 145 namespace bookmark_utils { | |
| 146 | |
| 147 QueryFields::QueryFields() {} | |
| 148 QueryFields::~QueryFields() {} | |
| 149 | |
| 150 void CloneBookmarkNode(BookmarkModel* model, | |
| 151 const std::vector<BookmarkNodeData::Element>& elements, | |
| 152 const BookmarkNode* parent, | |
| 153 int index_to_add_at, | |
| 154 bool reset_node_times) { | |
| 155 if (!parent->is_folder() || !model) { | |
| 156 NOTREACHED(); | |
| 157 return; | |
| 158 } | |
| 159 for (size_t i = 0; i < elements.size(); ++i) { | |
| 160 CloneBookmarkNodeImpl(model, elements[i], parent, | |
| 161 index_to_add_at + static_cast<int>(i), | |
| 162 reset_node_times); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 void CopyToClipboard(BookmarkModel* model, | |
| 167 const std::vector<const BookmarkNode*>& nodes, | |
| 168 bool remove_nodes) { | |
| 169 if (nodes.empty()) | |
| 170 return; | |
| 171 | |
| 172 // Create array of selected nodes with descendants filtered out. | |
| 173 std::vector<const BookmarkNode*> filtered_nodes; | |
| 174 for (size_t i = 0; i < nodes.size(); ++i) | |
| 175 if (!HasSelectedAncestor(model, nodes, nodes[i]->parent())) | |
| 176 filtered_nodes.push_back(nodes[i]); | |
| 177 | |
| 178 BookmarkNodeData(filtered_nodes). | |
| 179 WriteToClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
| 180 | |
| 181 if (remove_nodes) { | |
| 182 ScopedGroupBookmarkActions group_cut(model); | |
| 183 for (size_t i = 0; i < filtered_nodes.size(); ++i) { | |
| 184 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); | |
| 185 if (index > -1) | |
| 186 model->Remove(filtered_nodes[i]->parent(), index); | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 void PasteFromClipboard(BookmarkModel* model, | |
| 192 const BookmarkNode* parent, | |
| 193 int index) { | |
| 194 if (!parent) | |
| 195 return; | |
| 196 | |
| 197 BookmarkNodeData bookmark_data; | |
| 198 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) | |
| 199 return; | |
| 200 | |
| 201 if (index == -1) | |
| 202 index = parent->child_count(); | |
| 203 ScopedGroupBookmarkActions group_paste(model); | |
| 204 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | |
| 205 } | |
| 206 | |
| 207 bool CanPasteFromClipboard(const BookmarkNode* node) { | |
| 208 if (!node) | |
| 209 return false; | |
| 210 return BookmarkNodeData::ClipboardContainsBookmarks(); | |
| 211 } | |
| 212 | |
| 213 std::vector<const BookmarkNode*> GetMostRecentlyModifiedFolders( | |
| 214 BookmarkModel* model, | |
| 215 size_t max_count) { | |
| 216 std::vector<const BookmarkNode*> nodes; | |
| 217 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(), | |
| 218 PruneInvisibleFolders); | |
| 219 | |
| 220 while (iterator.has_next()) { | |
| 221 const BookmarkNode* parent = iterator.Next(); | |
| 222 if (parent->is_folder() && parent->date_folder_modified() > base::Time()) { | |
| 223 if (max_count == 0) { | |
| 224 nodes.push_back(parent); | |
| 225 } else { | |
| 226 std::vector<const BookmarkNode*>::iterator i = | |
| 227 std::upper_bound(nodes.begin(), nodes.end(), parent, | |
| 228 &MoreRecentlyModified); | |
| 229 if (nodes.size() < max_count || i != nodes.end()) { | |
| 230 nodes.insert(i, parent); | |
| 231 while (nodes.size() > max_count) | |
| 232 nodes.pop_back(); | |
| 233 } | |
| 234 } | |
| 235 } // else case, the root node, which we don't care about or imported nodes | |
| 236 // (which have a time of 0). | |
| 237 } | |
| 238 | |
| 239 if (nodes.size() < max_count) { | |
| 240 // Add the permanent nodes if there is space. The permanent nodes are the | |
| 241 // only children of the root_node. | |
| 242 const BookmarkNode* root_node = model->root_node(); | |
| 243 | |
| 244 for (int i = 0; i < root_node->child_count(); ++i) { | |
| 245 const BookmarkNode* node = root_node->GetChild(i); | |
| 246 if (node->IsVisible() && | |
| 247 std::find(nodes.begin(), nodes.end(), node) == nodes.end()) { | |
| 248 nodes.push_back(node); | |
| 249 | |
| 250 if (nodes.size() == max_count) | |
| 251 break; | |
| 252 } | |
| 253 } | |
| 254 } | |
| 255 return nodes; | |
| 256 } | |
| 257 | |
| 258 void GetMostRecentlyAddedEntries(BookmarkModel* model, | |
| 259 size_t count, | |
| 260 std::vector<const BookmarkNode*>* nodes) { | |
| 261 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | |
| 262 while (iterator.has_next()) { | |
| 263 const BookmarkNode* node = iterator.Next(); | |
| 264 if (node->is_url()) { | |
| 265 std::vector<const BookmarkNode*>::iterator insert_position = | |
| 266 std::upper_bound(nodes->begin(), nodes->end(), node, | |
| 267 &MoreRecentlyAdded); | |
| 268 if (nodes->size() < count || insert_position != nodes->end()) { | |
| 269 nodes->insert(insert_position, node); | |
| 270 while (nodes->size() > count) | |
| 271 nodes->pop_back(); | |
| 272 } | |
| 273 } | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2) { | |
| 278 return n1->date_added() > n2->date_added(); | |
| 279 } | |
| 280 | |
| 281 void GetBookmarksMatchingProperties(BookmarkModel* model, | |
| 282 const QueryFields& query, | |
| 283 size_t max_count, | |
| 284 const std::string& languages, | |
| 285 std::vector<const BookmarkNode*>* nodes) { | |
| 286 std::vector<base::string16> query_words; | |
| 287 query_parser::QueryParser parser; | |
| 288 if (query.word_phrase_query) { | |
| 289 parser.ParseQueryWords(base::i18n::ToLower(*query.word_phrase_query), | |
| 290 &query_words); | |
| 291 if (query_words.empty()) | |
| 292 return; | |
| 293 } | |
| 294 | |
| 295 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | |
| 296 while (iterator.has_next()) { | |
| 297 const BookmarkNode* node = iterator.Next(); | |
| 298 if ((!query_words.empty() && | |
| 299 !DoesBookmarkContainWords(node, query_words, languages)) || | |
| 300 model->is_permanent_node(node)) { | |
| 301 continue; | |
| 302 } | |
| 303 if (query.url) { | |
| 304 // Check against bare url spec and IDN-decoded url. | |
| 305 if (!node->is_url() || | |
| 306 !(base::UTF8ToUTF16(node->url().spec()) == *query.url || | |
| 307 net::FormatUrl( | |
| 308 node->url(), languages, net::kFormatUrlOmitNothing, | |
| 309 net::UnescapeRule::NORMAL, NULL, NULL, NULL) == *query.url)) { | |
| 310 continue; | |
| 311 } | |
| 312 } | |
| 313 if (query.title && node->GetTitle() != *query.title) | |
| 314 continue; | |
| 315 | |
| 316 nodes->push_back(node); | |
| 317 if (nodes->size() == max_count) | |
| 318 return; | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | |
| 323 registry->RegisterBooleanPref( | |
| 324 prefs::kShowBookmarkBar, | |
| 325 false, | |
| 326 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 327 registry->RegisterBooleanPref( | |
| 328 prefs::kEditBookmarksEnabled, | |
| 329 true, | |
| 330 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 331 registry->RegisterBooleanPref( | |
| 332 prefs::kShowAppsShortcutInBookmarkBar, | |
| 333 true, | |
| 334 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 335 } | |
| 336 | |
| 337 const BookmarkNode* GetParentForNewNodes( | |
| 338 const BookmarkNode* parent, | |
| 339 const std::vector<const BookmarkNode*>& selection, | |
| 340 int* index) { | |
| 341 const BookmarkNode* real_parent = parent; | |
| 342 | |
| 343 if (selection.size() == 1 && selection[0]->is_folder()) | |
| 344 real_parent = selection[0]; | |
| 345 | |
| 346 if (index) { | |
| 347 if (selection.size() == 1 && selection[0]->is_url()) { | |
| 348 *index = real_parent->GetIndexOf(selection[0]) + 1; | |
| 349 if (*index == 0) { | |
| 350 // Node doesn't exist in parent, add to end. | |
| 351 NOTREACHED(); | |
| 352 *index = real_parent->child_count(); | |
| 353 } | |
| 354 } else { | |
| 355 *index = real_parent->child_count(); | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 return real_parent; | |
| 360 } | |
| 361 | |
| 362 void DeleteBookmarkFolders(BookmarkModel* model, | |
| 363 const std::vector<int64>& ids) { | |
| 364 // Remove the folders that were removed. This has to be done after all the | |
| 365 // other changes have been committed. | |
| 366 for (std::vector<int64>::const_iterator iter = ids.begin(); | |
| 367 iter != ids.end(); | |
| 368 ++iter) { | |
| 369 const BookmarkNode* node = GetBookmarkNodeByID(model, *iter); | |
| 370 if (!node) | |
| 371 continue; | |
| 372 const BookmarkNode* parent = node->parent(); | |
| 373 model->Remove(parent, parent->GetIndexOf(node)); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 void AddIfNotBookmarked(BookmarkModel* model, | |
| 378 const GURL& url, | |
| 379 const base::string16& title) { | |
| 380 std::vector<const BookmarkNode*> bookmarks; | |
| 381 model->GetNodesByURL(url, &bookmarks); | |
| 382 if (!bookmarks.empty()) | |
| 383 return; // Nothing to do, a bookmark with that url already exists. | |
| 384 | |
| 385 model->client()->RecordAction(base::UserMetricsAction("BookmarkAdded")); | |
| 386 const BookmarkNode* parent = model->GetParentForNewNodes(); | |
| 387 model->AddURL(parent, parent->child_count(), title, url); | |
| 388 } | |
| 389 | |
| 390 void RemoveAllBookmarks(BookmarkModel* model, const GURL& url) { | |
| 391 std::vector<const BookmarkNode*> bookmarks; | |
| 392 model->GetNodesByURL(url, &bookmarks); | |
| 393 | |
| 394 // Remove all the bookmarks. | |
| 395 for (size_t i = 0; i < bookmarks.size(); ++i) { | |
| 396 const BookmarkNode* node = bookmarks[i]; | |
| 397 int index = node->parent()->GetIndexOf(node); | |
| 398 if (index > -1) | |
| 399 model->Remove(node->parent(), index); | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 base::string16 CleanUpUrlForMatching( | |
| 404 const GURL& gurl, | |
| 405 const std::string& languages, | |
| 406 base::OffsetAdjuster::Adjustments* adjustments) { | |
| 407 base::OffsetAdjuster::Adjustments tmp_adjustments; | |
| 408 return base::i18n::ToLower(net::FormatUrlWithAdjustments( | |
| 409 GURL(TruncateUrl(gurl.spec())), languages, | |
| 410 net::kFormatUrlOmitUsernamePassword, | |
| 411 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS, | |
| 412 NULL, NULL, adjustments ? adjustments : &tmp_adjustments)); | |
| 413 } | |
| 414 | |
| 415 base::string16 CleanUpTitleForMatching(const base::string16& title) { | |
| 416 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); | |
| 417 } | |
| 418 | |
| 419 } // namespace bookmark_utils | |
| 420 | |
| 421 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | |
| 422 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | |
| 423 return GetNodeByID(model->root_node(), id); | |
| 424 } | |
| OLD | NEW |