history_quick_provider.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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/browser_thread.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h" 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_types.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/registry_controlled_domains/registry_controlled_domain.h" 45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_parse.h" 46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_util.h" 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::InMemoryURLIndex; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::ScoredHistoryMatch; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using history::ScoredHistoryMatches; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HistoryQuickProvider::disabled_ = false; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::HistoryQuickProvider( 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteProviderListener* listener, 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : HistoryProvider(listener, profile, 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteProvider::TYPE_HISTORY_QUICK), 597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::Start(const AutocompleteInput& input, 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool minimal_changes) { 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) matches_.clear(); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (disabled_) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Don't bother with INVALID and FORCED_QUERY. Also pass when looking for 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // BEST_MATCH and there is no inline autocompletion because none of the HQP 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // matches can score highly enough to qualify. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((input.type() == AutocompleteInput::INVALID) || 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (input.type() == AutocompleteInput::FORCED_QUERY) || 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (input.matches_requested() == AutocompleteInput::BEST_MATCH && 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) input.prevent_inline_autocomplete())) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_ = input; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(pkasting): We should just block here until this loads. Any time 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // someone unloads the history backend, we'll get inconsistent inline 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // autocomplete behavior here. 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (GetIndex()) { 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks start_time = base::TimeTicks::Now(); 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DoAutocomplete(); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (input.text().length() < 6) { 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks end_time = base::TimeTicks::Now(); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string name = "HistoryQuickProvider.QueryIndexTime." + 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::IntToString(input.text().length()); 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::HistogramBase* counter = base::Histogram::FactoryGet( 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UpdateStarredStateOfMatches(); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.deletable); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.destination_url.is_valid()); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Delete the match from the InMemoryURLIndex. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetIndex()->DeleteURL(match.destination_url); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DeleteMatchFromMatches(match); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::~HistoryQuickProvider() {} 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DoAutocomplete() { 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the matching URLs from the DB. 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms( 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) autocomplete_input_.text(), 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) autocomplete_input_.cursor_position()); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (matches.empty()) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Figure out if HistoryURL provider has a URL-what-you-typed match 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // that ought to go first and what its score will be. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool will_have_url_what_you_typed_match_first = false; 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int url_what_you_typed_match_score = -1; // undefined 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // These are necessary (but not sufficient) conditions for the omnibox 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // input to be a URL-what-you-typed match. The username test checks that 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // either the username does not exist (a regular URL such as http://site/) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or, if the username exists (http://user@site/), there must be either 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a password or a port. Together these exclude pure username@site 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // inputs because these are likely to be an e-mail address. HistoryURL 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // provider won't promote the URL-what-you-typed match to first 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for these inputs. 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const bool can_have_url_what_you_typed_match_first = 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.canonicalized_url().is_valid() && 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (autocomplete_input_.type() != AutocompleteInput::QUERY) && 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (autocomplete_input_.type() != AutocompleteInput::FORCED_QUERY) && 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (!autocomplete_input_.parts().username.is_nonempty() || 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().password.is_nonempty() || 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().path.is_nonempty()); 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (can_have_url_what_you_typed_match_first) { 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryService* const history_service = 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryServiceFactory::GetForProfile(profile_, 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile::EXPLICIT_ACCESS); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We expect HistoryService to be available. In case it's not, 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (e.g., due to Profile corruption) we let HistoryQuick provider 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // completions (which may be available because it's a different 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // data structure) compete with the URL-what-you-typed match as 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // normal. 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (history_service) { 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::URLDatabase* url_db = history_service->InMemoryDatabase(); 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // url_db can be NULL if it hasn't finished initializing (or 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // failed to to initialize). In this case, we let HistoryQuick 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // provider completions compete with the URL-what-you-typed 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // match as normal. 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (url_db) { 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string host(UTF16ToUTF8(autocomplete_input_.text().substr( 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) autocomplete_input_.parts().host.begin, 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (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 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // guarantees no result will be offered as an 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; 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int max_match_score = (PreventInlineAutocomplete(autocomplete_input_) || 2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) !matches.begin()->can_inline) ? 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (AutocompleteResult::kLowestDefaultScore - 1) : 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) matches.begin()->raw_score; 2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (will_have_url_what_you_typed_match_first) { 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) max_match_score = std::min(max_match_score, 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url_what_you_typed_match_score - 1); 2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (ScoredHistoryMatches::const_iterator match_iter = matches.begin(); 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match_iter != matches.end(); ++match_iter) { 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ScoredHistoryMatch& history_match(*match_iter); 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Culls results corresponding to queries from the default search engine. 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // These are low-quality, difficult-to-understand matches for users, and the 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // SearchProvider should surface past queries in a better way anyway. 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!template_url || 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) !template_url->IsSearchURL(history_match.url_info.url())) { 2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Set max_match_score to the score we'll assign this result: 2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) max_match_score = std::min(max_match_score, history_match.raw_score); 2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches_.push_back(QuickMatchToACMatch(history_match, max_match_score)); 2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Mark this max_match_score as being used: 2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) max_match_score--; 2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch( 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ScoredHistoryMatch& history_match, 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int score) { 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const history::URLRow& info = history_match.url_info; 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteMatch match(this, score, !!info.visit_count(), 25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) history_match.url_matches.empty() ? AutocompleteMatchType::HISTORY_TITLE : 25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) AutocompleteMatchType::HISTORY_URL); 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.typed_count = info.typed_count(); 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.destination_url = info.url(); 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(match.destination_url.is_valid()); 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Format the URL autocomplete presentation. 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<size_t> offsets = 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) OffsetsFromTermMatches(history_match.url_matches); 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP); 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.fill_into_edit = 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::FormatUrlWithOffsets(info.url(), languages_, format_types, 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::UnescapeRule::SPACES, NULL, NULL, &offsets)); 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::TermMatches new_matches = 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ReplaceOffsetsInTermMatches(history_match.url_matches, offsets); 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.contents = net::FormatUrl(info.url(), languages_, format_types, 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::UnescapeRule::SPACES, NULL, NULL, NULL); 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.contents_class = 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SpansFromTermMatch(new_matches, match.contents.length(), true); 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch if (history_match.can_inline) { 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!new_matches.empty()); 277ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch size_t inline_autocomplete_offset = new_matches[0].offset + 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new_matches[0].length; 279ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // |inline_autocomplete_offset| may be beyond the end of the 280ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // |fill_into_edit| if the user has typed an URL with a scheme and the 281ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // last character typed is a slash. That slash is removed by the 282ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // FormatURLWithOffsets call above. 283ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch if (inline_autocomplete_offset < match.fill_into_edit.length()) { 284ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch match.inline_autocompletion = 285ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch match.fill_into_edit.substr(inline_autocomplete_offset); 286ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch } 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Format the description autocomplete presentation. 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.description = info.title(); 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.description_class = SpansFromTermMatch( 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_match.title_matches, match.description.length(), false); 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("typed count", info.typed_count()); 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("visit count", info.visit_count()); 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) match.RecordAdditionalInfo("last visit", info.last_visit()); 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return match; 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() { 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (index_for_testing_.get()) 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return index_for_testing_.get(); 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryService* const history_service = 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!history_service) 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return history_service->InMemoryIndex(); 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ACMatchClassifications HistoryQuickProvider::SpansFromTermMatch( 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const history::TermMatches& matches, 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t text_length, 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_url) { 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassification::Style url_style = 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) is_url ? ACMatchClassification::URL : ACMatchClassification::NONE; 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications spans; 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (matches.empty()) { 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (text_length) 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) spans.push_back(ACMatchClassification(0, url_style)); 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return spans; 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (matches[0].offset) 3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) spans.push_back(ACMatchClassification(0, url_style)); 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t match_count = matches.size(); 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < match_count;) { 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t offset = matches[i].offset; 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) spans.push_back(ACMatchClassification(offset, 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassification::MATCH | url_style)); 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Skip all adjacent matches. 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) do { 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset += matches[i].length; 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++i; 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } while ((i < match_count) && (offset == matches[i].offset)); 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (offset < text_length) 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) spans.push_back(ACMatchClassification(offset, url_style)); 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return spans; 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 344