history_quick_provider.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/history_quick_provider.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/break_iterator.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/field_trial.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h" 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h" 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_result.h" 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autocomplete/history_url_provider.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_database.h" 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_service_factory.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/in_memory_url_index.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/in_memory_url_index_types.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/scored_history_match.h" 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/omnibox/omnibox_field_trial.h" 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search/search.h" 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url.h" 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service.h" 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h" 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/common/autocomplete_match_type.h" 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h" 36558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "chrome/common/net/url_fixer_upper.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h" 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_types.h" 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/registry_controlled_domains/registry_controlled_domain.h" 44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_parse.h" 45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_util.h" 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::InMemoryURLIndex; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::ScoredHistoryMatch; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::ScoredHistoryMatches; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HistoryQuickProvider::disabled_ = false; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::HistoryQuickProvider( 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteProviderListener* listener, 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : HistoryProvider(listener, profile, 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteProvider::TYPE_HISTORY_QUICK), 587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::Start(const AutocompleteInput& input, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool minimal_changes) { 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) matches_.clear(); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (disabled_) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Don't bother with INVALID and FORCED_QUERY. Also pass when looking for 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // BEST_MATCH and there is no inline autocompletion because none of the HQP 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // matches can score highly enough to qualify. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((input.type() == AutocompleteInput::INVALID) || 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (input.type() == AutocompleteInput::FORCED_QUERY) || 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (input.matches_requested() == AutocompleteInput::BEST_MATCH && 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) input.prevent_inline_autocomplete())) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_ = input; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(pkasting): We should just block here until this loads. Any time 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // someone unloads the history backend, we'll get inconsistent inline 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // autocomplete behavior here. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (GetIndex()) { 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks start_time = base::TimeTicks::Now(); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DoAutocomplete(); 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (input.text().length() < 6) { 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks end_time = base::TimeTicks::Now(); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string name = "HistoryQuickProvider.QueryIndexTime." + 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::IntToString(input.text().length()); 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::HistogramBase* counter = base::Histogram::FactoryGet( 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UpdateStarredStateOfMatches(); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.deletable); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.destination_url.is_valid()); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Delete the match from the InMemoryURLIndex. 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetIndex()->DeleteURL(match.destination_url); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DeleteMatchFromMatches(match); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::~HistoryQuickProvider() {} 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DoAutocomplete() { 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the matching URLs from the DB. 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms( 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) autocomplete_input_.text(), 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) autocomplete_input_.cursor_position()); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (matches.empty()) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Figure out if HistoryURL provider has a URL-what-you-typed match 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // that ought to go first and what its score will be. 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool will_have_url_what_you_typed_match_first = false; 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int url_what_you_typed_match_score = -1; // undefined 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // These are necessary (but not sufficient) conditions for the omnibox 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // input to be a URL-what-you-typed match. The username test checks that 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // either the username does not exist (a regular URL such as http://site/) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or, if the username exists (http://user@site/), there must be either 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a password or a port. Together these exclude pure username@site 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // inputs because these are likely to be an e-mail address. HistoryURL 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // provider won't promote the URL-what-you-typed match to first 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for these inputs. 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const bool can_have_url_what_you_typed_match_first = 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.canonicalized_url().is_valid() && 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (autocomplete_input_.type() != AutocompleteInput::QUERY) && 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (autocomplete_input_.type() != AutocompleteInput::FORCED_QUERY) && 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (!autocomplete_input_.parts().username.is_nonempty() || 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().password.is_nonempty() || 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().path.is_nonempty()); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (can_have_url_what_you_typed_match_first) { 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryService* const history_service = 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryServiceFactory::GetForProfile(profile_, 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile::EXPLICIT_ACCESS); 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We expect HistoryService to be available. In case it's not, 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (e.g., due to Profile corruption) we let HistoryQuick provider 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // completions (which may be available because it's a different 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // data structure) compete with the URL-what-you-typed match as 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // normal. 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (history_service) { 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::URLDatabase* url_db = history_service->InMemoryDatabase(); 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // url_db can be NULL if it hasn't finished initializing (or 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // failed to to initialize). In this case, we let HistoryQuick 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // provider completions compete with the URL-what-you-typed 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // match as normal. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (url_db) { 1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string host(base::UTF16ToUTF8( 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) autocomplete_input_.text().substr( 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) autocomplete_input_.parts().host.begin, 1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) autocomplete_input_.parts().host.len))); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We want to put the URL-what-you-typed match first if either 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // * the user visited the URL before (intranet or internet). 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // * it's a URL on a host that user visited before and this 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is the root path of the host. (If the user types some 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of a path--more than a simple "/"--we let autocomplete compete 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // normally with the URL-what-you-typed match.) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(mpearson): Remove this hacky code and simply score URL-what- 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // you-typed in some sane way relative to possible completions: 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // URL-what-you-typed should get some sort of a boost relative 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to completions, but completions should naturally win if 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // they're a lot more popular. In this process, if the input 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is a bare intranet hostname that has been visited before, we 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // may want to enforce that the only completions that can outscore 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the URL-what-you-typed match are on the same host (i.e., aren't 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from a longer internet hostname for which the omnibox input is 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a prefix). 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (url_db->GetRowForURL( 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) autocomplete_input_.canonicalized_url(), NULL) != 0) { 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // We visited this URL before. 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) will_have_url_what_you_typed_match_first = true; 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // HistoryURLProvider gives visited what-you-typed URLs a high score. 1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_what_you_typed_match_score = 1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) HistoryURLProvider::kScoreForBestInlineableResult; 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else if (url_db->IsTypedHost(host) && 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (!autocomplete_input_.parts().path.is_nonempty() || 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ((autocomplete_input_.parts().path.len == 1) && 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (autocomplete_input_.text()[ 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().path.begin] == '/'))) && 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) !autocomplete_input_.parts().query.is_nonempty() && 1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) !autocomplete_input_.parts().ref.is_nonempty()) { 1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Not visited, but we've seen the host before. 1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) will_have_url_what_you_typed_match_first = true; 185a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const size_t registry_length = 186a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) net::registry_controlled_domains::GetRegistryLength( 187a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) host, 188a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 189a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 190a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (registry_length == 0) { 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Known intranet hosts get one score. 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_what_you_typed_match_score = 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) HistoryURLProvider::kScoreForUnvisitedIntranetResult; 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else { 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Known internet hosts get another. 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_what_you_typed_match_score = 1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) HistoryURLProvider::kScoreForWhatYouTypedResult; 1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Loop over every result and add it to matches_. In the process, 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // guarantee that scores are decreasing. |max_match_score| keeps 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // track of the highest score we can assign to any later results we 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // see. Also, if we're not allowing inline autocompletions in 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // general or the current best suggestion isn't inlineable, 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // artificially reduce the starting |max_match_score| (which 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // therefore applies to all results) to something low enough that 211c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // guarantees no result will be offered as an inline autocomplete 2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // suggestion. Also do a similar reduction if we think there will be 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a URL-what-you-typed match. (We want URL-what-you-typed matches for 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // visited URLs to beat out any longer URLs, no matter how frequently 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // they're visited.) The strength of this last reduction depends on the 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // likely score for the URL-what-you-typed result. 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |template_url_service| or |template_url| can be NULL in unit tests. 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TemplateURLService* template_url_service = 2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TemplateURLServiceFactory::GetForProfile(profile_); 2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TemplateURL* template_url = template_url_service ? 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) template_url_service->GetDefaultSearchProvider() : NULL; 223c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch int max_match_score = 224c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch (OmniboxFieldTrial::ReorderForLegalDefaultMatch( 225c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch autocomplete_input_.current_page_classification()) || 226c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch (!PreventInlineAutocomplete(autocomplete_input_) && 227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) matches.begin()->can_inline())) ? 228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) matches.begin()->raw_score() : 229c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch (AutocompleteResult::kLowestDefaultScore - 1); 2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (will_have_url_what_you_typed_match_first) { 2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) max_match_score = std::min(max_match_score, 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_what_you_typed_match_score - 1); 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (ScoredHistoryMatches::const_iterator match_iter = matches.begin(); 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match_iter != matches.end(); ++match_iter) { 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ScoredHistoryMatch& history_match(*match_iter); 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Culls results corresponding to queries from the default search engine. 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // These are low-quality, difficult-to-understand matches for users, and the 2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // SearchProvider should surface past queries in a better way anyway. 2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!template_url || 2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) !template_url->IsSearchURL(history_match.url_info.url())) { 2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Set max_match_score to the score we'll assign this result: 243f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) max_match_score = std::min(max_match_score, history_match.raw_score()); 2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches_.push_back(QuickMatchToACMatch(history_match, max_match_score)); 2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Mark this max_match_score as being used: 2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) max_match_score--; 2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch( 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ScoredHistoryMatch& history_match, 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int score) { 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const history::URLRow& info = history_match.url_info; 255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) AutocompleteMatch match( 256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) this, score, !!info.visit_count(), 257f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) history_match.url_matches().empty() ? 258f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) AutocompleteMatchType::HISTORY_TITLE : 25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) AutocompleteMatchType::HISTORY_URL); 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.typed_count = info.typed_count(); 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.destination_url = info.url(); 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.destination_url.is_valid()); 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Format the URL autocomplete presentation. 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<size_t> offsets = 266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) OffsetsFromTermMatches(history_match.url_matches()); 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP); 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.fill_into_edit = 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::FormatUrlWithOffsets(info.url(), languages_, format_types, 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::UnescapeRule::SPACES, NULL, NULL, &offsets)); 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::TermMatches new_matches = 274f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ReplaceOffsetsInTermMatches(history_match.url_matches(), offsets); 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.contents = net::FormatUrl(info.url(), languages_, format_types, 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::UnescapeRule::SPACES, NULL, NULL, NULL); 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.contents_class = 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SpansFromTermMatch(new_matches, match.contents.length(), true); 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (history_match.can_inline()) { 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!new_matches.empty()); 282ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch size_t inline_autocomplete_offset = new_matches[0].offset + 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new_matches[0].length; 284ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // |inline_autocomplete_offset| may be beyond the end of the 285ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // |fill_into_edit| if the user has typed an URL with a scheme and the 286ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // last character typed is a slash. That slash is removed by the 287ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // FormatURLWithOffsets call above. 288ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch if (inline_autocomplete_offset < match.fill_into_edit.length()) { 289ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch match.inline_autocompletion = 290ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch match.fill_into_edit.substr(inline_autocomplete_offset); 291ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch } 292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) match.allowed_to_be_default_match = match.inline_autocompletion.empty() || 293a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) !PreventInlineAutocomplete(autocomplete_input_); 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Format the description autocomplete presentation. 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.description = info.title(); 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.description_class = SpansFromTermMatch( 299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) history_match.title_matches(), match.description.length(), false); 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("typed count", info.typed_count()); 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("visit count", info.visit_count()); 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("last visit", info.last_visit()); 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return match; 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() { 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (index_for_testing_.get()) 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return index_for_testing_.get(); 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryService* const history_service = 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!history_service) 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return history_service->InMemoryIndex(); 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 319