history_quick_provider.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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)
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Don't bother with INVALID and FORCED_QUERY.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((input.type() == AutocompleteInput::INVALID) ||
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (input.type() == AutocompleteInput::FORCED_QUERY))
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  autocomplete_input_ = input;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(pkasting): We should just block here until this loads.  Any time
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // someone unloads the history backend, we'll get inconsistent inline
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete behavior here.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (GetIndex()) {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeTicks start_time = base::TimeTicks::Now();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DoAutocomplete();
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (input.text().length() < 6) {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeTicks end_time = base::TimeTicks::Now();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string name = "HistoryQuickProvider.QueryIndexTime." +
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::IntToString(input.text().length());
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::HistogramBase* counter = base::Histogram::FactoryGet(
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UpdateStarredStateOfMatches();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.deletable);
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delete the match from the InMemoryURLIndex.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetIndex()->DeleteURL(match.destination_url);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeleteMatchFromMatches(match);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::~HistoryQuickProvider() {}
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DoAutocomplete() {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the matching URLs from the DB.
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      autocomplete_input_.text(),
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      autocomplete_input_.cursor_position(),
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      AutocompleteProvider::kMaxMatches);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches.empty())
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Figure out if HistoryURL provider has a URL-what-you-typed match
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that ought to go first and what its score will be.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool will_have_url_what_you_typed_match_first = false;
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int url_what_you_typed_match_score = -1;  // undefined
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are necessary (but not sufficient) conditions for the omnibox
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input to be a URL-what-you-typed match.  The username test checks that
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // either the username does not exist (a regular URL such as http://site/)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or, if the username exists (http://user@site/), there must be either
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a password or a port.  Together these exclude pure username@site
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // inputs because these are likely to be an e-mail address.  HistoryURL
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provider won't promote the URL-what-you-typed match to first
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for these inputs.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool can_have_url_what_you_typed_match_first =
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (autocomplete_input_.type() != AutocompleteInput::QUERY) &&
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (!autocomplete_input_.parts().username.is_nonempty() ||
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().password.is_nonempty() ||
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().path.is_nonempty());
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (can_have_url_what_you_typed_match_first) {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryService* const history_service =
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        HistoryServiceFactory::GetForProfile(profile_,
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             Profile::EXPLICIT_ACCESS);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We expect HistoryService to be available.  In case it's not,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // (e.g., due to Profile corruption) we let HistoryQuick provider
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // completions (which may be available because it's a different
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // data structure) compete with the URL-what-you-typed match as
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // normal.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (history_service) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::URLDatabase* url_db = history_service->InMemoryDatabase();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // url_db can be NULL if it hasn't finished initializing (or
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // failed to to initialize).  In this case, we let HistoryQuick
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // provider completions compete with the URL-what-you-typed
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // match as normal.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (url_db) {
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const std::string host(base::UTF16ToUTF8(
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            autocomplete_input_.text().substr(
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                autocomplete_input_.parts().host.begin,
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                autocomplete_input_.parts().host.len)));
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We want to put the URL-what-you-typed match first if either
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * the user visited the URL before (intranet or internet).
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * it's a URL on a host that user visited before and this
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   is the root path of the host.  (If the user types some
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   of a path--more than a simple "/"--we let autocomplete compete
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   normally with the URL-what-you-typed match.)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // TODO(mpearson): Remove this hacky code and simply score URL-what-
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // you-typed in some sane way relative to possible completions:
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // URL-what-you-typed should get some sort of a boost relative
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // to completions, but completions should naturally win if
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // they're a lot more popular.  In this process, if the input
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // is a bare intranet hostname that has been visited before, we
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // may want to enforce that the only completions that can outscore
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // the URL-what-you-typed match are on the same host (i.e., aren't
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // from a longer internet hostname for which the omnibox input is
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // a prefix).
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (url_db->GetRowForURL(
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            autocomplete_input_.canonicalized_url(), NULL) != 0) {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // We visited this URL before.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // HistoryURLProvider gives visited what-you-typed URLs a high score.
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          url_what_you_typed_match_score =
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              HistoryURLProvider::kScoreForBestInlineableResult;
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        } else if (url_db->IsTypedHost(host) &&
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             (!autocomplete_input_.parts().path.is_nonempty() ||
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              ((autocomplete_input_.parts().path.len == 1) &&
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               (autocomplete_input_.text()[
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   autocomplete_input_.parts().path.begin] == '/'))) &&
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             !autocomplete_input_.parts().query.is_nonempty() &&
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             !autocomplete_input_.parts().ref.is_nonempty()) {
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // Not visited, but we've seen the host before.
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
180a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          const size_t registry_length =
181a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)              net::registry_controlled_domains::GetRegistryLength(
182a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  host,
183a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
184a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
185a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          if (registry_length == 0) {
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known intranet hosts get one score.
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForUnvisitedIntranetResult;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          } else {
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known internet hosts get another.
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForWhatYouTypedResult;
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          }
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loop over every result and add it to matches_.  In the process,
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // guarantee that scores are decreasing.  |max_match_score| keeps
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // track of the highest score we can assign to any later results we
202a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // see.  Also, reduce |max_match_score| if we think there will be
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a URL-what-you-typed match.  (We want URL-what-you-typed matches for
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // visited URLs to beat out any longer URLs, no matter how frequently
205a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // they're visited.)  The strength of this reduction depends on the
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // likely score for the URL-what-you-typed result.
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |template_url_service| or |template_url| can be NULL in unit tests.
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURLService* template_url_service =
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TemplateURLServiceFactory::GetForProfile(profile_);
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURL* template_url = template_url_service ?
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      template_url_service->GetDefaultSearchProvider() : NULL;
213a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  int max_match_score = matches.begin()->raw_score();
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (will_have_url_what_you_typed_match_first) {
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    max_match_score = std::min(max_match_score,
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        url_what_you_typed_match_score - 1);
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (ScoredHistoryMatches::const_iterator match_iter = matches.begin();
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       match_iter != matches.end(); ++match_iter) {
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match(*match_iter);
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Culls results corresponding to queries from the default search engine.
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // These are low-quality, difficult-to-understand matches for users, and the
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // SearchProvider should surface past queries in a better way anyway.
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!template_url ||
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        !template_url->IsSearchURL(history_match.url_info.url())) {
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Set max_match_score to the score we'll assign this result:
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      max_match_score = std::min(max_match_score, history_match.raw_score());
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      matches_.push_back(QuickMatchToACMatch(history_match, max_match_score));
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Mark this max_match_score as being used:
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      max_match_score--;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch(
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match,
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int score) {
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const history::URLRow& info = history_match.url_info;
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  AutocompleteMatch match(
240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      this, score, !!info.visit_count(),
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      history_match.url_matches().empty() ?
242f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          AutocompleteMatchType::HISTORY_TITLE :
24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          AutocompleteMatchType::HISTORY_URL);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.typed_count = info.typed_count();
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.destination_url = info.url();
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the URL autocomplete presentation.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.fill_into_edit =
2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      AutocompleteInput::FormattedStringWithEquivalentMeaning(
2535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          info.url(),
2545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          net::FormatUrl(info.url(), languages_, format_types,
2555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                         net::UnescapeRule::SPACES, NULL, NULL, NULL));
2565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  std::vector<size_t> offsets =
2575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      OffsetsFromTermMatches(history_match.url_matches());
2585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::OffsetAdjuster::Adjustments adjustments;
2595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  match.contents = net::FormatUrlWithAdjustments(
2605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      info.url(), languages_, format_types, net::UnescapeRule::SPACES, NULL,
2615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      NULL, &adjustments);
2625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::TermMatches new_matches =
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ReplaceOffsetsInTermMatches(history_match.url_matches(), offsets);
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents_class =
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SpansFromTermMatch(new_matches, match.contents.length(), true);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible.
2695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (history_match.can_inline()) {
2705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    DCHECK(!new_matches.empty());
271ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    size_t inline_autocomplete_offset = new_matches[0].offset +
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new_matches[0].length;
273ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // |inline_autocomplete_offset| may be beyond the end of the
274ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // |fill_into_edit| if the user has typed an URL with a scheme and the
275ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // last character typed is a slash.  That slash is removed by the
276ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // FormatURLWithOffsets call above.
277ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (inline_autocomplete_offset < match.fill_into_edit.length()) {
278ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      match.inline_autocompletion =
279ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          match.fill_into_edit.substr(inline_autocomplete_offset);
280ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    match.allowed_to_be_default_match = match.inline_autocompletion.empty() ||
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        !PreventInlineAutocomplete(autocomplete_input_);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the description autocomplete presentation.
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description = info.title();
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description_class = SpansFromTermMatch(
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      history_match.title_matches(), match.description.length(), false);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("typed count", info.typed_count());
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("visit count", info.visit_count());
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("last visit", info.last_visit());
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return match;
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() {
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (index_for_testing_.get())
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return index_for_testing_.get();
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryService* const history_service =
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!history_service)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return history_service->InMemoryIndex();
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
308