1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/omnibox/browser/history_url_provider.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/location.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "components/bookmarks/browser/bookmark_utils.h"
21 #include "components/history/core/browser/history_backend.h"
22 #include "components/history/core/browser/history_database.h"
23 #include "components/history/core/browser/history_service.h"
24 #include "components/history/core/browser/history_types.h"
25 #include "components/metrics/proto/omnibox_input_type.pb.h"
26 #include "components/omnibox/browser/autocomplete_match.h"
27 #include "components/omnibox/browser/autocomplete_provider_listener.h"
28 #include "components/omnibox/browser/autocomplete_result.h"
29 #include "components/omnibox/browser/in_memory_url_index_types.h"
30 #include "components/omnibox/browser/omnibox_field_trial.h"
31 #include "components/omnibox/browser/scored_history_match.h"
32 #include "components/omnibox/browser/url_prefix.h"
33 #include "components/search_engines/search_terms_data.h"
34 #include "components/search_engines/template_url_service.h"
35 #include "components/url_formatter/url_fixer.h"
36 #include "components/url_formatter/url_formatter.h"
37 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
39 #include "url/third_party/mozilla/url_parse.h"
40 #include "url/url_util.h"
44 // Acts like the > operator for URLInfo classes.
45 bool CompareHistoryMatch(const history::HistoryMatch
& a
,
46 const history::HistoryMatch
& b
) {
47 // A URL that has been typed at all is better than one that has never been
48 // typed. (Note "!"s on each side)
49 if (!a
.url_info
.typed_count() != !b
.url_info
.typed_count())
50 return a
.url_info
.typed_count() > b
.url_info
.typed_count();
52 // Innermost matches (matches after any scheme or "www.") are better than
53 // non-innermost matches.
54 if (a
.innermost_match
!= b
.innermost_match
)
55 return a
.innermost_match
;
57 // URLs that have been typed more often are better.
58 if (a
.url_info
.typed_count() != b
.url_info
.typed_count())
59 return a
.url_info
.typed_count() > b
.url_info
.typed_count();
61 // For URLs that have each been typed once, a host (alone) is better than a
63 if ((a
.url_info
.typed_count() == 1) && (a
.IsHostOnly() != b
.IsHostOnly()))
64 return a
.IsHostOnly();
66 // URLs that have been visited more often are better.
67 if (a
.url_info
.visit_count() != b
.url_info
.visit_count())
68 return a
.url_info
.visit_count() > b
.url_info
.visit_count();
70 // URLs that have been visited more recently are better.
71 return a
.url_info
.last_visit() > b
.url_info
.last_visit();
74 // Sorts and dedups the given list of matches.
75 void SortAndDedupMatches(history::HistoryMatches
* matches
) {
76 // Sort by quality, best first.
77 std::sort(matches
->begin(), matches
->end(), &CompareHistoryMatch
);
79 // Remove duplicate matches (caused by the search string appearing in one of
80 // the prefixes as well as after it). Consider the following scenario:
82 // User has visited "http://http.com" once and "http://htaccess.com" twice.
83 // User types "http". The autocomplete search with prefix "http://" returns
84 // the first host, while the search with prefix "" returns both hosts. Now
85 // we sort them into rank order:
86 // http://http.com (innermost_match)
87 // http://htaccess.com (!innermost_match, url_info.visit_count == 2)
88 // http://http.com (!innermost_match, url_info.visit_count == 1)
90 // The above scenario tells us we can't use std::unique(), since our
91 // duplicates are not always sequential. It also tells us we should remove
92 // the lower-quality duplicate(s), since otherwise the returned results won't
93 // be ordered correctly. This is easy to do: we just always remove the later
94 // element of a duplicate pair.
95 // Be careful! Because the vector contents may change as we remove elements,
96 // we use an index instead of an iterator in the outer loop, and don't
97 // precalculate the ending position.
98 for (size_t i
= 0; i
< matches
->size(); ++i
) {
99 for (history::HistoryMatches::iterator
j(matches
->begin() + i
+ 1);
100 j
!= matches
->end(); ) {
101 if ((*matches
)[i
].url_info
.url() == j
->url_info
.url())
102 j
= matches
->erase(j
);
109 // Calculates a new relevance score applying half-life time decaying to |count|
110 // using |time_since_last_visit| and |score_buckets|. This function will never
111 // return a score higher than |undecayed_relevance|; in other words, it can only
112 // demote the old score.
113 double CalculateRelevanceUsingScoreBuckets(
114 const HUPScoringParams::ScoreBuckets
& score_buckets
,
115 const base::TimeDelta
& time_since_last_visit
,
116 int undecayed_relevance
,
118 // Back off if above relevance cap.
119 if ((score_buckets
.relevance_cap() != -1) &&
120 (undecayed_relevance
>= score_buckets
.relevance_cap()))
121 return undecayed_relevance
;
123 // Time based decay using half-life time.
124 double decayed_count
= count
;
125 if (decayed_count
> 0)
126 decayed_count
*= score_buckets
.HalfLifeTimeDecay(time_since_last_visit
);
128 // Find a threshold where decayed_count >= bucket.
129 const HUPScoringParams::ScoreBuckets::CountMaxRelevance
* score_bucket
= NULL
;
130 for (size_t i
= 0; i
< score_buckets
.buckets().size(); ++i
) {
131 score_bucket
= &score_buckets
.buckets()[i
];
132 if (decayed_count
>= score_bucket
->first
)
133 break; // Buckets are in descending order, so we can ignore the rest.
136 return (score_bucket
&& (undecayed_relevance
> score_bucket
->second
)) ?
137 score_bucket
->second
: undecayed_relevance
;
140 // Returns a new relevance score for the given |match| based on the
141 // |old_relevance| score and |scoring_params|. The new relevance score is
142 // guaranteed to be less than or equal to |old_relevance|. In other words, this
143 // function can only demote a score, never boost it. Returns |old_relevance| if
144 // experimental scoring is disabled.
145 int CalculateRelevanceScoreUsingScoringParams(
146 const history::HistoryMatch
& match
,
148 const HUPScoringParams
& scoring_params
) {
149 if (!scoring_params
.experimental_scoring_enabled
)
150 return old_relevance
;
152 const base::TimeDelta time_since_last_visit
=
153 base::Time::Now() - match
.url_info
.last_visit();
155 int relevance
= CalculateRelevanceUsingScoreBuckets(
156 scoring_params
.typed_count_buckets
, time_since_last_visit
, old_relevance
,
157 match
.url_info
.typed_count());
159 // Additional demotion (on top of typed_count demotion) of URLs that were
161 if (match
.url_info
.typed_count() == 0) {
162 relevance
= CalculateRelevanceUsingScoreBuckets(
163 scoring_params
.visited_count_buckets
, time_since_last_visit
, relevance
,
164 match
.url_info
.visit_count());
167 DCHECK_LE(relevance
, old_relevance
);
171 // Extracts typed_count, visit_count, and last_visited time from the URLRow and
172 // puts them in the additional info field of the |match| for display in
174 void RecordAdditionalInfoFromUrlRow(const history::URLRow
& info
,
175 AutocompleteMatch
* match
) {
176 match
->RecordAdditionalInfo("typed count", info
.typed_count());
177 match
->RecordAdditionalInfo("visit count", info
.visit_count());
178 match
->RecordAdditionalInfo("last visit", info
.last_visit());
181 // If |create_if_necessary| is true, ensures that |matches| contains an entry
182 // for |info|, creating a new such entry if necessary (using |input_location|
183 // and |match_in_scheme|).
185 // If |promote| is true, this also ensures the entry is the first element in
186 // |matches|, moving or adding it to the front as appropriate. When |promote|
187 // is false, existing matches are left in place, and newly added matches are
188 // placed at the back.
190 // It's OK to call this function with both |create_if_necessary| and |promote|
191 // false, in which case we'll do nothing.
193 // Returns whether the match exists regardless if it was promoted/created.
194 bool CreateOrPromoteMatch(const history::URLRow
& info
,
195 size_t input_location
,
196 bool match_in_scheme
,
197 history::HistoryMatches
* matches
,
198 bool create_if_necessary
,
200 // |matches| may already have an entry for this.
201 for (history::HistoryMatches::iterator
i(matches
->begin());
202 i
!= matches
->end(); ++i
) {
203 if (i
->url_info
.url() == info
.url()) {
204 // Rotate it to the front if the caller wishes.
206 std::rotate(matches
->begin(), i
, i
+ 1);
211 if (!create_if_necessary
)
214 // No entry, so create one.
215 history::HistoryMatch
match(info
, input_location
, match_in_scheme
, true);
217 matches
->push_front(match
);
219 matches
->push_back(match
);
224 // Returns whether |match| is suitable for inline autocompletion.
225 bool CanPromoteMatchForInlineAutocomplete(const history::HistoryMatch
& match
) {
226 // We can promote this match if it's been typed at least n times, where n == 1
227 // for "simple" (host-only) URLs and n == 2 for others. We set a higher bar
228 // for these long URLs because it's less likely that users will want to visit
229 // them again. Even though we don't increment the typed_count for pasted-in
230 // URLs, if the user manually edits the URL or types some long thing in by
231 // hand, we wouldn't want to immediately start autocompleting it.
232 return match
.url_info
.typed_count() &&
233 ((match
.url_info
.typed_count() > 1) || match
.IsHostOnly());
236 // Given the user's |input| and a |match| created from it, reduce the match's
237 // URL to just a host. If this host still matches the user input, return it.
238 // Returns the empty string on failure.
239 GURL
ConvertToHostOnly(const history::HistoryMatch
& match
,
240 const base::string16
& input
) {
241 // See if we should try to do host-only suggestions for this URL. Nonstandard
242 // schemes means there's no authority section, so suggesting the host name
243 // is useless. File URLs are standard, but host suggestion is not useful for
245 const GURL
& url
= match
.url_info
.url();
246 if (!url
.is_valid() || !url
.IsStandard() || url
.SchemeIsFile())
249 // Transform to a host-only match. Bail if the host no longer matches the
250 // user input (e.g. because the user typed more than just a host).
251 GURL host
= url
.GetWithEmptyPath();
252 if ((host
.spec().length() < (match
.input_location
+ input
.length())))
253 return GURL(); // User typing is longer than this host suggestion.
255 const base::string16 spec
= base::UTF8ToUTF16(host
.spec());
256 if (spec
.compare(match
.input_location
, input
.length(), input
))
257 return GURL(); // User typing is no longer a prefix.
264 // -----------------------------------------------------------------
265 // SearchTermsDataSnapshot
267 // Implementation of SearchTermsData that takes a snapshot of another
268 // SearchTermsData by copying all the responses to the different getters into
269 // member strings, then returning those strings when its own getters are called.
270 // This will typically be constructed on the UI thread from
271 // UIThreadSearchTermsData but is subsequently safe to use on any thread.
272 class SearchTermsDataSnapshot
: public SearchTermsData
{
274 explicit SearchTermsDataSnapshot(const SearchTermsData
& search_terms_data
);
275 ~SearchTermsDataSnapshot() override
;
277 std::string
GoogleBaseURLValue() const override
;
278 std::string
GetApplicationLocale() const override
;
279 base::string16
GetRlzParameterValue(bool from_app_list
) const override
;
280 std::string
GetSearchClient() const override
;
281 bool IsShowingSearchTermsOnSearchResultsPages() const override
;
282 std::string
InstantExtendedEnabledParam(bool for_search
) const override
;
283 std::string
ForceInstantResultsParam(bool for_prerender
) const override
;
284 std::string
NTPIsThemedParam() const override
;
285 std::string
GoogleImageSearchSource() const override
;
288 std::string google_base_url_value_
;
289 std::string application_locale_
;
290 base::string16 rlz_parameter_value_
;
291 std::string search_client_
;
292 bool is_showing_search_terms_on_search_results_pages_
;
293 std::string instant_extended_enabled_param_
;
294 std::string instant_extended_enabled_param_for_search_
;
295 std::string force_instant_results_param_
;
296 std::string force_instant_results_param_for_prerender_
;
297 std::string ntp_is_themed_param_
;
298 std::string google_image_search_source_
;
300 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot
);
303 SearchTermsDataSnapshot::SearchTermsDataSnapshot(
304 const SearchTermsData
& search_terms_data
)
305 : google_base_url_value_(search_terms_data
.GoogleBaseURLValue()),
306 application_locale_(search_terms_data
.GetApplicationLocale()),
307 rlz_parameter_value_(search_terms_data
.GetRlzParameterValue(false)),
308 search_client_(search_terms_data
.GetSearchClient()),
309 is_showing_search_terms_on_search_results_pages_(
310 search_terms_data
.IsShowingSearchTermsOnSearchResultsPages()),
311 instant_extended_enabled_param_(
312 search_terms_data
.InstantExtendedEnabledParam(false)),
313 instant_extended_enabled_param_for_search_(
314 search_terms_data
.InstantExtendedEnabledParam(true)),
315 force_instant_results_param_(
316 search_terms_data
.ForceInstantResultsParam(false)),
317 force_instant_results_param_for_prerender_(
318 search_terms_data
.ForceInstantResultsParam(true)),
319 ntp_is_themed_param_(search_terms_data
.NTPIsThemedParam()),
320 google_image_search_source_(search_terms_data
.GoogleImageSearchSource()) {
323 SearchTermsDataSnapshot::~SearchTermsDataSnapshot() {
326 std::string
SearchTermsDataSnapshot::GoogleBaseURLValue() const {
327 return google_base_url_value_
;
330 std::string
SearchTermsDataSnapshot::GetApplicationLocale() const {
331 return application_locale_
;
334 base::string16
SearchTermsDataSnapshot::GetRlzParameterValue(
335 bool from_app_list
) const {
336 return rlz_parameter_value_
;
339 std::string
SearchTermsDataSnapshot::GetSearchClient() const {
340 return search_client_
;
343 bool SearchTermsDataSnapshot::IsShowingSearchTermsOnSearchResultsPages() const {
344 return is_showing_search_terms_on_search_results_pages_
;
347 std::string
SearchTermsDataSnapshot::InstantExtendedEnabledParam(
348 bool for_search
) const {
349 return for_search
? instant_extended_enabled_param_
:
350 instant_extended_enabled_param_for_search_
;
353 std::string
SearchTermsDataSnapshot::ForceInstantResultsParam(
354 bool for_prerender
) const {
355 return for_prerender
? force_instant_results_param_
:
356 force_instant_results_param_for_prerender_
;
359 std::string
SearchTermsDataSnapshot::NTPIsThemedParam() const {
360 return ntp_is_themed_param_
;
363 std::string
SearchTermsDataSnapshot::GoogleImageSearchSource() const {
364 return google_image_search_source_
;
367 // -----------------------------------------------------------------
368 // HistoryURLProvider
370 // These ugly magic numbers will go away once we switch all scoring
371 // behavior (including URL-what-you-typed) to HistoryQuick provider.
372 const int HistoryURLProvider::kScoreForBestInlineableResult
= 1413;
373 const int HistoryURLProvider::kScoreForUnvisitedIntranetResult
= 1403;
374 const int HistoryURLProvider::kScoreForWhatYouTypedResult
= 1203;
375 const int HistoryURLProvider::kBaseScoreForNonInlineableResult
= 900;
377 // VisitClassifier is used to classify the type of visit to a particular url.
378 class HistoryURLProvider::VisitClassifier
{
381 INVALID
, // Navigations to the URL are not allowed.
382 UNVISITED_INTRANET
, // A navigable URL for which we have no visit data but
383 // which is known to refer to a visited intranet host.
384 VISITED
, // The site has been previously visited.
387 VisitClassifier(HistoryURLProvider
* provider
,
388 const AutocompleteInput
& input
,
389 history::URLDatabase
* db
);
391 // Returns the type of visit for the specified input.
392 Type
type() const { return type_
; }
394 // Returns the URLRow for the visit.
395 const history::URLRow
& url_row() const { return url_row_
; }
398 HistoryURLProvider
* provider_
;
399 history::URLDatabase
* db_
;
401 history::URLRow url_row_
;
403 DISALLOW_COPY_AND_ASSIGN(VisitClassifier
);
406 HistoryURLProvider::VisitClassifier::VisitClassifier(
407 HistoryURLProvider
* provider
,
408 const AutocompleteInput
& input
,
409 history::URLDatabase
* db
)
410 : provider_(provider
),
413 const GURL
& url
= input
.canonicalized_url();
414 // Detect email addresses. These cases will look like "http://user@site/",
415 // and because the history backend strips auth creds, we'll get a bogus exact
416 // match below if the user has visited "site".
417 if (!url
.is_valid() ||
418 ((input
.type() == metrics::OmniboxInputType::UNKNOWN
) &&
419 input
.parts().username
.is_nonempty() &&
420 !input
.parts().password
.is_nonempty() &&
421 !input
.parts().path
.is_nonempty()))
424 if (db_
->GetRowForURL(url
, &url_row_
)) {
429 if (provider_
->CanFindIntranetURL(db_
, input
)) {
430 // The user typed an intranet hostname that they've visited (albeit with a
431 // different port and/or path) before.
432 url_row_
= history::URLRow(url
);
433 type_
= UNVISITED_INTRANET
;
437 HistoryURLProviderParams::HistoryURLProviderParams(
438 const AutocompleteInput
& input
,
440 const AutocompleteMatch
& what_you_typed_match
,
441 const std::string
& languages
,
442 TemplateURL
* default_search_provider
,
443 const SearchTermsData
& search_terms_data
)
444 : message_loop(base::MessageLoop::current()),
446 prevent_inline_autocomplete(input
.prevent_inline_autocomplete()),
447 trim_http(trim_http
),
448 what_you_typed_match(what_you_typed_match
),
450 exact_suggestion_is_in_history(false),
451 promote_type(NEITHER
),
452 languages(languages
),
453 default_search_provider(default_search_provider
?
454 new TemplateURL(default_search_provider
->data()) : NULL
),
455 search_terms_data(new SearchTermsDataSnapshot(search_terms_data
)) {
458 HistoryURLProviderParams::~HistoryURLProviderParams() {
461 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient
* client
,
462 AutocompleteProviderListener
* listener
)
463 : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL
, client
),
466 // Initialize HUP scoring params based on the current experiment.
467 OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_
);
470 void HistoryURLProvider::Start(const AutocompleteInput
& input
,
471 bool minimal_changes
) {
472 // NOTE: We could try hard to do less work in the |minimal_changes| case
473 // here; some clever caching would let us reuse the raw matches from the
474 // history DB without re-querying. However, we'd still have to go back to
475 // the history thread to mark these up properly, and if pass 2 is currently
476 // running, we'd need to wait for it to return to the main thread before
477 // doing this (we can't just write new data for it to read due to thread
478 // safety issues). At that point it's just as fast, and easier, to simply
479 // re-run the query from scratch and ignore |minimal_changes|.
481 // Cancel any in-progress query.
486 if (input
.from_omnibox_focus() ||
487 (input
.type() == metrics::OmniboxInputType::INVALID
) ||
488 (input
.type() == metrics::OmniboxInputType::FORCED_QUERY
))
491 // Do some fixup on the user input before matching against it, so we provide
492 // good results for local file paths, input with spaces, etc.
493 const FixupReturn
fixup_return(FixupUserInput(input
));
494 if (!fixup_return
.first
)
497 url_formatter::SegmentURL(fixup_return
.second
, &parts
);
498 AutocompleteInput
fixed_up_input(input
);
499 fixed_up_input
.UpdateText(fixup_return
.second
, base::string16::npos
, parts
);
501 // Create a match for what the user typed.
502 const bool trim_http
= !AutocompleteInput::HasHTTPScheme(input
.text());
503 AutocompleteMatch
what_you_typed_match(SuggestExactInput(
504 fixed_up_input
, fixed_up_input
.canonicalized_url(), trim_http
));
505 what_you_typed_match
.relevance
= CalculateRelevance(WHAT_YOU_TYPED
, 0);
507 // Add the WYT match as a fallback in case we can't get the history service or
508 // URL DB; otherwise, we'll replace this match lower down. Don't do this for
509 // queries, though -- while we can sometimes mark up a match for them, it's
510 // not what the user wants, and just adds noise.
511 if (fixed_up_input
.type() != metrics::OmniboxInputType::QUERY
)
512 matches_
.push_back(what_you_typed_match
);
514 // We'll need the history service to run both passes, so try to obtain it.
515 history::HistoryService
* const history_service
=
516 client()->GetHistoryService();
517 if (!history_service
)
520 // Get the default search provider and search terms data now since we have to
521 // retrieve these on the UI thread, and the second pass runs on the history
522 // thread. |template_url_service| can be NULL when testing.
523 TemplateURLService
* template_url_service
= client()->GetTemplateURLService();
524 TemplateURL
* default_search_provider
= template_url_service
?
525 template_url_service
->GetDefaultSearchProvider() : NULL
;
527 // Create the data structure for the autocomplete passes. We'll save this off
528 // onto the |params_| member for later deletion below if we need to run pass
530 scoped_ptr
<HistoryURLProviderParams
> params(new HistoryURLProviderParams(
531 fixed_up_input
, trim_http
, what_you_typed_match
,
532 client()->GetAcceptLanguages(), default_search_provider
,
533 client()->GetSearchTermsData()));
534 // Note that we use the non-fixed-up input here, since fixup may strip
535 // trailing whitespace.
536 params
->prevent_inline_autocomplete
= PreventInlineAutocomplete(input
);
538 // Pass 1: Get the in-memory URL database, and use it to find and promote
539 // the inline autocomplete match, if any.
540 history::URLDatabase
* url_db
= history_service
->InMemoryDatabase();
541 // url_db can be NULL if it hasn't finished initializing (or failed to
542 // initialize). In this case all we can do is fall back on the second
545 // TODO(pkasting): We should just block here until this loads. Any time
546 // someone unloads the history backend, we'll get inconsistent inline
547 // autocomplete behavior here.
549 DoAutocomplete(NULL
, url_db
, params
.get());
551 PromoteMatchesIfNecessary(*params
);
552 // NOTE: We don't reset |params| here since at least the |promote_type|
553 // field on it will be read by the second pass -- see comments in
557 // Pass 2: Ask the history service to call us back on the history thread,
558 // where we can read the full on-disk DB.
559 if (input
.want_asynchronous_matches()) {
561 params_
= params
.release(); // This object will be destroyed in
562 // QueryComplete() once we're done with it.
563 history_service
->ScheduleAutocomplete(
564 base::Bind(&HistoryURLProvider::ExecuteWithDB
, this, params_
));
568 void HistoryURLProvider::Stop(bool clear_cached_results
,
569 bool due_to_user_inactivity
) {
573 params_
->cancel_flag
.Set();
576 AutocompleteMatch
HistoryURLProvider::SuggestExactInput(
577 const AutocompleteInput
& input
,
578 const GURL
& destination_url
,
580 // The FormattedStringWithEquivalentMeaning() call below requires callers to
581 // be on the main thread.
582 DCHECK(thread_checker_
.CalledOnValidThread());
584 AutocompleteMatch
match(this, 0, false,
585 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
587 if (destination_url
.is_valid()) {
588 match
.destination_url
= destination_url
;
590 // Trim off "http://" if the user didn't type it.
592 !AutocompleteInput::HasHTTPScheme(input
.text()));
593 base::string16
display_string(url_formatter::FormatUrl(
594 destination_url
, std::string(),
595 url_formatter::kFormatUrlOmitAll
& ~url_formatter::kFormatUrlOmitHTTP
,
596 net::UnescapeRule::SPACES
, nullptr, nullptr, nullptr));
597 const size_t offset
= trim_http
? TrimHttpPrefix(&display_string
) : 0;
598 match
.fill_into_edit
=
599 AutocompleteInput::FormattedStringWithEquivalentMeaning(
600 destination_url
, display_string
, client()->GetSchemeClassifier());
601 // The what-you-typed match is generally only allowed to be default for
602 // URL inputs. (It's also allowed to be default for UNKNOWN inputs
603 // where the destination is a known intranet site. In this case,
604 // |allowed_to_be_default_match| is revised in FixupExactSuggestion().)
605 match
.allowed_to_be_default_match
=
606 (input
.type() == metrics::OmniboxInputType::URL
) ||
607 !OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs();
608 // NOTE: Don't set match.inline_autocompletion to something non-empty here;
609 // it's surprising and annoying.
611 // Try to highlight "innermost" match location. If we fix up "w" into
612 // "www.w.com", we want to highlight the fifth character, not the first.
613 // This relies on match.destination_url being the non-prefix-trimmed version
614 // of match.contents.
615 match
.contents
= display_string
;
616 const URLPrefix
* best_prefix
= URLPrefix::BestURLPrefix(
617 base::UTF8ToUTF16(destination_url
.spec()), input
.text());
618 // It's possible for match.destination_url to not contain the user's input
619 // at all (so |best_prefix| is NULL), for example if the input is
620 // "view-source:x" and |destination_url| has an inserted "http://" in the
622 if (best_prefix
== NULL
) {
623 AutocompleteMatch::ClassifyMatchInString(input
.text(),
625 ACMatchClassification::URL
,
626 &match
.contents_class
);
628 AutocompleteMatch::ClassifyLocationInString(
629 best_prefix
->prefix
.length() - offset
, input
.text().length(),
630 match
.contents
.length(), ACMatchClassification::URL
,
631 &match
.contents_class
);
638 void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams
* params
,
639 history::HistoryBackend
* backend
,
640 history::URLDatabase
* db
) {
641 // We may get called with a NULL database if it couldn't be properly
644 params
->failed
= true;
645 } else if (!params
->cancel_flag
.IsSet()) {
646 base::TimeTicks beginning_time
= base::TimeTicks::Now();
648 DoAutocomplete(backend
, db
, params
);
650 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime",
651 base::TimeTicks::Now() - beginning_time
);
654 // Return the results (if any) to the main thread.
655 params
->message_loop
->task_runner()->PostTask(
656 FROM_HERE
, base::Bind(&HistoryURLProvider::QueryComplete
, this, params
));
659 HistoryURLProvider::~HistoryURLProvider() {
660 // Note: This object can get leaked on shutdown if there are pending
661 // requests on the database (which hold a reference to us). Normally, these
662 // messages get flushed for each thread. We do a round trip from main, to
663 // history, back to main while holding a reference. If the main thread
664 // completes before the history thread, the message to delegate back to the
665 // main thread will not run and the reference will leak. Therefore, don't do
666 // anything on destruction.
670 int HistoryURLProvider::CalculateRelevance(MatchType match_type
,
672 switch (match_type
) {
673 case INLINE_AUTOCOMPLETE
:
674 return kScoreForBestInlineableResult
;
676 case UNVISITED_INTRANET
:
677 return kScoreForUnvisitedIntranetResult
;
680 return kScoreForWhatYouTypedResult
;
683 return kBaseScoreForNonInlineableResult
+ match_number
;
688 ACMatchClassifications
HistoryURLProvider::ClassifyDescription(
689 const base::string16
& input_text
,
690 const base::string16
& description
) {
691 base::string16 clean_description
=
692 bookmarks::CleanUpTitleForMatching(description
);
693 TermMatches
description_matches(SortAndDeoverlapMatches(
694 MatchTermInString(input_text
, clean_description
, 0)));
695 WordStarts description_word_starts
;
696 String16VectorFromString16(clean_description
, false,
697 &description_word_starts
);
698 // If HistoryURL retrieves any matches (and hence we reach this code), we
699 // are guaranteed that the beginning of input_text must be a word break.
700 WordStarts
offsets(1, 0u);
701 description_matches
= ScoredHistoryMatch::FilterTermMatchesByWordStarts(
702 description_matches
, offsets
, description_word_starts
, 0,
704 return SpansFromTermMatch(
705 description_matches
, clean_description
.length(), false);
708 void HistoryURLProvider::DoAutocomplete(history::HistoryBackend
* backend
,
709 history::URLDatabase
* db
,
710 HistoryURLProviderParams
* params
) {
711 // Get the matching URLs from the DB.
712 params
->matches
.clear();
713 history::URLRows url_matches
;
714 const URLPrefixes
& prefixes
= URLPrefix::GetURLPrefixes();
715 for (URLPrefixes::const_iterator
i(prefixes
.begin()); i
!= prefixes
.end();
717 if (params
->cancel_flag
.IsSet())
718 return; // Canceled in the middle of a query, give up.
720 // We only need kMaxMatches results in the end, but before we get there we
721 // need to promote lower-quality matches that are prefixes of higher-quality
722 // matches, and remove lower-quality redirects. So we ask for more results
723 // than we need, of every prefix type, in hopes this will give us far more
724 // than enough to work with. CullRedirects() will then reduce the list to
725 // the best kMaxMatches results.
726 db
->AutocompleteForPrefix(
727 base::UTF16ToUTF8(i
->prefix
+ params
->input
.text()), kMaxMatches
* 2,
728 !backend
, &url_matches
);
729 for (history::URLRows::const_iterator
j(url_matches
.begin());
730 j
!= url_matches
.end(); ++j
) {
731 const URLPrefix
* best_prefix
= URLPrefix::BestURLPrefix(
732 base::UTF8ToUTF16(j
->url().spec()), base::string16());
734 params
->matches
.push_back(history::HistoryMatch(
735 *j
, i
->prefix
.length(), !i
->num_components
,
736 i
->num_components
>= best_prefix
->num_components
));
740 // Create sorted list of suggestions.
741 CullPoorMatches(params
);
742 SortAndDedupMatches(¶ms
->matches
);
744 // Try to create a shorter suggestion from the best match.
745 // We consider the what you typed match eligible for display when it's
746 // navigable and there's a reasonable chance the user intended to do
747 // something other than search. We use a variety of heuristics to determine
748 // this, e.g. whether the user explicitly typed a scheme, or if omnibox
749 // searching has been disabled by policy. In the cases where we've parsed as
750 // UNKNOWN, we'll still show an accidental search infobar if need be.
751 VisitClassifier
classifier(this, params
->input
, db
);
752 params
->have_what_you_typed_match
=
753 (params
->input
.type() != metrics::OmniboxInputType::QUERY
) &&
754 ((params
->input
.type() != metrics::OmniboxInputType::UNKNOWN
) ||
755 (classifier
.type() == VisitClassifier::UNVISITED_INTRANET
) ||
756 !params
->trim_http
||
757 (AutocompleteInput::NumNonHostComponents(params
->input
.parts()) > 0) ||
758 !params
->default_search_provider
);
759 const bool have_shorter_suggestion_suitable_for_inline_autocomplete
=
760 PromoteOrCreateShorterSuggestion(db
, params
);
762 // Check whether what the user typed appears in history.
763 const bool can_check_history_for_exact_match
=
764 // Checking what_you_typed_match.destination_url.is_valid() tells us
765 // whether SuggestExactInput() succeeded in constructing a valid match.
766 params
->what_you_typed_match
.destination_url
.is_valid() &&
767 // Additionally, in the case where the user has typed "foo.com" and
768 // visited (but not typed) "foo/", and the input is "foo", the first pass
769 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the
770 // second pass can suggest the exact input as a better URL. Since we need
771 // both passes to agree, and since during the first pass there's no way to
772 // know about "foo/", ensure that if the promote type was set to
773 // FRONT_HISTORY_MATCH during the first pass, the second pass will not
774 // consider the exact suggestion to be in history and therefore will not
775 // suggest the exact input as a better match. (Note that during the first
776 // pass, this conditional will always succeed since |promote_type| is
777 // initialized to NEITHER.)
778 (params
->promote_type
!= HistoryURLProviderParams::FRONT_HISTORY_MATCH
);
779 params
->exact_suggestion_is_in_history
= can_check_history_for_exact_match
&&
780 FixupExactSuggestion(db
, classifier
, params
);
782 // If we succeeded in fixing up the exact match based on the user's history,
783 // we should treat it as the best match regardless of input type. If not,
784 // then we check whether there's an inline autocompletion we can create from
785 // this input, so we can promote that as the best match.
786 if (params
->exact_suggestion_is_in_history
) {
787 params
->promote_type
= HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
;
788 } else if (!params
->matches
.empty() &&
789 (have_shorter_suggestion_suitable_for_inline_autocomplete
||
790 CanPromoteMatchForInlineAutocomplete(params
->matches
[0]))) {
791 // Note that we promote this inline-autocompleted match even when
792 // params->prevent_inline_autocomplete is true. This is safe because in
793 // this case the match will be marked as "not allowed to be default", and
794 // a non-inlined match that is "allowed to be default" will be reordered
795 // above it by the controller/AutocompleteResult. We ensure there is such
796 // a match in two ways:
797 // * If params->have_what_you_typed_match is true, we force the
798 // what-you-typed match to be added in this case. See comments in
799 // PromoteMatchesIfNecessary().
800 // * Otherwise, we should have some sort of QUERY or UNKNOWN input that
801 // the SearchProvider will provide a defaultable WYT match for.
802 params
->promote_type
= HistoryURLProviderParams::FRONT_HISTORY_MATCH
;
804 // Failed to promote any URLs. Use the What You Typed match, if we have it.
805 params
->promote_type
= params
->have_what_you_typed_match
?
806 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
:
807 HistoryURLProviderParams::NEITHER
;
810 const size_t max_results
=
811 kMaxMatches
+ (params
->exact_suggestion_is_in_history
? 1 : 0);
813 // Remove redirects and trim list to size. We want to provide up to
814 // kMaxMatches results plus the What You Typed result, if it was added to
815 // params->matches above.
816 CullRedirects(backend
, ¶ms
->matches
, max_results
);
817 } else if (params
->matches
.size() > max_results
) {
818 // Simply trim the list to size.
819 params
->matches
.resize(max_results
);
823 void HistoryURLProvider::PromoteMatchesIfNecessary(
824 const HistoryURLProviderParams
& params
) {
825 if (params
.promote_type
== HistoryURLProviderParams::NEITHER
)
827 if (params
.promote_type
== HistoryURLProviderParams::FRONT_HISTORY_MATCH
) {
829 HistoryMatchToACMatch(params
, 0, INLINE_AUTOCOMPLETE
,
830 CalculateRelevance(INLINE_AUTOCOMPLETE
, 0)));
832 // There are two cases where we need to add the what-you-typed-match:
833 // * If params.promote_type is WHAT_YOU_TYPED_MATCH, we're being explicitly
835 // * If params.have_what_you_typed_match is true, then params.promote_type
836 // can't be NEITHER (see code near the end of DoAutocomplete()), so if
837 // it's not WHAT_YOU_TYPED_MATCH, it must be FRONT_HISTORY_MATCH, and
838 // we'll have promoted the history match above. If
839 // params.prevent_inline_autocomplete is also true, then this match
840 // will be marked "not allowed to be default", and we need to add the
841 // what-you-typed match to ensure there's a legal default match for the
842 // controller/AutocompleteResult to promote. (If
843 // params.have_what_you_typed_match is false, the SearchProvider should
844 // take care of adding this defaultable match.)
845 if ((params
.promote_type
== HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
) ||
846 (params
.prevent_inline_autocomplete
&&
847 params
.have_what_you_typed_match
)) {
848 matches_
.push_back(params
.what_you_typed_match
);
852 void HistoryURLProvider::QueryComplete(
853 HistoryURLProviderParams
* params_gets_deleted
) {
854 // Ensure |params_gets_deleted| gets deleted on exit.
855 scoped_ptr
<HistoryURLProviderParams
> params(params_gets_deleted
);
857 // If the user hasn't already started another query, clear our member pointer
858 // so we can't write into deleted memory.
859 if (params_
== params_gets_deleted
)
862 // Don't send responses for queries that have been canceled.
863 if (params
->cancel_flag
.IsSet())
864 return; // Already set done_ when we canceled, no need to set it again.
866 // Don't modify |matches_| if the query failed, since it might have a default
867 // match in it, whereas |params->matches| will be empty.
868 if (!params
->failed
) {
870 PromoteMatchesIfNecessary(*params
);
872 // Determine relevance of highest scoring match, if any.
873 int relevance
= matches_
.empty() ?
874 CalculateRelevance(NORMAL
,
875 static_cast<int>(params
->matches
.size() - 1)) :
876 matches_
[0].relevance
;
878 // Convert the history matches to autocomplete matches. If we promoted the
879 // first match, skip over it.
880 const size_t first_match
=
881 (params
->exact_suggestion_is_in_history
||
882 (params
->promote_type
==
883 HistoryURLProviderParams::FRONT_HISTORY_MATCH
)) ? 1 : 0;
884 for (size_t i
= first_match
; i
< params
->matches
.size(); ++i
) {
885 // All matches score one less than the previous match.
887 // The experimental scoring must not change the top result's score.
888 if (!matches_
.empty()) {
889 relevance
= CalculateRelevanceScoreUsingScoringParams(
890 params
->matches
[i
], relevance
, scoring_params_
);
892 matches_
.push_back(HistoryMatchToACMatch(*params
, i
, NORMAL
, relevance
));
897 listener_
->OnProviderUpdate(true);
900 bool HistoryURLProvider::FixupExactSuggestion(
901 history::URLDatabase
* db
,
902 const VisitClassifier
& classifier
,
903 HistoryURLProviderParams
* params
) const {
904 MatchType type
= INLINE_AUTOCOMPLETE
;
905 switch (classifier
.type()) {
906 case VisitClassifier::INVALID
:
908 case VisitClassifier::UNVISITED_INTRANET
:
909 type
= UNVISITED_INTRANET
;
912 DCHECK_EQ(VisitClassifier::VISITED
, classifier
.type());
913 // We have data for this match, use it.
914 params
->what_you_typed_match
.deletable
= true;
915 params
->what_you_typed_match
.description
= classifier
.url_row().title();
916 RecordAdditionalInfoFromUrlRow(classifier
.url_row(),
917 ¶ms
->what_you_typed_match
);
918 params
->what_you_typed_match
.description_class
= ClassifyDescription(
919 params
->input
.text(), params
->what_you_typed_match
.description
);
920 if (!classifier
.url_row().typed_count()) {
921 // If we reach here, we must be in the second pass, and we must not have
922 // this row's data available during the first pass. That means we
923 // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to
924 // maintain the ordering between passes consistent, we need to score it
925 // the same way here.
926 type
= CanFindIntranetURL(db
, params
->input
) ?
927 UNVISITED_INTRANET
: WHAT_YOU_TYPED
;
932 if (OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs()) {
933 const GURL
& url
= params
->what_you_typed_match
.destination_url
;
934 const url::Parsed
& parsed
= url
.parsed_for_possibly_invalid_spec();
935 // If the what-you-typed result looks like a single word (which can be
936 // interpreted as an intranet address) followed by a pound sign ("#"),
937 // leave the score for the url-what-you-typed result as is and also
938 // don't mark it as allowed to be the default match. It will likely be
939 // outscored by a search query from the SearchProvider or, if not, the
940 // search query default match will in any case--which is allowed to be the
941 // default match--will be reordered to be first. This test fixes cases
942 // such as "c#" and "c# foo" where the user has visited an intranet site
943 // "c". We want the search-what-you-typed score to beat the
944 // URL-what-you-typed score in this case. Most of the below test tries to
945 // make sure that this code does not trigger if the user did anything to
946 // indicate the desired match is a URL. For instance, "c/# foo" will not
947 // pass the test because that will be classified as input type URL. The
948 // parsed.CountCharactersBefore() in the test looks for the presence of a
949 // reference fragment in the URL by checking whether the position differs
950 // included the delimiter (pound sign) versus not including the delimiter.
951 // (One cannot simply check url.ref() because it will not distinguish
952 // between the input "c" and the input "c#", both of which will have empty
953 // reference fragments.)
954 if ((type
== UNVISITED_INTRANET
) &&
955 (params
->input
.type() != metrics::OmniboxInputType::URL
) &&
956 url
.username().empty() && url
.password().empty() &&
957 url
.port().empty() && (url
.path() == "/") && url
.query().empty() &&
958 (parsed
.CountCharactersBefore(url::Parsed::REF
, true) !=
959 parsed
.CountCharactersBefore(url::Parsed::REF
, false))) {
964 params
->what_you_typed_match
.allowed_to_be_default_match
= true;
965 params
->what_you_typed_match
.relevance
= CalculateRelevance(type
, 0);
967 // If there are any other matches, then don't promote this match here, in
968 // hopes the caller will be able to inline autocomplete a better suggestion.
969 // DoAutocomplete() will fall back on this match if inline autocompletion
970 // fails. This matches how we react to never-visited URL inputs in the non-
972 if (type
== UNVISITED_INTRANET
&& !params
->matches
.empty())
975 // Put it on the front of the HistoryMatches for redirect culling.
976 CreateOrPromoteMatch(classifier
.url_row(), base::string16::npos
, false,
977 ¶ms
->matches
, true, true);
981 bool HistoryURLProvider::CanFindIntranetURL(
982 history::URLDatabase
* db
,
983 const AutocompleteInput
& input
) const {
984 // Normally passing the first two conditions below ought to guarantee the
985 // third condition, but because FixupUserInput() can run and modify the
986 // input's text and parts between Parse() and here, it seems better to be
987 // paranoid and check.
988 if ((input
.type() != metrics::OmniboxInputType::UNKNOWN
) ||
989 !base::LowerCaseEqualsASCII(input
.scheme(), url::kHttpScheme
) ||
990 !input
.parts().host
.is_nonempty())
992 const std::string
host(base::UTF16ToUTF8(
993 input
.text().substr(input
.parts().host
.begin
, input
.parts().host
.len
)));
994 const size_t registry_length
=
995 net::registry_controlled_domains::GetRegistryLength(
997 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
998 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
999 return registry_length
== 0 && db
->IsTypedHost(host
);
1002 bool HistoryURLProvider::PromoteOrCreateShorterSuggestion(
1003 history::URLDatabase
* db
,
1004 HistoryURLProviderParams
* params
) {
1005 if (params
->matches
.empty())
1006 return false; // No matches, nothing to do.
1008 // Determine the base URL from which to search, and whether that URL could
1009 // itself be added as a match. We can add the base iff it's not "effectively
1010 // the same" as any "what you typed" match.
1011 const history::HistoryMatch
& match
= params
->matches
[0];
1012 GURL search_base
= ConvertToHostOnly(match
, params
->input
.text());
1013 bool can_add_search_base_to_matches
= !params
->have_what_you_typed_match
;
1014 if (search_base
.is_empty()) {
1015 // Search from what the user typed when we couldn't reduce the best match
1016 // to a host. Careful: use a substring of |match| here, rather than the
1017 // first match in |params|, because they might have different prefixes. If
1018 // the user typed "google.com", params->what_you_typed_match will hold
1019 // "http://google.com/", but |match| might begin with
1020 // "http://www.google.com/".
1021 // TODO: this should be cleaned up, and is probably incorrect for IDN.
1022 std::string new_match
= match
.url_info
.url().possibly_invalid_spec().
1023 substr(0, match
.input_location
+ params
->input
.text().length());
1024 search_base
= GURL(new_match
);
1025 if (search_base
.is_empty())
1026 return false; // Can't construct a URL from which to start a search.
1027 } else if (!can_add_search_base_to_matches
) {
1028 can_add_search_base_to_matches
=
1029 (search_base
!= params
->what_you_typed_match
.destination_url
);
1031 if (search_base
== match
.url_info
.url())
1032 return false; // Couldn't shorten |match|, so no URLs to search over.
1034 // Search the DB for short URLs between our base and |match|.
1035 history::URLRow
info(search_base
);
1036 bool promote
= true;
1037 // A short URL is only worth suggesting if it's been visited at least a third
1038 // as often as the longer URL.
1039 const int min_visit_count
= ((match
.url_info
.visit_count() - 1) / 3) + 1;
1040 // For stability between the in-memory and on-disk autocomplete passes, when
1041 // the long URL has been typed before, only suggest shorter URLs that have
1042 // also been typed. Otherwise, the on-disk pass could suggest a shorter URL
1043 // (which hasn't been typed) that the in-memory pass doesn't know about,
1044 // thereby making the top match, and thus the behavior of inline
1045 // autocomplete, unstable.
1046 const int min_typed_count
= match
.url_info
.typed_count() ? 1 : 0;
1047 if (!db
->FindShortestURLFromBase(search_base
.possibly_invalid_spec(),
1048 match
.url_info
.url().possibly_invalid_spec(), min_visit_count
,
1049 min_typed_count
, can_add_search_base_to_matches
, &info
)) {
1050 if (!can_add_search_base_to_matches
)
1051 return false; // Couldn't find anything and can't add the search base.
1053 // Try to get info on the search base itself. Promote it to the top if the
1054 // original best match isn't good enough to autocomplete.
1055 db
->GetRowForURL(search_base
, &info
);
1056 promote
= match
.url_info
.typed_count() <= 1;
1059 // Promote or add the desired URL to the list of matches.
1060 const bool ensure_can_inline
=
1061 promote
&& CanPromoteMatchForInlineAutocomplete(match
);
1062 return CreateOrPromoteMatch(info
, match
.input_location
, match
.match_in_scheme
,
1063 ¶ms
->matches
, true, promote
) &&
1067 void HistoryURLProvider::CullPoorMatches(
1068 HistoryURLProviderParams
* params
) const {
1069 const base::Time
& threshold(history::AutocompleteAgeThreshold());
1070 for (history::HistoryMatches::iterator
i(params
->matches
.begin());
1071 i
!= params
->matches
.end(); ) {
1072 if (RowQualifiesAsSignificant(i
->url_info
, threshold
) &&
1073 (!params
->default_search_provider
||
1074 !params
->default_search_provider
->IsSearchURL(
1075 i
->url_info
.url(), *params
->search_terms_data
))) {
1078 i
= params
->matches
.erase(i
);
1083 void HistoryURLProvider::CullRedirects(history::HistoryBackend
* backend
,
1084 history::HistoryMatches
* matches
,
1085 size_t max_results
) const {
1086 for (size_t source
= 0;
1087 (source
< matches
->size()) && (source
< max_results
); ) {
1088 const GURL
& url
= (*matches
)[source
].url_info
.url();
1089 // TODO(brettw) this should go away when everything uses GURL.
1090 history::RedirectList redirects
;
1091 backend
->QueryRedirectsFrom(url
, &redirects
);
1092 if (!redirects
.empty()) {
1093 // Remove all but the first occurrence of any of these redirects in the
1094 // search results. We also must add the URL we queried for, since it may
1095 // not be the first match and we'd want to remove it.
1097 // For example, when A redirects to B and our matches are [A, X, B],
1098 // we'll get B as the redirects from, and we want to remove the second
1099 // item of that pair, removing B. If A redirects to B and our matches are
1100 // [B, X, A], we'll want to remove A instead.
1101 redirects
.push_back(url
);
1102 source
= RemoveSubsequentMatchesOf(matches
, source
, redirects
);
1104 // Advance to next item.
1109 if (matches
->size() > max_results
)
1110 matches
->resize(max_results
);
1113 size_t HistoryURLProvider::RemoveSubsequentMatchesOf(
1114 history::HistoryMatches
* matches
,
1115 size_t source_index
,
1116 const std::vector
<GURL
>& remove
) const {
1117 size_t next_index
= source_index
+ 1; // return value = item after source
1119 // Find the first occurrence of any URL in the redirect chain. We want to
1120 // keep this one since it is rated the highest.
1121 history::HistoryMatches::iterator
first(std::find_first_of(
1122 matches
->begin(), matches
->end(), remove
.begin(), remove
.end(),
1123 history::HistoryMatch::EqualsGURL
));
1124 DCHECK(first
!= matches
->end()) << "We should have always found at least the "
1127 // Find any following occurrences of any URL in the redirect chain, these
1128 // should be deleted.
1129 for (history::HistoryMatches::iterator
next(std::find_first_of(first
+ 1,
1130 matches
->end(), remove
.begin(), remove
.end(),
1131 history::HistoryMatch::EqualsGURL
));
1132 next
!= matches
->end(); next
= std::find_first_of(next
, matches
->end(),
1133 remove
.begin(), remove
.end(), history::HistoryMatch::EqualsGURL
)) {
1134 // Remove this item. When we remove an item before the source index, we
1135 // need to shift it to the right and remember that so we can return it.
1136 next
= matches
->erase(next
);
1137 if (static_cast<size_t>(next
- matches
->begin()) < next_index
)
1143 AutocompleteMatch
HistoryURLProvider::HistoryMatchToACMatch(
1144 const HistoryURLProviderParams
& params
,
1145 size_t match_number
,
1146 MatchType match_type
,
1148 // The FormattedStringWithEquivalentMeaning() call below requires callers to
1149 // be on the main thread.
1150 DCHECK(thread_checker_
.CalledOnValidThread());
1152 const history::HistoryMatch
& history_match
= params
.matches
[match_number
];
1153 const history::URLRow
& info
= history_match
.url_info
;
1154 AutocompleteMatch
match(this, relevance
,
1155 !!info
.visit_count(), AutocompleteMatchType::HISTORY_URL
);
1156 match
.typed_count
= info
.typed_count();
1157 match
.destination_url
= info
.url();
1158 DCHECK(match
.destination_url
.is_valid());
1159 size_t inline_autocomplete_offset
=
1160 history_match
.input_location
+ params
.input
.text().length();
1161 std::string languages
= (match_type
== WHAT_YOU_TYPED
) ?
1162 std::string() : params
.languages
;
1163 const url_formatter::FormatUrlTypes format_types
=
1164 url_formatter::kFormatUrlOmitAll
&
1165 ~((params
.trim_http
&& !history_match
.match_in_scheme
)
1167 : url_formatter::kFormatUrlOmitHTTP
);
1168 match
.fill_into_edit
=
1169 AutocompleteInput::FormattedStringWithEquivalentMeaning(
1171 url_formatter::FormatUrl(info
.url(), languages
, format_types
,
1172 net::UnescapeRule::SPACES
, nullptr, nullptr,
1173 &inline_autocomplete_offset
),
1174 client()->GetSchemeClassifier());
1175 if (!params
.prevent_inline_autocomplete
&&
1176 (inline_autocomplete_offset
!= base::string16::npos
)) {
1177 DCHECK(inline_autocomplete_offset
<= match
.fill_into_edit
.length());
1178 match
.inline_autocompletion
=
1179 match
.fill_into_edit
.substr(inline_autocomplete_offset
);
1181 // The latter part of the test effectively asks "is the inline completion
1182 // empty?" (i.e., is this match effectively the what-you-typed match?).
1183 match
.allowed_to_be_default_match
= !params
.prevent_inline_autocomplete
||
1184 ((inline_autocomplete_offset
!= base::string16::npos
) &&
1185 (inline_autocomplete_offset
>= match
.fill_into_edit
.length()));
1187 size_t match_start
= history_match
.input_location
;
1188 match
.contents
= url_formatter::FormatUrl(info
.url(), languages
, format_types
,
1189 net::UnescapeRule::SPACES
, nullptr,
1190 nullptr, &match_start
);
1191 if ((match_start
!= base::string16::npos
) &&
1192 (inline_autocomplete_offset
!= base::string16::npos
) &&
1193 (inline_autocomplete_offset
!= match_start
)) {
1194 DCHECK(inline_autocomplete_offset
> match_start
);
1195 AutocompleteMatch::ClassifyLocationInString(match_start
,
1196 inline_autocomplete_offset
- match_start
, match
.contents
.length(),
1197 ACMatchClassification::URL
, &match
.contents_class
);
1199 AutocompleteMatch::ClassifyLocationInString(base::string16::npos
, 0,
1200 match
.contents
.length(), ACMatchClassification::URL
,
1201 &match
.contents_class
);
1203 match
.description
= info
.title();
1204 match
.description_class
=
1205 ClassifyDescription(params
.input
.text(), match
.description
);
1206 RecordAdditionalInfoFromUrlRow(info
, &match
);