history_quick_provider.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autocomplete/history_url_provider.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_database.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_service_factory.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/in_memory_url_index.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/in_memory_url_index_types.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/scored_history_match.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/omnibox/omnibox_field_trial.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/metrics/proto/omnibox_input_type.pb.h"
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match_type.h"
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url.h"
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.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)
53116680a4aac90f2aa7413d9095a592090648e557Ben MurdochHistoryQuickProvider::HistoryQuickProvider(Profile* profile)
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : HistoryProvider(profile, AutocompleteProvider::TYPE_HISTORY_QUICK),
557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::Start(const AutocompleteInput& input,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool minimal_changes) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  matches_.clear();
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (disabled_)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Don't bother with INVALID and FORCED_QUERY.
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if ((input.type() == metrics::OmniboxInputType::INVALID) ||
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  autocomplete_input_ = input;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(pkasting): We should just block here until this loads.  Any time
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // someone unloads the history backend, we'll get inconsistent inline
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete behavior here.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (GetIndex()) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeTicks start_time = base::TimeTicks::Now();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DoAutocomplete();
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (input.text().length() < 6) {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeTicks end_time = base::TimeTicks::Now();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string name = "HistoryQuickProvider.QueryIndexTime." +
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::IntToString(input.text().length());
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::HistogramBase* counter = base::Histogram::FactoryGet(
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::~HistoryQuickProvider() {}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DoAutocomplete() {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the matching URLs from the DB.
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      autocomplete_input_.text(),
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      autocomplete_input_.cursor_position(),
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      AutocompleteProvider::kMaxMatches);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches.empty())
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Figure out if HistoryURL provider has a URL-what-you-typed match
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that ought to go first and what its score will be.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool will_have_url_what_you_typed_match_first = false;
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int url_what_you_typed_match_score = -1;  // undefined
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are necessary (but not sufficient) conditions for the omnibox
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input to be a URL-what-you-typed match.  The username test checks that
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // either the username does not exist (a regular URL such as http://site/)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or, if the username exists (http://user@site/), there must be either
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a password or a port.  Together these exclude pure username@site
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // inputs because these are likely to be an e-mail address.  HistoryURL
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provider won't promote the URL-what-you-typed match to first
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for these inputs.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool can_have_url_what_you_typed_match_first =
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (autocomplete_input_.type() != metrics::OmniboxInputType::QUERY) &&
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (!autocomplete_input_.parts().username.is_nonempty() ||
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().password.is_nonempty() ||
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().path.is_nonempty());
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (can_have_url_what_you_typed_match_first) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryService* const history_service =
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        HistoryServiceFactory::GetForProfile(profile_,
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             Profile::EXPLICIT_ACCESS);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We expect HistoryService to be available.  In case it's not,
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // (e.g., due to Profile corruption) we let HistoryQuick provider
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // completions (which may be available because it's a different
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // data structure) compete with the URL-what-you-typed match as
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // normal.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (history_service) {
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::URLDatabase* url_db = history_service->InMemoryDatabase();
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // url_db can be NULL if it hasn't finished initializing (or
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // failed to to initialize).  In this case, we let HistoryQuick
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // provider completions compete with the URL-what-you-typed
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // match as normal.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (url_db) {
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const std::string host(base::UTF16ToUTF8(
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            autocomplete_input_.text().substr(
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                autocomplete_input_.parts().host.begin,
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                autocomplete_input_.parts().host.len)));
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We want to put the URL-what-you-typed match first if either
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * the user visited the URL before (intranet or internet).
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * it's a URL on a host that user visited before and this
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   is the root path of the host.  (If the user types some
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   of a path--more than a simple "/"--we let autocomplete compete
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   normally with the URL-what-you-typed match.)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // TODO(mpearson): Remove this hacky code and simply score URL-what-
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // you-typed in some sane way relative to possible completions:
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // URL-what-you-typed should get some sort of a boost relative
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // to completions, but completions should naturally win if
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // they're a lot more popular.  In this process, if the input
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // is a bare intranet hostname that has been visited before, we
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // may want to enforce that the only completions that can outscore
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // the URL-what-you-typed match are on the same host (i.e., aren't
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // from a longer internet hostname for which the omnibox input is
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // a prefix).
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (url_db->GetRowForURL(
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            autocomplete_input_.canonicalized_url(), NULL) != 0) {
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // We visited this URL before.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // HistoryURLProvider gives visited what-you-typed URLs a high score.
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          url_what_you_typed_match_score =
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              HistoryURLProvider::kScoreForBestInlineableResult;
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        } else if (url_db->IsTypedHost(host) &&
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             (!autocomplete_input_.parts().path.is_nonempty() ||
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              ((autocomplete_input_.parts().path.len == 1) &&
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               (autocomplete_input_.text()[
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   autocomplete_input_.parts().path.begin] == '/'))) &&
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             !autocomplete_input_.parts().query.is_nonempty() &&
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             !autocomplete_input_.parts().ref.is_nonempty()) {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // Not visited, but we've seen the host before.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
168a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          const size_t registry_length =
169a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)              net::registry_controlled_domains::GetRegistryLength(
170a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  host,
171a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
172a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                  net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
173a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          if (registry_length == 0) {
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known intranet hosts get one score.
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForUnvisitedIntranetResult;
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          } else {
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known internet hosts get another.
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForWhatYouTypedResult;
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          }
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loop over every result and add it to matches_.  In the process,
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // guarantee that scores are decreasing.  |max_match_score| keeps
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // track of the highest score we can assign to any later results we
190a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // see.  Also, reduce |max_match_score| if we think there will be
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a URL-what-you-typed match.  (We want URL-what-you-typed matches for
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // visited URLs to beat out any longer URLs, no matter how frequently
193a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // they're visited.)  The strength of this reduction depends on the
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // likely score for the URL-what-you-typed result.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |template_url_service| or |template_url| can be NULL in unit tests.
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURLService* template_url_service =
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TemplateURLServiceFactory::GetForProfile(profile_);
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURL* template_url = template_url_service ?
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      template_url_service->GetDefaultSearchProvider() : NULL;
201a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  int max_match_score = matches.begin()->raw_score();
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (will_have_url_what_you_typed_match_first) {
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    max_match_score = std::min(max_match_score,
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        url_what_you_typed_match_score - 1);
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (ScoredHistoryMatches::const_iterator match_iter = matches.begin();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       match_iter != matches.end(); ++match_iter) {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match(*match_iter);
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Culls results corresponding to queries from the default search engine.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // These are low-quality, difficult-to-understand matches for users, and the
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // SearchProvider should surface past queries in a better way anyway.
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!template_url ||
213f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        !template_url->IsSearchURL(history_match.url_info.url(),
214f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                   template_url_service->search_terms_data())) {
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Set max_match_score to the score we'll assign this result:
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      max_match_score = std::min(max_match_score, history_match.raw_score());
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      matches_.push_back(QuickMatchToACMatch(history_match, max_match_score));
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Mark this max_match_score as being used:
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      max_match_score--;
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch(
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match,
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int score) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const history::URLRow& info = history_match.url_info;
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  AutocompleteMatch match(
229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      this, score, !!info.visit_count(),
230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      history_match.url_matches().empty() ?
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          AutocompleteMatchType::HISTORY_TITLE :
23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          AutocompleteMatchType::HISTORY_URL);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.typed_count = info.typed_count();
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.destination_url = info.url();
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the URL autocomplete presentation.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.fill_into_edit =
2415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      AutocompleteInput::FormattedStringWithEquivalentMeaning(
2425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          info.url(),
2435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          net::FormatUrl(info.url(), languages_, format_types,
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         net::UnescapeRule::SPACES, NULL, NULL, NULL),
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          ChromeAutocompleteSchemeClassifier(profile_));
2465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  std::vector<size_t> offsets =
2475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      OffsetsFromTermMatches(history_match.url_matches());
2485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::OffsetAdjuster::Adjustments adjustments;
2495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  match.contents = net::FormatUrlWithAdjustments(
2505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      info.url(), languages_, format_types, net::UnescapeRule::SPACES, NULL,
2515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      NULL, &adjustments);
2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::TermMatches new_matches =
254f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ReplaceOffsetsInTermMatches(history_match.url_matches(), offsets);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents_class =
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SpansFromTermMatch(new_matches, match.contents.length(), true);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible.
2595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (history_match.can_inline()) {
2605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    DCHECK(!new_matches.empty());
261ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    size_t inline_autocomplete_offset = new_matches[0].offset +
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new_matches[0].length;
263ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // |inline_autocomplete_offset| may be beyond the end of the
264ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // |fill_into_edit| if the user has typed an URL with a scheme and the
265ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // last character typed is a slash.  That slash is removed by the
266ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // FormatURLWithOffsets call above.
267ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (inline_autocomplete_offset < match.fill_into_edit.length()) {
268ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      match.inline_autocompletion =
269ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          match.fill_into_edit.substr(inline_autocomplete_offset);
270ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    match.allowed_to_be_default_match = match.inline_autocompletion.empty() ||
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        !PreventInlineAutocomplete(autocomplete_input_);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  match.EnsureUWYTIsAllowedToBeDefault(
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      autocomplete_input_.canonicalized_url(),
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      TemplateURLServiceFactory::GetForProfile(profile_));
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the description autocomplete presentation.
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description = info.title();
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description_class = SpansFromTermMatch(
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      history_match.title_matches(), match.description.length(), false);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("typed count", info.typed_count());
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("visit count", info.visit_count());
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("last visit", info.last_visit());
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return match;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() {
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (index_for_testing_.get())
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return index_for_testing_.get();
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryService* const history_service =
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!history_service)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return history_service->InMemoryIndex();
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
301