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
,
117 int undecayed_count
) {
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
= undecayed_count
;
125 double decay_factor
= score_buckets
.HalfLifeTimeDecay(time_since_last_visit
);
126 if (decayed_count
> 0)
127 decayed_count
*= decay_factor
;
129 const HUPScoringParams::ScoreBuckets::CountMaxRelevance
* score_bucket
= NULL
;
130 const double factor
= (score_buckets
.use_decay_factor() ?
131 decay_factor
: decayed_count
);
132 for (size_t i
= 0; i
< score_buckets
.buckets().size(); ++i
) {
133 score_bucket
= &score_buckets
.buckets()[i
];
134 if (factor
>= score_bucket
->first
)
138 return (score_bucket
&& (undecayed_relevance
> score_bucket
->second
)) ?
139 score_bucket
->second
: undecayed_relevance
;
142 // Returns a new relevance score for the given |match| based on the
143 // |old_relevance| score and |scoring_params|. The new relevance score is
144 // guaranteed to be less than or equal to |old_relevance|. In other words, this
145 // function can only demote a score, never boost it. Returns |old_relevance| if
146 // experimental scoring is disabled.
147 int CalculateRelevanceScoreUsingScoringParams(
148 const history::HistoryMatch
& match
,
150 const HUPScoringParams
& scoring_params
) {
151 const base::TimeDelta time_since_last_visit
=
152 base::Time::Now() - match
.url_info
.last_visit();
154 int relevance
= CalculateRelevanceUsingScoreBuckets(
155 scoring_params
.typed_count_buckets
, time_since_last_visit
, old_relevance
,
156 match
.url_info
.typed_count());
158 // Additional demotion (on top of typed_count demotion) of URLs that were
160 if (match
.url_info
.typed_count() == 0) {
161 relevance
= CalculateRelevanceUsingScoreBuckets(
162 scoring_params
.visited_count_buckets
, time_since_last_visit
, relevance
,
163 match
.url_info
.visit_count());
166 DCHECK_LE(relevance
, old_relevance
);
170 // Extracts typed_count, visit_count, and last_visited time from the URLRow and
171 // puts them in the additional info field of the |match| for display in
173 void RecordAdditionalInfoFromUrlRow(const history::URLRow
& info
,
174 AutocompleteMatch
* match
) {
175 match
->RecordAdditionalInfo("typed count", info
.typed_count());
176 match
->RecordAdditionalInfo("visit count", info
.visit_count());
177 match
->RecordAdditionalInfo("last visit", info
.last_visit());
180 // If |create_if_necessary| is true, ensures that |matches| contains an entry
181 // for |info|, creating a new such entry if necessary (using |input_location|
182 // and |match_in_scheme|).
184 // If |promote| is true, this also ensures the entry is the first element in
185 // |matches|, moving or adding it to the front as appropriate. When |promote|
186 // is false, existing matches are left in place, and newly added matches are
187 // placed at the back.
189 // It's OK to call this function with both |create_if_necessary| and |promote|
190 // false, in which case we'll do nothing.
192 // Returns whether the match exists regardless if it was promoted/created.
193 bool CreateOrPromoteMatch(const history::URLRow
& info
,
194 size_t input_location
,
195 bool match_in_scheme
,
196 history::HistoryMatches
* matches
,
197 bool create_if_necessary
,
199 // |matches| may already have an entry for this.
200 for (history::HistoryMatches::iterator
i(matches
->begin());
201 i
!= matches
->end(); ++i
) {
202 if (i
->url_info
.url() == info
.url()) {
203 // Rotate it to the front if the caller wishes.
205 std::rotate(matches
->begin(), i
, i
+ 1);
210 if (!create_if_necessary
)
213 // No entry, so create one.
214 history::HistoryMatch
match(info
, input_location
, match_in_scheme
, true);
216 matches
->push_front(match
);
218 matches
->push_back(match
);
223 // Returns whether |match| is suitable for inline autocompletion.
224 bool CanPromoteMatchForInlineAutocomplete(const history::HistoryMatch
& match
) {
225 // We can promote this match if it's been typed at least n times, where n == 1
226 // for "simple" (host-only) URLs and n == 2 for others. We set a higher bar
227 // for these long URLs because it's less likely that users will want to visit
228 // them again. Even though we don't increment the typed_count for pasted-in
229 // URLs, if the user manually edits the URL or types some long thing in by
230 // hand, we wouldn't want to immediately start autocompleting it.
231 return match
.url_info
.typed_count() &&
232 ((match
.url_info
.typed_count() > 1) || match
.IsHostOnly());
235 // Given the user's |input| and a |match| created from it, reduce the match's
236 // URL to just a host. If this host still matches the user input, return it.
237 // Returns the empty string on failure.
238 GURL
ConvertToHostOnly(const history::HistoryMatch
& match
,
239 const base::string16
& input
) {
240 // See if we should try to do host-only suggestions for this URL. Nonstandard
241 // schemes means there's no authority section, so suggesting the host name
242 // is useless. File URLs are standard, but host suggestion is not useful for
244 const GURL
& url
= match
.url_info
.url();
245 if (!url
.is_valid() || !url
.IsStandard() || url
.SchemeIsFile())
248 // Transform to a host-only match. Bail if the host no longer matches the
249 // user input (e.g. because the user typed more than just a host).
250 GURL host
= url
.GetWithEmptyPath();
251 if ((host
.spec().length() < (match
.input_location
+ input
.length())))
252 return GURL(); // User typing is longer than this host suggestion.
254 const base::string16 spec
= base::UTF8ToUTF16(host
.spec());
255 if (spec
.compare(match
.input_location
, input
.length(), input
))
256 return GURL(); // User typing is no longer a prefix.
263 // -----------------------------------------------------------------
264 // SearchTermsDataSnapshot
266 // Implementation of SearchTermsData that takes a snapshot of another
267 // SearchTermsData by copying all the responses to the different getters into
268 // member strings, then returning those strings when its own getters are called.
269 // This will typically be constructed on the UI thread from
270 // UIThreadSearchTermsData but is subsequently safe to use on any thread.
271 class SearchTermsDataSnapshot
: public SearchTermsData
{
273 explicit SearchTermsDataSnapshot(const SearchTermsData
& search_terms_data
);
274 ~SearchTermsDataSnapshot() override
;
276 std::string
GoogleBaseURLValue() const override
;
277 std::string
GetApplicationLocale() const override
;
278 base::string16
GetRlzParameterValue(bool from_app_list
) const override
;
279 std::string
GetSearchClient() const override
;
280 bool IsShowingSearchTermsOnSearchResultsPages() const override
;
281 std::string
InstantExtendedEnabledParam(bool for_search
) const override
;
282 std::string
ForceInstantResultsParam(bool for_prerender
) const override
;
283 std::string
NTPIsThemedParam() const override
;
284 std::string
GoogleImageSearchSource() const override
;
287 std::string google_base_url_value_
;
288 std::string application_locale_
;
289 base::string16 rlz_parameter_value_
;
290 std::string search_client_
;
291 bool is_showing_search_terms_on_search_results_pages_
;
292 std::string instant_extended_enabled_param_
;
293 std::string instant_extended_enabled_param_for_search_
;
294 std::string force_instant_results_param_
;
295 std::string force_instant_results_param_for_prerender_
;
296 std::string ntp_is_themed_param_
;
297 std::string google_image_search_source_
;
299 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot
);
302 SearchTermsDataSnapshot::SearchTermsDataSnapshot(
303 const SearchTermsData
& search_terms_data
)
304 : google_base_url_value_(search_terms_data
.GoogleBaseURLValue()),
305 application_locale_(search_terms_data
.GetApplicationLocale()),
306 rlz_parameter_value_(search_terms_data
.GetRlzParameterValue(false)),
307 search_client_(search_terms_data
.GetSearchClient()),
308 is_showing_search_terms_on_search_results_pages_(
309 search_terms_data
.IsShowingSearchTermsOnSearchResultsPages()),
310 instant_extended_enabled_param_(
311 search_terms_data
.InstantExtendedEnabledParam(false)),
312 instant_extended_enabled_param_for_search_(
313 search_terms_data
.InstantExtendedEnabledParam(true)),
314 force_instant_results_param_(
315 search_terms_data
.ForceInstantResultsParam(false)),
316 force_instant_results_param_for_prerender_(
317 search_terms_data
.ForceInstantResultsParam(true)),
318 ntp_is_themed_param_(search_terms_data
.NTPIsThemedParam()),
319 google_image_search_source_(search_terms_data
.GoogleImageSearchSource()) {
322 SearchTermsDataSnapshot::~SearchTermsDataSnapshot() {
325 std::string
SearchTermsDataSnapshot::GoogleBaseURLValue() const {
326 return google_base_url_value_
;
329 std::string
SearchTermsDataSnapshot::GetApplicationLocale() const {
330 return application_locale_
;
333 base::string16
SearchTermsDataSnapshot::GetRlzParameterValue(
334 bool from_app_list
) const {
335 return rlz_parameter_value_
;
338 std::string
SearchTermsDataSnapshot::GetSearchClient() const {
339 return search_client_
;
342 bool SearchTermsDataSnapshot::IsShowingSearchTermsOnSearchResultsPages() const {
343 return is_showing_search_terms_on_search_results_pages_
;
346 std::string
SearchTermsDataSnapshot::InstantExtendedEnabledParam(
347 bool for_search
) const {
348 return for_search
? instant_extended_enabled_param_
:
349 instant_extended_enabled_param_for_search_
;
352 std::string
SearchTermsDataSnapshot::ForceInstantResultsParam(
353 bool for_prerender
) const {
354 return for_prerender
? force_instant_results_param_
:
355 force_instant_results_param_for_prerender_
;
358 std::string
SearchTermsDataSnapshot::NTPIsThemedParam() const {
359 return ntp_is_themed_param_
;
362 std::string
SearchTermsDataSnapshot::GoogleImageSearchSource() const {
363 return google_image_search_source_
;
366 // -----------------------------------------------------------------
367 // HistoryURLProvider
369 // These ugly magic numbers will go away once we switch all scoring
370 // behavior (including URL-what-you-typed) to HistoryQuick provider.
371 const int HistoryURLProvider::kScoreForBestInlineableResult
= 1413;
372 const int HistoryURLProvider::kScoreForUnvisitedIntranetResult
= 1403;
373 const int HistoryURLProvider::kScoreForWhatYouTypedResult
= 1203;
374 const int HistoryURLProvider::kBaseScoreForNonInlineableResult
= 900;
376 // VisitClassifier is used to classify the type of visit to a particular url.
377 class HistoryURLProvider::VisitClassifier
{
380 INVALID
, // Navigations to the URL are not allowed.
381 UNVISITED_INTRANET
, // A navigable URL for which we have no visit data but
382 // which is known to refer to a visited intranet host.
383 VISITED
, // The site has been previously visited.
386 VisitClassifier(HistoryURLProvider
* provider
,
387 const AutocompleteInput
& input
,
388 history::URLDatabase
* db
);
390 // Returns the type of visit for the specified input.
391 Type
type() const { return type_
; }
393 // Returns the URLRow for the visit.
394 const history::URLRow
& url_row() const { return url_row_
; }
397 HistoryURLProvider
* provider_
;
398 history::URLDatabase
* db_
;
400 history::URLRow url_row_
;
402 DISALLOW_COPY_AND_ASSIGN(VisitClassifier
);
405 HistoryURLProvider::VisitClassifier::VisitClassifier(
406 HistoryURLProvider
* provider
,
407 const AutocompleteInput
& input
,
408 history::URLDatabase
* db
)
409 : provider_(provider
),
412 const GURL
& url
= input
.canonicalized_url();
413 // Detect email addresses. These cases will look like "http://user@site/",
414 // and because the history backend strips auth creds, we'll get a bogus exact
415 // match below if the user has visited "site".
416 if (!url
.is_valid() ||
417 ((input
.type() == metrics::OmniboxInputType::UNKNOWN
) &&
418 input
.parts().username
.is_nonempty() &&
419 !input
.parts().password
.is_nonempty() &&
420 !input
.parts().path
.is_nonempty()))
423 if (db_
->GetRowForURL(url
, &url_row_
)) {
428 if (provider_
->CanFindIntranetURL(db_
, input
)) {
429 // The user typed an intranet hostname that they've visited (albeit with a
430 // different port and/or path) before.
431 url_row_
= history::URLRow(url
);
432 type_
= UNVISITED_INTRANET
;
436 HistoryURLProviderParams::HistoryURLProviderParams(
437 const AutocompleteInput
& input
,
439 const AutocompleteMatch
& what_you_typed_match
,
440 const std::string
& languages
,
441 TemplateURL
* default_search_provider
,
442 const SearchTermsData
& search_terms_data
)
443 : message_loop(base::MessageLoop::current()),
445 prevent_inline_autocomplete(input
.prevent_inline_autocomplete()),
446 trim_http(trim_http
),
447 what_you_typed_match(what_you_typed_match
),
449 exact_suggestion_is_in_history(false),
450 promote_type(NEITHER
),
451 languages(languages
),
452 default_search_provider(default_search_provider
?
453 new TemplateURL(default_search_provider
->data()) : NULL
),
454 search_terms_data(new SearchTermsDataSnapshot(search_terms_data
)) {
457 HistoryURLProviderParams::~HistoryURLProviderParams() {
460 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient
* client
,
461 AutocompleteProviderListener
* listener
)
462 : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL
, client
),
465 // Initialize the default HUP scoring params.
466 OmniboxFieldTrial::GetDefaultHUPScoringParams(&scoring_params_
);
467 // Initialize HUP scoring params based on the current experiment.
468 OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_
);
471 void HistoryURLProvider::Start(const AutocompleteInput
& input
,
472 bool minimal_changes
) {
473 // NOTE: We could try hard to do less work in the |minimal_changes| case
474 // here; some clever caching would let us reuse the raw matches from the
475 // history DB without re-querying. However, we'd still have to go back to
476 // the history thread to mark these up properly, and if pass 2 is currently
477 // running, we'd need to wait for it to return to the main thread before
478 // doing this (we can't just write new data for it to read due to thread
479 // safety issues). At that point it's just as fast, and easier, to simply
480 // re-run the query from scratch and ignore |minimal_changes|.
482 // Cancel any in-progress query.
487 if (input
.from_omnibox_focus() ||
488 (input
.type() == metrics::OmniboxInputType::INVALID
) ||
489 (input
.type() == metrics::OmniboxInputType::FORCED_QUERY
))
492 // Do some fixup on the user input before matching against it, so we provide
493 // good results for local file paths, input with spaces, etc.
494 const FixupReturn
fixup_return(FixupUserInput(input
));
495 if (!fixup_return
.first
)
498 url_formatter::SegmentURL(fixup_return
.second
, &parts
);
499 AutocompleteInput
fixed_up_input(input
);
500 fixed_up_input
.UpdateText(fixup_return
.second
, base::string16::npos
, parts
);
502 // Create a match for what the user typed.
503 const bool trim_http
= !AutocompleteInput::HasHTTPScheme(input
.text());
504 AutocompleteMatch
what_you_typed_match(SuggestExactInput(
505 fixed_up_input
, fixed_up_input
.canonicalized_url(), trim_http
));
506 what_you_typed_match
.relevance
= CalculateRelevance(WHAT_YOU_TYPED
, 0);
508 // Add the WYT match as a fallback in case we can't get the history service or
509 // URL DB; otherwise, we'll replace this match lower down. Don't do this for
510 // queries, though -- while we can sometimes mark up a match for them, it's
511 // not what the user wants, and just adds noise.
512 if (fixed_up_input
.type() != metrics::OmniboxInputType::QUERY
)
513 matches_
.push_back(what_you_typed_match
);
515 // We'll need the history service to run both passes, so try to obtain it.
516 history::HistoryService
* const history_service
=
517 client()->GetHistoryService();
518 if (!history_service
)
521 // Get the default search provider and search terms data now since we have to
522 // retrieve these on the UI thread, and the second pass runs on the history
523 // thread. |template_url_service| can be NULL when testing.
524 TemplateURLService
* template_url_service
= client()->GetTemplateURLService();
525 TemplateURL
* default_search_provider
= template_url_service
?
526 template_url_service
->GetDefaultSearchProvider() : NULL
;
528 // Create the data structure for the autocomplete passes. We'll save this off
529 // onto the |params_| member for later deletion below if we need to run pass
531 scoped_ptr
<HistoryURLProviderParams
> params(new HistoryURLProviderParams(
532 fixed_up_input
, trim_http
, what_you_typed_match
,
533 client()->GetAcceptLanguages(), default_search_provider
,
534 client()->GetSearchTermsData()));
535 // Note that we use the non-fixed-up input here, since fixup may strip
536 // trailing whitespace.
537 params
->prevent_inline_autocomplete
= PreventInlineAutocomplete(input
);
539 // Pass 1: Get the in-memory URL database, and use it to find and promote
540 // the inline autocomplete match, if any.
541 history::URLDatabase
* url_db
= history_service
->InMemoryDatabase();
542 // url_db can be NULL if it hasn't finished initializing (or failed to
543 // initialize). In this case all we can do is fall back on the second
546 // TODO(pkasting): We should just block here until this loads. Any time
547 // someone unloads the history backend, we'll get inconsistent inline
548 // autocomplete behavior here.
550 DoAutocomplete(NULL
, url_db
, params
.get());
552 PromoteMatchesIfNecessary(*params
);
553 // NOTE: We don't reset |params| here since at least the |promote_type|
554 // field on it will be read by the second pass -- see comments in
558 // Pass 2: Ask the history service to call us back on the history thread,
559 // where we can read the full on-disk DB.
560 if (input
.want_asynchronous_matches()) {
562 params_
= params
.release(); // This object will be destroyed in
563 // QueryComplete() once we're done with it.
564 history_service
->ScheduleAutocomplete(
565 base::Bind(&HistoryURLProvider::ExecuteWithDB
, this, params_
));
569 void HistoryURLProvider::Stop(bool clear_cached_results
,
570 bool due_to_user_inactivity
) {
574 params_
->cancel_flag
.Set();
577 AutocompleteMatch
HistoryURLProvider::SuggestExactInput(
578 const AutocompleteInput
& input
,
579 const GURL
& destination_url
,
581 // The FormattedStringWithEquivalentMeaning() call below requires callers to
582 // be on the main thread.
583 DCHECK(thread_checker_
.CalledOnValidThread());
585 AutocompleteMatch
match(this, 0, false,
586 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
588 if (destination_url
.is_valid()) {
589 match
.destination_url
= destination_url
;
591 // Trim off "http://" if the user didn't type it.
593 !AutocompleteInput::HasHTTPScheme(input
.text()));
594 base::string16
display_string(url_formatter::FormatUrl(
595 destination_url
, std::string(),
596 url_formatter::kFormatUrlOmitAll
& ~url_formatter::kFormatUrlOmitHTTP
,
597 net::UnescapeRule::SPACES
, nullptr, nullptr, nullptr));
598 const size_t offset
= trim_http
? TrimHttpPrefix(&display_string
) : 0;
599 match
.fill_into_edit
=
600 AutocompleteInput::FormattedStringWithEquivalentMeaning(
601 destination_url
, display_string
, client()->GetSchemeClassifier());
602 // The what-you-typed match is generally only allowed to be default for
603 // URL inputs. (It's also allowed to be default for UNKNOWN inputs
604 // where the destination is a known intranet site. In this case,
605 // |allowed_to_be_default_match| is revised in FixupExactSuggestion().)
606 match
.allowed_to_be_default_match
=
607 (input
.type() == metrics::OmniboxInputType::URL
) ||
608 !OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs();
609 // NOTE: Don't set match.inline_autocompletion to something non-empty here;
610 // it's surprising and annoying.
612 // Try to highlight "innermost" match location. If we fix up "w" into
613 // "www.w.com", we want to highlight the fifth character, not the first.
614 // This relies on match.destination_url being the non-prefix-trimmed version
615 // of match.contents.
616 match
.contents
= display_string
;
617 const URLPrefix
* best_prefix
= URLPrefix::BestURLPrefix(
618 base::UTF8ToUTF16(destination_url
.spec()), input
.text());
619 // It's possible for match.destination_url to not contain the user's input
620 // at all (so |best_prefix| is NULL), for example if the input is
621 // "view-source:x" and |destination_url| has an inserted "http://" in the
623 if (best_prefix
== NULL
) {
624 AutocompleteMatch::ClassifyMatchInString(input
.text(),
626 ACMatchClassification::URL
,
627 &match
.contents_class
);
629 AutocompleteMatch::ClassifyLocationInString(
630 best_prefix
->prefix
.length() - offset
, input
.text().length(),
631 match
.contents
.length(), ACMatchClassification::URL
,
632 &match
.contents_class
);
639 void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams
* params
,
640 history::HistoryBackend
* backend
,
641 history::URLDatabase
* db
) {
642 // We may get called with a NULL database if it couldn't be properly
645 params
->failed
= true;
646 } else if (!params
->cancel_flag
.IsSet()) {
647 base::TimeTicks beginning_time
= base::TimeTicks::Now();
649 DoAutocomplete(backend
, db
, params
);
651 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime",
652 base::TimeTicks::Now() - beginning_time
);
655 // Return the results (if any) to the main thread.
656 params
->message_loop
->task_runner()->PostTask(
657 FROM_HERE
, base::Bind(&HistoryURLProvider::QueryComplete
, this, params
));
660 HistoryURLProvider::~HistoryURLProvider() {
661 // Note: This object can get leaked on shutdown if there are pending
662 // requests on the database (which hold a reference to us). Normally, these
663 // messages get flushed for each thread. We do a round trip from main, to
664 // history, back to main while holding a reference. If the main thread
665 // completes before the history thread, the message to delegate back to the
666 // main thread will not run and the reference will leak. Therefore, don't do
667 // anything on destruction.
671 int HistoryURLProvider::CalculateRelevance(MatchType match_type
,
673 switch (match_type
) {
674 case INLINE_AUTOCOMPLETE
:
675 return kScoreForBestInlineableResult
;
677 case UNVISITED_INTRANET
:
678 return kScoreForUnvisitedIntranetResult
;
681 return kScoreForWhatYouTypedResult
;
684 return kBaseScoreForNonInlineableResult
+ match_number
;
689 ACMatchClassifications
HistoryURLProvider::ClassifyDescription(
690 const base::string16
& input_text
,
691 const base::string16
& description
) {
692 base::string16 clean_description
=
693 bookmarks::CleanUpTitleForMatching(description
);
694 TermMatches
description_matches(SortAndDeoverlapMatches(
695 MatchTermInString(input_text
, clean_description
, 0)));
696 WordStarts description_word_starts
;
697 String16VectorFromString16(clean_description
, false,
698 &description_word_starts
);
699 // If HistoryURL retrieves any matches (and hence we reach this code), we
700 // are guaranteed that the beginning of input_text must be a word break.
701 WordStarts
offsets(1, 0u);
702 description_matches
= ScoredHistoryMatch::FilterTermMatchesByWordStarts(
703 description_matches
, offsets
, description_word_starts
, 0,
705 return SpansFromTermMatch(
706 description_matches
, clean_description
.length(), false);
709 void HistoryURLProvider::DoAutocomplete(history::HistoryBackend
* backend
,
710 history::URLDatabase
* db
,
711 HistoryURLProviderParams
* params
) {
712 // Get the matching URLs from the DB.
713 params
->matches
.clear();
714 history::URLRows url_matches
;
715 const URLPrefixes
& prefixes
= URLPrefix::GetURLPrefixes();
716 for (URLPrefixes::const_iterator
i(prefixes
.begin()); i
!= prefixes
.end();
718 if (params
->cancel_flag
.IsSet())
719 return; // Canceled in the middle of a query, give up.
721 // We only need kMaxMatches results in the end, but before we get there we
722 // need to promote lower-quality matches that are prefixes of higher-quality
723 // matches, and remove lower-quality redirects. So we ask for more results
724 // than we need, of every prefix type, in hopes this will give us far more
725 // than enough to work with. CullRedirects() will then reduce the list to
726 // the best kMaxMatches results.
727 db
->AutocompleteForPrefix(
728 base::UTF16ToUTF8(i
->prefix
+ params
->input
.text()), kMaxMatches
* 2,
729 !backend
, &url_matches
);
730 for (history::URLRows::const_iterator
j(url_matches
.begin());
731 j
!= url_matches
.end(); ++j
) {
732 const URLPrefix
* best_prefix
= URLPrefix::BestURLPrefix(
733 base::UTF8ToUTF16(j
->url().spec()), base::string16());
735 params
->matches
.push_back(history::HistoryMatch(
736 *j
, i
->prefix
.length(), !i
->num_components
,
737 i
->num_components
>= best_prefix
->num_components
));
741 // Create sorted list of suggestions.
742 CullPoorMatches(params
);
743 SortAndDedupMatches(¶ms
->matches
);
745 // Try to create a shorter suggestion from the best match.
746 // We consider the what you typed match eligible for display when it's
747 // navigable and there's a reasonable chance the user intended to do
748 // something other than search. We use a variety of heuristics to determine
749 // this, e.g. whether the user explicitly typed a scheme, or if omnibox
750 // searching has been disabled by policy. In the cases where we've parsed as
751 // UNKNOWN, we'll still show an accidental search infobar if need be.
752 VisitClassifier
classifier(this, params
->input
, db
);
753 params
->have_what_you_typed_match
=
754 (params
->input
.type() != metrics::OmniboxInputType::QUERY
) &&
755 ((params
->input
.type() != metrics::OmniboxInputType::UNKNOWN
) ||
756 (classifier
.type() == VisitClassifier::UNVISITED_INTRANET
) ||
757 !params
->trim_http
||
758 (AutocompleteInput::NumNonHostComponents(params
->input
.parts()) > 0) ||
759 !params
->default_search_provider
);
760 const bool have_shorter_suggestion_suitable_for_inline_autocomplete
=
761 PromoteOrCreateShorterSuggestion(db
, params
);
763 // Check whether what the user typed appears in history.
764 const bool can_check_history_for_exact_match
=
765 // Checking what_you_typed_match.destination_url.is_valid() tells us
766 // whether SuggestExactInput() succeeded in constructing a valid match.
767 params
->what_you_typed_match
.destination_url
.is_valid() &&
768 // Additionally, in the case where the user has typed "foo.com" and
769 // visited (but not typed) "foo/", and the input is "foo", the first pass
770 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the
771 // second pass can suggest the exact input as a better URL. Since we need
772 // both passes to agree, and since during the first pass there's no way to
773 // know about "foo/", ensure that if the promote type was set to
774 // FRONT_HISTORY_MATCH during the first pass, the second pass will not
775 // consider the exact suggestion to be in history and therefore will not
776 // suggest the exact input as a better match. (Note that during the first
777 // pass, this conditional will always succeed since |promote_type| is
778 // initialized to NEITHER.)
779 (params
->promote_type
!= HistoryURLProviderParams::FRONT_HISTORY_MATCH
);
780 params
->exact_suggestion_is_in_history
= can_check_history_for_exact_match
&&
781 FixupExactSuggestion(db
, classifier
, params
);
783 // If we succeeded in fixing up the exact match based on the user's history,
784 // we should treat it as the best match regardless of input type. If not,
785 // then we check whether there's an inline autocompletion we can create from
786 // this input, so we can promote that as the best match.
787 if (params
->exact_suggestion_is_in_history
) {
788 params
->promote_type
= HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
;
789 } else if (!params
->matches
.empty() &&
790 (have_shorter_suggestion_suitable_for_inline_autocomplete
||
791 CanPromoteMatchForInlineAutocomplete(params
->matches
[0]))) {
792 // Note that we promote this inline-autocompleted match even when
793 // params->prevent_inline_autocomplete is true. This is safe because in
794 // this case the match will be marked as "not allowed to be default", and
795 // a non-inlined match that is "allowed to be default" will be reordered
796 // above it by the controller/AutocompleteResult. We ensure there is such
797 // a match in two ways:
798 // * If params->have_what_you_typed_match is true, we force the
799 // what-you-typed match to be added in this case. See comments in
800 // PromoteMatchesIfNecessary().
801 // * Otherwise, we should have some sort of QUERY or UNKNOWN input that
802 // the SearchProvider will provide a defaultable WYT match for.
803 params
->promote_type
= HistoryURLProviderParams::FRONT_HISTORY_MATCH
;
805 // Failed to promote any URLs. Use the What You Typed match, if we have it.
806 params
->promote_type
= params
->have_what_you_typed_match
?
807 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
:
808 HistoryURLProviderParams::NEITHER
;
811 const size_t max_results
=
812 kMaxMatches
+ (params
->exact_suggestion_is_in_history
? 1 : 0);
814 // Remove redirects and trim list to size. We want to provide up to
815 // kMaxMatches results plus the What You Typed result, if it was added to
816 // params->matches above.
817 CullRedirects(backend
, ¶ms
->matches
, max_results
);
818 } else if (params
->matches
.size() > max_results
) {
819 // Simply trim the list to size.
820 params
->matches
.resize(max_results
);
824 void HistoryURLProvider::PromoteMatchesIfNecessary(
825 const HistoryURLProviderParams
& params
) {
826 if (params
.promote_type
== HistoryURLProviderParams::NEITHER
)
828 if (params
.promote_type
== HistoryURLProviderParams::FRONT_HISTORY_MATCH
) {
830 HistoryMatchToACMatch(params
, 0, INLINE_AUTOCOMPLETE
,
831 CalculateRelevance(INLINE_AUTOCOMPLETE
, 0)));
833 // There are two cases where we need to add the what-you-typed-match:
834 // * If params.promote_type is WHAT_YOU_TYPED_MATCH, we're being explicitly
836 // * If params.have_what_you_typed_match is true, then params.promote_type
837 // can't be NEITHER (see code near the end of DoAutocomplete()), so if
838 // it's not WHAT_YOU_TYPED_MATCH, it must be FRONT_HISTORY_MATCH, and
839 // we'll have promoted the history match above. If
840 // params.prevent_inline_autocomplete is also true, then this match
841 // will be marked "not allowed to be default", and we need to add the
842 // what-you-typed match to ensure there's a legal default match for the
843 // controller/AutocompleteResult to promote. (If
844 // params.have_what_you_typed_match is false, the SearchProvider should
845 // take care of adding this defaultable match.)
846 if ((params
.promote_type
== HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH
) ||
847 (params
.prevent_inline_autocomplete
&&
848 params
.have_what_you_typed_match
)) {
849 matches_
.push_back(params
.what_you_typed_match
);
853 void HistoryURLProvider::QueryComplete(
854 HistoryURLProviderParams
* params_gets_deleted
) {
855 // Ensure |params_gets_deleted| gets deleted on exit.
856 scoped_ptr
<HistoryURLProviderParams
> params(params_gets_deleted
);
858 // If the user hasn't already started another query, clear our member pointer
859 // so we can't write into deleted memory.
860 if (params_
== params_gets_deleted
)
863 // Don't send responses for queries that have been canceled.
864 if (params
->cancel_flag
.IsSet())
865 return; // Already set done_ when we canceled, no need to set it again.
867 // Don't modify |matches_| if the query failed, since it might have a default
868 // match in it, whereas |params->matches| will be empty.
869 if (!params
->failed
) {
871 PromoteMatchesIfNecessary(*params
);
873 // Determine relevance of highest scoring match, if any.
874 int relevance
= matches_
.empty() ?
875 CalculateRelevance(NORMAL
,
876 static_cast<int>(params
->matches
.size() - 1)) :
877 matches_
[0].relevance
;
879 // Convert the history matches to autocomplete matches. If we promoted the
880 // first match, skip over it.
881 const size_t first_match
=
882 (params
->exact_suggestion_is_in_history
||
883 (params
->promote_type
==
884 HistoryURLProviderParams::FRONT_HISTORY_MATCH
)) ? 1 : 0;
885 for (size_t i
= first_match
; i
< params
->matches
.size(); ++i
) {
886 // All matches score one less than the previous match.
888 // The experimental scoring must not change the top result's score.
889 if (!matches_
.empty()) {
890 relevance
= CalculateRelevanceScoreUsingScoringParams(
891 params
->matches
[i
], relevance
, scoring_params_
);
893 matches_
.push_back(HistoryMatchToACMatch(*params
, i
, NORMAL
, relevance
));
898 listener_
->OnProviderUpdate(true);
901 bool HistoryURLProvider::FixupExactSuggestion(
902 history::URLDatabase
* db
,
903 const VisitClassifier
& classifier
,
904 HistoryURLProviderParams
* params
) const {
905 MatchType type
= INLINE_AUTOCOMPLETE
;
906 switch (classifier
.type()) {
907 case VisitClassifier::INVALID
:
909 case VisitClassifier::UNVISITED_INTRANET
:
910 type
= UNVISITED_INTRANET
;
913 DCHECK_EQ(VisitClassifier::VISITED
, classifier
.type());
914 // We have data for this match, use it.
915 params
->what_you_typed_match
.deletable
= true;
916 params
->what_you_typed_match
.description
= classifier
.url_row().title();
917 RecordAdditionalInfoFromUrlRow(classifier
.url_row(),
918 ¶ms
->what_you_typed_match
);
919 params
->what_you_typed_match
.description_class
= ClassifyDescription(
920 params
->input
.text(), params
->what_you_typed_match
.description
);
921 if (!classifier
.url_row().typed_count()) {
922 // If we reach here, we must be in the second pass, and we must not have
923 // this row's data available during the first pass. That means we
924 // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to
925 // maintain the ordering between passes consistent, we need to score it
926 // the same way here.
927 type
= CanFindIntranetURL(db
, params
->input
) ?
928 UNVISITED_INTRANET
: WHAT_YOU_TYPED
;
933 if (OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs()) {
934 const GURL
& url
= params
->what_you_typed_match
.destination_url
;
935 const url::Parsed
& parsed
= url
.parsed_for_possibly_invalid_spec();
936 // If the what-you-typed result looks like a single word (which can be
937 // interpreted as an intranet address) followed by a pound sign ("#"),
938 // leave the score for the url-what-you-typed result as is and also
939 // don't mark it as allowed to be the default match. It will likely be
940 // outscored by a search query from the SearchProvider or, if not, the
941 // search query default match will in any case--which is allowed to be the
942 // default match--will be reordered to be first. This test fixes cases
943 // such as "c#" and "c# foo" where the user has visited an intranet site
944 // "c". We want the search-what-you-typed score to beat the
945 // URL-what-you-typed score in this case. Most of the below test tries to
946 // make sure that this code does not trigger if the user did anything to
947 // indicate the desired match is a URL. For instance, "c/# foo" will not
948 // pass the test because that will be classified as input type URL. The
949 // parsed.CountCharactersBefore() in the test looks for the presence of a
950 // reference fragment in the URL by checking whether the position differs
951 // included the delimiter (pound sign) versus not including the delimiter.
952 // (One cannot simply check url.ref() because it will not distinguish
953 // between the input "c" and the input "c#", both of which will have empty
954 // reference fragments.)
955 if ((type
== UNVISITED_INTRANET
) &&
956 (params
->input
.type() != metrics::OmniboxInputType::URL
) &&
957 url
.username().empty() && url
.password().empty() &&
958 url
.port().empty() && (url
.path() == "/") && url
.query().empty() &&
959 (parsed
.CountCharactersBefore(url::Parsed::REF
, true) !=
960 parsed
.CountCharactersBefore(url::Parsed::REF
, false))) {
965 params
->what_you_typed_match
.allowed_to_be_default_match
= true;
966 params
->what_you_typed_match
.relevance
= CalculateRelevance(type
, 0);
968 // If there are any other matches, then don't promote this match here, in
969 // hopes the caller will be able to inline autocomplete a better suggestion.
970 // DoAutocomplete() will fall back on this match if inline autocompletion
971 // fails. This matches how we react to never-visited URL inputs in the non-
973 if (type
== UNVISITED_INTRANET
&& !params
->matches
.empty())
976 // Put it on the front of the HistoryMatches for redirect culling.
977 CreateOrPromoteMatch(classifier
.url_row(), base::string16::npos
, false,
978 ¶ms
->matches
, true, true);
982 bool HistoryURLProvider::CanFindIntranetURL(
983 history::URLDatabase
* db
,
984 const AutocompleteInput
& input
) const {
985 // Normally passing the first two conditions below ought to guarantee the
986 // third condition, but because FixupUserInput() can run and modify the
987 // input's text and parts between Parse() and here, it seems better to be
988 // paranoid and check.
989 if ((input
.type() != metrics::OmniboxInputType::UNKNOWN
) ||
990 !base::LowerCaseEqualsASCII(input
.scheme(), url::kHttpScheme
) ||
991 !input
.parts().host
.is_nonempty())
993 const std::string
host(base::UTF16ToUTF8(
994 input
.text().substr(input
.parts().host
.begin
, input
.parts().host
.len
)));
995 const size_t registry_length
=
996 net::registry_controlled_domains::GetRegistryLength(
998 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
999 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
1000 return registry_length
== 0 && db
->IsTypedHost(host
);
1003 bool HistoryURLProvider::PromoteOrCreateShorterSuggestion(
1004 history::URLDatabase
* db
,
1005 HistoryURLProviderParams
* params
) {
1006 if (params
->matches
.empty())
1007 return false; // No matches, nothing to do.
1009 // Determine the base URL from which to search, and whether that URL could
1010 // itself be added as a match. We can add the base iff it's not "effectively
1011 // the same" as any "what you typed" match.
1012 const history::HistoryMatch
& match
= params
->matches
[0];
1013 GURL search_base
= ConvertToHostOnly(match
, params
->input
.text());
1014 bool can_add_search_base_to_matches
= !params
->have_what_you_typed_match
;
1015 if (search_base
.is_empty()) {
1016 // Search from what the user typed when we couldn't reduce the best match
1017 // to a host. Careful: use a substring of |match| here, rather than the
1018 // first match in |params|, because they might have different prefixes. If
1019 // the user typed "google.com", params->what_you_typed_match will hold
1020 // "http://google.com/", but |match| might begin with
1021 // "http://www.google.com/".
1022 // TODO: this should be cleaned up, and is probably incorrect for IDN.
1023 std::string new_match
= match
.url_info
.url().possibly_invalid_spec().
1024 substr(0, match
.input_location
+ params
->input
.text().length());
1025 search_base
= GURL(new_match
);
1026 if (search_base
.is_empty())
1027 return false; // Can't construct a URL from which to start a search.
1028 } else if (!can_add_search_base_to_matches
) {
1029 can_add_search_base_to_matches
=
1030 (search_base
!= params
->what_you_typed_match
.destination_url
);
1032 if (search_base
== match
.url_info
.url())
1033 return false; // Couldn't shorten |match|, so no URLs to search over.
1035 // Search the DB for short URLs between our base and |match|.
1036 history::URLRow
info(search_base
);
1037 bool promote
= true;
1038 // A short URL is only worth suggesting if it's been visited at least a third
1039 // as often as the longer URL.
1040 const int min_visit_count
= ((match
.url_info
.visit_count() - 1) / 3) + 1;
1041 // For stability between the in-memory and on-disk autocomplete passes, when
1042 // the long URL has been typed before, only suggest shorter URLs that have
1043 // also been typed. Otherwise, the on-disk pass could suggest a shorter URL
1044 // (which hasn't been typed) that the in-memory pass doesn't know about,
1045 // thereby making the top match, and thus the behavior of inline
1046 // autocomplete, unstable.
1047 const int min_typed_count
= match
.url_info
.typed_count() ? 1 : 0;
1048 if (!db
->FindShortestURLFromBase(search_base
.possibly_invalid_spec(),
1049 match
.url_info
.url().possibly_invalid_spec(), min_visit_count
,
1050 min_typed_count
, can_add_search_base_to_matches
, &info
)) {
1051 if (!can_add_search_base_to_matches
)
1052 return false; // Couldn't find anything and can't add the search base.
1054 // Try to get info on the search base itself. Promote it to the top if the
1055 // original best match isn't good enough to autocomplete.
1056 db
->GetRowForURL(search_base
, &info
);
1057 promote
= match
.url_info
.typed_count() <= 1;
1060 // Promote or add the desired URL to the list of matches.
1061 const bool ensure_can_inline
=
1062 promote
&& CanPromoteMatchForInlineAutocomplete(match
);
1063 return CreateOrPromoteMatch(info
, match
.input_location
, match
.match_in_scheme
,
1064 ¶ms
->matches
, true, promote
) &&
1068 void HistoryURLProvider::CullPoorMatches(
1069 HistoryURLProviderParams
* params
) const {
1070 const base::Time
& threshold(history::AutocompleteAgeThreshold());
1071 for (history::HistoryMatches::iterator
i(params
->matches
.begin());
1072 i
!= params
->matches
.end(); ) {
1073 if (RowQualifiesAsSignificant(i
->url_info
, threshold
) &&
1074 (!params
->default_search_provider
||
1075 !params
->default_search_provider
->IsSearchURL(
1076 i
->url_info
.url(), *params
->search_terms_data
))) {
1079 i
= params
->matches
.erase(i
);
1084 void HistoryURLProvider::CullRedirects(history::HistoryBackend
* backend
,
1085 history::HistoryMatches
* matches
,
1086 size_t max_results
) const {
1087 for (size_t source
= 0;
1088 (source
< matches
->size()) && (source
< max_results
); ) {
1089 const GURL
& url
= (*matches
)[source
].url_info
.url();
1090 // TODO(brettw) this should go away when everything uses GURL.
1091 history::RedirectList redirects
;
1092 backend
->QueryRedirectsFrom(url
, &redirects
);
1093 if (!redirects
.empty()) {
1094 // Remove all but the first occurrence of any of these redirects in the
1095 // search results. We also must add the URL we queried for, since it may
1096 // not be the first match and we'd want to remove it.
1098 // For example, when A redirects to B and our matches are [A, X, B],
1099 // we'll get B as the redirects from, and we want to remove the second
1100 // item of that pair, removing B. If A redirects to B and our matches are
1101 // [B, X, A], we'll want to remove A instead.
1102 redirects
.push_back(url
);
1103 source
= RemoveSubsequentMatchesOf(matches
, source
, redirects
);
1105 // Advance to next item.
1110 if (matches
->size() > max_results
)
1111 matches
->resize(max_results
);
1114 size_t HistoryURLProvider::RemoveSubsequentMatchesOf(
1115 history::HistoryMatches
* matches
,
1116 size_t source_index
,
1117 const std::vector
<GURL
>& remove
) const {
1118 size_t next_index
= source_index
+ 1; // return value = item after source
1120 // Find the first occurrence of any URL in the redirect chain. We want to
1121 // keep this one since it is rated the highest.
1122 history::HistoryMatches::iterator
first(std::find_first_of(
1123 matches
->begin(), matches
->end(), remove
.begin(), remove
.end(),
1124 history::HistoryMatch::EqualsGURL
));
1125 DCHECK(first
!= matches
->end()) << "We should have always found at least the "
1128 // Find any following occurrences of any URL in the redirect chain, these
1129 // should be deleted.
1130 for (history::HistoryMatches::iterator
next(std::find_first_of(first
+ 1,
1131 matches
->end(), remove
.begin(), remove
.end(),
1132 history::HistoryMatch::EqualsGURL
));
1133 next
!= matches
->end(); next
= std::find_first_of(next
, matches
->end(),
1134 remove
.begin(), remove
.end(), history::HistoryMatch::EqualsGURL
)) {
1135 // Remove this item. When we remove an item before the source index, we
1136 // need to shift it to the right and remember that so we can return it.
1137 next
= matches
->erase(next
);
1138 if (static_cast<size_t>(next
- matches
->begin()) < next_index
)
1144 AutocompleteMatch
HistoryURLProvider::HistoryMatchToACMatch(
1145 const HistoryURLProviderParams
& params
,
1146 size_t match_number
,
1147 MatchType match_type
,
1149 // The FormattedStringWithEquivalentMeaning() call below requires callers to
1150 // be on the main thread.
1151 DCHECK(thread_checker_
.CalledOnValidThread());
1153 const history::HistoryMatch
& history_match
= params
.matches
[match_number
];
1154 const history::URLRow
& info
= history_match
.url_info
;
1155 AutocompleteMatch
match(this, relevance
,
1156 !!info
.visit_count(), AutocompleteMatchType::HISTORY_URL
);
1157 match
.typed_count
= info
.typed_count();
1158 match
.destination_url
= info
.url();
1159 DCHECK(match
.destination_url
.is_valid());
1160 size_t inline_autocomplete_offset
=
1161 history_match
.input_location
+ params
.input
.text().length();
1162 std::string languages
= (match_type
== WHAT_YOU_TYPED
) ?
1163 std::string() : params
.languages
;
1164 const url_formatter::FormatUrlTypes format_types
=
1165 url_formatter::kFormatUrlOmitAll
&
1166 ~((params
.trim_http
&& !history_match
.match_in_scheme
)
1168 : url_formatter::kFormatUrlOmitHTTP
);
1169 match
.fill_into_edit
=
1170 AutocompleteInput::FormattedStringWithEquivalentMeaning(
1172 url_formatter::FormatUrl(info
.url(), languages
, format_types
,
1173 net::UnescapeRule::SPACES
, nullptr, nullptr,
1174 &inline_autocomplete_offset
),
1175 client()->GetSchemeClassifier());
1176 if (!params
.prevent_inline_autocomplete
&&
1177 (inline_autocomplete_offset
!= base::string16::npos
)) {
1178 DCHECK(inline_autocomplete_offset
<= match
.fill_into_edit
.length());
1179 match
.inline_autocompletion
=
1180 match
.fill_into_edit
.substr(inline_autocomplete_offset
);
1182 // The latter part of the test effectively asks "is the inline completion
1183 // empty?" (i.e., is this match effectively the what-you-typed match?).
1184 match
.allowed_to_be_default_match
= !params
.prevent_inline_autocomplete
||
1185 ((inline_autocomplete_offset
!= base::string16::npos
) &&
1186 (inline_autocomplete_offset
>= match
.fill_into_edit
.length()));
1188 size_t match_start
= history_match
.input_location
;
1189 match
.contents
= url_formatter::FormatUrl(info
.url(), languages
, format_types
,
1190 net::UnescapeRule::SPACES
, nullptr,
1191 nullptr, &match_start
);
1192 if ((match_start
!= base::string16::npos
) &&
1193 (inline_autocomplete_offset
!= base::string16::npos
) &&
1194 (inline_autocomplete_offset
!= match_start
)) {
1195 DCHECK(inline_autocomplete_offset
> match_start
);
1196 AutocompleteMatch::ClassifyLocationInString(match_start
,
1197 inline_autocomplete_offset
- match_start
, match
.contents
.length(),
1198 ACMatchClassification::URL
, &match
.contents_class
);
1200 AutocompleteMatch::ClassifyLocationInString(base::string16::npos
, 0,
1201 match
.contents
.length(), ACMatchClassification::URL
,
1202 &match
.contents_class
);
1204 match
.description
= info
.title();
1205 match
.description_class
=
1206 ClassifyDescription(params
.input
.text(), match
.description
);
1207 RecordAdditionalInfoFromUrlRow(info
, &match
);