| 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_index.h" | 5 #include "components/bookmarks/browser/bookmark_index.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <list> | 10 #include <list> |
| 11 | 11 |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/utf_offset_string_conversions.h" | 14 #include "base/strings/utf_offset_string_conversions.h" |
| 15 #include "components/bookmarks/browser/bookmark_client.h" | 15 #include "components/bookmarks/browser/bookmark_client.h" |
| 16 #include "components/bookmarks/browser/bookmark_match.h" | 16 #include "components/bookmarks/browser/bookmark_match.h" |
| 17 #include "components/bookmarks/browser/bookmark_node.h" | 17 #include "components/bookmarks/browser/bookmark_node.h" |
| 18 #include "components/bookmarks/browser/bookmark_utils.h" | 18 #include "components/bookmarks/browser/bookmark_utils.h" |
| 19 #include "components/query_parser/snippet.h" | 19 #include "components/query_parser/snippet.h" |
| 20 #include "third_party/icu/source/common/unicode/normalizer2.h" | 20 #include "third_party/icu/source/common/unicode/normalizer2.h" |
| 21 | 21 |
| 22 using bookmarks::BookmarkClient; | 22 namespace bookmarks { |
| 23 | 23 |
| 24 typedef BookmarkClient::NodeTypedCountPair NodeTypedCountPair; | 24 typedef BookmarkClient::NodeTypedCountPair NodeTypedCountPair; |
| 25 typedef BookmarkClient::NodeTypedCountPairs NodeTypedCountPairs; | 25 typedef BookmarkClient::NodeTypedCountPairs NodeTypedCountPairs; |
| 26 | 26 |
| 27 namespace bookmarks { | |
| 28 | |
| 29 namespace { | 27 namespace { |
| 30 | 28 |
| 31 // Returns a normalized version of the UTF16 string |text|. If it fails to | 29 // Returns a normalized version of the UTF16 string |text|. If it fails to |
| 32 // normalize the string, returns |text| itself as a best-effort. | 30 // normalize the string, returns |text| itself as a best-effort. |
| 33 base::string16 Normalize(const base::string16& text) { | 31 base::string16 Normalize(const base::string16& text) { |
| 34 UErrorCode status = U_ZERO_ERROR; | 32 UErrorCode status = U_ZERO_ERROR; |
| 35 const icu::Normalizer2* normalizer2 = | 33 const icu::Normalizer2* normalizer2 = |
| 36 icu::Normalizer2::getInstance(NULL, "nfkc", UNORM2_COMPOSE, status); | 34 icu::Normalizer2::getInstance(NULL, "nfkc", UNORM2_COMPOSE, status); |
| 37 icu::UnicodeString unicode_text( | 35 icu::UnicodeString unicode_text( |
| 38 text.data(), static_cast<int32_t>(text.length())); | 36 text.data(), static_cast<int32_t>(text.length())); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 109 } |
| 112 | 110 |
| 113 void BookmarkIndex::Add(const BookmarkNode* node) { | 111 void BookmarkIndex::Add(const BookmarkNode* node) { |
| 114 if (!node->is_url()) | 112 if (!node->is_url()) |
| 115 return; | 113 return; |
| 116 std::vector<base::string16> terms = | 114 std::vector<base::string16> terms = |
| 117 ExtractQueryWords(Normalize(node->GetTitle())); | 115 ExtractQueryWords(Normalize(node->GetTitle())); |
| 118 for (size_t i = 0; i < terms.size(); ++i) | 116 for (size_t i = 0; i < terms.size(); ++i) |
| 119 RegisterNode(terms[i], node); | 117 RegisterNode(terms[i], node); |
| 120 if (index_urls_) { | 118 if (index_urls_) { |
| 121 terms = ExtractQueryWords(bookmark_utils::CleanUpUrlForMatching( | 119 terms = |
| 122 node->url(), languages_, NULL)); | 120 ExtractQueryWords(CleanUpUrlForMatching(node->url(), languages_, NULL)); |
| 123 for (size_t i = 0; i < terms.size(); ++i) | 121 for (size_t i = 0; i < terms.size(); ++i) |
| 124 RegisterNode(terms[i], node); | 122 RegisterNode(terms[i], node); |
| 125 } | 123 } |
| 126 } | 124 } |
| 127 | 125 |
| 128 void BookmarkIndex::Remove(const BookmarkNode* node) { | 126 void BookmarkIndex::Remove(const BookmarkNode* node) { |
| 129 if (!node->is_url()) | 127 if (!node->is_url()) |
| 130 return; | 128 return; |
| 131 | 129 |
| 132 std::vector<base::string16> terms = | 130 std::vector<base::string16> terms = |
| 133 ExtractQueryWords(Normalize(node->GetTitle())); | 131 ExtractQueryWords(Normalize(node->GetTitle())); |
| 134 for (size_t i = 0; i < terms.size(); ++i) | 132 for (size_t i = 0; i < terms.size(); ++i) |
| 135 UnregisterNode(terms[i], node); | 133 UnregisterNode(terms[i], node); |
| 136 if (index_urls_) { | 134 if (index_urls_) { |
| 137 terms = ExtractQueryWords(bookmark_utils::CleanUpUrlForMatching( | 135 terms = |
| 138 node->url(), languages_, NULL)); | 136 ExtractQueryWords(CleanUpUrlForMatching(node->url(), languages_, NULL)); |
| 139 for (size_t i = 0; i < terms.size(); ++i) | 137 for (size_t i = 0; i < terms.size(); ++i) |
| 140 UnregisterNode(terms[i], node); | 138 UnregisterNode(terms[i], node); |
| 141 } | 139 } |
| 142 } | 140 } |
| 143 | 141 |
| 144 void BookmarkIndex::GetBookmarksMatching(const base::string16& input_query, | 142 void BookmarkIndex::GetBookmarksMatching(const base::string16& input_query, |
| 145 size_t max_count, | 143 size_t max_count, |
| 146 std::vector<BookmarkMatch>* results) { | 144 std::vector<BookmarkMatch>* results) { |
| 147 const base::string16 query = Normalize(input_query); | 145 const base::string16 query = Normalize(input_query); |
| 148 std::vector<base::string16> terms = ExtractQueryWords(query); | 146 std::vector<base::string16> terms = ExtractQueryWords(query); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 // was a simple per-word search, while the more complex matching | 212 // was a simple per-word search, while the more complex matching |
| 215 // of QueryParser may filter it out. For example, the query | 213 // of QueryParser may filter it out. For example, the query |
| 216 // ["thi"] will match the bookmark titled [Thinking], but since | 214 // ["thi"] will match the bookmark titled [Thinking], but since |
| 217 // ["thi"] is quoted we don't want to do a prefix match. | 215 // ["thi"] is quoted we don't want to do a prefix match. |
| 218 query_parser::QueryWordVector title_words, url_words; | 216 query_parser::QueryWordVector title_words, url_words; |
| 219 const base::string16 lower_title = | 217 const base::string16 lower_title = |
| 220 base::i18n::ToLower(Normalize(node->GetTitle())); | 218 base::i18n::ToLower(Normalize(node->GetTitle())); |
| 221 parser->ExtractQueryWords(lower_title, &title_words); | 219 parser->ExtractQueryWords(lower_title, &title_words); |
| 222 base::OffsetAdjuster::Adjustments adjustments; | 220 base::OffsetAdjuster::Adjustments adjustments; |
| 223 if (index_urls_) { | 221 if (index_urls_) { |
| 224 parser->ExtractQueryWords(bookmark_utils::CleanUpUrlForMatching( | 222 parser->ExtractQueryWords( |
| 225 node->url(), languages_, &adjustments), &url_words); | 223 CleanUpUrlForMatching(node->url(), languages_, &adjustments), |
| 224 &url_words); |
| 226 } | 225 } |
| 227 query_parser::Snippet::MatchPositions title_matches, url_matches; | 226 query_parser::Snippet::MatchPositions title_matches, url_matches; |
| 228 for (size_t i = 0; i < query_nodes.size(); ++i) { | 227 for (size_t i = 0; i < query_nodes.size(); ++i) { |
| 229 const bool has_title_matches = | 228 const bool has_title_matches = |
| 230 query_nodes[i]->HasMatchIn(title_words, &title_matches); | 229 query_nodes[i]->HasMatchIn(title_words, &title_matches); |
| 231 const bool has_url_matches = index_urls_ && | 230 const bool has_url_matches = index_urls_ && |
| 232 query_nodes[i]->HasMatchIn(url_words, &url_matches); | 231 query_nodes[i]->HasMatchIn(url_words, &url_matches); |
| 233 if (!has_title_matches && !has_url_matches) | 232 if (!has_title_matches && !has_url_matches) |
| 234 return; | 233 return; |
| 235 query_parser::QueryParser::SortAndCoalesceMatchPositions(&title_matches); | 234 query_parser::QueryParser::SortAndCoalesceMatchPositions(&title_matches); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 // We can get here if the node has the same term more than once. For | 361 // We can get here if the node has the same term more than once. For |
| 363 // example, a bookmark with the title 'foo foo' would end up here. | 362 // example, a bookmark with the title 'foo foo' would end up here. |
| 364 return; | 363 return; |
| 365 } | 364 } |
| 366 i->second.erase(node); | 365 i->second.erase(node); |
| 367 if (i->second.empty()) | 366 if (i->second.empty()) |
| 368 index_.erase(i); | 367 index_.erase(i); |
| 369 } | 368 } |
| 370 | 369 |
| 371 } // namespace bookmarks | 370 } // namespace bookmarks |
| OLD | NEW |