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/history/url_index_private_data.h" | 5 #include "chrome/browser/history/url_index_private_data.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <limits> | 9 #include <limits> |
10 #include <numeric> | 10 #include <numeric> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/i18n/break_iterator.h" | 16 #include "base/i18n/break_iterator.h" |
17 #include "base/i18n/case_conversion.h" | 17 #include "base/i18n/case_conversion.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 22 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
23 #include "chrome/browser/autocomplete/url_prefix.h" | 23 #include "chrome/browser/autocomplete/url_prefix.h" |
24 #include "chrome/browser/history/history_database.h" | 24 #include "chrome/browser/history/history_database.h" |
25 #include "chrome/browser/history/history_db_task.h" | 25 #include "chrome/browser/history/history_db_task.h" |
26 #include "chrome/browser/history/history_service.h" | 26 #include "chrome/browser/history/history_service.h" |
27 #include "chrome/browser/history/in_memory_url_index.h" | 27 #include "chrome/browser/history/in_memory_url_index.h" |
28 #include "components/bookmarks/core/browser/bookmark_service.h" | |
29 #include "components/bookmarks/core/browser/bookmark_utils.h" | 28 #include "components/bookmarks/core/browser/bookmark_utils.h" |
| 29 #include "components/history/core/browser/history_client.h" |
30 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
31 | 31 |
32 #if defined(USE_SYSTEM_PROTOBUF) | 32 #if defined(USE_SYSTEM_PROTOBUF) |
33 #include <google/protobuf/repeated_field.h> | 33 #include <google/protobuf/repeated_field.h> |
34 #else | 34 #else |
35 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" | 35 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" |
36 #endif | 36 #endif |
37 | 37 |
38 using google::protobuf::RepeatedField; | 38 using google::protobuf::RepeatedField; |
39 using google::protobuf::RepeatedPtrField; | 39 using google::protobuf::RepeatedPtrField; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 URLID url_id) | 112 URLID url_id) |
113 : private_data_(private_data), | 113 : private_data_(private_data), |
114 url_id_(url_id), | 114 url_id_(url_id), |
115 succeeded_(false) { | 115 succeeded_(false) { |
116 } | 116 } |
117 | 117 |
118 bool UpdateRecentVisitsFromHistoryDBTask::RunOnDBThread( | 118 bool UpdateRecentVisitsFromHistoryDBTask::RunOnDBThread( |
119 HistoryBackend* backend, | 119 HistoryBackend* backend, |
120 HistoryDatabase* db) { | 120 HistoryDatabase* db) { |
121 // Make sure the private data is going to get as many recent visits as | 121 // Make sure the private data is going to get as many recent visits as |
122 // ScoredHistoryMatch::GetFrecency() hopes to use. | 122 // ScoredHistoryMatch::GetFrequency() hopes to use. |
123 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); | 123 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); |
124 succeeded_ = db->GetMostRecentVisitsForURL(url_id_, | 124 succeeded_ = db->GetMostRecentVisitsForURL(url_id_, |
125 kMaxVisitsToStoreInCache, | 125 kMaxVisitsToStoreInCache, |
126 &recent_visits_); | 126 &recent_visits_); |
127 if (!succeeded_) | 127 if (!succeeded_) |
128 recent_visits_.clear(); | 128 recent_visits_.clear(); |
129 return true; // Always claim to be done; do not retry failures. | 129 return true; // Always claim to be done; do not retry failures. |
130 } | 130 } |
131 | 131 |
132 void UpdateRecentVisitsFromHistoryDBTask::DoneRunOnMainThread() { | 132 void UpdateRecentVisitsFromHistoryDBTask::DoneRunOnMainThread() { |
(...skipping 12 matching lines...) Expand all Loading... |
145 saved_cache_version_(kCurrentCacheFileVersion), | 145 saved_cache_version_(kCurrentCacheFileVersion), |
146 pre_filter_item_count_(0), | 146 pre_filter_item_count_(0), |
147 post_filter_item_count_(0), | 147 post_filter_item_count_(0), |
148 post_scoring_item_count_(0) { | 148 post_scoring_item_count_(0) { |
149 } | 149 } |
150 | 150 |
151 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( | 151 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( |
152 base::string16 search_string, | 152 base::string16 search_string, |
153 size_t cursor_position, | 153 size_t cursor_position, |
154 const std::string& languages, | 154 const std::string& languages, |
155 BookmarkService* bookmark_service) { | 155 HistoryClient* history_client) { |
156 // If cursor position is set and useful (not at either end of the | 156 // If cursor position is set and useful (not at either end of the |
157 // string), allow the search string to be broken at cursor position. | 157 // string), allow the search string to be broken at cursor position. |
158 // We do this by pretending there's a space where the cursor is. | 158 // We do this by pretending there's a space where the cursor is. |
159 if ((cursor_position != base::string16::npos) && | 159 if ((cursor_position != base::string16::npos) && |
160 (cursor_position < search_string.length()) && | 160 (cursor_position < search_string.length()) && |
161 (cursor_position > 0)) { | 161 (cursor_position > 0)) { |
162 search_string.insert(cursor_position, base::ASCIIToUTF16(" ")); | 162 search_string.insert(cursor_position, base::ASCIIToUTF16(" ")); |
163 } | 163 } |
164 pre_filter_item_count_ = 0; | 164 pre_filter_item_count_ = 0; |
165 post_filter_item_count_ = 0; | 165 post_filter_item_count_ = 0; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 // Don't score matches when there are no terms to score against. (It's | 237 // Don't score matches when there are no terms to score against. (It's |
238 // possible that the word break iterater that extracts words to search | 238 // possible that the word break iterater that extracts words to search |
239 // for in the database allows some whitespace "words" whereas Tokenize | 239 // for in the database allows some whitespace "words" whereas Tokenize |
240 // excludes a long list of whitespace.) One could write a scoring | 240 // excludes a long list of whitespace.) One could write a scoring |
241 // function that gives a reasonable order to matches when there | 241 // function that gives a reasonable order to matches when there |
242 // are no terms (i.e., all the words are some form of whitespace), | 242 // are no terms (i.e., all the words are some form of whitespace), |
243 // but this is such a rare edge case that it's not worth the time. | 243 // but this is such a rare edge case that it's not worth the time. |
244 return scored_items; | 244 return scored_items; |
245 } | 245 } |
246 scored_items = std::for_each(history_id_set.begin(), history_id_set.end(), | 246 scored_items = std::for_each(history_id_set.begin(), history_id_set.end(), |
247 AddHistoryMatch(*this, languages, bookmark_service, lower_raw_string, | 247 AddHistoryMatch(*this, languages, history_client, lower_raw_string, |
248 lower_raw_terms, base::Time::Now())).ScoredMatches(); | 248 lower_raw_terms, base::Time::Now())).ScoredMatches(); |
249 | 249 |
250 // Select and sort only the top kMaxMatches results. | 250 // Select and sort only the top kMaxMatches results. |
251 if (scored_items.size() > AutocompleteProvider::kMaxMatches) { | 251 if (scored_items.size() > AutocompleteProvider::kMaxMatches) { |
252 std::partial_sort(scored_items.begin(), | 252 std::partial_sort(scored_items.begin(), |
253 scored_items.begin() + | 253 scored_items.begin() + |
254 AutocompleteProvider::kMaxMatches, | 254 AutocompleteProvider::kMaxMatches, |
255 scored_items.end(), | 255 scored_items.end(), |
256 ScoredHistoryMatch::MatchScoreGreater); | 256 ScoredHistoryMatch::MatchScoreGreater); |
257 scored_items.resize(AutocompleteProvider::kMaxMatches); | 257 scored_items.resize(AutocompleteProvider::kMaxMatches); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 data_copy->word_id_history_map_ = word_id_history_map_; | 483 data_copy->word_id_history_map_ = word_id_history_map_; |
484 data_copy->history_id_word_map_ = history_id_word_map_; | 484 data_copy->history_id_word_map_ = history_id_word_map_; |
485 data_copy->history_info_map_ = history_info_map_; | 485 data_copy->history_info_map_ = history_info_map_; |
486 data_copy->word_starts_map_ = word_starts_map_; | 486 data_copy->word_starts_map_ = word_starts_map_; |
487 return data_copy; | 487 return data_copy; |
488 // Not copied: | 488 // Not copied: |
489 // search_term_cache_ | 489 // search_term_cache_ |
490 // pre_filter_item_count_ | 490 // pre_filter_item_count_ |
491 // post_filter_item_count_ | 491 // post_filter_item_count_ |
492 // post_scoring_item_count_ | 492 // post_scoring_item_count_ |
493 }; | 493 } |
494 | 494 |
495 bool URLIndexPrivateData::Empty() const { | 495 bool URLIndexPrivateData::Empty() const { |
496 return history_info_map_.empty(); | 496 return history_info_map_.empty(); |
497 } | 497 } |
498 | 498 |
499 void URLIndexPrivateData::Clear() { | 499 void URLIndexPrivateData::Clear() { |
500 last_time_rebuilt_from_history_ = base::Time(); | 500 last_time_rebuilt_from_history_ = base::Time(); |
501 word_list_.clear(); | 501 word_list_.clear(); |
502 available_words_.clear(); | 502 available_words_.clear(); |
503 word_map_.clear(); | 503 word_map_.clear(); |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 word_starts_map_[history_id] = word_starts; | 717 word_starts_map_[history_id] = word_starts; |
718 | 718 |
719 // Update the recent visits information or schedule the update | 719 // Update the recent visits information or schedule the update |
720 // as appropriate. | 720 // as appropriate. |
721 if (history_db) { | 721 if (history_db) { |
722 // We'd like to check that we're on the history DB thread. | 722 // We'd like to check that we're on the history DB thread. |
723 // However, unittest code actually calls this on the UI thread. | 723 // However, unittest code actually calls this on the UI thread. |
724 // So we don't do any thread checks. | 724 // So we don't do any thread checks. |
725 VisitVector recent_visits; | 725 VisitVector recent_visits; |
726 // Make sure the private data is going to get as many recent visits as | 726 // Make sure the private data is going to get as many recent visits as |
727 // ScoredHistoryMatch::GetFrecency() hopes to use. | 727 // ScoredHistoryMatch::GetFrequency() hopes to use. |
728 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); | 728 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); |
729 if (history_db->GetMostRecentVisitsForURL(row_id, | 729 if (history_db->GetMostRecentVisitsForURL(row_id, |
730 kMaxVisitsToStoreInCache, | 730 kMaxVisitsToStoreInCache, |
731 &recent_visits)) | 731 &recent_visits)) |
732 UpdateRecentVisits(row_id, recent_visits); | 732 UpdateRecentVisits(row_id, recent_visits); |
733 } else { | 733 } else { |
734 DCHECK(history_service); | 734 DCHECK(history_service); |
735 ScheduleUpdateRecentVisits(history_service, row_id); | 735 ScheduleUpdateRecentVisits(history_service, row_id); |
736 } | 736 } |
737 | 737 |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1257 : used_(true) {} | 1257 : used_(true) {} |
1258 | 1258 |
1259 URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {} | 1259 URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {} |
1260 | 1260 |
1261 | 1261 |
1262 // URLIndexPrivateData::AddHistoryMatch ---------------------------------------- | 1262 // URLIndexPrivateData::AddHistoryMatch ---------------------------------------- |
1263 | 1263 |
1264 URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch( | 1264 URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch( |
1265 const URLIndexPrivateData& private_data, | 1265 const URLIndexPrivateData& private_data, |
1266 const std::string& languages, | 1266 const std::string& languages, |
1267 BookmarkService* bookmark_service, | 1267 HistoryClient* history_client, |
1268 const base::string16& lower_string, | 1268 const base::string16& lower_string, |
1269 const String16Vector& lower_terms, | 1269 const String16Vector& lower_terms, |
1270 const base::Time now) | 1270 const base::Time now) |
1271 : private_data_(private_data), | 1271 : private_data_(private_data), |
1272 languages_(languages), | 1272 languages_(languages), |
1273 bookmark_service_(bookmark_service), | 1273 history_client_(history_client), |
1274 lower_string_(lower_string), | 1274 lower_string_(lower_string), |
1275 lower_terms_(lower_terms), | 1275 lower_terms_(lower_terms), |
1276 now_(now) { | 1276 now_(now) { |
1277 // Calculate offsets for each term. For instance, the offset for | 1277 // Calculate offsets for each term. For instance, the offset for |
1278 // ".net" should be 1, indicating that the actual word-part of the term | 1278 // ".net" should be 1, indicating that the actual word-part of the term |
1279 // starts at offset 1. | 1279 // starts at offset 1. |
1280 lower_terms_to_word_starts_offsets_.resize(lower_terms_.size(), 0u); | 1280 lower_terms_to_word_starts_offsets_.resize(lower_terms_.size(), 0u); |
1281 for (size_t i = 0; i < lower_terms_.size(); ++i) { | 1281 for (size_t i = 0; i < lower_terms_.size(); ++i) { |
1282 base::i18n::BreakIterator iter(lower_terms_[i], | 1282 base::i18n::BreakIterator iter(lower_terms_[i], |
1283 base::i18n::BreakIterator::BREAK_WORD); | 1283 base::i18n::BreakIterator::BREAK_WORD); |
(...skipping 15 matching lines...) Expand all Loading... |
1299 HistoryInfoMap::const_iterator hist_pos = | 1299 HistoryInfoMap::const_iterator hist_pos = |
1300 private_data_.history_info_map_.find(history_id); | 1300 private_data_.history_info_map_.find(history_id); |
1301 if (hist_pos != private_data_.history_info_map_.end()) { | 1301 if (hist_pos != private_data_.history_info_map_.end()) { |
1302 const URLRow& hist_item = hist_pos->second.url_row; | 1302 const URLRow& hist_item = hist_pos->second.url_row; |
1303 const VisitInfoVector& visits = hist_pos->second.visits; | 1303 const VisitInfoVector& visits = hist_pos->second.visits; |
1304 WordStartsMap::const_iterator starts_pos = | 1304 WordStartsMap::const_iterator starts_pos = |
1305 private_data_.word_starts_map_.find(history_id); | 1305 private_data_.word_starts_map_.find(history_id); |
1306 DCHECK(starts_pos != private_data_.word_starts_map_.end()); | 1306 DCHECK(starts_pos != private_data_.word_starts_map_.end()); |
1307 ScoredHistoryMatch match(hist_item, visits, languages_, lower_string_, | 1307 ScoredHistoryMatch match(hist_item, visits, languages_, lower_string_, |
1308 lower_terms_, lower_terms_to_word_starts_offsets_, | 1308 lower_terms_, lower_terms_to_word_starts_offsets_, |
1309 starts_pos->second, now_, bookmark_service_); | 1309 starts_pos->second, now_, history_client_); |
1310 if (match.raw_score() > 0) | 1310 if (match.raw_score() > 0) |
1311 scored_matches_.push_back(match); | 1311 scored_matches_.push_back(match); |
1312 } | 1312 } |
1313 } | 1313 } |
1314 | 1314 |
1315 | 1315 |
1316 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- | 1316 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- |
1317 | 1317 |
1318 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( | 1318 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( |
1319 const HistoryInfoMap& history_info_map) | 1319 const HistoryInfoMap& history_info_map) |
(...skipping 18 matching lines...) Expand all Loading... |
1338 // recently visited (within the last 12/24 hours) as highly important. Get | 1338 // recently visited (within the last 12/24 hours) as highly important. Get |
1339 // input from mpearson. | 1339 // input from mpearson. |
1340 if (r1.typed_count() != r2.typed_count()) | 1340 if (r1.typed_count() != r2.typed_count()) |
1341 return (r1.typed_count() > r2.typed_count()); | 1341 return (r1.typed_count() > r2.typed_count()); |
1342 if (r1.visit_count() != r2.visit_count()) | 1342 if (r1.visit_count() != r2.visit_count()) |
1343 return (r1.visit_count() > r2.visit_count()); | 1343 return (r1.visit_count() > r2.visit_count()); |
1344 return (r1.last_visit() > r2.last_visit()); | 1344 return (r1.last_visit() > r2.last_visit()); |
1345 } | 1345 } |
1346 | 1346 |
1347 } // namespace history | 1347 } // namespace history |
OLD | NEW |