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_url_provider.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_backend.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_database.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_service_factory.h"
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/history/in_memory_url_index_types.h"
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/history/scored_history_match.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/bookmarks/browser/bookmark_utils.h"
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/history_types.h"
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/metrics/proto/omnibox_input_type.pb.h"
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match.h"
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_provider_listener.h"
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/omnibox/autocomplete_result.h"
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/omnibox/omnibox_field_trial.h"
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/url_prefix.h"
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.h"
40f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "content/public/browser/browser_thread.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/gurl.h"
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_parse.h"
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_util.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Acts like the > operator for URLInfo classes.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool CompareHistoryMatch(const history::HistoryMatch& a,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const history::HistoryMatch& b) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A URL that has been typed at all is better than one that has never been
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // typed.  (Note "!"s on each side)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!a.url_info.typed_count() != !b.url_info.typed_count())
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a.url_info.typed_count() > b.url_info.typed_count();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Innermost matches (matches after any scheme or "www.") are better than
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // non-innermost matches.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (a.innermost_match != b.innermost_match)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a.innermost_match;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLs that have been typed more often are better.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (a.url_info.typed_count() != b.url_info.typed_count())
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a.url_info.typed_count() > b.url_info.typed_count();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For URLs that have each been typed once, a host (alone) is better than a
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // page inside.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((a.url_info.typed_count() == 1) && (a.IsHostOnly() != b.IsHostOnly()))
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a.IsHostOnly();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLs that have been visited more often are better.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (a.url_info.visit_count() != b.url_info.visit_count())
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a.url_info.visit_count() > b.url_info.visit_count();
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLs that have been visited more recently are better.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return a.url_info.last_visit() > b.url_info.last_visit();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Sorts and dedups the given list of matches.
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void SortAndDedupMatches(history::HistoryMatches* matches) {
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Sort by quality, best first.
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::sort(matches->begin(), matches->end(), &CompareHistoryMatch);
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Remove duplicate matches (caused by the search string appearing in one of
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // the prefixes as well as after it).  Consider the following scenario:
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // User has visited "http://http.com" once and "http://htaccess.com" twice.
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // User types "http".  The autocomplete search with prefix "http://" returns
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // the first host, while the search with prefix "" returns both hosts.  Now
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // we sort them into rank order:
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   http://http.com     (innermost_match)
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   http://htaccess.com (!innermost_match, url_info.visit_count == 2)
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   http://http.com     (!innermost_match, url_info.visit_count == 1)
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // The above scenario tells us we can't use std::unique(), since our
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // duplicates are not always sequential.  It also tells us we should remove
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // the lower-quality duplicate(s), since otherwise the returned results won't
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // be ordered correctly.  This is easy to do: we just always remove the later
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // element of a duplicate pair.
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Be careful!  Because the vector contents may change as we remove elements,
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // we use an index instead of an iterator in the outer loop, and don't
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // precalculate the ending position.
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < matches->size(); ++i) {
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (history::HistoryMatches::iterator j(matches->begin() + i + 1);
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         j != matches->end(); ) {
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if ((*matches)[i].url_info.url() == j->url_info.url())
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        j = matches->erase(j);
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      else
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        ++j;
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Calculates a new relevance score applying half-life time decaying to |count|
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// using |time_since_last_visit| and |score_buckets|.  This function will never
117f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// return a score higher than |undecayed_relevance|; in other words, it can only
118f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// demote the old score.
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)double CalculateRelevanceUsingScoreBuckets(
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const HUPScoringParams::ScoreBuckets& score_buckets,
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::TimeDelta& time_since_last_visit,
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int undecayed_relevance,
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int count) {
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Back off if above relevance cap.
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if ((score_buckets.relevance_cap() != -1) &&
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (undecayed_relevance >= score_buckets.relevance_cap()))
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return undecayed_relevance;
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Time based decay using half-life time.
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  double decayed_count = count;
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (decayed_count > 0)
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    decayed_count *= score_buckets.HalfLifeTimeDecay(time_since_last_visit);
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Find a threshold where decayed_count >= bucket.
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const HUPScoringParams::ScoreBuckets::CountMaxRelevance* score_bucket = NULL;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = 0; i < score_buckets.buckets().size(); ++i) {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    score_bucket = &score_buckets.buckets()[i];
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (decayed_count >= score_bucket->first)
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;  // Buckets are in descending order, so we can ignore the rest.
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return (score_bucket && (undecayed_relevance > score_bucket->second)) ?
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      score_bucket->second : undecayed_relevance;
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
146f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Returns a new relevance score for the given |match| based on the
147f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// |old_relevance| score and |scoring_params|.  The new relevance score is
148f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// guaranteed to be less than or equal to |old_relevance|.  In other words, this
149f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// function can only demote a score, never boost it.  Returns |old_relevance| if
1506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// experimental scoring is disabled.
151f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)int CalculateRelevanceScoreUsingScoringParams(
152f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const history::HistoryMatch& match,
153f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int old_relevance,
154f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const HUPScoringParams& scoring_params) {
155f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!scoring_params.experimental_scoring_enabled)
156f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return old_relevance;
157f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const base::TimeDelta time_since_last_visit =
159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      base::Time::Now() - match.url_info.last_visit();
160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  int relevance = CalculateRelevanceUsingScoreBuckets(
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      scoring_params.typed_count_buckets, time_since_last_visit, old_relevance,
163f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      match.url_info.typed_count());
164f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
165f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Additional demotion (on top of typed_count demotion) of URLs that were
166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // never typed.
167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (match.url_info.typed_count() == 0) {
168f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    relevance = CalculateRelevanceUsingScoreBuckets(
169f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        scoring_params.visited_count_buckets, time_since_last_visit, relevance,
170f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        match.url_info.visit_count());
171f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
172f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
173f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK_LE(relevance, old_relevance);
174f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return relevance;
175f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
176f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
177f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Extracts typed_count, visit_count, and last_visited time from the URLRow and
178f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// puts them in the additional info field of the |match| for display in
179f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// about:omnibox.
180f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void RecordAdditionalInfoFromUrlRow(const history::URLRow& info,
181f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                    AutocompleteMatch* match) {
182f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  match->RecordAdditionalInfo("typed count", info.typed_count());
183f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  match->RecordAdditionalInfo("visit count", info.visit_count());
184f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  match->RecordAdditionalInfo("last visit", info.last_visit());
185f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
186f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
187f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// If |create_if_necessary| is true, ensures that |matches| contains an entry
188f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// for |info|, creating a new such entry if necessary (using |input_location|
189f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// and |match_in_scheme|).
190f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//
191f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// If |promote| is true, this also ensures the entry is the first element in
192f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// |matches|, moving or adding it to the front as appropriate.  When |promote|
193f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// is false, existing matches are left in place, and newly added matches are
194f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// placed at the back.
195f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//
196f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// It's OK to call this function with both |create_if_necessary| and |promote|
197f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// false, in which case we'll do nothing.
198f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//
199f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Returns whether the match exists regardless if it was promoted/created.
200f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool CreateOrPromoteMatch(const history::URLRow& info,
201f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          size_t input_location,
202f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          bool match_in_scheme,
203f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          history::HistoryMatches* matches,
204f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          bool create_if_necessary,
205f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          bool promote) {
206f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |matches| may already have an entry for this.
207f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (history::HistoryMatches::iterator i(matches->begin());
208f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       i != matches->end(); ++i) {
209f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (i->url_info.url() == info.url()) {
210f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      // Rotate it to the front if the caller wishes.
211f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (promote)
212f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        std::rotate(matches->begin(), i, i + 1);
213f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return true;
214f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
215f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
216f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
217f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!create_if_necessary)
218f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return false;
219f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
220f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // No entry, so create one.
221f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  history::HistoryMatch match(info, input_location, match_in_scheme, true);
222f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (promote)
223f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    matches->push_front(match);
224f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  else
225f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    matches->push_back(match);
226f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
227f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return true;
228f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Returns whether |match| is suitable for inline autocompletion.
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool CanPromoteMatchForInlineAutocomplete(const history::HistoryMatch& match) {
2326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // We can promote this match if it's been typed at least n times, where n == 1
2336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // for "simple" (host-only) URLs and n == 2 for others.  We set a higher bar
2346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // for these long URLs because it's less likely that users will want to visit
2356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // them again.  Even though we don't increment the typed_count for pasted-in
2366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // URLs, if the user manually edits the URL or types some long thing in by
2376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // hand, we wouldn't want to immediately start autocompleting it.
2386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return match.url_info.typed_count() &&
2396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      ((match.url_info.typed_count() > 1) || match.IsHostOnly());
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Given the user's |input| and a |match| created from it, reduce the match's
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// URL to just a host.  If this host still matches the user input, return it.
244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Returns the empty string on failure.
245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GURL ConvertToHostOnly(const history::HistoryMatch& match,
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       const base::string16& input) {
247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // See if we should try to do host-only suggestions for this URL. Nonstandard
248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // schemes means there's no authority section, so suggesting the host name
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // is useless. File URLs are standard, but host suggestion is not useful for
250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // them either.
251f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const GURL& url = match.url_info.url();
252f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile())
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return GURL();
254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Transform to a host-only match.  Bail if the host no longer matches the
256f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // user input (e.g. because the user typed more than just a host).
257f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GURL host = url.GetWithEmptyPath();
258f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if ((host.spec().length() < (match.input_location + input.length())))
259f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return GURL();  // User typing is longer than this host suggestion.
260f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
261f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const base::string16 spec = base::UTF8ToUTF16(host.spec());
262f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (spec.compare(match.input_location, input.length(), input))
263f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return GURL();  // User typing is no longer a prefix.
264f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
265f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return host;
266f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// -----------------------------------------------------------------
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SearchTermsDataSnapshot
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Implementation of SearchTermsData that takes a snapshot of another
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SearchTermsData by copying all the responses to the different getters into
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// member strings, then returning those strings when its own getters are called.
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This will typically be constructed on the UI thread from
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// UIThreadSearchTermsData but is subsequently safe to use on any thread.
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SearchTermsDataSnapshot : public SearchTermsData {
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit SearchTermsDataSnapshot(const SearchTermsData& search_terms_data);
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~SearchTermsDataSnapshot();
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual std::string GoogleBaseURLValue() const OVERRIDE;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual std::string GetApplicationLocale() const OVERRIDE;
285c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual base::string16 GetRlzParameterValue(
286c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      bool from_app_list) const OVERRIDE;
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual std::string GetSearchClient() const OVERRIDE;
288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual bool EnableAnswersInSuggest() const OVERRIDE;
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual bool IsShowingSearchTermsOnSearchResultsPages() const OVERRIDE;
290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual std::string InstantExtendedEnabledParam(
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool for_search) const OVERRIDE;
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual std::string ForceInstantResultsParam(
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool for_prerender) const OVERRIDE;
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual std::string NTPIsThemedParam() const OVERRIDE;
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual std::string GoogleImageSearchSource() const OVERRIDE;
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string google_base_url_value_;
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string application_locale_;
300a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 rlz_parameter_value_;
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string search_client_;
302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool enable_answers_in_suggest_;
303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_showing_search_terms_on_search_results_pages_;
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string instant_extended_enabled_param_;
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string instant_extended_enabled_param_for_search_;
306116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string force_instant_results_param_;
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string force_instant_results_param_for_prerender_;
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string ntp_is_themed_param_;
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string google_image_search_source_;
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot);
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SearchTermsDataSnapshot::SearchTermsDataSnapshot(
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SearchTermsData& search_terms_data)
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : google_base_url_value_(search_terms_data.GoogleBaseURLValue()),
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      application_locale_(search_terms_data.GetApplicationLocale()),
318c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      rlz_parameter_value_(search_terms_data.GetRlzParameterValue(false)),
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      search_client_(search_terms_data.GetSearchClient()),
320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      enable_answers_in_suggest_(search_terms_data.EnableAnswersInSuggest()),
321116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      is_showing_search_terms_on_search_results_pages_(
322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          search_terms_data.IsShowingSearchTermsOnSearchResultsPages()),
323116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      instant_extended_enabled_param_(
324116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          search_terms_data.InstantExtendedEnabledParam(false)),
325116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      instant_extended_enabled_param_for_search_(
326116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          search_terms_data.InstantExtendedEnabledParam(true)),
327116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      force_instant_results_param_(
328116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          search_terms_data.ForceInstantResultsParam(false)),
329116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      force_instant_results_param_for_prerender_(
330116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          search_terms_data.ForceInstantResultsParam(true)),
331116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ntp_is_themed_param_(search_terms_data.NTPIsThemedParam()),
332116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      google_image_search_source_(search_terms_data.GoogleImageSearchSource()) {
333116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SearchTermsDataSnapshot::~SearchTermsDataSnapshot() {
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const {
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return google_base_url_value_;
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string SearchTermsDataSnapshot::GetApplicationLocale() const {
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return application_locale_;
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
346c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbase::string16 SearchTermsDataSnapshot::GetRlzParameterValue(
347c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool from_app_list) const {
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return rlz_parameter_value_;
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string SearchTermsDataSnapshot::GetSearchClient() const {
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return search_client_;
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
355116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool SearchTermsDataSnapshot::EnableAnswersInSuggest() const {
356116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return enable_answers_in_suggest_;
357116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
358116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
359116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool SearchTermsDataSnapshot::IsShowingSearchTermsOnSearchResultsPages() const {
360116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return is_showing_search_terms_on_search_results_pages_;
361116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
363116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string SearchTermsDataSnapshot::InstantExtendedEnabledParam(
364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool for_search) const {
365116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return for_search ? instant_extended_enabled_param_ :
366116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      instant_extended_enabled_param_for_search_;
367116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
368116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string SearchTermsDataSnapshot::ForceInstantResultsParam(
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool for_prerender) const {
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return for_prerender ? force_instant_results_param_ :
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      force_instant_results_param_for_prerender_;
373116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
374116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string SearchTermsDataSnapshot::NTPIsThemedParam() const {
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return ntp_is_themed_param_;
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
379116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string SearchTermsDataSnapshot::GoogleImageSearchSource() const {
380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return google_image_search_source_;
381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// -----------------------------------------------------------------
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// HistoryURLProvider
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These ugly magic numbers will go away once we switch all scoring
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// behavior (including URL-what-you-typed) to HistoryQuick provider.
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int HistoryURLProvider::kScoreForBestInlineableResult = 1413;
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int HistoryURLProvider::kScoreForUnvisitedIntranetResult = 1403;
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int HistoryURLProvider::kScoreForWhatYouTypedResult = 1203;
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int HistoryURLProvider::kBaseScoreForNonInlineableResult = 900;
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// VisitClassifier is used to classify the type of visit to a particular url.
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryURLProvider::VisitClassifier {
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Type {
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INVALID,             // Navigations to the URL are not allowed.
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    UNVISITED_INTRANET,  // A navigable URL for which we have no visit data but
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         // which is known to refer to a visited intranet host.
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VISITED,             // The site has been previously visited.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VisitClassifier(HistoryURLProvider* provider,
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  const AutocompleteInput& input,
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  history::URLDatabase* db);
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type of visit for the specified input.
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type type() const { return type_; }
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the URLRow for the visit.
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const history::URLRow& url_row() const { return url_row_; }
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProvider* provider_;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::URLDatabase* db_;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type type_;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::URLRow url_row_;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(VisitClassifier);
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryURLProvider::VisitClassifier::VisitClassifier(
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryURLProvider* provider,
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const AutocompleteInput& input,
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::URLDatabase* db)
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : provider_(provider),
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      db_(db),
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      type_(INVALID) {
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& url = input.canonicalized_url();
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Detect email addresses.  These cases will look like "http://user@site/",
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and because the history backend strips auth creds, we'll get a bogus exact
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // match below if the user has visited "site".
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url.is_valid() ||
434f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ((input.type() == metrics::OmniboxInputType::UNKNOWN) &&
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       input.parts().username.is_nonempty() &&
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       !input.parts().password.is_nonempty() &&
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       !input.parts().path.is_nonempty()))
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (db_->GetRowForURL(url, &url_row_)) {
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    type_ = VISITED;
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (provider_->CanFindIntranetURL(db_, input)) {
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The user typed an intranet hostname that they've visited (albeit with a
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // different port and/or path) before.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_row_ = history::URLRow(url);
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    type_ = UNVISITED_INTRANET;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryURLProviderParams::HistoryURLProviderParams(
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const AutocompleteInput& input,
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool trim_http,
456f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const AutocompleteMatch& what_you_typed_match,
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& languages,
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TemplateURL* default_search_provider,
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SearchTermsData& search_terms_data)
46090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : message_loop(base::MessageLoop::current()),
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      input(input),
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prevent_inline_autocomplete(input.prevent_inline_autocomplete()),
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      trim_http(trim_http),
464f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      what_you_typed_match(what_you_typed_match),
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      failed(false),
4666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      exact_suggestion_is_in_history(false),
4676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      promote_type(NEITHER),
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      languages(languages),
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      default_search_provider(default_search_provider ?
470f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          new TemplateURL(default_search_provider->data()) : NULL),
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) {
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryURLProviderParams::~HistoryURLProviderParams() {
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryURLProvider::HistoryURLProvider(AutocompleteProviderListener* listener,
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       Profile* profile)
479116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : HistoryProvider(profile, AutocompleteProvider::TYPE_HISTORY_URL),
480116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      listener_(listener),
4815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      params_(NULL) {
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Initialize HUP scoring params based on the current experiment.
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_);
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void HistoryURLProvider::Start(const AutocompleteInput& input,
4870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               bool minimal_changes) {
4880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // NOTE: We could try hard to do less work in the |minimal_changes| case
4890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // here; some clever caching would let us reuse the raw matches from the
4900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // history DB without re-querying.  However, we'd still have to go back to
4910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // the history thread to mark these up properly, and if pass 2 is currently
4920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // running, we'd need to wait for it to return to the main thread before
4930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // doing this (we can't just write new data for it to read due to thread
4940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // safety issues).  At that point it's just as fast, and easier, to simply
4950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // re-run the query from scratch and ignore |minimal_changes|.
4960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Cancel any in-progress query.
4980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Stop(false);
4990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
50046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  matches_.clear();
50146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
502f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if ((input.type() == metrics::OmniboxInputType::INVALID) ||
503f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
504f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return;
505f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
506f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Do some fixup on the user input before matching against it, so we provide
507f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // good results for local file paths, input with spaces, etc.
508f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const FixupReturn fixup_return(FixupUserInput(input));
509f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!fixup_return.first)
51046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
511f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  url::Parsed parts;
512f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  url_fixer::SegmentURL(fixup_return.second, &parts);
513f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  AutocompleteInput fixed_up_input(input);
514f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  fixed_up_input.UpdateText(fixup_return.second, base::string16::npos, parts);
51546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
516f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Create a match for what the user typed.
51746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text());
518f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  AutocompleteMatch what_you_typed_match(SuggestExactInput(
519f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      fixed_up_input.text(), fixed_up_input.canonicalized_url(), trim_http));
520f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  what_you_typed_match.relevance = CalculateRelevance(WHAT_YOU_TYPED, 0);
521f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
522f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Add the WYT match as a fallback in case we can't get the history service or
523f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // URL DB; otherwise, we'll replace this match lower down.  Don't do this for
524f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // queries, though -- while we can sometimes mark up a match for them, it's
525f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // not what the user wants, and just adds noise.
526f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (fixed_up_input.type() != metrics::OmniboxInputType::QUERY)
527f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    matches_.push_back(what_you_typed_match);
52846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
52946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // We'll need the history service to run both passes, so try to obtain it.
53046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!profile_)
53146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
53246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  HistoryService* const history_service =
53346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
53446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!history_service)
53546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
53646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
53746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Get the default search provider and search terms data now since we have to
53846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // retrieve these on the UI thread, and the second pass runs on the history
53946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // thread. |template_url_service| can be NULL when testing.
54046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TemplateURLService* template_url_service =
54146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      TemplateURLServiceFactory::GetForProfile(profile_);
54246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TemplateURL* default_search_provider = template_url_service ?
54346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      template_url_service->GetDefaultSearchProvider() : NULL;
54446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  UIThreadSearchTermsData data(profile_);
54546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
54646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Create the data structure for the autocomplete passes.  We'll save this off
54746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // onto the |params_| member for later deletion below if we need to run pass
54846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // 2.
54946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<HistoryURLProviderParams> params(
55046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      new HistoryURLProviderParams(
551f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          fixed_up_input, trim_http, what_you_typed_match,
55246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
55346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          default_search_provider, data));
55446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Note that we use the non-fixed-up input here, since fixup may strip
55546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // trailing whitespace.
55646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  params->prevent_inline_autocomplete = PreventInlineAutocomplete(input);
55746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
55846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Pass 1: Get the in-memory URL database, and use it to find and promote
55946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // the inline autocomplete match, if any.
56046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  history::URLDatabase* url_db = history_service->InMemoryDatabase();
56146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // url_db can be NULL if it hasn't finished initializing (or failed to
56246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // initialize).  In this case all we can do is fall back on the second
56346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // pass.
56446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  //
56546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // TODO(pkasting): We should just block here until this loads.  Any time
56646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // someone unloads the history backend, we'll get inconsistent inline
56746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // autocomplete behavior here.
56846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (url_db) {
56946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    DoAutocomplete(NULL, url_db, params.get());
57046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    matches_.clear();
5716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    PromoteMatchesIfNecessary(*params);
5726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // NOTE: We don't reset |params| here since at least the |promote_type|
5736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // field on it will be read by the second pass -- see comments in
5746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // DoAutocomplete().
57546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
57646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
57746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Pass 2: Ask the history service to call us back on the history thread,
57846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // where we can read the full on-disk DB.
579f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (input.want_asynchronous_matches()) {
58046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    done_ = false;
58146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    params_ = params.release();  // This object will be destroyed in
58246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                 // QueryComplete() once we're done with it.
583116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    history_service->ScheduleAutocomplete(
584116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        base::Bind(&HistoryURLProvider::ExecuteWithDB, this, params_));
58546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
5860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
5870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void HistoryURLProvider::Stop(bool clear_cached_results) {
5890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  done_ = true;
5900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (params_)
5920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    params_->cancel_flag.Set();
5930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
5940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryURLProvider::SuggestExactInput(
596a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& text,
5970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const GURL& destination_url,
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool trim_http) {
599f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The FormattedStringWithEquivalentMeaning() call below requires callers to
600f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // be on the UI thread.
601f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
602f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      !content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
603f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
6040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  AutocompleteMatch match(this, 0, false,
60590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                          AutocompleteMatchType::URL_WHAT_YOU_TYPED);
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (destination_url.is_valid()) {
6080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    match.destination_url = destination_url;
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Trim off "http://" if the user didn't type it.
6110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    DCHECK(!trim_http || !AutocompleteInput::HasHTTPScheme(text));
612a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 display_string(
613116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        net::FormatUrl(destination_url, std::string(),
614116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       net::kFormatUrlOmitAll & ~net::kFormatUrlOmitHTTP,
615116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       net::UnescapeRule::SPACES, NULL, NULL, NULL));
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0;
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    match.fill_into_edit =
618116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        AutocompleteInput::FormattedStringWithEquivalentMeaning(
619116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            destination_url,
620116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            display_string,
621116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            ChromeAutocompleteSchemeClassifier(profile_));
622c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    match.allowed_to_be_default_match = true;
623ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // NOTE: Don't set match.inline_autocompletion to something non-empty here;
624ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // it's surprising and annoying.
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Try to highlight "innermost" match location.  If we fix up "w" into
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // "www.w.com", we want to highlight the fifth character, not the first.
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This relies on match.destination_url being the non-prefix-trimmed version
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // of match.contents.
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    match.contents = display_string;
6315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(
6325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::UTF8ToUTF16(destination_url.spec()), text);
63358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // It's possible for match.destination_url to not contain the user's input
63458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // at all (so |best_prefix| is NULL), for example if the input is
63558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // "view-source:x" and |destination_url| has an inserted "http://" in the
63658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // middle.
63758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (best_prefix == NULL) {
6380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      AutocompleteMatch::ClassifyMatchInString(text, match.contents,
63958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                               ACMatchClassification::URL,
64058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                               &match.contents_class);
64158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    } else {
64258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      AutocompleteMatch::ClassifyLocationInString(
6430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          best_prefix->prefix.length() - offset, text.length(),
64458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          match.contents.length(), ACMatchClassification::URL,
64558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &match.contents_class);
64658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    match.is_history_what_you_typed_match = true;
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return match;
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
654116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams* params,
655116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       history::HistoryBackend* backend,
656116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       history::URLDatabase* db) {
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We may get called with a NULL database if it couldn't be properly
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialized.
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!db) {
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    params->failed = true;
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (!params->cancel_flag.IsSet()) {
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeTicks beginning_time = base::TimeTicks::Now();
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DoAutocomplete(backend, db, params);
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime",
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        base::TimeTicks::Now() - beginning_time);
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the results (if any) to the main thread.
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params->message_loop->PostTask(FROM_HERE, base::Bind(
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &HistoryURLProvider::QueryComplete, this, params));
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)HistoryURLProvider::~HistoryURLProvider() {
676f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Note: This object can get leaked on shutdown if there are pending
677f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // requests on the database (which hold a reference to us). Normally, these
678f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // messages get flushed for each thread. We do a round trip from main, to
679f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // history, back to main while holding a reference. If the main thread
680f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // completes before the history thread, the message to delegate back to the
681f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // main thread will not run and the reference will leak. Therefore, don't do
682f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // anything on destruction.
683f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
684f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
685f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// static
686f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)int HistoryURLProvider::CalculateRelevance(MatchType match_type,
687f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                           int match_number) {
688f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  switch (match_type) {
689f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case INLINE_AUTOCOMPLETE:
690f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return kScoreForBestInlineableResult;
691f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
692f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case UNVISITED_INTRANET:
693f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return kScoreForUnvisitedIntranetResult;
694f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
695f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case WHAT_YOU_TYPED:
696f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return kScoreForWhatYouTypedResult;
697f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
698f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    default:  // NORMAL
699f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return kBaseScoreForNonInlineableResult + match_number;
700f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
701f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
702f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
703f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// static
704f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ACMatchClassifications HistoryURLProvider::ClassifyDescription(
705f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const base::string16& input_text,
706f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const base::string16& description) {
707116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::string16 clean_description =
708116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bookmarks::CleanUpTitleForMatching(description);
709f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  history::TermMatches description_matches(SortAndDeoverlapMatches(
710f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      history::MatchTermInString(input_text, clean_description, 0)));
711f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  history::WordStarts description_word_starts;
712f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  history::String16VectorFromString16(
713f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      clean_description, false, &description_word_starts);
714f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // If HistoryURL retrieves any matches (and hence we reach this code), we
715f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // are guaranteed that the beginning of input_text must be a word break.
716f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  history::WordStarts offsets(1, 0u);
717f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  description_matches =
718f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      history::ScoredHistoryMatch::FilterTermMatchesByWordStarts(
719f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          description_matches, offsets, description_word_starts, 0,
720f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          std::string::npos);
721f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return SpansFromTermMatch(
722f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      description_matches, clean_description.length(), false);
723f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
724f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend,
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        history::URLDatabase* db,
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        HistoryURLProviderParams* params) {
728f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Get the matching URLs from the DB.
7296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  params->matches.clear();
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::URLRows url_matches;
731f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes();
732f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end();
733f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       ++i) {
734f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (params->cancel_flag.IsSet())
735f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return;  // Canceled in the middle of a query, give up.
736f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
737f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // We only need kMaxMatches results in the end, but before we get there we
738f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // need to promote lower-quality matches that are prefixes of higher-quality
739f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // matches, and remove lower-quality redirects.  So we ask for more results
740f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // than we need, of every prefix type, in hopes this will give us far more
741f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // than enough to work with.  CullRedirects() will then reduce the list to
742f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // the best kMaxMatches results.
743f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    db->AutocompleteForPrefix(
744f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        base::UTF16ToUTF8(i->prefix + params->input.text()), kMaxMatches * 2,
745f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        !backend, &url_matches);
746f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    for (history::URLRows::const_iterator j(url_matches.begin());
747f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          j != url_matches.end(); ++j) {
748f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(
749f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          base::UTF8ToUTF16(j->url().spec()), base::string16());
750f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      DCHECK(best_prefix);
7516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      params->matches.push_back(history::HistoryMatch(
752f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          *j, i->prefix.length(), !i->num_components,
753f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          i->num_components >= best_prefix->num_components));
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create sorted list of suggestions.
7586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  CullPoorMatches(params);
7596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SortAndDedupMatches(&params->matches);
760f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
761f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Try to create a shorter suggestion from the best match.
762116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // We consider the what you typed match eligible for display when there's a
763116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // reasonable chance the user actually cares:
764f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // * Their input can be opened as a URL, and
765f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // * We parsed the input as a URL, or it starts with an explicit "http:" or
7666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // "https:".
767f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Otherwise, this is just low-quality noise.  In the cases where we've parsed
768f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // as UNKNOWN, we'll still show an accidental search infobar if need be.
769f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  VisitClassifier classifier(this, params->input, db);
7706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  params->have_what_you_typed_match =
771f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (params->input.type() != metrics::OmniboxInputType::QUERY) &&
772f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) ||
773f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       (classifier.type() == VisitClassifier::UNVISITED_INTRANET) ||
774f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       !params->trim_http ||
775f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0));
7766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const bool have_shorter_suggestion_suitable_for_inline_autocomplete =
7776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      PromoteOrCreateShorterSuggestion(
7786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          db, params->have_what_you_typed_match, params);
7796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
7806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Check whether what the user typed appears in history.
7816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const bool can_check_history_for_exact_match =
7826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // Checking what_you_typed_match.is_history_what_you_typed_match tells us
7836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // whether SuggestExactInput() succeeded in constructing a valid match.
7846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      params->what_you_typed_match.is_history_what_you_typed_match &&
7856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // Additionally, in the case where the user has typed "foo.com" and
7866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // visited (but not typed) "foo/", and the input is "foo", the first pass
7876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the
7886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // second pass can suggest the exact input as a better URL.  Since we need
7896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // both passes to agree, and since during the first pass there's no way to
7906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // know about "foo/", ensure that if the promote type was set to
7916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // FRONT_HISTORY_MATCH during the first pass, the second pass will not
7926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // consider the exact suggestion to be in history and therefore will not
7936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // suggest the exact input as a better match.  (Note that during the first
7946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // pass, this conditional will always succeed since |promote_type| is
7956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // initialized to NEITHER.)
7966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      (params->promote_type != HistoryURLProviderParams::FRONT_HISTORY_MATCH);
7976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  params->exact_suggestion_is_in_history = can_check_history_for_exact_match &&
7986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      FixupExactSuggestion(db, classifier, params);
7996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
8006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // If we succeeded in fixing up the exact match based on the user's history,
8016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // we should treat it as the best match regardless of input type.  If not,
8026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // then we check whether there's an inline autocompletion we can create from
8036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // this input, so we can promote that as the best match.
8046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (params->exact_suggestion_is_in_history) {
8056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH;
8066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  } else if (!params->prevent_inline_autocomplete && !params->matches.empty() &&
8076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      (have_shorter_suggestion_suitable_for_inline_autocomplete ||
8086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       CanPromoteMatchForInlineAutocomplete(params->matches[0]))) {
8096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH;
8106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  } else {
8116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // Failed to promote any URLs.  Use the What You Typed match, if we have it.
8126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    params->promote_type = params->have_what_you_typed_match ?
8136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH :
8146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        HistoryURLProviderParams::NEITHER;
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const size_t max_results =
8186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0);
8195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (backend) {
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Remove redirects and trim list to size.  We want to provide up to
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // kMaxMatches results plus the What You Typed result, if it was added to
8226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // params->matches above.
8236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    CullRedirects(backend, &params->matches, max_results);
8246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  } else if (params->matches.size() > max_results) {
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Simply trim the list to size.
8266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    params->matches.resize(max_results);
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void HistoryURLProvider::PromoteMatchesIfNecessary(
8316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const HistoryURLProviderParams& params) {
8326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (params.promote_type == HistoryURLProviderParams::FRONT_HISTORY_MATCH) {
8336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    matches_.push_back(HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE,
8346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        CalculateRelevance(INLINE_AUTOCOMPLETE, 0)));
8356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    if (OmniboxFieldTrial::AddUWYTMatchEvenIfPromotedURLs() &&
8366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        params.have_what_you_typed_match) {
8376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      matches_.push_back(params.what_you_typed_match);
8385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
8396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  } else if (params.promote_type ==
8406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) {
8416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    matches_.push_back(params.what_you_typed_match);
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryURLProvider::QueryComplete(
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryURLProviderParams* params_gets_deleted) {
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensure |params_gets_deleted| gets deleted on exit.
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted);
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the user hasn't already started another query, clear our member pointer
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so we can't write into deleted memory.
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (params_ == params_gets_deleted)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    params_ = NULL;
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't send responses for queries that have been canceled.
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (params->cancel_flag.IsSet())
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;  // Already set done_ when we canceled, no need to set it again.
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't modify |matches_| if the query failed, since it might have a default
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // match in it, whereas |params->matches| will be empty.
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!params->failed) {
8626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    matches_.clear();
8636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    PromoteMatchesIfNecessary(*params);
8646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
8656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // Determine relevance of highest scoring match, if any.
8666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    int relevance = matches_.empty() ?
8676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        CalculateRelevance(NORMAL,
8686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                           static_cast<int>(params->matches.size() - 1)) :
8696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        matches_[0].relevance;
8706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
8716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // Convert the history matches to autocomplete matches.  If we promoted the
8726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    // first match, skip over it.
8736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    const size_t first_match =
8746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        (params->exact_suggestion_is_in_history ||
8756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         (params->promote_type ==
8766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)             HistoryURLProviderParams::FRONT_HISTORY_MATCH)) ? 1 : 0;
8776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    for (size_t i = first_match; i < params->matches.size(); ++i) {
8786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // All matches score one less than the previous match.
8796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      --relevance;
8806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      // The experimental scoring must not change the top result's score.
8816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      if (!matches_.empty()) {
8826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        relevance = CalculateRelevanceScoreUsingScoringParams(
8836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            params->matches[i], relevance, scoring_params_);
8846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      }
8856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      matches_.push_back(HistoryMatchToACMatch(*params, i, NORMAL, relevance));
8866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  done_ = true;
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  listener_->OnProviderUpdate(true);
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HistoryURLProvider::FixupExactSuggestion(
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::URLDatabase* db,
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const VisitClassifier& classifier,
8966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    HistoryURLProviderParams* params) const {
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchType type = INLINE_AUTOCOMPLETE;
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (classifier.type()) {
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case VisitClassifier::INVALID:
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
9012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case VisitClassifier::UNVISITED_INTRANET:
9022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      type = UNVISITED_INTRANET;
9032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
9042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
9052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DCHECK_EQ(VisitClassifier::VISITED, classifier.type());
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We have data for this match, use it.
907f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      params->what_you_typed_match.deletable = true;
908f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      params->what_you_typed_match.description = classifier.url_row().title();
909f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RecordAdditionalInfoFromUrlRow(classifier.url_row(),
910f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                     &params->what_you_typed_match);
911f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      params->what_you_typed_match.description_class = ClassifyDescription(
912f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          params->input.text(), params->what_you_typed_match.description);
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!classifier.url_row().typed_count()) {
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // If we reach here, we must be in the second pass, and we must not have
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // this row's data available during the first pass.  That means we
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // maintain the ordering between passes consistent, we need to score it
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // the same way here.
919f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        type = CanFindIntranetURL(db, params->input) ?
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            UNVISITED_INTRANET : WHAT_YOU_TYPED;
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const GURL& url = params->what_you_typed_match.destination_url;
9265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the what-you-typed result looks like a single word (which can be
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // interpreted as an intranet address) followed by a pound sign ("#"),
929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // leave the score for the url-what-you-typed result as is.  It will be
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // outscored by a search query from the SearchProvider. This test fixes
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // cases such as "c#" and "c# foo" where the user has visited an intranet
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // site "c".  We want the search-what-you-typed score to beat the
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // URL-what-you-typed score in this case.  Most of the below test tries to
934c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // make sure that this code does not trigger if the user did anything to
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // indicate the desired match is a URL.  For instance, "c/# foo" will not
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // pass the test because that will be classified as input type URL.  The
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // parsed.CountCharactersBefore() in the test looks for the presence of a
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // reference fragment in the URL by checking whether the position differs
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // included the delimiter (pound sign) versus not including the delimiter.
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // (One cannot simply check url.ref() because it will not distinguish
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // between the input "c" and the input "c#", both of which will have empty
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // reference fragments.)
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if ((type == UNVISITED_INTRANET) &&
944f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (params->input.type() != metrics::OmniboxInputType::URL) &&
945f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url.username().empty() && url.password().empty() && url.port().empty() &&
946f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (url.path() == "/") && url.query().empty() &&
9475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      (parsed.CountCharactersBefore(url::Parsed::REF, true) !=
9485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu       parsed.CountCharactersBefore(url::Parsed::REF, false))) {
949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
952f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  params->what_you_typed_match.relevance = CalculateRelevance(type, 0);
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there are any other matches, then don't promote this match here, in
955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // hopes the caller will be able to inline autocomplete a better suggestion.
956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // DoAutocomplete() will fall back on this match if inline autocompletion
957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // fails.  This matches how we react to never-visited URL inputs in the non-
958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // intranet case.
9596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (type == UNVISITED_INTRANET && !params->matches.empty())
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Put it on the front of the HistoryMatches for redirect culling.
963a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false,
9646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                       &params->matches, true, true);
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HistoryURLProvider::CanFindIntranetURL(
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::URLDatabase* db,
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const AutocompleteInput& input) const {
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Normally passing the first two conditions below ought to guarantee the
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // third condition, but because FixupUserInput() can run and modify the
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input's text and parts between Parse() and here, it seems better to be
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // paranoid and check.
975f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if ((input.type() != metrics::OmniboxInputType::UNKNOWN) ||
976010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      !LowerCaseEqualsASCII(input.scheme(), url::kHttpScheme) ||
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !input.parts().host.is_nonempty())
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
9795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string host(base::UTF16ToUTF8(
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      input.text().substr(input.parts().host.begin, input.parts().host.len)));
981a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const size_t registry_length =
982a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      net::registry_controlled_domains::GetRegistryLength(
983a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          host,
984a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
985a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
986a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return registry_length == 0 && db->IsTypedHost(host);
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)bool HistoryURLProvider::PromoteOrCreateShorterSuggestion(
9905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::URLDatabase* db,
9915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool have_what_you_typed_match,
9926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    HistoryURLProviderParams* params) {
9936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (params->matches.empty())
9946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return false;  // No matches, nothing to do.
9955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determine the base URL from which to search, and whether that URL could
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // itself be added as a match.  We can add the base iff it's not "effectively
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the same" as any "what you typed" match.
9996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const history::HistoryMatch& match = params->matches[0];
10006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  GURL search_base = ConvertToHostOnly(match, params->input.text());
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool can_add_search_base_to_matches = !have_what_you_typed_match;
10025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (search_base.is_empty()) {
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Search from what the user typed when we couldn't reduce the best match
10045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to a host.  Careful: use a substring of |match| here, rather than the
10055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // first match in |params|, because they might have different prefixes.  If
1006f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // the user typed "google.com", params->what_you_typed_match will hold
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // "http://google.com/", but |match| might begin with
10085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // "http://www.google.com/".
10095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO: this should be cleaned up, and is probably incorrect for IDN.
10105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string new_match = match.url_info.url().possibly_invalid_spec().
10116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        substr(0, match.input_location + params->input.text().length());
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    search_base = GURL(new_match);
10135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (search_base.is_empty())
10146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      return false;  // Can't construct a URL from which to start a search.
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (!can_add_search_base_to_matches) {
10165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    can_add_search_base_to_matches =
10176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        (search_base != params->what_you_typed_match.destination_url);
10185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (search_base == match.url_info.url())
10206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return false;  // Couldn't shorten |match|, so no URLs to search over.
10215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Search the DB for short URLs between our base and |match|.
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::URLRow info(search_base);
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool promote = true;
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A short URL is only worth suggesting if it's been visited at least a third
10265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as often as the longer URL.
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1;
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For stability between the in-memory and on-disk autocomplete passes, when
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the long URL has been typed before, only suggest shorter URLs that have
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // also been typed.  Otherwise, the on-disk pass could suggest a shorter URL
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (which hasn't been typed) that the in-memory pass doesn't know about,
10325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thereby making the top match, and thus the behavior of inline
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete, unstable.
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int min_typed_count = match.url_info.typed_count() ? 1 : 0;
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!db->FindShortestURLFromBase(search_base.possibly_invalid_spec(),
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          match.url_info.url().possibly_invalid_spec(), min_visit_count,
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          min_typed_count, can_add_search_base_to_matches, &info)) {
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!can_add_search_base_to_matches)
10396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      return false;  // Couldn't find anything and can't add the search base.
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Try to get info on the search base itself.  Promote it to the top if the
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // original best match isn't good enough to autocomplete.
10435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    db->GetRowForURL(search_base, &info);
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    promote = match.url_info.typed_count() <= 1;
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Promote or add the desired URL to the list of matches.
10486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const bool ensure_can_inline =
1049f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      promote && CanPromoteMatchForInlineAutocomplete(match);
10506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return CreateOrPromoteMatch(info, match.input_location, match.match_in_scheme,
10515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                              &params->matches, true, promote) &&
10526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      ensure_can_inline;
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryURLProvider::CullPoorMatches(
10566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    HistoryURLProviderParams* params) const {
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const base::Time& threshold(history::AutocompleteAgeThreshold());
10586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  for (history::HistoryMatches::iterator i(params->matches.begin());
10596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)       i != params->matches.end(); ) {
10602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (RowQualifiesAsSignificant(i->url_info, threshold) &&
10616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        (!params->default_search_provider ||
10626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         !params->default_search_provider->IsSearchURL(
10636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)             i->url_info.url(), *params->search_terms_data))) {
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++i;
10652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
10666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      i = params->matches.erase(i);
10672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
10685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend,
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       history::HistoryMatches* matches,
10735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       size_t max_results) const {
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t source = 0;
10755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       (source < matches->size()) && (source < max_results); ) {
10765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url = (*matches)[source].url_info.url();
10775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(brettw) this should go away when everything uses GURL.
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::RedirectList redirects;
1079116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    backend->QueryRedirectsFrom(url, &redirects);
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!redirects.empty()) {
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Remove all but the first occurrence of any of these redirects in the
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // search results. We also must add the URL we queried for, since it may
10835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // not be the first match and we'd want to remove it.
10845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      //
10855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // For example, when A redirects to B and our matches are [A, X, B],
10865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // we'll get B as the redirects from, and we want to remove the second
10875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // item of that pair, removing B. If A redirects to B and our matches are
10885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // [B, X, A], we'll want to remove A instead.
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      redirects.push_back(url);
10905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      source = RemoveSubsequentMatchesOf(matches, source, redirects);
10915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
10925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Advance to next item.
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      source++;
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (matches->size() > max_results)
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    matches->resize(max_results);
10995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)size_t HistoryURLProvider::RemoveSubsequentMatchesOf(
11025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    history::HistoryMatches* matches,
11035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t source_index,
11045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<GURL>& remove) const {
11055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t next_index = source_index + 1;  // return value = item after source
11065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Find the first occurrence of any URL in the redirect chain. We want to
11085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keep this one since it is rated the highest.
11095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history::HistoryMatches::iterator first(std::find_first_of(
11105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      matches->begin(), matches->end(), remove.begin(), remove.end(),
11115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::HistoryMatch::EqualsGURL));
11125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(first != matches->end()) << "We should have always found at least the "
11135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "original URL.";
11145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Find any following occurrences of any URL in the redirect chain, these
11165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should be deleted.
11175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (history::HistoryMatches::iterator next(std::find_first_of(first + 1,
11185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           matches->end(), remove.begin(), remove.end(),
11195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           history::HistoryMatch::EqualsGURL));
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       next != matches->end(); next = std::find_first_of(next, matches->end(),
11215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           remove.begin(), remove.end(), history::HistoryMatch::EqualsGURL)) {
11225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Remove this item. When we remove an item before the source index, we
11235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // need to shift it to the right and remember that so we can return it.
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    next = matches->erase(next);
11255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (static_cast<size_t>(next - matches->begin()) < next_index)
11265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      --next_index;
11275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return next_index;
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch(
11328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const HistoryURLProviderParams& params,
11336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    size_t match_number,
11345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MatchType match_type,
11355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int relevance) {
11366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // The FormattedStringWithEquivalentMeaning() call below requires callers to
11376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // be on the UI thread.
11386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
11396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      !content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
11406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
11416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const history::HistoryMatch& history_match = params.matches[match_number];
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const history::URLRow& info = history_match.url_info;
11435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteMatch match(this, relevance,
114490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !!info.visit_count(), AutocompleteMatchType::HISTORY_URL);
11455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.typed_count = info.typed_count();
11465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.destination_url = info.url();
11475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
11485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t inline_autocomplete_offset =
11498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      history_match.input_location + params.input.text().length();
11505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string languages = (match_type == WHAT_YOU_TYPED) ?
11518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      std::string() : params.languages;
11525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
11538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ~((params.trim_http && !history_match.match_in_scheme) ?
11545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          0 : net::kFormatUrlOmitHTTP);
11555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.fill_into_edit =
1156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutocompleteInput::FormattedStringWithEquivalentMeaning(
1157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          info.url(),
11585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          net::FormatUrl(info.url(), languages, format_types,
11595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         net::UnescapeRule::SPACES, NULL, NULL,
1160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         &inline_autocomplete_offset),
1161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          ChromeAutocompleteSchemeClassifier(profile_));
11628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!params.prevent_inline_autocomplete &&
1163a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      (inline_autocomplete_offset != base::string16::npos)) {
1164ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1165ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    match.inline_autocompletion =
1166ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        match.fill_into_edit.substr(inline_autocomplete_offset);
1167ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
1168c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // The latter part of the test effectively asks "is the inline completion
1169c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // empty?" (i.e., is this match effectively the what-you-typed match?).
11708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  match.allowed_to_be_default_match = !params.prevent_inline_autocomplete ||
1171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ((inline_autocomplete_offset != base::string16::npos) &&
1172c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch       (inline_autocomplete_offset >= match.fill_into_edit.length()));
11735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t match_start = history_match.input_location;
11755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.contents = net::FormatUrl(info.url(), languages,
11765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1177a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if ((match_start != base::string16::npos) &&
1178a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      (inline_autocomplete_offset != base::string16::npos) &&
11795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (inline_autocomplete_offset != match_start)) {
11805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(inline_autocomplete_offset > match_start);
11815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AutocompleteMatch::ClassifyLocationInString(match_start,
11825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        inline_autocomplete_offset - match_start, match.contents.length(),
11835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ACMatchClassification::URL, &match.contents_class);
11845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1185a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
11865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        match.contents.length(), ACMatchClassification::URL,
11875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        &match.contents_class);
11885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match.description = info.title();
1190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  match.description_class =
1191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ClassifyDescription(params.input.text(), match.description);
11922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  RecordAdditionalInfoFromUrlRow(info, &match);
11935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return match;
11945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1195