| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/bookmarks/browser/bookmark_utils.h" | 5 #include "components/bookmarks/browser/bookmark_utils.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 for (size_t i = 0; i < words.size(); ++i) { | 86 for (size_t i = 0; i < words.size(); ++i) { |
| 87 if (!base::i18n::StringSearchIgnoringCaseAndAccents( | 87 if (!base::i18n::StringSearchIgnoringCaseAndAccents( |
| 88 words[i], text, NULL, NULL)) { | 88 words[i], text, NULL, NULL)) { |
| 89 return false; | 89 return false; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 return true; | 92 return true; |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Returns true if |node|s title or url contains the strings in |words|. | 95 // Returns true if |node|s title or url contains the strings in |words|. |
| 96 // |languages| argument is user's accept-language setting to decode IDN. | |
| 97 bool DoesBookmarkContainWords(const BookmarkNode* node, | 96 bool DoesBookmarkContainWords(const BookmarkNode* node, |
| 98 const std::vector<base::string16>& words, | 97 const std::vector<base::string16>& words) { |
| 99 const std::string& languages) { | |
| 100 return DoesBookmarkTextContainWords(node->GetTitle(), words) || | 98 return DoesBookmarkTextContainWords(node->GetTitle(), words) || |
| 101 DoesBookmarkTextContainWords(base::UTF8ToUTF16(node->url().spec()), | 99 DoesBookmarkTextContainWords(base::UTF8ToUTF16(node->url().spec()), |
| 102 words) || | 100 words) || |
| 103 DoesBookmarkTextContainWords( | 101 DoesBookmarkTextContainWords( |
| 104 url_formatter::FormatUrl( | 102 url_formatter::FormatUrl( |
| 105 node->url(), languages, url_formatter::kFormatUrlOmitNothing, | 103 node->url(), url_formatter::kFormatUrlOmitNothing, |
| 106 net::UnescapeRule::NORMAL, NULL, NULL, NULL), | 104 net::UnescapeRule::NORMAL, NULL, NULL, NULL), |
| 107 words); | 105 words); |
| 108 } | 106 } |
| 109 | 107 |
| 110 // This is used with a tree iterator to skip subtrees which are not visible. | 108 // This is used with a tree iterator to skip subtrees which are not visible. |
| 111 bool PruneInvisibleFolders(const BookmarkNode* node) { | 109 bool PruneInvisibleFolders(const BookmarkNode* node) { |
| 112 return !node->IsVisible(); | 110 return !node->IsVisible(); |
| 113 } | 111 } |
| 114 | 112 |
| 115 // This traces parents up to root, determines if node is contained in a | 113 // This traces parents up to root, determines if node is contained in a |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 DISALLOW_COPY_AND_ASSIGN(VectorIterator); | 182 DISALLOW_COPY_AND_ASSIGN(VectorIterator); |
| 185 }; | 183 }; |
| 186 | 184 |
| 187 template <class type> | 185 template <class type> |
| 188 void GetBookmarksMatchingPropertiesImpl( | 186 void GetBookmarksMatchingPropertiesImpl( |
| 189 type& iterator, | 187 type& iterator, |
| 190 BookmarkModel* model, | 188 BookmarkModel* model, |
| 191 const QueryFields& query, | 189 const QueryFields& query, |
| 192 const std::vector<base::string16>& query_words, | 190 const std::vector<base::string16>& query_words, |
| 193 size_t max_count, | 191 size_t max_count, |
| 194 const std::string& languages, | |
| 195 std::vector<const BookmarkNode*>* nodes) { | 192 std::vector<const BookmarkNode*>* nodes) { |
| 196 while (iterator.has_next()) { | 193 while (iterator.has_next()) { |
| 197 const BookmarkNode* node = iterator.Next(); | 194 const BookmarkNode* node = iterator.Next(); |
| 198 if ((!query_words.empty() && | 195 if ((!query_words.empty() && |
| 199 !DoesBookmarkContainWords(node, query_words, languages)) || | 196 !DoesBookmarkContainWords(node, query_words)) || |
| 200 model->is_permanent_node(node)) { | 197 model->is_permanent_node(node)) { |
| 201 continue; | 198 continue; |
| 202 } | 199 } |
| 203 if (query.title && node->GetTitle() != *query.title) | 200 if (query.title && node->GetTitle() != *query.title) |
| 204 continue; | 201 continue; |
| 205 | 202 |
| 206 nodes->push_back(node); | 203 nodes->push_back(node); |
| 207 if (nodes->size() == max_count) | 204 if (nodes->size() == max_count) |
| 208 return; | 205 return; |
| 209 } | 206 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 } | 388 } |
| 392 } | 389 } |
| 393 | 390 |
| 394 bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2) { | 391 bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2) { |
| 395 return n1->date_added() > n2->date_added(); | 392 return n1->date_added() > n2->date_added(); |
| 396 } | 393 } |
| 397 | 394 |
| 398 void GetBookmarksMatchingProperties(BookmarkModel* model, | 395 void GetBookmarksMatchingProperties(BookmarkModel* model, |
| 399 const QueryFields& query, | 396 const QueryFields& query, |
| 400 size_t max_count, | 397 size_t max_count, |
| 401 const std::string& languages, | |
| 402 std::vector<const BookmarkNode*>* nodes) { | 398 std::vector<const BookmarkNode*>* nodes) { |
| 403 std::vector<base::string16> query_words; | 399 std::vector<base::string16> query_words; |
| 404 query_parser::QueryParser parser; | 400 query_parser::QueryParser parser; |
| 405 if (query.word_phrase_query) { | 401 if (query.word_phrase_query) { |
| 406 parser.ParseQueryWords(base::i18n::ToLower(*query.word_phrase_query), | 402 parser.ParseQueryWords(base::i18n::ToLower(*query.word_phrase_query), |
| 407 query_parser::MatchingAlgorithm::DEFAULT, | 403 query_parser::MatchingAlgorithm::DEFAULT, |
| 408 &query_words); | 404 &query_words); |
| 409 if (query_words.empty()) | 405 if (query_words.empty()) |
| 410 return; | 406 return; |
| 411 } | 407 } |
| 412 | 408 |
| 413 if (query.url) { | 409 if (query.url) { |
| 414 // Shortcut into the BookmarkModel if searching for URL. | 410 // Shortcut into the BookmarkModel if searching for URL. |
| 415 GURL url(*query.url); | 411 GURL url(*query.url); |
| 416 std::vector<const BookmarkNode*> url_matched_nodes; | 412 std::vector<const BookmarkNode*> url_matched_nodes; |
| 417 if (url.is_valid()) | 413 if (url.is_valid()) |
| 418 model->GetNodesByURL(url, &url_matched_nodes); | 414 model->GetNodesByURL(url, &url_matched_nodes); |
| 419 VectorIterator iterator(&url_matched_nodes); | 415 VectorIterator iterator(&url_matched_nodes); |
| 420 GetBookmarksMatchingPropertiesImpl<VectorIterator>( | 416 GetBookmarksMatchingPropertiesImpl<VectorIterator>( |
| 421 iterator, model, query, query_words, max_count, languages, nodes); | 417 iterator, model, query, query_words, max_count, nodes); |
| 422 } else { | 418 } else { |
| 423 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | 419 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); |
| 424 GetBookmarksMatchingPropertiesImpl< | 420 GetBookmarksMatchingPropertiesImpl< |
| 425 ui::TreeNodeIterator<const BookmarkNode>>( | 421 ui::TreeNodeIterator<const BookmarkNode>>( |
| 426 iterator, model, query, query_words, max_count, languages, nodes); | 422 iterator, model, query, query_words, max_count, nodes); |
| 427 } | 423 } |
| 428 } | 424 } |
| 429 | 425 |
| 430 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | 426 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| 431 registry->RegisterBooleanPref( | 427 registry->RegisterBooleanPref( |
| 432 prefs::kShowBookmarkBar, | 428 prefs::kShowBookmarkBar, |
| 433 false, | 429 false, |
| 434 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 430 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 435 registry->RegisterBooleanPref(prefs::kEditBookmarksEnabled, true); | 431 registry->RegisterBooleanPref(prefs::kEditBookmarksEnabled, true); |
| 436 registry->RegisterBooleanPref( | 432 registry->RegisterBooleanPref( |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 for (size_t i = 0; i < bookmarks.size(); ++i) { | 510 for (size_t i = 0; i < bookmarks.size(); ++i) { |
| 515 const BookmarkNode* node = bookmarks[i]; | 511 const BookmarkNode* node = bookmarks[i]; |
| 516 int index = node->parent()->GetIndexOf(node); | 512 int index = node->parent()->GetIndexOf(node); |
| 517 if (index > -1 && model->client()->CanBeEditedByUser(node)) | 513 if (index > -1 && model->client()->CanBeEditedByUser(node)) |
| 518 model->Remove(node); | 514 model->Remove(node); |
| 519 } | 515 } |
| 520 } | 516 } |
| 521 | 517 |
| 522 base::string16 CleanUpUrlForMatching( | 518 base::string16 CleanUpUrlForMatching( |
| 523 const GURL& gurl, | 519 const GURL& gurl, |
| 524 const std::string& languages, | |
| 525 base::OffsetAdjuster::Adjustments* adjustments) { | 520 base::OffsetAdjuster::Adjustments* adjustments) { |
| 526 base::OffsetAdjuster::Adjustments tmp_adjustments; | 521 base::OffsetAdjuster::Adjustments tmp_adjustments; |
| 527 return base::i18n::ToLower(url_formatter::FormatUrlWithAdjustments( | 522 return base::i18n::ToLower(url_formatter::FormatUrlWithAdjustments( |
| 528 GURL(TruncateUrl(gurl.spec())), languages, | 523 GURL(TruncateUrl(gurl.spec())), |
| 529 url_formatter::kFormatUrlOmitUsernamePassword, | 524 url_formatter::kFormatUrlOmitUsernamePassword, |
| 530 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | | 525 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | |
| 531 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS, | 526 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS, |
| 532 nullptr, nullptr, adjustments ? adjustments : &tmp_adjustments)); | 527 nullptr, nullptr, adjustments ? adjustments : &tmp_adjustments)); |
| 533 } | 528 } |
| 534 | 529 |
| 535 base::string16 CleanUpTitleForMatching(const base::string16& title) { | 530 base::string16 CleanUpTitleForMatching(const base::string16& title) { |
| 536 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); | 531 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); |
| 537 } | 532 } |
| 538 | 533 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 568 bool HasDescendantsOf(const std::vector<const BookmarkNode*>& list, | 563 bool HasDescendantsOf(const std::vector<const BookmarkNode*>& list, |
| 569 const BookmarkNode* root) { | 564 const BookmarkNode* root) { |
| 570 for (const BookmarkNode* node : list) { | 565 for (const BookmarkNode* node : list) { |
| 571 if (IsDescendantOf(node, root)) | 566 if (IsDescendantOf(node, root)) |
| 572 return true; | 567 return true; |
| 573 } | 568 } |
| 574 return false; | 569 return false; |
| 575 } | 570 } |
| 576 | 571 |
| 577 } // namespace bookmarks | 572 } // namespace bookmarks |
| OLD | NEW |