| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/bookmarks/bookmark_index.h" | 5 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/string16.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_utils.h" | 13 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 13 #include "chrome/browser/history/history_database.h" | 14 #include "chrome/browser/history/history_database.h" |
| 14 #include "chrome/browser/history/query_parser.h" | 15 #include "chrome/browser/history/query_parser.h" |
| 15 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
| 16 | 17 |
| 17 BookmarkIndex::NodeSet::const_iterator | 18 BookmarkIndex::NodeSet::const_iterator |
| 18 BookmarkIndex::Match::nodes_begin() const { | 19 BookmarkIndex::Match::nodes_begin() const { |
| 19 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); | 20 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); |
| 20 } | 21 } |
| 21 | 22 |
| 22 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { | 23 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { |
| 23 return nodes.empty() ? terms.front()->second.end() : nodes.end(); | 24 return nodes.empty() ? terms.front()->second.end() : nodes.end(); |
| 24 } | 25 } |
| 25 | 26 |
| 26 void BookmarkIndex::Add(const BookmarkNode* node) { | 27 void BookmarkIndex::Add(const BookmarkNode* node) { |
| 27 if (!node->is_url()) | 28 if (!node->is_url()) |
| 28 return; | 29 return; |
| 29 std::vector<std::wstring> terms = ExtractQueryWords(node->GetTitle()); | 30 std::vector<string16> terms = ExtractQueryWords(node->GetTitleAsString16()); |
| 30 for (size_t i = 0; i < terms.size(); ++i) | 31 for (size_t i = 0; i < terms.size(); ++i) |
| 31 RegisterNode(terms[i], node); | 32 RegisterNode(terms[i], node); |
| 32 } | 33 } |
| 33 | 34 |
| 34 void BookmarkIndex::Remove(const BookmarkNode* node) { | 35 void BookmarkIndex::Remove(const BookmarkNode* node) { |
| 35 if (!node->is_url()) | 36 if (!node->is_url()) |
| 36 return; | 37 return; |
| 37 | 38 |
| 38 std::vector<std::wstring> terms = ExtractQueryWords(node->GetTitle()); | 39 std::vector<string16> terms = ExtractQueryWords(node->GetTitleAsString16()); |
| 39 for (size_t i = 0; i < terms.size(); ++i) | 40 for (size_t i = 0; i < terms.size(); ++i) |
| 40 UnregisterNode(terms[i], node); | 41 UnregisterNode(terms[i], node); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void BookmarkIndex::GetBookmarksWithTitlesMatching( | 44 void BookmarkIndex::GetBookmarksWithTitlesMatching( |
| 44 const std::wstring& query, | 45 const string16& query, |
| 45 size_t max_count, | 46 size_t max_count, |
| 46 std::vector<bookmark_utils::TitleMatch>* results) { | 47 std::vector<bookmark_utils::TitleMatch>* results) { |
| 47 std::vector<std::wstring> terms = ExtractQueryWords(query); | 48 std::vector<string16> terms = ExtractQueryWords(query); |
| 48 if (terms.empty()) | 49 if (terms.empty()) |
| 49 return; | 50 return; |
| 50 | 51 |
| 51 Matches matches; | 52 Matches matches; |
| 52 for (size_t i = 0; i < terms.size(); ++i) { | 53 for (size_t i = 0; i < terms.size(); ++i) { |
| 53 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) | 54 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) |
| 54 return; | 55 return; |
| 55 } | 56 } |
| 56 | 57 |
| 57 NodeTypedCountPairs node_typed_counts; | 58 NodeTypedCountPairs node_typed_counts; |
| 58 SortMatches(matches, &node_typed_counts); | 59 SortMatches(matches, &node_typed_counts); |
| 59 | 60 |
| 60 // We use a QueryParser to fill in match positions for us. It's not the most | 61 // We use a QueryParser to fill in match positions for us. It's not the most |
| 61 // efficient way to go about this, but by the time we get here we know what | 62 // efficient way to go about this, but by the time we get here we know what |
| 62 // matches and so this shouldn't be performance critical. | 63 // matches and so this shouldn't be performance critical. |
| 63 QueryParser parser; | 64 QueryParser parser; |
| 64 ScopedVector<QueryNode> query_nodes; | 65 ScopedVector<QueryNode> query_nodes; |
| 65 parser.ParseQuery(WideToUTF16(query), &query_nodes.get()); | 66 parser.ParseQuery(query, &query_nodes.get()); |
| 66 | 67 |
| 67 // The highest typed counts should be at the beginning of the results vector | 68 // The highest typed counts should be at the beginning of the results vector |
| 68 // so that the best matches will always be included in the results. The loop | 69 // so that the best matches will always be included in the results. The loop |
| 69 // that calculates result relevance in HistoryContentsProvider::ConvertResults | 70 // that calculates result relevance in HistoryContentsProvider::ConvertResults |
| 70 // will run backwards to assure higher relevance will be attributed to the | 71 // will run backwards to assure higher relevance will be attributed to the |
| 71 // best matches. | 72 // best matches. |
| 72 for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin(); | 73 for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin(); |
| 73 i != node_typed_counts.end() && results->size() < max_count; ++i) | 74 i != node_typed_counts.end() && results->size() < max_count; ++i) |
| 74 AddMatchToResults(i->first, &parser, query_nodes.get(), results); | 75 AddMatchToResults(i->first, &parser, query_nodes.get(), results); |
| 75 } | 76 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 const BookmarkNode* node, | 109 const BookmarkNode* node, |
| 109 QueryParser* parser, | 110 QueryParser* parser, |
| 110 const std::vector<QueryNode*>& query_nodes, | 111 const std::vector<QueryNode*>& query_nodes, |
| 111 std::vector<bookmark_utils::TitleMatch>* results) { | 112 std::vector<bookmark_utils::TitleMatch>* results) { |
| 112 bookmark_utils::TitleMatch title_match; | 113 bookmark_utils::TitleMatch title_match; |
| 113 // Check that the result matches the query. The previous search | 114 // Check that the result matches the query. The previous search |
| 114 // was a simple per-word search, while the more complex matching | 115 // was a simple per-word search, while the more complex matching |
| 115 // of QueryParser may filter it out. For example, the query | 116 // of QueryParser may filter it out. For example, the query |
| 116 // ["thi"] will match the bookmark titled [Thinking], but since | 117 // ["thi"] will match the bookmark titled [Thinking], but since |
| 117 // ["thi"] is quoted we don't want to do a prefix match. | 118 // ["thi"] is quoted we don't want to do a prefix match. |
| 118 if (parser->DoesQueryMatch(WideToUTF16(node->GetTitle()), query_nodes, | 119 if (parser->DoesQueryMatch(node->GetTitleAsString16(), query_nodes, |
| 119 &(title_match.match_positions))) { | 120 &(title_match.match_positions))) { |
| 120 title_match.node = node; | 121 title_match.node = node; |
| 121 results->push_back(title_match); | 122 results->push_back(title_match); |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 | 125 |
| 125 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const std::wstring& term, | 126 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const string16& term, |
| 126 bool first_term, | 127 bool first_term, |
| 127 Matches* matches) { | 128 Matches* matches) { |
| 128 Index::const_iterator i = index_.lower_bound(term); | 129 Index::const_iterator i = index_.lower_bound(term); |
| 129 if (i == index_.end()) | 130 if (i == index_.end()) |
| 130 return false; | 131 return false; |
| 131 | 132 |
| 132 if (!QueryParser::IsWordLongEnoughForPrefixSearch(WideToUTF16(term))) { | 133 if (!QueryParser::IsWordLongEnoughForPrefixSearch(term)) { |
| 133 // Term is too short for prefix match, compare using exact match. | 134 // Term is too short for prefix match, compare using exact match. |
| 134 if (i->first != term) | 135 if (i->first != term) |
| 135 return false; // No bookmarks with this term. | 136 return false; // No bookmarks with this term. |
| 136 | 137 |
| 137 if (first_term) { | 138 if (first_term) { |
| 138 Match match; | 139 Match match; |
| 139 match.terms.push_back(i); | 140 match.terms.push_back(i); |
| 140 matches->push_back(match); | 141 matches->push_back(match); |
| 141 return true; | 142 return true; |
| 142 } | 143 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (!intersection.empty()) { | 198 if (!intersection.empty()) { |
| 198 result->push_back(Match()); | 199 result->push_back(Match()); |
| 199 Match& combined_match = result->back(); | 200 Match& combined_match = result->back(); |
| 200 combined_match.terms = match.terms; | 201 combined_match.terms = match.terms; |
| 201 combined_match.terms.push_back(index_i); | 202 combined_match.terms.push_back(index_i); |
| 202 combined_match.nodes.swap(intersection); | 203 combined_match.nodes.swap(intersection); |
| 203 } | 204 } |
| 204 } | 205 } |
| 205 } | 206 } |
| 206 | 207 |
| 207 std::vector<std::wstring> BookmarkIndex::ExtractQueryWords( | 208 std::vector<string16> BookmarkIndex::ExtractQueryWords(const string16& query) { |
| 208 const std::wstring& query) { | |
| 209 std::vector<string16> terms; | 209 std::vector<string16> terms; |
| 210 if (query.empty()) | 210 if (query.empty()) |
| 211 return std::vector<std::wstring>(); | 211 return std::vector<string16>(); |
| 212 QueryParser parser; | 212 QueryParser parser; |
| 213 // TODO: use ICU normalization. | 213 // TODO: use ICU normalization. |
| 214 parser.ExtractQueryWords(l10n_util::ToLower(WideToUTF16(query)), &terms); | 214 parser.ExtractQueryWords(l10n_util::ToLower(query), &terms); |
| 215 | |
| 216 // TODO(brettw) just remove this and return |terms| when this is converted | |
| 217 // to string16. | |
| 218 #if defined(WCHAR_T_IS_UTF32) | |
| 219 std::vector<std::wstring> wterms; | |
| 220 for (size_t i = 0; i < terms.size(); i++) | |
| 221 wterms.push_back(UTF16ToWide(terms[i])); | |
| 222 return wterms; | |
| 223 #else | |
| 224 return terms; | 215 return terms; |
| 225 #endif | |
| 226 } | 216 } |
| 227 | 217 |
| 228 void BookmarkIndex::RegisterNode(const std::wstring& term, | 218 void BookmarkIndex::RegisterNode(const string16& term, |
| 229 const BookmarkNode* node) { | 219 const BookmarkNode* node) { |
| 230 if (std::find(index_[term].begin(), index_[term].end(), node) != | 220 if (std::find(index_[term].begin(), index_[term].end(), node) != |
| 231 index_[term].end()) { | 221 index_[term].end()) { |
| 232 // We've already added node for term. | 222 // We've already added node for term. |
| 233 return; | 223 return; |
| 234 } | 224 } |
| 235 index_[term].insert(node); | 225 index_[term].insert(node); |
| 236 } | 226 } |
| 237 | 227 |
| 238 void BookmarkIndex::UnregisterNode(const std::wstring& term, | 228 void BookmarkIndex::UnregisterNode(const string16& term, |
| 239 const BookmarkNode* node) { | 229 const BookmarkNode* node) { |
| 240 Index::iterator i = index_.find(term); | 230 Index::iterator i = index_.find(term); |
| 241 if (i == index_.end()) { | 231 if (i == index_.end()) { |
| 242 // We can get here if the node has the same term more than once. For | 232 // We can get here if the node has the same term more than once. For |
| 243 // example, a bookmark with the title 'foo foo' would end up here. | 233 // example, a bookmark with the title 'foo foo' would end up here. |
| 244 return; | 234 return; |
| 245 } | 235 } |
| 246 i->second.erase(node); | 236 i->second.erase(node); |
| 247 if (i->second.empty()) | 237 if (i->second.empty()) |
| 248 index_.erase(i); | 238 index_.erase(i); |
| 249 } | 239 } |
| OLD | NEW |