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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
(...skipping 18 matching lines...) Expand all Loading... |
29 typedef BookmarkClient::NodeTypedCountPair NodeTypedCountPair; | 29 typedef BookmarkClient::NodeTypedCountPair NodeTypedCountPair; |
30 typedef BookmarkClient::NodeTypedCountPairs NodeTypedCountPairs; | 30 typedef BookmarkClient::NodeTypedCountPairs NodeTypedCountPairs; |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 // Returns a normalized version of the UTF16 string |text|. If it fails to | 34 // Returns a normalized version of the UTF16 string |text|. If it fails to |
35 // normalize the string, returns |text| itself as a best-effort. | 35 // normalize the string, returns |text| itself as a best-effort. |
36 base::string16 Normalize(const base::string16& text) { | 36 base::string16 Normalize(const base::string16& text) { |
37 UErrorCode status = U_ZERO_ERROR; | 37 UErrorCode status = U_ZERO_ERROR; |
38 const icu::Normalizer2* normalizer2 = | 38 const icu::Normalizer2* normalizer2 = |
39 icu::Normalizer2::getInstance(NULL, "nfkc", UNORM2_COMPOSE, status); | 39 icu::Normalizer2::getInstance(nullptr, "nfkc", UNORM2_COMPOSE, status); |
40 if (U_FAILURE(status)) { | 40 if (U_FAILURE(status)) { |
41 // Log and crash right away to capture the error code in the crash report. | 41 // Log and crash right away to capture the error code in the crash report. |
42 LOG(FATAL) << "failed to create a normalizer: " << u_errorName(status); | 42 LOG(FATAL) << "failed to create a normalizer: " << u_errorName(status); |
43 } | 43 } |
44 icu::UnicodeString unicode_text( | 44 icu::UnicodeString unicode_text( |
45 text.data(), static_cast<int32_t>(text.length())); | 45 text.data(), static_cast<int32_t>(text.length())); |
46 icu::UnicodeString unicode_normalized_text; | 46 icu::UnicodeString unicode_normalized_text; |
47 normalizer2->normalize(unicode_text, unicode_normalized_text, status); | 47 normalizer2->normalize(unicode_text, unicode_normalized_text, status); |
48 if (U_FAILURE(status)) { | 48 if (U_FAILURE(status)) { |
49 // This should not happen. Log the error and fall back. | 49 // This should not happen. Log the error and fall back. |
(...skipping 15 matching lines...) Expand all Loading... |
65 | 65 |
66 // Extract the const Node* stored in a BookmarkClient::NodeTypedCountPair. | 66 // Extract the const Node* stored in a BookmarkClient::NodeTypedCountPair. |
67 struct NodeTypedCountPairExtractNodeFunctor { | 67 struct NodeTypedCountPairExtractNodeFunctor { |
68 const BookmarkNode* operator()(const NodeTypedCountPair& pair) const { | 68 const BookmarkNode* operator()(const NodeTypedCountPair& pair) const { |
69 return pair.first; | 69 return pair.first; |
70 } | 70 } |
71 }; | 71 }; |
72 | 72 |
73 } // namespace | 73 } // namespace |
74 | 74 |
75 BookmarkIndex::BookmarkIndex(BookmarkClient* client, | 75 BookmarkIndex::BookmarkIndex(BookmarkClient* client) |
76 const std::string& languages) | 76 : client_(client) { |
77 : client_(client), | |
78 languages_(languages) { | |
79 DCHECK(client_); | 77 DCHECK(client_); |
80 } | 78 } |
81 | 79 |
82 BookmarkIndex::~BookmarkIndex() { | 80 BookmarkIndex::~BookmarkIndex() { |
83 } | 81 } |
84 | 82 |
85 void BookmarkIndex::Add(const BookmarkNode* node) { | 83 void BookmarkIndex::Add(const BookmarkNode* node) { |
86 if (!node->is_url()) | 84 if (!node->is_url()) |
87 return; | 85 return; |
88 std::vector<base::string16> terms = | 86 std::vector<base::string16> terms = |
89 ExtractQueryWords(Normalize(node->GetTitle())); | 87 ExtractQueryWords(Normalize(node->GetTitle())); |
90 for (size_t i = 0; i < terms.size(); ++i) | 88 for (size_t i = 0; i < terms.size(); ++i) |
91 RegisterNode(terms[i], node); | 89 RegisterNode(terms[i], node); |
92 terms = | 90 terms = |
93 ExtractQueryWords(CleanUpUrlForMatching(node->url(), languages_, NULL)); | 91 ExtractQueryWords(CleanUpUrlForMatching(node->url(), nullptr)); |
94 for (size_t i = 0; i < terms.size(); ++i) | 92 for (size_t i = 0; i < terms.size(); ++i) |
95 RegisterNode(terms[i], node); | 93 RegisterNode(terms[i], node); |
96 } | 94 } |
97 | 95 |
98 void BookmarkIndex::Remove(const BookmarkNode* node) { | 96 void BookmarkIndex::Remove(const BookmarkNode* node) { |
99 if (!node->is_url()) | 97 if (!node->is_url()) |
100 return; | 98 return; |
101 | 99 |
102 std::vector<base::string16> terms = | 100 std::vector<base::string16> terms = |
103 ExtractQueryWords(Normalize(node->GetTitle())); | 101 ExtractQueryWords(Normalize(node->GetTitle())); |
104 for (size_t i = 0; i < terms.size(); ++i) | 102 for (size_t i = 0; i < terms.size(); ++i) |
105 UnregisterNode(terms[i], node); | 103 UnregisterNode(terms[i], node); |
106 terms = | 104 terms = |
107 ExtractQueryWords(CleanUpUrlForMatching(node->url(), languages_, NULL)); | 105 ExtractQueryWords(CleanUpUrlForMatching(node->url(), nullptr)); |
108 for (size_t i = 0; i < terms.size(); ++i) | 106 for (size_t i = 0; i < terms.size(); ++i) |
109 UnregisterNode(terms[i], node); | 107 UnregisterNode(terms[i], node); |
110 } | 108 } |
111 | 109 |
112 void BookmarkIndex::GetBookmarksMatching( | 110 void BookmarkIndex::GetBookmarksMatching( |
113 const base::string16& input_query, | 111 const base::string16& input_query, |
114 size_t max_count, | 112 size_t max_count, |
115 query_parser::MatchingAlgorithm matching_algorithm, | 113 query_parser::MatchingAlgorithm matching_algorithm, |
116 std::vector<BookmarkMatch>* results) { | 114 std::vector<BookmarkMatch>* results) { |
117 const base::string16 query = Normalize(input_query); | 115 const base::string16 query = Normalize(input_query); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // was a simple per-word search, while the more complex matching | 173 // was a simple per-word search, while the more complex matching |
176 // of QueryParser may filter it out. For example, the query | 174 // of QueryParser may filter it out. For example, the query |
177 // ["thi"] will match the bookmark titled [Thinking], but since | 175 // ["thi"] will match the bookmark titled [Thinking], but since |
178 // ["thi"] is quoted we don't want to do a prefix match. | 176 // ["thi"] is quoted we don't want to do a prefix match. |
179 query_parser::QueryWordVector title_words, url_words; | 177 query_parser::QueryWordVector title_words, url_words; |
180 const base::string16 lower_title = | 178 const base::string16 lower_title = |
181 base::i18n::ToLower(Normalize(node->GetTitle())); | 179 base::i18n::ToLower(Normalize(node->GetTitle())); |
182 parser->ExtractQueryWords(lower_title, &title_words); | 180 parser->ExtractQueryWords(lower_title, &title_words); |
183 base::OffsetAdjuster::Adjustments adjustments; | 181 base::OffsetAdjuster::Adjustments adjustments; |
184 parser->ExtractQueryWords( | 182 parser->ExtractQueryWords( |
185 CleanUpUrlForMatching(node->url(), languages_, &adjustments), | 183 CleanUpUrlForMatching(node->url(), &adjustments), |
186 &url_words); | 184 &url_words); |
187 query_parser::Snippet::MatchPositions title_matches, url_matches; | 185 query_parser::Snippet::MatchPositions title_matches, url_matches; |
188 for (size_t i = 0; i < query_nodes.size(); ++i) { | 186 for (size_t i = 0; i < query_nodes.size(); ++i) { |
189 const bool has_title_matches = | 187 const bool has_title_matches = |
190 query_nodes[i]->HasMatchIn(title_words, &title_matches); | 188 query_nodes[i]->HasMatchIn(title_words, &title_matches); |
191 const bool has_url_matches = | 189 const bool has_url_matches = |
192 query_nodes[i]->HasMatchIn(url_words, &url_matches); | 190 query_nodes[i]->HasMatchIn(url_words, &url_matches); |
193 if (!has_title_matches && !has_url_matches) | 191 if (!has_title_matches && !has_url_matches) |
194 return; | 192 return; |
195 query_parser::QueryParser::SortAndCoalesceMatchPositions(&title_matches); | 193 query_parser::QueryParser::SortAndCoalesceMatchPositions(&title_matches); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 // We can get here if the node has the same term more than once. For | 284 // We can get here if the node has the same term more than once. For |
287 // example, a bookmark with the title 'foo foo' would end up here. | 285 // example, a bookmark with the title 'foo foo' would end up here. |
288 return; | 286 return; |
289 } | 287 } |
290 i->second.erase(node); | 288 i->second.erase(node); |
291 if (i->second.empty()) | 289 if (i->second.empty()) |
292 index_.erase(i); | 290 index_.erase(i); |
293 } | 291 } |
294 | 292 |
295 } // namespace bookmarks | 293 } // namespace bookmarks |
OLD | NEW |