1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/predictors/autocomplete_action_predictor.h"
#include <math.h>
#include <vector>
#include "base/bind.h"
#include "base/guid.h"
#include "base/i18n/case_conversion.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_log.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/autocomplete_result.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/in_memory_database.h"
#include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
#include "chrome/browser/predictors/predictor_database.h"
#include "chrome/browser/predictors/predictor_database_factory.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/prerender/prerender_handle.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
namespace {
const float kConfidenceCutoff[] = {
0.8f,
0.5f
};
COMPILE_ASSERT(arraysize(kConfidenceCutoff) ==
predictors::AutocompleteActionPredictor::LAST_PREDICT_ACTION,
ConfidenceCutoff_count_mismatch);
const size_t kMinimumUserTextLength = 1;
const int kMinimumNumberOfHits = 3;
enum DatabaseAction {
DATABASE_ACTION_ADD,
DATABASE_ACTION_UPDATE,
DATABASE_ACTION_DELETE_SOME,
DATABASE_ACTION_DELETE_ALL,
DATABASE_ACTION_COUNT
};
} // namespace
namespace predictors {
const int AutocompleteActionPredictor::kMaximumDaysToKeepEntry = 14;
AutocompleteActionPredictor::AutocompleteActionPredictor(Profile* profile)
: profile_(profile),
main_profile_predictor_(NULL),
incognito_predictor_(NULL),
initialized_(false) {
if (profile_->IsOffTheRecord()) {
main_profile_predictor_ = AutocompleteActionPredictorFactory::GetForProfile(
profile_->GetOriginalProfile());
DCHECK(main_profile_predictor_);
main_profile_predictor_->incognito_predictor_ = this;
if (main_profile_predictor_->initialized_)
CopyFromMainProfile();
} else {
// Request the in-memory database from the history to force it to load so
// it's available as soon as possible.
HistoryService* history_service = HistoryServiceFactory::GetForProfile(
profile_, Profile::EXPLICIT_ACCESS);
if (history_service)
history_service->InMemoryDatabase();
table_ =
PredictorDatabaseFactory::GetForProfile(profile_)->autocomplete_table();
// Observe all main frame loads so we can wait for the first to complete
// before accessing DB and IO threads to build the local cache.
notification_registrar_.Add(this,
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::NotificationService::AllSources());
}
}
AutocompleteActionPredictor::~AutocompleteActionPredictor() {
if (main_profile_predictor_)
main_profile_predictor_->incognito_predictor_ = NULL;
else if (incognito_predictor_)
incognito_predictor_->main_profile_predictor_ = NULL;
if (prerender_handle_.get())
prerender_handle_->OnCancel();
}
void AutocompleteActionPredictor::RegisterTransitionalMatches(
const string16& user_text,
const AutocompleteResult& result) {
if (user_text.length() < kMinimumUserTextLength)
return;
const string16 lower_user_text(base::i18n::ToLower(user_text));
// Merge this in to an existing match if we already saw |user_text|
std::vector<TransitionalMatch>::iterator match_it =
std::find(transitional_matches_.begin(), transitional_matches_.end(),
lower_user_text);
if (match_it == transitional_matches_.end()) {
TransitionalMatch transitional_match;
transitional_match.user_text = lower_user_text;
match_it = transitional_matches_.insert(transitional_matches_.end(),
transitional_match);
}
for (AutocompleteResult::const_iterator i(result.begin()); i != result.end();
++i) {
if (std::find(match_it->urls.begin(), match_it->urls.end(),
i->destination_url) == match_it->urls.end()) {
match_it->urls.push_back(i->destination_url);
}
}
}
void AutocompleteActionPredictor::ClearTransitionalMatches() {
transitional_matches_.clear();
}
void AutocompleteActionPredictor::StartPrerendering(
const GURL& url,
const content::SessionStorageNamespaceMap& session_storage_namespace_map,
const gfx::Size& size) {
if (prerender_handle_)
prerender_handle_->OnCancel();
if (prerender::PrerenderManager* prerender_manager =
prerender::PrerenderManagerFactory::GetForProfile(profile_)) {
content::SessionStorageNamespace* session_storage_namespace = NULL;
content::SessionStorageNamespaceMap::const_iterator it =
session_storage_namespace_map.find("");
if (it != session_storage_namespace_map.end())
session_storage_namespace = it->second;
prerender_handle_.reset(
prerender_manager->AddPrerenderFromOmnibox(
url, session_storage_namespace, size));
} else {
prerender_handle_.reset();
}
}
// Given a match, return a recommended action.
AutocompleteActionPredictor::Action
AutocompleteActionPredictor::RecommendAction(
const string16& user_text,
const AutocompleteMatch& match) const {
bool is_in_db = false;
const double confidence = CalculateConfidence(user_text, match, &is_in_db);
DCHECK(confidence >= 0.0 && confidence <= 1.0);
UMA_HISTOGRAM_BOOLEAN("AutocompleteActionPredictor.MatchIsInDb", is_in_db);
if (is_in_db) {
// Multiple enties with the same URL are fine as the confidence may be
// different.
tracked_urls_.push_back(std::make_pair(match.destination_url, confidence));
UMA_HISTOGRAM_COUNTS_100("AutocompleteActionPredictor.Confidence",
confidence * 100);
}
// Map the confidence to an action.
Action action = ACTION_NONE;
for (int i = 0; i < LAST_PREDICT_ACTION; ++i) {
if (confidence >= kConfidenceCutoff[i]) {
action = static_cast<Action>(i);
break;
}
}
// Downgrade prerender to preconnect if this is a search match or if omnibox
// prerendering is disabled. There are cases when Instant will not handle a
// search suggestion and in those cases it would be good to prerender the
// search results, however search engines have not been set up to correctly
// handle being prerendered and until they are we should avoid it.
// http://crbug.com/117495
if (action == ACTION_PRERENDER &&
(AutocompleteMatch::IsSearchType(match.type) ||
!prerender::IsOmniboxEnabled(profile_))) {
action = ACTION_PRECONNECT;
}
return action;
}
// Return true if the suggestion type warrants a TCP/IP preconnection.
// i.e., it is now quite likely that the user will select the related domain.
// static
bool AutocompleteActionPredictor::IsPreconnectable(
const AutocompleteMatch& match) {
return AutocompleteMatch::IsSearchType(match.type);
}
void AutocompleteActionPredictor::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
CreateLocalCachesFromDatabase();
notification_registrar_.Remove(
this,
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::NotificationService::AllSources());
break;
case chrome::NOTIFICATION_HISTORY_URLS_DELETED: {
DCHECK(initialized_);
const content::Details<const history::URLsDeletedDetails>
urls_deleted_details =
content::Details<const history::URLsDeletedDetails>(details);
if (urls_deleted_details->all_history)
DeleteAllRows();
else
DeleteRowsWithURLs(urls_deleted_details->rows);
break;
}
// This notification does not catch all instances of the user navigating
// from the Omnibox, but it does catch the cases where the dropdown is open
// and those are the events we're most interested in.
case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: {
DCHECK(initialized_);
// TODO(dominich): This doesn't need to be synchronous. Investigate
// posting it as a task to be run later.
OnOmniboxOpenedUrl(*content::Details<AutocompleteLog>(details).ptr());
break;
}
case chrome::NOTIFICATION_HISTORY_LOADED: {
TryDeleteOldEntries(content::Details<HistoryService>(details).ptr());
notification_registrar_.Remove(this,
chrome::NOTIFICATION_HISTORY_LOADED,
content::Source<Profile>(profile_));
break;
}
default:
NOTREACHED() << "Unexpected notification observed.";
break;
}
}
void AutocompleteActionPredictor::CreateLocalCachesFromDatabase() {
// Create local caches using the database as loaded. We will garbage collect
// rows from the caches and the database once the history service is
// available.
std::vector<AutocompleteActionPredictorTable::Row>* rows =
new std::vector<AutocompleteActionPredictorTable::Row>();
content::BrowserThread::PostTaskAndReply(content::BrowserThread::DB,
FROM_HERE,
base::Bind(&AutocompleteActionPredictorTable::GetAllRows, table_, rows),
base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(),
base::Owned(rows)));
}
void AutocompleteActionPredictor::DeleteAllRows() {
if (!initialized_)
return;
db_cache_.clear();
db_id_cache_.clear();
if (table_.get()) {
content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows,
table_));
}
UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction",
DATABASE_ACTION_DELETE_ALL, DATABASE_ACTION_COUNT);
}
void AutocompleteActionPredictor::DeleteRowsWithURLs(
const history::URLRows& rows) {
if (!initialized_)
return;
std::vector<AutocompleteActionPredictorTable::Row::Id> id_list;
for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) {
if (std::find_if(rows.begin(), rows.end(),
history::URLRow::URLRowHasURL(it->first.url)) != rows.end()) {
const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first);
DCHECK(id_it != db_id_cache_.end());
id_list.push_back(id_it->second);
db_id_cache_.erase(id_it);
db_cache_.erase(it++);
} else {
++it;
}
}
if (table_.get()) {
content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
base::Bind(&AutocompleteActionPredictorTable::DeleteRows, table_,
id_list));
}
UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction",
DATABASE_ACTION_DELETE_SOME, DATABASE_ACTION_COUNT);
}
void AutocompleteActionPredictor::OnOmniboxOpenedUrl(
const AutocompleteLog& log) {
if (log.text.length() < kMinimumUserTextLength)
return;
const AutocompleteMatch& match = log.result.match_at(log.selected_index);
UMA_HISTOGRAM_BOOLEAN(
StringPrintf("Prerender.OmniboxNavigationsCouldPrerender%s",
prerender::PrerenderManager::GetModeString()).c_str(),
prerender::IsOmniboxEnabled(profile_));
const GURL& opened_url = match.destination_url;
const string16 lower_user_text(base::i18n::ToLower(log.text));
// Traverse transitional matches for those that have a user_text that is a
// prefix of |lower_user_text|.
std::vector<AutocompleteActionPredictorTable::Row> rows_to_add;
std::vector<AutocompleteActionPredictorTable::Row> rows_to_update;
for (std::vector<TransitionalMatch>::const_iterator it =
transitional_matches_.begin(); it != transitional_matches_.end();
++it) {
if (!StartsWith(lower_user_text, it->user_text, true))
continue;
// Add entries to the database for those matches.
for (std::vector<GURL>::const_iterator url_it = it->urls.begin();
url_it != it->urls.end(); ++url_it) {
DCHECK(it->user_text.length() >= kMinimumUserTextLength);
const DBCacheKey key = { it->user_text, *url_it };
const bool is_hit = (*url_it == opened_url);
AutocompleteActionPredictorTable::Row row;
row.user_text = key.user_text;
row.url = key.url;
DBCacheMap::iterator it = db_cache_.find(key);
if (it == db_cache_.end()) {
row.id = base::GenerateGUID();
row.number_of_hits = is_hit ? 1 : 0;
row.number_of_misses = is_hit ? 0 : 1;
rows_to_add.push_back(row);
} else {
DCHECK(db_id_cache_.find(key) != db_id_cache_.end());
row.id = db_id_cache_.find(key)->second;
row.number_of_hits = it->second.number_of_hits + (is_hit ? 1 : 0);
row.number_of_misses = it->second.number_of_misses + (is_hit ? 0 : 1);
rows_to_update.push_back(row);
}
}
}
if (rows_to_add.size() > 0 || rows_to_update.size() > 0)
AddAndUpdateRows(rows_to_add, rows_to_update);
ClearTransitionalMatches();
// Check against tracked urls and log accuracy for the confidence we
// predicted.
for (std::vector<std::pair<GURL, double> >::const_iterator it =
tracked_urls_.begin(); it != tracked_urls_.end();
++it) {
if (opened_url == it->first) {
UMA_HISTOGRAM_COUNTS_100("AutocompleteActionPredictor.AccurateCount",
it->second * 100);
}
}
tracked_urls_.clear();
}
void AutocompleteActionPredictor::AddAndUpdateRows(
const AutocompleteActionPredictorTable::Rows& rows_to_add,
const AutocompleteActionPredictorTable::Rows& rows_to_update) {
if (!initialized_)
return;
for (AutocompleteActionPredictorTable::Rows::const_iterator it =
rows_to_add.begin(); it != rows_to_add.end(); ++it) {
const DBCacheKey key = { it->user_text, it->url };
DBCacheValue value = { it->number_of_hits, it->number_of_misses };
DCHECK(db_cache_.find(key) == db_cache_.end());
db_cache_[key] = value;
db_id_cache_[key] = it->id;
UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction",
DATABASE_ACTION_ADD, DATABASE_ACTION_COUNT);
}
for (AutocompleteActionPredictorTable::Rows::const_iterator it =
rows_to_update.begin(); it != rows_to_update.end(); ++it) {
const DBCacheKey key = { it->user_text, it->url };
DBCacheMap::iterator db_it = db_cache_.find(key);
DCHECK(db_it != db_cache_.end());
DCHECK(db_id_cache_.find(key) != db_id_cache_.end());
db_it->second.number_of_hits = it->number_of_hits;
db_it->second.number_of_misses = it->number_of_misses;
UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction",
DATABASE_ACTION_UPDATE, DATABASE_ACTION_COUNT);
}
if (table_.get()) {
content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
base::Bind(&AutocompleteActionPredictorTable::AddAndUpdateRows,
table_, rows_to_add, rows_to_update));
}
}
void AutocompleteActionPredictor::CreateCaches(
std::vector<AutocompleteActionPredictorTable::Row>* rows) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!profile_->IsOffTheRecord());
DCHECK(!initialized_);
DCHECK(db_cache_.empty());
DCHECK(db_id_cache_.empty());
for (std::vector<AutocompleteActionPredictorTable::Row>::const_iterator it =
rows->begin(); it != rows->end(); ++it) {
const DBCacheKey key = { it->user_text, it->url };
const DBCacheValue value = { it->number_of_hits, it->number_of_misses };
db_cache_[key] = value;
db_id_cache_[key] = it->id;
}
// If the history service is ready, delete any old or invalid entries.
HistoryService* history_service =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (!TryDeleteOldEntries(history_service)) {
// Wait for the notification that the history service is ready and the URL
// DB is loaded.
notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
content::Source<Profile>(profile_));
}
}
bool AutocompleteActionPredictor::TryDeleteOldEntries(HistoryService* service) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!profile_->IsOffTheRecord());
DCHECK(!initialized_);
if (!service)
return false;
history::URLDatabase* url_db = service->InMemoryDatabase();
if (!url_db)
return false;
DeleteOldEntries(url_db);
return true;
}
void AutocompleteActionPredictor::DeleteOldEntries(
history::URLDatabase* url_db) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!profile_->IsOffTheRecord());
DCHECK(!initialized_);
DCHECK(table_.get());
std::vector<AutocompleteActionPredictorTable::Row::Id> ids_to_delete;
DeleteOldIdsFromCaches(url_db, &ids_to_delete);
content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
base::Bind(&AutocompleteActionPredictorTable::DeleteRows, table_,
ids_to_delete));
FinishInitialization();
if (incognito_predictor_)
incognito_predictor_->CopyFromMainProfile();
}
void AutocompleteActionPredictor::DeleteOldIdsFromCaches(
history::URLDatabase* url_db,
std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!profile_->IsOffTheRecord());
DCHECK(!initialized_);
DCHECK(url_db);
DCHECK(id_list);
id_list->clear();
for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) {
history::URLRow url_row;
if ((url_db->GetRowForURL(it->first.url, &url_row) == 0) ||
((base::Time::Now() - url_row.last_visit()).InDays() >
kMaximumDaysToKeepEntry)) {
const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first);
DCHECK(id_it != db_id_cache_.end());
id_list->push_back(id_it->second);
db_id_cache_.erase(id_it);
db_cache_.erase(it++);
} else {
++it;
}
}
}
void AutocompleteActionPredictor::CopyFromMainProfile() {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(profile_->IsOffTheRecord());
DCHECK(!initialized_);
DCHECK(main_profile_predictor_);
DCHECK(main_profile_predictor_->initialized_);
db_cache_ = main_profile_predictor_->db_cache_;
db_id_cache_ = main_profile_predictor_->db_id_cache_;
FinishInitialization();
}
void AutocompleteActionPredictor::FinishInitialization() {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!initialized_);
// Incognito and normal profiles should listen only to omnibox notifications
// from their own profile, but both should listen to history deletions from
// the main profile, since opening the history page in either case actually
// opens the non-incognito history (and lets users delete from there).
notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
content::Source<Profile>(profile_));
notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
content::Source<Profile>(profile_->GetOriginalProfile()));
initialized_ = true;
}
double AutocompleteActionPredictor::CalculateConfidence(
const string16& user_text,
const AutocompleteMatch& match,
bool* is_in_db) const {
const DBCacheKey key = { user_text, match.destination_url };
*is_in_db = false;
if (user_text.length() < kMinimumUserTextLength)
return 0.0;
const DBCacheMap::const_iterator iter = db_cache_.find(key);
if (iter == db_cache_.end())
return 0.0;
*is_in_db = true;
return CalculateConfidenceForDbEntry(iter);
}
double AutocompleteActionPredictor::CalculateConfidenceForDbEntry(
DBCacheMap::const_iterator iter) const {
const DBCacheValue& value = iter->second;
if (value.number_of_hits < kMinimumNumberOfHits)
return 0.0;
const double number_of_hits = static_cast<double>(value.number_of_hits);
return number_of_hits / (number_of_hits + value.number_of_misses);
}
AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() {
}
AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() {
}
} // namespace predictors
|