OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "components/bookmarks/core/browser/bookmark_client.h" |
14 #include "chrome/browser/history/history_service.h" | 14 #include "components/bookmarks/core/browser/bookmark_node.h" |
15 #include "chrome/browser/history/history_service_factory.h" | |
16 #include "chrome/browser/history/url_database.h" | |
17 #include "components/bookmarks/core/browser/bookmark_title_match.h" | 15 #include "components/bookmarks/core/browser/bookmark_title_match.h" |
18 #include "components/query_parser/query_parser.h" | 16 #include "components/query_parser/query_parser.h" |
19 #include "third_party/icu/source/common/unicode/normalizer2.h" | 17 #include "third_party/icu/source/common/unicode/normalizer2.h" |
20 | 18 |
21 namespace { | 19 namespace { |
22 | 20 |
23 // Returns a normalized version of the UTF16 string |text|. If it fails to | 21 // Returns a normalized version of the UTF16 string |text|. If it fails to |
24 // normalize the string, returns |text| itself as a best-effort. | 22 // normalize the string, returns |text| itself as a best-effort. |
25 base::string16 Normalize(const base::string16& text) { | 23 base::string16 Normalize(const base::string16& text) { |
26 UErrorCode status = U_ZERO_ERROR; | 24 UErrorCode status = U_ZERO_ERROR; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 62 |
65 BookmarkIndex::NodeSet::const_iterator | 63 BookmarkIndex::NodeSet::const_iterator |
66 BookmarkIndex::Match::nodes_begin() const { | 64 BookmarkIndex::Match::nodes_begin() const { |
67 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); | 65 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); |
68 } | 66 } |
69 | 67 |
70 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { | 68 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { |
71 return nodes.empty() ? terms.front()->second.end() : nodes.end(); | 69 return nodes.empty() ? terms.front()->second.end() : nodes.end(); |
72 } | 70 } |
73 | 71 |
74 BookmarkIndex::BookmarkIndex(content::BrowserContext* browser_context) | 72 BookmarkIndex::BookmarkIndex(BookmarkClient* client) : client_(client) { |
75 : browser_context_(browser_context) { | |
76 } | 73 } |
77 | 74 |
78 BookmarkIndex::~BookmarkIndex() { | 75 BookmarkIndex::~BookmarkIndex() { |
79 } | 76 } |
80 | 77 |
81 void BookmarkIndex::Add(const BookmarkNode* node) { | 78 void BookmarkIndex::Add(const BookmarkNode* node) { |
82 if (!node->is_url()) | 79 if (!node->is_url()) |
83 return; | 80 return; |
84 std::vector<base::string16> terms = | 81 std::vector<base::string16> terms = |
85 ExtractQueryWords(Normalize(node->GetTitle())); | 82 ExtractQueryWords(Normalize(node->GetTitle())); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // that calculates result relevance in HistoryContentsProvider::ConvertResults | 124 // that calculates result relevance in HistoryContentsProvider::ConvertResults |
128 // will run backwards to assure higher relevance will be attributed to the | 125 // will run backwards to assure higher relevance will be attributed to the |
129 // best matches. | 126 // best matches. |
130 for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin(); | 127 for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin(); |
131 i != node_typed_counts.end() && results->size() < max_count; ++i) | 128 i != node_typed_counts.end() && results->size() < max_count; ++i) |
132 AddMatchToResults(i->first, &parser, query_nodes.get(), results); | 129 AddMatchToResults(i->first, &parser, query_nodes.get(), results); |
133 } | 130 } |
134 | 131 |
135 void BookmarkIndex::SortMatches(const Matches& matches, | 132 void BookmarkIndex::SortMatches(const Matches& matches, |
136 NodeTypedCountPairs* node_typed_counts) const { | 133 NodeTypedCountPairs* node_typed_counts) const { |
137 HistoryService* const history_service = browser_context_ ? | 134 for (Matches::const_iterator i = matches.begin(); i != matches.end(); ++i) { |
138 HistoryServiceFactory::GetForProfile( | 135 const NodeSet& nodes = |
139 Profile::FromBrowserContext(browser_context_), | 136 i->nodes.empty() ? i->terms.front()->second : i->nodes; |
140 Profile::EXPLICIT_ACCESS) : NULL; | 137 if (client_->SupportsTypedCountForNodes()) { |
| 138 client_->GetTypedCountForNodes(nodes, node_typed_counts); |
| 139 } else { |
| 140 for (NodeSet::const_iterator n = nodes.begin(); n != nodes.end(); ++n) { |
| 141 node_typed_counts->push_back(NodeTypedCountPair(*n, 0)); |
| 142 } |
| 143 } |
| 144 } |
141 | 145 |
142 history::URLDatabase* url_db = history_service ? | 146 if (client_->SupportsTypedCountForNodes()) { |
143 history_service->InMemoryDatabase() : NULL; | 147 std::sort(node_typed_counts->begin(), |
144 | 148 node_typed_counts->end(), |
145 for (Matches::const_iterator i = matches.begin(); i != matches.end(); ++i) | 149 &NodeTypedCountPairSortFunc); |
146 ExtractBookmarkNodePairs(url_db, *i, node_typed_counts); | 150 // Eliminate duplicates. |
147 | 151 node_typed_counts->erase( |
148 std::sort(node_typed_counts->begin(), node_typed_counts->end(), | 152 std::unique(node_typed_counts->begin(), node_typed_counts->end()), |
149 &NodeTypedCountPairSortFunc); | 153 node_typed_counts->end()); |
150 // Eliminate duplicates. | |
151 node_typed_counts->erase(std::unique(node_typed_counts->begin(), | |
152 node_typed_counts->end()), | |
153 node_typed_counts->end()); | |
154 } | |
155 | |
156 void BookmarkIndex::ExtractBookmarkNodePairs( | |
157 history::URLDatabase* url_db, | |
158 const Match& match, | |
159 NodeTypedCountPairs* node_typed_counts) const { | |
160 | |
161 for (NodeSet::const_iterator i = match.nodes_begin(); | |
162 i != match.nodes_end(); ++i) { | |
163 int typed_count = 0; | |
164 | |
165 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but | |
166 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot | |
167 // fetch the URLRow, it is safe to assume that its |typed_count| is 0. | |
168 history::URLRow url; | |
169 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) | |
170 typed_count = url.typed_count(); | |
171 | |
172 NodeTypedCountPair pair(*i, typed_count); | |
173 node_typed_counts->push_back(pair); | |
174 } | 154 } |
175 } | 155 } |
176 | 156 |
177 void BookmarkIndex::AddMatchToResults( | 157 void BookmarkIndex::AddMatchToResults( |
178 const BookmarkNode* node, | 158 const BookmarkNode* node, |
179 query_parser::QueryParser* parser, | 159 query_parser::QueryParser* parser, |
180 const std::vector<query_parser::QueryNode*>& query_nodes, | 160 const std::vector<query_parser::QueryNode*>& query_nodes, |
181 std::vector<BookmarkTitleMatch>* results) { | 161 std::vector<BookmarkTitleMatch>* results) { |
182 BookmarkTitleMatch title_match; | 162 BookmarkTitleMatch title_match; |
183 // Check that the result matches the query. The previous search | 163 // Check that the result matches the query. The previous search |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 Index::iterator i = index_.find(term); | 274 Index::iterator i = index_.find(term); |
295 if (i == index_.end()) { | 275 if (i == index_.end()) { |
296 // We can get here if the node has the same term more than once. For | 276 // We can get here if the node has the same term more than once. For |
297 // example, a bookmark with the title 'foo foo' would end up here. | 277 // example, a bookmark with the title 'foo foo' would end up here. |
298 return; | 278 return; |
299 } | 279 } |
300 i->second.erase(node); | 280 i->second.erase(node); |
301 if (i->second.empty()) | 281 if (i->second.empty()) |
302 index_.erase(i); | 282 index_.erase(i); |
303 } | 283 } |
OLD | NEW |