history_quick_provider.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/string_util.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/time.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/utf_string_conversions.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"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/url_fixer_upper.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/search.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.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 "googleurl/src/url_parse.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "googleurl/src/url_util.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/registry_controlled_domains/registry_controlled_domain.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),
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)),
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reorder_for_inlining_(false) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum InliningOption {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINING_PROHIBITED = 0,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINING_ALLOWED = 1,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINING_AUTO_BUT_NOT_IN_FIELD_TRIAL = 2,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINING_FIELD_TRIAL_DEFAULT_GROUP = 3,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINING_FIELD_TRIAL_EXPERIMENT_GROUP = 4,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NUM_OPTIONS = 5
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should always be overwritten
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InliningOption inlining_option = NUM_OPTIONS;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string switch_value = CommandLine::ForCurrentProcess()->
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetSwitchValueASCII(switches::kOmniboxInlineHistoryQuickProvider);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (switch_value == switches::kOmniboxInlineHistoryQuickProviderAllowed) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    inlining_option = INLINING_ALLOWED;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    always_prevent_inline_autocomplete_ = false;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (switch_value ==
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             switches::kOmniboxInlineHistoryQuickProviderProhibited) {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    inlining_option = INLINING_PROHIBITED;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    always_prevent_inline_autocomplete_ = true;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We'll assume any other flag means automatic.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Automatic means eligible for the field trial.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For the field trial stuff to work correctly, we must be running
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // on the same thread as the thread that created the field trial,
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // which happens via a call to OmniboxFieldTrial::Active in
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // chrome_browser_main.cc on the main thread.  Let's check this to
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // be sure.  We check "if we've heard of the UI thread then we'd better
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // be on it."  The first part is necessary so unit tests pass.  (Many
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // unit tests don't set up the threading naming system; hence
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // CurrentlyOn(UI thread) will fail.)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!content::BrowserThread::IsWellKnownThread(
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               content::BrowserThread::UI) ||
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (OmniboxFieldTrial::InDisallowInlineHQPFieldTrial()) {
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (OmniboxFieldTrial::InDisallowInlineHQPFieldTrialExperimentGroup()) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        always_prevent_inline_autocomplete_ = true;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        inlining_option = INLINING_FIELD_TRIAL_EXPERIMENT_GROUP;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        always_prevent_inline_autocomplete_ = false;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        inlining_option = INLINING_FIELD_TRIAL_DEFAULT_GROUP;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      always_prevent_inline_autocomplete_ = false;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      inlining_option = INLINING_AUTO_BUT_NOT_IN_FIELD_TRIAL;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a beacon to the logs that'll allow us to identify later what
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // inlining state a user is in.  Do this by incrementing a bucket in
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a histogram, where the bucket represents the user's inlining state.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION(
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "Omnibox.InlineHistoryQuickProviderFieldTrialBeacon",
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      inlining_option, NUM_OPTIONS);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  reorder_for_inlining_ = CommandLine::ForCurrentProcess()->
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetSwitchValueASCII(switches::
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          kOmniboxHistoryQuickProviderReorderForInlining) ==
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switches::kOmniboxHistoryQuickProviderReorderForInliningEnabled;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::Start(const AutocompleteInput& input,
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool minimal_changes) {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  matches_.clear();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (disabled_)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't bother with INVALID and FORCED_QUERY.  Also pass when looking for
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BEST_MATCH and there is no inline autocompletion because none of the HQP
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // matches can score highly enough to qualify.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((input.type() == AutocompleteInput::INVALID) ||
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (input.type() == AutocompleteInput::FORCED_QUERY) ||
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (input.matches_requested() == AutocompleteInput::BEST_MATCH &&
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       input.prevent_inline_autocomplete()))
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  autocomplete_input_ = input;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(pkasting): We should just block here until this loads.  Any time
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // someone unloads the history backend, we'll get inconsistent inline
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete behavior here.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (GetIndex()) {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeTicks start_time = base::TimeTicks::Now();
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DoAutocomplete();
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (input.text().length() < 6) {
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeTicks end_time = base::TimeTicks::Now();
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string name = "HistoryQuickProvider.QueryIndexTime." +
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::IntToString(input.text().length());
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::HistogramBase* counter = base::Histogram::FactoryGet(
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UpdateStarredStateOfMatches();
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.deletable);
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delete the match from the InMemoryURLIndex.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetIndex()->DeleteURL(match.destination_url);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeleteMatchFromMatches(match);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryQuickProvider::~HistoryQuickProvider() {}
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryQuickProvider::DoAutocomplete() {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the matching URLs from the DB.
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      autocomplete_input_.text(),
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      autocomplete_input_.cursor_position());
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches.empty())
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If we're allowed to reorder results in order to get an inlineable
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // result to appear first (and hence have a HistoryQuickProvider
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // suggestion possibly appear first), find the first inlineable
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // result and then swap it to the front.  Obviously, don't do this
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // if we're told to prevent inline autocompletion.  (If we're told
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // we're going to prevent inline autocompletion, we're going to
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // later demote the score of all results so none will be inlined.
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Hence there's no need to reorder the results so an inlineable one
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // appears first.)
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (reorder_for_inlining_ &&
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !PreventInlineAutocomplete(autocomplete_input_)) {
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (ScoredHistoryMatches::iterator i(matches.begin());
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         (i != matches.end()) &&
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             (i->raw_score >= AutocompleteResult::kLowestDefaultScore);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         ++i) {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (i->can_inline) {  // this test is only true once because of the break
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (i != matches.begin())
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          std::rotate(matches.begin(), i, i + 1);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Figure out if HistoryURL provider has a URL-what-you-typed match
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that ought to go first and what its score will be.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool will_have_url_what_you_typed_match_first = false;
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int url_what_you_typed_match_score = -1;  // undefined
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are necessary (but not sufficient) conditions for the omnibox
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input to be a URL-what-you-typed match.  The username test checks that
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // either the username does not exist (a regular URL such as http://site/)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or, if the username exists (http://user@site/), there must be either
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a password or a port.  Together these exclude pure username@site
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // inputs because these are likely to be an e-mail address.  HistoryURL
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provider won't promote the URL-what-you-typed match to first
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for these inputs.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool can_have_url_what_you_typed_match_first =
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      autocomplete_input_.canonicalized_url().is_valid() &&
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (autocomplete_input_.type() != AutocompleteInput::QUERY) &&
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (autocomplete_input_.type() != AutocompleteInput::FORCED_QUERY) &&
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (!autocomplete_input_.parts().username.is_nonempty() ||
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().password.is_nonempty() ||
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       autocomplete_input_.parts().path.is_nonempty());
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (can_have_url_what_you_typed_match_first) {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryService* const history_service =
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        HistoryServiceFactory::GetForProfile(profile_,
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             Profile::EXPLICIT_ACCESS);
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We expect HistoryService to be available.  In case it's not,
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // (e.g., due to Profile corruption) we let HistoryQuick provider
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // completions (which may be available because it's a different
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // data structure) compete with the URL-what-you-typed match as
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // normal.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (history_service) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::URLDatabase* url_db = history_service->InMemoryDatabase();
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // url_db can be NULL if it hasn't finished initializing (or
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // failed to to initialize).  In this case, we let HistoryQuick
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // provider completions compete with the URL-what-you-typed
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // match as normal.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (url_db) {
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const std::string host(UTF16ToUTF8(autocomplete_input_.text().substr(
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            autocomplete_input_.parts().host.begin,
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            autocomplete_input_.parts().host.len)));
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We want to put the URL-what-you-typed match first if either
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * the user visited the URL before (intranet or internet).
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // * it's a URL on a host that user visited before and this
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   is the root path of the host.  (If the user types some
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   of a path--more than a simple "/"--we let autocomplete compete
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //   normally with the URL-what-you-typed match.)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // TODO(mpearson): Remove this hacky code and simply score URL-what-
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // you-typed in some sane way relative to possible completions:
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // URL-what-you-typed should get some sort of a boost relative
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // to completions, but completions should naturally win if
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // they're a lot more popular.  In this process, if the input
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // is a bare intranet hostname that has been visited before, we
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // may want to enforce that the only completions that can outscore
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // the URL-what-you-typed match are on the same host (i.e., aren't
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // from a longer internet hostname for which the omnibox input is
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // a prefix).
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (url_db->GetRowForURL(
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            autocomplete_input_.canonicalized_url(), NULL) != 0) {
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // We visited this URL before.
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // HistoryURLProvider gives visited what-you-typed URLs a high score.
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          url_what_you_typed_match_score =
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              HistoryURLProvider::kScoreForBestInlineableResult;
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        } else if (url_db->IsTypedHost(host) &&
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             (!autocomplete_input_.parts().path.is_nonempty() ||
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              ((autocomplete_input_.parts().path.len == 1) &&
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               (autocomplete_input_.text()[
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   autocomplete_input_.parts().path.begin] == '/'))) &&
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             !autocomplete_input_.parts().query.is_nonempty() &&
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             !autocomplete_input_.parts().ref.is_nonempty()) {
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          // Not visited, but we've seen the host before.
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          will_have_url_what_you_typed_match_first = true;
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          if (net::RegistryControlledDomainService::GetRegistryLength(
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              host, false) == 0) {
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known intranet hosts get one score.
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForUnvisitedIntranetResult;
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          } else {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            // Known internet hosts get another.
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            url_what_you_typed_match_score =
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HistoryURLProvider::kScoreForWhatYouTypedResult;
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          }
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loop over every result and add it to matches_.  In the process,
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // guarantee that scores are decreasing.  |max_match_score| keeps
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // track of the highest score we can assign to any later results we
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // see.  Also, if we're not allowing inline autocompletions in
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // general or the current best suggestion isn't inlineable,
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // artificially reduce the starting |max_match_score| (which
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // therefore applies to all results) to something low enough that
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // guarantees no result will be offered as an autocomplete
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // suggestion.  Also do a similar reduction if we think there will be
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a URL-what-you-typed match.  (We want URL-what-you-typed matches for
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // visited URLs to beat out any longer URLs, no matter how frequently
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // they're visited.)  The strength of this last reduction depends on the
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // likely score for the URL-what-you-typed result.
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |template_url_service| or |template_url| can be NULL in unit tests.
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURLService* template_url_service =
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TemplateURLServiceFactory::GetForProfile(profile_);
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURL* template_url = template_url_service ?
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      template_url_service->GetDefaultSearchProvider() : NULL;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int max_match_score = (PreventInlineAutocomplete(autocomplete_input_) ||
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !matches.begin()->can_inline) ?
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (AutocompleteResult::kLowestDefaultScore - 1) :
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      matches.begin()->raw_score;
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (will_have_url_what_you_typed_match_first) {
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    max_match_score = std::min(max_match_score,
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        url_what_you_typed_match_score - 1);
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (ScoredHistoryMatches::const_iterator match_iter = matches.begin();
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       match_iter != matches.end(); ++match_iter) {
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match(*match_iter);
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Culls results corresponding to queries from the default search engine.
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // These are low-quality, difficult-to-understand matches for users, and the
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // SearchProvider should surface past queries in a better way anyway.
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!template_url ||
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        !template_url->IsSearchURL(history_match.url_info.url())) {
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Set max_match_score to the score we'll assign this result:
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      max_match_score = std::min(max_match_score, history_match.raw_score);
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      matches_.push_back(QuickMatchToACMatch(history_match, max_match_score));
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Mark this max_match_score as being used:
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      max_match_score--;
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch(
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ScoredHistoryMatch& history_match,
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int score) {
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const history::URLRow& info = history_match.url_info;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteMatch match(this, score, !!info.visit_count(),
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history_match.url_matches.empty() ?
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          AutocompleteMatch::HISTORY_TITLE : AutocompleteMatch::HISTORY_URL);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.typed_count = info.typed_count();
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.destination_url = info.url();
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the URL autocomplete presentation.
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<size_t> offsets =
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OffsetsFromTermMatches(history_match.url_matches);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.fill_into_edit =
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(),
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          net::FormatUrlWithOffsets(info.url(), languages_, format_types,
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              net::UnescapeRule::SPACES, NULL, NULL, &offsets));
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::TermMatches new_matches =
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ReplaceOffsetsInTermMatches(history_match.url_matches, offsets);
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents = net::FormatUrl(info.url(), languages_, format_types,
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              net::UnescapeRule::SPACES, NULL, NULL, NULL);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents_class =
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SpansFromTermMatch(new_matches, match.contents.length(), true);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!history_match.can_inline) {
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    match.inline_autocomplete_offset = string16::npos;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!new_matches.empty());
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    match.inline_autocomplete_offset = new_matches[0].offset +
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new_matches[0].length;
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The following will happen if the user has typed an URL with a scheme
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // and the last character typed is a slash because that slash is removed
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // by the FormatURLWithOffsets call above.
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (match.inline_autocomplete_offset > match.fill_into_edit.length())
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      match.inline_autocomplete_offset = match.fill_into_edit.length();
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the description autocomplete presentation.
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description = info.title();
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description_class = SpansFromTermMatch(
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history_match.title_matches, match.description.length(), false);
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("typed count", info.typed_count());
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("visit count", info.visit_count());
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.RecordAdditionalInfo("last visit", info.last_visit());
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return match;
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() {
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (index_for_testing_.get())
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return index_for_testing_.get();
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryService* const history_service =
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!history_service)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return history_service->InMemoryIndex();
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ACMatchClassifications HistoryQuickProvider::SpansFromTermMatch(
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const history::TermMatches& matches,
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t text_length,
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_url) {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassification::Style url_style =
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatchClassifications spans;
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches.empty()) {
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (text_length)
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      spans.push_back(ACMatchClassification(0, url_style));
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return spans;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches[0].offset)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    spans.push_back(ACMatchClassification(0, url_style));
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t match_count = matches.size();
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < match_count;) {
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t offset = matches[i].offset;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    spans.push_back(ACMatchClassification(offset,
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ACMatchClassification::MATCH | url_style));
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Skip all adjacent matches.
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    do {
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      offset += matches[i].length;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++i;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } while ((i < match_count) && (offset == matches[i].offset));
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (offset < text_length)
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      spans.push_back(ACMatchClassification(offset, url_style));
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return spans;
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
422