Chromium Code Reviews| 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/autocomplete/network_action_predictor.h" | 5 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete.h" | 17 #include "chrome/browser/autocomplete/autocomplete.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_match.h" | 18 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 19 #include "chrome/browser/autocomplete/network_action_predictor_database.h" | |
| 20 #include "chrome/browser/history/history.h" | 19 #include "chrome/browser/history/history.h" |
| 21 #include "chrome/browser/history/history_notifications.h" | 20 #include "chrome/browser/history/history_notifications.h" |
| 22 #include "chrome/browser/history/in_memory_database.h" | 21 #include "chrome/browser/history/in_memory_database.h" |
| 22 #include "chrome/browser/predictors/predictor_database.h" | |
| 23 #include "chrome/browser/predictors/predictor_database_factory.h" | |
| 23 #include "chrome/browser/prerender/prerender_field_trial.h" | 24 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 24 #include "chrome/browser/prerender/prerender_manager.h" | 25 #include "chrome/browser/prerender/prerender_manager.h" |
| 25 #include "chrome/browser/prerender/prerender_manager_factory.h" | 26 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
| 28 #include "chrome/common/guid.h" | 29 #include "chrome/common/guid.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_details.h" | 31 #include "content/public/browser/notification_details.h" |
| 31 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 32 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 const float kConfidenceCutoff[] = { | 37 const float kConfidenceCutoff[] = { |
| 37 0.8f, | 38 0.8f, |
| 38 0.5f | 39 0.5f |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 const size_t kMinimumUserTextLength = 1; | 42 const size_t kMinimumUserTextLength = 1; |
| 42 const int kMinimumNumberOfHits = 3; | 43 const int kMinimumNumberOfHits = 3; |
| 43 | 44 |
| 44 COMPILE_ASSERT(arraysize(kConfidenceCutoff) == | 45 COMPILE_ASSERT(arraysize(kConfidenceCutoff) == |
| 45 NetworkActionPredictor::LAST_PREDICT_ACTION, | 46 predictors::AutocompleteActionPredictor::LAST_PREDICT_ACTION, |
| 46 ConfidenceCutoff_count_mismatch); | 47 ConfidenceCutoff_count_mismatch); |
| 47 | 48 |
| 48 enum DatabaseAction { | 49 enum DatabaseAction { |
| 49 DATABASE_ACTION_ADD, | 50 DATABASE_ACTION_ADD, |
| 50 DATABASE_ACTION_UPDATE, | 51 DATABASE_ACTION_UPDATE, |
| 51 DATABASE_ACTION_DELETE_SOME, | 52 DATABASE_ACTION_DELETE_SOME, |
| 52 DATABASE_ACTION_DELETE_ALL, | 53 DATABASE_ACTION_DELETE_ALL, |
| 53 DATABASE_ACTION_COUNT | 54 DATABASE_ACTION_COUNT |
| 54 }; | 55 }; |
| 55 | 56 |
| 56 bool IsAutocompleteMatchSearchType(const AutocompleteMatch& match) { | 57 bool IsAutocompleteMatchSearchType(const AutocompleteMatch& match) { |
| 57 switch (match.type) { | 58 switch (match.type) { |
| 58 // Matches using the user's default search engine. | 59 // Matches using the user's default search engine. |
| 59 case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED: | 60 case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED: |
| 60 case AutocompleteMatch::SEARCH_HISTORY: | 61 case AutocompleteMatch::SEARCH_HISTORY: |
| 61 case AutocompleteMatch::SEARCH_SUGGEST: | 62 case AutocompleteMatch::SEARCH_SUGGEST: |
| 62 // A match that uses a non-default search engine (e.g. for tab-to-search). | 63 // A match that uses a non-default search engine (e.g. for tab-to-search). |
| 63 case AutocompleteMatch::SEARCH_OTHER_ENGINE: | 64 case AutocompleteMatch::SEARCH_OTHER_ENGINE: |
| 64 return true; | 65 return true; |
| 65 | 66 |
| 66 default: | 67 default: |
| 67 return false; | 68 return false; |
| 68 } | 69 } |
| 69 } | 70 } |
| 70 | 71 |
| 71 } | 72 } // namespace |
| 72 | 73 |
| 73 const int NetworkActionPredictor::kMaximumDaysToKeepEntry = 14; | 74 namespace predictors { |
| 74 | 75 |
| 75 double NetworkActionPredictor::hit_weight_ = 1.0; | 76 const int AutocompleteActionPredictor::kMaximumDaysToKeepEntry = 14; |
| 76 | 77 |
| 77 NetworkActionPredictor::NetworkActionPredictor(Profile* profile) | 78 double AutocompleteActionPredictor::hit_weight_ = 1.0; |
| 79 | |
| 80 AutocompleteActionPredictor::AutocompleteActionPredictor(Profile* profile) | |
| 78 : profile_(profile), | 81 : profile_(profile), |
| 79 db_(new NetworkActionPredictorDatabase(profile)), | 82 db_(PredictorDatabaseFactory::GetForProfile( |
| 83 profile)->autocomplete_table()), | |
| 80 initialized_(false) { | 84 initialized_(false) { |
| 81 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | |
| 82 base::Bind(&NetworkActionPredictorDatabase::Initialize, db_)); | |
| 83 | |
| 84 // Request the in-memory database from the history to force it to load so it's | 85 // Request the in-memory database from the history to force it to load so it's |
| 85 // available as soon as possible. | 86 // available as soon as possible. |
| 86 HistoryService* history_service = | 87 HistoryService* history_service = |
| 87 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 88 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 88 if (history_service) | 89 if (history_service) |
| 89 history_service->InMemoryDatabase(); | 90 history_service->InMemoryDatabase(); |
| 90 | 91 |
| 91 // Create local caches using the database as loaded. We will garbage collect | 92 // Create local caches using the database as loaded. We will garbage collect |
| 92 // rows from the caches and the database once the history service is | 93 // rows from the caches and the database once the history service is |
| 93 // available. | 94 // available. |
| 94 std::vector<NetworkActionPredictorDatabase::Row>* rows = | 95 std::vector<AutocompleteActionPredictorTable::Row>* rows = |
| 95 new std::vector<NetworkActionPredictorDatabase::Row>(); | 96 new std::vector<AutocompleteActionPredictorTable::Row>(); |
| 96 content::BrowserThread::PostTaskAndReply( | 97 content::BrowserThread::PostTaskAndReply( |
| 97 content::BrowserThread::DB, FROM_HERE, | 98 content::BrowserThread::DB, FROM_HERE, |
| 98 base::Bind(&NetworkActionPredictorDatabase::GetAllRows, db_, rows), | 99 base::Bind(&AutocompleteActionPredictorTable::GetAllRows, |
| 99 base::Bind(&NetworkActionPredictor::CreateCaches, AsWeakPtr(), | 100 db_, |
| 101 rows), | |
| 102 base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(), | |
| 100 base::Owned(rows))); | 103 base::Owned(rows))); |
| 101 | |
| 102 } | 104 } |
| 103 | 105 |
| 104 NetworkActionPredictor::~NetworkActionPredictor() { | 106 AutocompleteActionPredictor::~AutocompleteActionPredictor() { |
| 105 } | 107 } |
| 106 | 108 |
| 107 void NetworkActionPredictor::RegisterTransitionalMatches( | 109 void AutocompleteActionPredictor::RegisterTransitionalMatches( |
| 108 const string16& user_text, | 110 const string16& user_text, |
| 109 const AutocompleteResult& result) { | 111 const AutocompleteResult& result) { |
| 110 if (user_text.length() < kMinimumUserTextLength) | 112 if (user_text.length() < kMinimumUserTextLength) |
| 111 return; | 113 return; |
| 112 const string16 lower_user_text(base::i18n::ToLower(user_text)); | 114 const string16 lower_user_text(base::i18n::ToLower(user_text)); |
| 113 | 115 |
| 114 // Merge this in to an existing match if we already saw |user_text| | 116 // Merge this in to an existing match if we already saw |user_text| |
| 115 std::vector<TransitionalMatch>::iterator match_it = | 117 std::vector<TransitionalMatch>::iterator match_it = |
| 116 std::find(transitional_matches_.begin(), transitional_matches_.end(), | 118 std::find(transitional_matches_.begin(), transitional_matches_.end(), |
| 117 lower_user_text); | 119 lower_user_text); |
| 118 | 120 |
| 119 if (match_it == transitional_matches_.end()) { | 121 if (match_it == transitional_matches_.end()) { |
| 120 TransitionalMatch transitional_match; | 122 TransitionalMatch transitional_match; |
| 121 transitional_match.user_text = lower_user_text; | 123 transitional_match.user_text = lower_user_text; |
| 122 match_it = transitional_matches_.insert(transitional_matches_.end(), | 124 match_it = transitional_matches_.insert(transitional_matches_.end(), |
| 123 transitional_match); | 125 transitional_match); |
| 124 } | 126 } |
| 125 | 127 |
| 126 for (AutocompleteResult::const_iterator it = result.begin(); | 128 for (AutocompleteResult::const_iterator it = result.begin(); |
| 127 it != result.end(); ++it) { | 129 it != result.end(); ++it) { |
| 128 if (std::find(match_it->urls.begin(), match_it->urls.end(), | 130 if (std::find(match_it->urls.begin(), match_it->urls.end(), |
| 129 it->destination_url) == match_it->urls.end()) { | 131 it->destination_url) == match_it->urls.end()) { |
| 130 match_it->urls.push_back(it->destination_url); | 132 match_it->urls.push_back(it->destination_url); |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 135 void NetworkActionPredictor::ClearTransitionalMatches() { | 137 void AutocompleteActionPredictor::ClearTransitionalMatches() { |
| 136 transitional_matches_.clear(); | 138 transitional_matches_.clear(); |
| 137 } | 139 } |
| 138 | 140 |
| 139 // Given a match, return a recommended action. | 141 // Given a match, return a recommended action. |
| 140 NetworkActionPredictor::Action NetworkActionPredictor::RecommendAction( | 142 AutocompleteActionPredictor::Action |
| 141 const string16& user_text, | 143 AutocompleteActionPredictor::RecommendAction( |
| 142 const AutocompleteMatch& match) const { | 144 const string16& user_text, |
| 145 const AutocompleteMatch& match) const { | |
| 143 bool is_in_db = false; | 146 bool is_in_db = false; |
| 144 const double confidence = CalculateConfidence(user_text, match, &is_in_db); | 147 const double confidence = CalculateConfidence(user_text, match, &is_in_db); |
| 145 DCHECK(confidence >= 0.0 && confidence <= 1.0); | 148 DCHECK(confidence >= 0.0 && confidence <= 1.0); |
| 146 | 149 |
| 147 UMA_HISTOGRAM_BOOLEAN("NetworkActionPredictor.MatchIsInDb", is_in_db); | 150 UMA_HISTOGRAM_BOOLEAN("AutocompleteActionPredictor.MatchIsInDb", is_in_db); |
| 148 | 151 |
| 149 if (is_in_db) { | 152 if (is_in_db) { |
| 150 // Multiple enties with the same URL are fine as the confidence may be | 153 // Multiple enties with the same URL are fine as the confidence may be |
| 151 // different. | 154 // different. |
| 152 tracked_urls_.push_back(std::make_pair(match.destination_url, confidence)); | 155 tracked_urls_.push_back(std::make_pair(match.destination_url, confidence)); |
| 153 UMA_HISTOGRAM_COUNTS_100("NetworkActionPredictor.Confidence", | 156 UMA_HISTOGRAM_COUNTS_100("AutocompleteActionPredictor.Confidence", |
| 154 confidence * 100); | 157 confidence * 100); |
| 155 } | 158 } |
| 156 | 159 |
| 157 // Map the confidence to an action. | 160 // Map the confidence to an action. |
| 158 Action action = ACTION_NONE; | 161 Action action = ACTION_NONE; |
| 159 for (int i = 0; i < LAST_PREDICT_ACTION; ++i) { | 162 for (int i = 0; i < LAST_PREDICT_ACTION; ++i) { |
| 160 if (confidence >= kConfidenceCutoff[i]) { | 163 if (confidence >= kConfidenceCutoff[i]) { |
| 161 action = static_cast<Action>(i); | 164 action = static_cast<Action>(i); |
| 162 break; | 165 break; |
| 163 } | 166 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 174 !prerender::IsOmniboxEnabled(profile_))) { | 177 !prerender::IsOmniboxEnabled(profile_))) { |
| 175 action = ACTION_PRECONNECT; | 178 action = ACTION_PRECONNECT; |
| 176 } | 179 } |
| 177 | 180 |
| 178 return action; | 181 return action; |
| 179 } | 182 } |
| 180 | 183 |
| 181 // Return true if the suggestion type warrants a TCP/IP preconnection. | 184 // Return true if the suggestion type warrants a TCP/IP preconnection. |
| 182 // i.e., it is now quite likely that the user will select the related domain. | 185 // i.e., it is now quite likely that the user will select the related domain. |
| 183 // static | 186 // static |
| 184 bool NetworkActionPredictor::IsPreconnectable(const AutocompleteMatch& match) { | 187 bool AutocompleteActionPredictor::IsPreconnectable( |
| 188 const AutocompleteMatch& match) { | |
| 185 return IsAutocompleteMatchSearchType(match); | 189 return IsAutocompleteMatchSearchType(match); |
| 186 } | 190 } |
| 187 | 191 |
| 188 void NetworkActionPredictor::Shutdown() { | 192 void AutocompleteActionPredictor::Shutdown() { |
| 189 db_->OnPredictorDestroyed(); | |
| 190 } | 193 } |
| 191 | 194 |
| 192 void NetworkActionPredictor::Observe( | 195 void AutocompleteActionPredictor::Observe( |
| 193 int type, | 196 int type, |
| 194 const content::NotificationSource& source, | 197 const content::NotificationSource& source, |
| 195 const content::NotificationDetails& details) { | 198 const content::NotificationDetails& details) { |
| 196 switch (type) { | 199 switch (type) { |
| 197 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { | 200 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { |
| 198 DCHECK(initialized_); | 201 DCHECK(initialized_); |
| 199 const content::Details<const history::URLsDeletedDetails> | 202 const content::Details<const history::URLsDeletedDetails> |
| 200 urls_deleted_details = | 203 urls_deleted_details = |
| 201 content::Details<const history::URLsDeletedDetails>(details); | 204 content::Details<const history::URLsDeletedDetails>(details); |
| 202 if (urls_deleted_details->all_history) | 205 if (urls_deleted_details->all_history) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 227 content::Source<Profile>(profile_)); | 230 content::Source<Profile>(profile_)); |
| 228 break; | 231 break; |
| 229 } | 232 } |
| 230 | 233 |
| 231 default: | 234 default: |
| 232 NOTREACHED() << "Unexpected notification observed."; | 235 NOTREACHED() << "Unexpected notification observed."; |
| 233 break; | 236 break; |
| 234 } | 237 } |
| 235 } | 238 } |
| 236 | 239 |
| 237 void NetworkActionPredictor::OnOmniboxOpenedUrl(const AutocompleteLog& log) { | 240 void AutocompleteActionPredictor::OnOmniboxOpenedUrl( |
| 241 const AutocompleteLog& log) { | |
| 238 if (log.text.length() < kMinimumUserTextLength) | 242 if (log.text.length() < kMinimumUserTextLength) |
| 239 return; | 243 return; |
| 240 | 244 |
| 241 const AutocompleteMatch& match = log.result.match_at(log.selected_index); | 245 const AutocompleteMatch& match = log.result.match_at(log.selected_index); |
| 242 | 246 |
| 243 UMA_HISTOGRAM_BOOLEAN( | 247 UMA_HISTOGRAM_BOOLEAN( |
| 244 StringPrintf("Prerender.OmniboxNavigationsCouldPrerender_%.1f%s", | 248 StringPrintf("Prerender.OmniboxNavigationsCouldPrerender_%.1f%s", |
| 245 get_hit_weight(), | 249 get_hit_weight(), |
| 246 prerender::PrerenderManager::GetModeString()).c_str(), | 250 prerender::PrerenderManager::GetModeString()).c_str(), |
| 247 prerender::IsOmniboxEnabled(profile_)); | 251 prerender::IsOmniboxEnabled(profile_)); |
| 248 | 252 |
| 249 const GURL& opened_url = match.destination_url; | 253 const GURL& opened_url = match.destination_url; |
| 250 | 254 |
| 251 // If the Omnibox triggered a prerender but the URL doesn't match the one the | 255 // If the Omnibox triggered a prerender but the URL doesn't match the one the |
| 252 // user is navigating to, cancel the prerender. | 256 // user is navigating to, cancel the prerender. |
| 253 prerender::PrerenderManager* prerender_manager = | 257 prerender::PrerenderManager* prerender_manager = |
| 254 prerender::PrerenderManagerFactory::GetForProfile(profile_); | 258 prerender::PrerenderManagerFactory::GetForProfile(profile_); |
| 255 // |prerender_manager| can be NULL in incognito mode or if prerendering is | 259 // |prerender_manager| can be NULL in incognito mode or if prerendering is |
| 256 // otherwise disabled. | 260 // otherwise disabled. |
| 257 if (prerender_manager && !prerender_manager->IsPrerendering(opened_url)) | 261 if (prerender_manager && !prerender_manager->IsPrerendering(opened_url)) |
| 258 prerender_manager->CancelOmniboxPrerenders(); | 262 prerender_manager->CancelOmniboxPrerenders(); |
| 259 | 263 |
| 260 const string16 lower_user_text(base::i18n::ToLower(log.text)); | 264 const string16 lower_user_text(base::i18n::ToLower(log.text)); |
| 261 | 265 |
| 262 BeginTransaction(); | |
| 263 // Traverse transitional matches for those that have a user_text that is a | 266 // Traverse transitional matches for those that have a user_text that is a |
| 264 // prefix of |lower_user_text|. | 267 // prefix of |lower_user_text|. |
| 268 std::vector<AutocompleteActionPredictorTable::Row> rows_to_add; | |
| 269 std::vector<AutocompleteActionPredictorTable::Row> rows_to_update; | |
| 270 | |
| 265 for (std::vector<TransitionalMatch>::const_iterator it = | 271 for (std::vector<TransitionalMatch>::const_iterator it = |
| 266 transitional_matches_.begin(); it != transitional_matches_.end(); | 272 transitional_matches_.begin(); it != transitional_matches_.end(); |
| 267 ++it) { | 273 ++it) { |
| 268 if (!StartsWith(lower_user_text, it->user_text, true)) | 274 if (!StartsWith(lower_user_text, it->user_text, true)) |
| 269 continue; | 275 continue; |
| 270 | 276 |
| 271 // Add entries to the database for those matches. | 277 // Add entries to the database for those matches. |
| 272 for (std::vector<GURL>::const_iterator url_it = it->urls.begin(); | 278 for (std::vector<GURL>::const_iterator url_it = it->urls.begin(); |
| 273 url_it != it->urls.end(); ++url_it) { | 279 url_it != it->urls.end(); ++url_it) { |
| 274 DCHECK(it->user_text.length() >= kMinimumUserTextLength); | 280 DCHECK(it->user_text.length() >= kMinimumUserTextLength); |
| 275 const DBCacheKey key = { it->user_text, *url_it }; | 281 const DBCacheKey key = { it->user_text, *url_it }; |
| 276 const bool is_hit = (*url_it == opened_url); | 282 const bool is_hit = (*url_it == opened_url); |
| 277 | 283 |
| 278 NetworkActionPredictorDatabase::Row row; | 284 AutocompleteActionPredictorTable::Row row; |
| 279 row.user_text = key.user_text; | 285 row.user_text = key.user_text; |
| 280 row.url = key.url; | 286 row.url = key.url; |
| 281 | 287 |
| 282 DBCacheMap::iterator it = db_cache_.find(key); | 288 DBCacheMap::iterator it = db_cache_.find(key); |
| 283 if (it == db_cache_.end()) { | 289 if (it == db_cache_.end()) { |
| 284 row.id = guid::GenerateGUID(); | 290 row.id = guid::GenerateGUID(); |
| 285 row.number_of_hits = is_hit ? 1 : 0; | 291 row.number_of_hits = is_hit ? 1 : 0; |
| 286 row.number_of_misses = is_hit ? 0 : 1; | 292 row.number_of_misses = is_hit ? 0 : 1; |
| 287 | 293 |
| 288 AddRow(key, row); | 294 DBCacheValue value = { row.number_of_hits, row.number_of_misses }; |
| 295 db_cache_[key] = value; | |
| 296 db_id_cache_[key] = row.id; | |
| 297 | |
| 298 rows_to_add.push_back(row); | |
| 299 UMA_HISTOGRAM_ENUMERATION( | |
| 300 "AutocompleteActionPredictor.DatabaseAction", | |
| 301 DATABASE_ACTION_ADD, DATABASE_ACTION_COUNT); | |
| 289 } else { | 302 } else { |
| 290 DCHECK(db_id_cache_.find(key) != db_id_cache_.end()); | 303 DCHECK(db_id_cache_.find(key) != db_id_cache_.end()); |
| 291 row.id = db_id_cache_.find(key)->second; | 304 row.id = db_id_cache_.find(key)->second; |
| 292 row.number_of_hits = it->second.number_of_hits + (is_hit ? 1 : 0); | 305 row.number_of_hits = it->second.number_of_hits + (is_hit ? 1 : 0); |
| 293 row.number_of_misses = it->second.number_of_misses + (is_hit ? 0 : 1); | 306 row.number_of_misses = it->second.number_of_misses + (is_hit ? 0 : 1); |
| 294 | 307 |
| 295 UpdateRow(it, row); | 308 it->second.number_of_hits = row.number_of_hits; |
| 309 it->second.number_of_misses = row.number_of_misses; | |
| 310 | |
| 311 rows_to_update.push_back(row); | |
| 312 UMA_HISTOGRAM_ENUMERATION( | |
| 313 "AutocompleteActionPredictor.DatabaseAction", | |
| 314 DATABASE_ACTION_UPDATE, DATABASE_ACTION_COUNT); | |
| 296 } | 315 } |
| 297 } | 316 } |
| 298 } | 317 } |
| 299 CommitTransaction(); | 318 if (rows_to_add.size() > 0 || rows_to_update.size() > 0) |
| 319 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | |
| 320 base::Bind(&AutocompleteActionPredictorTable::AddAndUpdateRows, | |
| 321 db_, | |
| 322 rows_to_add, | |
| 323 rows_to_update)); | |
| 300 | 324 |
| 301 ClearTransitionalMatches(); | 325 ClearTransitionalMatches(); |
| 302 | 326 |
| 303 // Check against tracked urls and log accuracy for the confidence we | 327 // Check against tracked urls and log accuracy for the confidence we |
| 304 // predicted. | 328 // predicted. |
| 305 for (std::vector<std::pair<GURL, double> >::const_iterator it = | 329 for (std::vector<std::pair<GURL, double> >::const_iterator it = |
| 306 tracked_urls_.begin(); it != tracked_urls_.end(); | 330 tracked_urls_.begin(); it != tracked_urls_.end(); |
| 307 ++it) { | 331 ++it) { |
| 308 if (opened_url == it->first) { | 332 if (opened_url == it->first) { |
| 309 UMA_HISTOGRAM_COUNTS_100("NetworkActionPredictor.AccurateCount", | 333 UMA_HISTOGRAM_COUNTS_100("AutocompleteActionPredictor.AccurateCount", |
| 310 it->second * 100); | 334 it->second * 100); |
| 311 } | 335 } |
| 312 } | 336 } |
| 313 tracked_urls_.clear(); | 337 tracked_urls_.clear(); |
| 314 } | 338 } |
| 315 | 339 |
| 316 void NetworkActionPredictor::DeleteOldIdsFromCaches( | 340 void AutocompleteActionPredictor::DeleteOldIdsFromCaches( |
| 317 history::URLDatabase* url_db, | 341 history::URLDatabase* url_db, |
| 318 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list) { | 342 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list) { |
| 319 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 343 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 320 DCHECK(url_db); | 344 DCHECK(url_db); |
| 321 DCHECK(id_list); | 345 DCHECK(id_list); |
| 322 id_list->clear(); | 346 id_list->clear(); |
| 323 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { | 347 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { |
| 324 history::URLRow url_row; | 348 history::URLRow url_row; |
| 325 | 349 |
| 326 if ((url_db->GetRowForURL(it->first.url, &url_row) == 0) || | 350 if ((url_db->GetRowForURL(it->first.url, &url_row) == 0) || |
| 327 ((base::Time::Now() - url_row.last_visit()).InDays() > | 351 ((base::Time::Now() - url_row.last_visit()).InDays() > |
| 328 kMaximumDaysToKeepEntry)) { | 352 kMaximumDaysToKeepEntry)) { |
| 329 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); | 353 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); |
| 330 DCHECK(id_it != db_id_cache_.end()); | 354 DCHECK(id_it != db_id_cache_.end()); |
| 331 id_list->push_back(id_it->second); | 355 id_list->push_back(id_it->second); |
| 332 db_id_cache_.erase(id_it); | 356 db_id_cache_.erase(id_it); |
| 333 db_cache_.erase(it++); | 357 db_cache_.erase(it++); |
| 334 } else { | 358 } else { |
| 335 ++it; | 359 ++it; |
| 336 } | 360 } |
| 337 } | 361 } |
| 338 } | 362 } |
| 339 | 363 |
| 340 void NetworkActionPredictor::DeleteOldEntries(history::URLDatabase* url_db) { | 364 void AutocompleteActionPredictor::DeleteOldEntries( |
| 365 history::URLDatabase* url_db) { | |
| 341 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 366 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 342 DCHECK(!initialized_); | 367 DCHECK(!initialized_); |
| 343 | 368 |
| 344 std::vector<NetworkActionPredictorDatabase::Row::Id> ids_to_delete; | 369 std::vector<AutocompleteActionPredictorTable::Row::Id> ids_to_delete; |
| 345 DeleteOldIdsFromCaches(url_db, &ids_to_delete); | 370 DeleteOldIdsFromCaches(url_db, &ids_to_delete); |
| 346 | 371 |
| 347 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 372 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 348 base::Bind(&NetworkActionPredictorDatabase::DeleteRows, db_, | 373 base::Bind(&AutocompleteActionPredictorTable::DeleteRows, |
| 374 db_, | |
| 349 ids_to_delete)); | 375 ids_to_delete)); |
| 350 | 376 |
| 351 // Register for notifications and set the |initialized_| flag. | 377 // Register for notifications and set the |initialized_| flag. |
| 352 notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 378 notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 353 content::Source<Profile>(profile_)); | 379 content::Source<Profile>(profile_)); |
| 354 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 380 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 355 content::Source<Profile>(profile_)); | 381 content::Source<Profile>(profile_)); |
| 356 initialized_ = true; | 382 initialized_ = true; |
| 357 } | 383 } |
| 358 | 384 |
| 359 void NetworkActionPredictor::CreateCaches( | 385 void AutocompleteActionPredictor::CreateCaches( |
| 360 std::vector<NetworkActionPredictorDatabase::Row>* rows) { | 386 std::vector<AutocompleteActionPredictorTable::Row>* rows) { |
| 361 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 387 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 362 DCHECK(!initialized_); | 388 DCHECK(!initialized_); |
| 363 DCHECK(db_cache_.empty()); | 389 DCHECK(db_cache_.empty()); |
| 364 DCHECK(db_id_cache_.empty()); | 390 DCHECK(db_id_cache_.empty()); |
| 365 | 391 |
| 366 for (std::vector<NetworkActionPredictorDatabase::Row>::const_iterator it = | 392 for (std::vector<AutocompleteActionPredictorTable::Row>::const_iterator it = |
| 367 rows->begin(); it != rows->end(); ++it) { | 393 rows->begin(); it != rows->end(); ++it) { |
| 368 const DBCacheKey key = { it->user_text, it->url }; | 394 const DBCacheKey key = { it->user_text, it->url }; |
| 369 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; | 395 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; |
| 370 db_cache_[key] = value; | 396 db_cache_[key] = value; |
| 371 db_id_cache_[key] = it->id; | 397 db_id_cache_[key] = it->id; |
| 372 } | 398 } |
| 373 | 399 |
| 374 // If the history service is ready, delete any old or invalid entries. | 400 // If the history service is ready, delete any old or invalid entries. |
| 375 HistoryService* history_service = | 401 HistoryService* history_service = |
| 376 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 402 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 377 if (!TryDeleteOldEntries(history_service)) { | 403 if (!TryDeleteOldEntries(history_service)) { |
| 378 // Wait for the notification that the history service is ready and the URL | 404 // Wait for the notification that the history service is ready and the URL |
| 379 // DB is loaded. | 405 // DB is loaded. |
| 380 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | 406 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, |
| 381 content::Source<Profile>(profile_)); | 407 content::Source<Profile>(profile_)); |
| 382 } | 408 } |
| 383 } | 409 } |
| 384 | 410 |
| 385 bool NetworkActionPredictor::TryDeleteOldEntries(HistoryService* service) { | 411 bool AutocompleteActionPredictor::TryDeleteOldEntries(HistoryService* service) { |
| 386 if (!service) | 412 if (!service) |
| 387 return false; | 413 return false; |
| 388 | 414 |
| 389 history::URLDatabase* url_db = service->InMemoryDatabase(); | 415 history::URLDatabase* url_db = service->InMemoryDatabase(); |
| 390 if (!url_db) | 416 if (!url_db) |
| 391 return false; | 417 return false; |
| 392 | 418 |
| 393 DeleteOldEntries(url_db); | 419 DeleteOldEntries(url_db); |
| 394 return true; | 420 return true; |
| 395 } | 421 } |
| 396 | 422 |
| 397 double NetworkActionPredictor::CalculateConfidence( | 423 double AutocompleteActionPredictor::CalculateConfidence( |
| 398 const string16& user_text, | 424 const string16& user_text, |
| 399 const AutocompleteMatch& match, | 425 const AutocompleteMatch& match, |
| 400 bool* is_in_db) const { | 426 bool* is_in_db) const { |
| 401 const DBCacheKey key = { user_text, match.destination_url }; | 427 const DBCacheKey key = { user_text, match.destination_url }; |
| 402 | 428 |
| 403 *is_in_db = false; | 429 *is_in_db = false; |
| 404 if (user_text.length() < kMinimumUserTextLength) | 430 if (user_text.length() < kMinimumUserTextLength) |
| 405 return 0.0; | 431 return 0.0; |
| 406 | 432 |
| 407 const DBCacheMap::const_iterator iter = db_cache_.find(key); | 433 const DBCacheMap::const_iterator iter = db_cache_.find(key); |
| 408 if (iter == db_cache_.end()) | 434 if (iter == db_cache_.end()) |
| 409 return 0.0; | 435 return 0.0; |
| 410 | 436 |
| 411 *is_in_db = true; | 437 *is_in_db = true; |
| 412 return CalculateConfidenceForDbEntry(iter); | 438 return CalculateConfidenceForDbEntry(iter); |
| 413 } | 439 } |
| 414 | 440 |
| 415 double NetworkActionPredictor::CalculateConfidenceForDbEntry( | 441 double AutocompleteActionPredictor::CalculateConfidenceForDbEntry( |
| 416 DBCacheMap::const_iterator iter) const { | 442 DBCacheMap::const_iterator iter) const { |
| 417 const DBCacheValue& value = iter->second; | 443 const DBCacheValue& value = iter->second; |
| 418 if (value.number_of_hits < kMinimumNumberOfHits) | 444 if (value.number_of_hits < kMinimumNumberOfHits) |
| 419 return 0.0; | 445 return 0.0; |
| 420 | 446 |
| 421 const double number_of_hits = value.number_of_hits * hit_weight_; | 447 const double number_of_hits = value.number_of_hits * hit_weight_; |
| 422 return number_of_hits / (number_of_hits + value.number_of_misses); | 448 return number_of_hits / (number_of_hits + value.number_of_misses); |
| 423 } | 449 } |
| 424 | 450 |
| 425 void NetworkActionPredictor::AddRow( | 451 void AutocompleteActionPredictor::AddRow( |
| 426 const DBCacheKey& key, | 452 const DBCacheKey& key, |
| 427 const NetworkActionPredictorDatabase::Row& row) { | 453 const AutocompleteActionPredictorTable::Row& row) { |
| 428 if (!initialized_) | 454 if (!initialized_) |
| 429 return; | 455 return; |
| 430 | 456 |
| 431 DBCacheValue value = { row.number_of_hits, row.number_of_misses }; | 457 DBCacheValue value = { row.number_of_hits, row.number_of_misses }; |
| 432 db_cache_[key] = value; | 458 db_cache_[key] = value; |
| 433 db_id_cache_[key] = row.id; | 459 db_id_cache_[key] = row.id; |
| 434 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 460 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 435 base::Bind(&NetworkActionPredictorDatabase::AddRow, db_, row)); | 461 base::Bind(&AutocompleteActionPredictorTable::AddRow, |
| 462 db_, | |
| 463 row)); | |
| 436 | 464 |
| 437 UMA_HISTOGRAM_ENUMERATION("NetworkActionPredictor.DatabaseAction", | 465 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", |
| 438 DATABASE_ACTION_ADD, DATABASE_ACTION_COUNT); | 466 DATABASE_ACTION_ADD, DATABASE_ACTION_COUNT); |
| 439 } | 467 } |
| 440 | 468 |
| 441 void NetworkActionPredictor::UpdateRow( | 469 void AutocompleteActionPredictor::UpdateRow( |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
Having an iterator argument seems to implicitly as
Shishir
2012/03/15 07:55:48
These two calls are needed only for unittests to b
| |
| 442 DBCacheMap::iterator it, | 470 DBCacheMap::iterator it, |
| 443 const NetworkActionPredictorDatabase::Row& row) { | 471 const AutocompleteActionPredictorTable::Row& row) { |
| 444 if (!initialized_) | 472 if (!initialized_) |
| 445 return; | 473 return; |
| 446 | 474 |
| 447 DCHECK(it != db_cache_.end()); | 475 DCHECK(it != db_cache_.end()); |
| 448 it->second.number_of_hits = row.number_of_hits; | 476 it->second.number_of_hits = row.number_of_hits; |
| 449 it->second.number_of_misses = row.number_of_misses; | 477 it->second.number_of_misses = row.number_of_misses; |
| 450 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 478 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 451 base::Bind(&NetworkActionPredictorDatabase::UpdateRow, db_, row)); | 479 base::Bind(&AutocompleteActionPredictorTable::UpdateRow, |
| 452 UMA_HISTOGRAM_ENUMERATION("NetworkActionPredictor.DatabaseAction", | 480 db_, |
| 481 row)); | |
| 482 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", | |
| 453 DATABASE_ACTION_UPDATE, DATABASE_ACTION_COUNT); | 483 DATABASE_ACTION_UPDATE, DATABASE_ACTION_COUNT); |
| 454 } | 484 } |
| 455 | 485 |
| 456 void NetworkActionPredictor::DeleteAllRows() { | 486 void AutocompleteActionPredictor::DeleteAllRows() { |
| 457 if (!initialized_) | 487 if (!initialized_) |
| 458 return; | 488 return; |
| 459 | 489 |
| 460 db_cache_.clear(); | 490 db_cache_.clear(); |
| 461 db_id_cache_.clear(); | 491 db_id_cache_.clear(); |
| 462 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 492 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 463 base::Bind(&NetworkActionPredictorDatabase::DeleteAllRows, db_)); | 493 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, |
| 464 UMA_HISTOGRAM_ENUMERATION("NetworkActionPredictor.DatabaseAction", | 494 db_)); |
| 495 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", | |
| 465 DATABASE_ACTION_DELETE_ALL, DATABASE_ACTION_COUNT); | 496 DATABASE_ACTION_DELETE_ALL, DATABASE_ACTION_COUNT); |
| 466 } | 497 } |
| 467 | 498 |
| 468 void NetworkActionPredictor::DeleteRowsWithURLs(const std::set<GURL>& urls) { | 499 void AutocompleteActionPredictor::DeleteRowsWithURLs( |
| 500 const std::set<GURL>& urls) { | |
| 469 if (!initialized_) | 501 if (!initialized_) |
| 470 return; | 502 return; |
| 471 | 503 |
| 472 std::vector<NetworkActionPredictorDatabase::Row::Id> id_list; | 504 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; |
| 473 | 505 |
| 474 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { | 506 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { |
| 475 if (urls.find(it->first.url) != urls.end()) { | 507 if (urls.find(it->first.url) != urls.end()) { |
| 476 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); | 508 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); |
| 477 DCHECK(id_it != db_id_cache_.end()); | 509 DCHECK(id_it != db_id_cache_.end()); |
| 478 id_list.push_back(id_it->second); | 510 id_list.push_back(id_it->second); |
| 479 db_id_cache_.erase(id_it); | 511 db_id_cache_.erase(id_it); |
| 480 db_cache_.erase(it++); | 512 db_cache_.erase(it++); |
| 481 } else { | 513 } else { |
| 482 ++it; | 514 ++it; |
| 483 } | 515 } |
| 484 } | 516 } |
| 485 | 517 |
| 486 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 518 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 487 base::Bind(&NetworkActionPredictorDatabase::DeleteRows, db_, id_list)); | 519 base::Bind(&AutocompleteActionPredictorTable::DeleteRows, |
| 488 UMA_HISTOGRAM_ENUMERATION("NetworkActionPredictor.DatabaseAction", | 520 db_, |
| 521 id_list)); | |
| 522 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", | |
| 489 DATABASE_ACTION_DELETE_SOME, DATABASE_ACTION_COUNT); | 523 DATABASE_ACTION_DELETE_SOME, DATABASE_ACTION_COUNT); |
| 490 } | 524 } |
| 491 | 525 |
| 492 void NetworkActionPredictor::BeginTransaction() { | 526 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { |
| 493 if (!initialized_) | |
| 494 return; | |
| 495 | |
| 496 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | |
| 497 base::Bind(&NetworkActionPredictorDatabase::BeginTransaction, db_)); | |
| 498 } | 527 } |
| 499 | 528 |
| 500 void NetworkActionPredictor::CommitTransaction() { | 529 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { |
| 501 if (!initialized_) | |
| 502 return; | |
| 503 | |
| 504 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | |
| 505 base::Bind(&NetworkActionPredictorDatabase::CommitTransaction, db_)); | |
| 506 } | 530 } |
| 507 | 531 |
| 508 NetworkActionPredictor::TransitionalMatch::TransitionalMatch() { | 532 } // namespace predictors |
| 509 } | |
| 510 | |
| 511 NetworkActionPredictor::TransitionalMatch::~TransitionalMatch() { | |
| 512 } | |
| OLD | NEW |