Enable searching for printer provider apps when a printer is plugged in
[chromium-blink-merge.git] / components / omnibox / history_url_provider.cc
blobfaa4b2fb0a74843053f8ccbef10fefc9a11fce33
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/history_url_provider.h"
7 #include <algorithm>
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/autocomplete_match.h"
27 #include "components/omnibox/autocomplete_provider_listener.h"
28 #include "components/omnibox/autocomplete_result.h"
29 #include "components/omnibox/in_memory_url_index_types.h"
30 #include "components/omnibox/omnibox_field_trial.h"
31 #include "components/omnibox/scored_history_match.h"
32 #include "components/omnibox/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_fixer/url_fixer.h"
36 #include "net/base/net_util.h"
37 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
38 #include "url/gurl.h"
39 #include "url/third_party/mozilla/url_parse.h"
40 #include "url/url_util.h"
42 namespace {
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
62 // page inside.
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);
103 else
104 ++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 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 = 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,
147 int old_relevance,
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
160 // never typed.
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);
168 return 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
173 // about:omnibox.
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,
199 bool promote) {
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.
205 if (promote)
206 std::rotate(matches->begin(), i, i + 1);
207 return true;
211 if (!create_if_necessary)
212 return false;
214 // No entry, so create one.
215 history::HistoryMatch match(info, input_location, match_in_scheme, true);
216 if (promote)
217 matches->push_front(match);
218 else
219 matches->push_back(match);
221 return true;
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
244 // them either.
245 const GURL& url = match.url_info.url();
246 if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile())
247 return GURL();
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.
259 return host;
262 } // namespace
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 {
273 public:
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 EnableAnswersInSuggest() const override;
282 bool IsShowingSearchTermsOnSearchResultsPages() const override;
283 std::string InstantExtendedEnabledParam(bool for_search) const override;
284 std::string ForceInstantResultsParam(bool for_prerender) const override;
285 std::string NTPIsThemedParam() const override;
286 std::string GoogleImageSearchSource() const override;
288 private:
289 std::string google_base_url_value_;
290 std::string application_locale_;
291 base::string16 rlz_parameter_value_;
292 std::string search_client_;
293 bool enable_answers_in_suggest_;
294 bool is_showing_search_terms_on_search_results_pages_;
295 std::string instant_extended_enabled_param_;
296 std::string instant_extended_enabled_param_for_search_;
297 std::string force_instant_results_param_;
298 std::string force_instant_results_param_for_prerender_;
299 std::string ntp_is_themed_param_;
300 std::string google_image_search_source_;
302 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot);
305 SearchTermsDataSnapshot::SearchTermsDataSnapshot(
306 const SearchTermsData& search_terms_data)
307 : google_base_url_value_(search_terms_data.GoogleBaseURLValue()),
308 application_locale_(search_terms_data.GetApplicationLocale()),
309 rlz_parameter_value_(search_terms_data.GetRlzParameterValue(false)),
310 search_client_(search_terms_data.GetSearchClient()),
311 enable_answers_in_suggest_(search_terms_data.EnableAnswersInSuggest()),
312 is_showing_search_terms_on_search_results_pages_(
313 search_terms_data.IsShowingSearchTermsOnSearchResultsPages()),
314 instant_extended_enabled_param_(
315 search_terms_data.InstantExtendedEnabledParam(false)),
316 instant_extended_enabled_param_for_search_(
317 search_terms_data.InstantExtendedEnabledParam(true)),
318 force_instant_results_param_(
319 search_terms_data.ForceInstantResultsParam(false)),
320 force_instant_results_param_for_prerender_(
321 search_terms_data.ForceInstantResultsParam(true)),
322 ntp_is_themed_param_(search_terms_data.NTPIsThemedParam()),
323 google_image_search_source_(search_terms_data.GoogleImageSearchSource()) {
326 SearchTermsDataSnapshot::~SearchTermsDataSnapshot() {
329 std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const {
330 return google_base_url_value_;
333 std::string SearchTermsDataSnapshot::GetApplicationLocale() const {
334 return application_locale_;
337 base::string16 SearchTermsDataSnapshot::GetRlzParameterValue(
338 bool from_app_list) const {
339 return rlz_parameter_value_;
342 std::string SearchTermsDataSnapshot::GetSearchClient() const {
343 return search_client_;
346 bool SearchTermsDataSnapshot::EnableAnswersInSuggest() const {
347 return enable_answers_in_suggest_;
350 bool SearchTermsDataSnapshot::IsShowingSearchTermsOnSearchResultsPages() const {
351 return is_showing_search_terms_on_search_results_pages_;
354 std::string SearchTermsDataSnapshot::InstantExtendedEnabledParam(
355 bool for_search) const {
356 return for_search ? instant_extended_enabled_param_ :
357 instant_extended_enabled_param_for_search_;
360 std::string SearchTermsDataSnapshot::ForceInstantResultsParam(
361 bool for_prerender) const {
362 return for_prerender ? force_instant_results_param_ :
363 force_instant_results_param_for_prerender_;
366 std::string SearchTermsDataSnapshot::NTPIsThemedParam() const {
367 return ntp_is_themed_param_;
370 std::string SearchTermsDataSnapshot::GoogleImageSearchSource() const {
371 return google_image_search_source_;
374 // -----------------------------------------------------------------
375 // HistoryURLProvider
377 // These ugly magic numbers will go away once we switch all scoring
378 // behavior (including URL-what-you-typed) to HistoryQuick provider.
379 const int HistoryURLProvider::kScoreForBestInlineableResult = 1413;
380 const int HistoryURLProvider::kScoreForUnvisitedIntranetResult = 1403;
381 const int HistoryURLProvider::kScoreForWhatYouTypedResult = 1203;
382 const int HistoryURLProvider::kBaseScoreForNonInlineableResult = 900;
384 // VisitClassifier is used to classify the type of visit to a particular url.
385 class HistoryURLProvider::VisitClassifier {
386 public:
387 enum Type {
388 INVALID, // Navigations to the URL are not allowed.
389 UNVISITED_INTRANET, // A navigable URL for which we have no visit data but
390 // which is known to refer to a visited intranet host.
391 VISITED, // The site has been previously visited.
394 VisitClassifier(HistoryURLProvider* provider,
395 const AutocompleteInput& input,
396 history::URLDatabase* db);
398 // Returns the type of visit for the specified input.
399 Type type() const { return type_; }
401 // Returns the URLRow for the visit.
402 const history::URLRow& url_row() const { return url_row_; }
404 private:
405 HistoryURLProvider* provider_;
406 history::URLDatabase* db_;
407 Type type_;
408 history::URLRow url_row_;
410 DISALLOW_COPY_AND_ASSIGN(VisitClassifier);
413 HistoryURLProvider::VisitClassifier::VisitClassifier(
414 HistoryURLProvider* provider,
415 const AutocompleteInput& input,
416 history::URLDatabase* db)
417 : provider_(provider),
418 db_(db),
419 type_(INVALID) {
420 const GURL& url = input.canonicalized_url();
421 // Detect email addresses. These cases will look like "http://user@site/",
422 // and because the history backend strips auth creds, we'll get a bogus exact
423 // match below if the user has visited "site".
424 if (!url.is_valid() ||
425 ((input.type() == metrics::OmniboxInputType::UNKNOWN) &&
426 input.parts().username.is_nonempty() &&
427 !input.parts().password.is_nonempty() &&
428 !input.parts().path.is_nonempty()))
429 return;
431 if (db_->GetRowForURL(url, &url_row_)) {
432 type_ = VISITED;
433 return;
436 if (provider_->CanFindIntranetURL(db_, input)) {
437 // The user typed an intranet hostname that they've visited (albeit with a
438 // different port and/or path) before.
439 url_row_ = history::URLRow(url);
440 type_ = UNVISITED_INTRANET;
444 HistoryURLProviderParams::HistoryURLProviderParams(
445 const AutocompleteInput& input,
446 bool trim_http,
447 const AutocompleteMatch& what_you_typed_match,
448 const std::string& languages,
449 TemplateURL* default_search_provider,
450 const SearchTermsData& search_terms_data)
451 : message_loop(base::MessageLoop::current()),
452 input(input),
453 prevent_inline_autocomplete(input.prevent_inline_autocomplete()),
454 trim_http(trim_http),
455 what_you_typed_match(what_you_typed_match),
456 failed(false),
457 exact_suggestion_is_in_history(false),
458 promote_type(NEITHER),
459 languages(languages),
460 default_search_provider(default_search_provider ?
461 new TemplateURL(default_search_provider->data()) : NULL),
462 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) {
465 HistoryURLProviderParams::~HistoryURLProviderParams() {
468 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient* client,
469 AutocompleteProviderListener* listener)
470 : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL, client),
471 listener_(listener),
472 params_(NULL) {
473 // Initialize HUP scoring params based on the current experiment.
474 OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_);
477 void HistoryURLProvider::Start(const AutocompleteInput& input,
478 bool minimal_changes) {
479 // NOTE: We could try hard to do less work in the |minimal_changes| case
480 // here; some clever caching would let us reuse the raw matches from the
481 // history DB without re-querying. However, we'd still have to go back to
482 // the history thread to mark these up properly, and if pass 2 is currently
483 // running, we'd need to wait for it to return to the main thread before
484 // doing this (we can't just write new data for it to read due to thread
485 // safety issues). At that point it's just as fast, and easier, to simply
486 // re-run the query from scratch and ignore |minimal_changes|.
488 // Cancel any in-progress query.
489 Stop(false, false);
491 matches_.clear();
493 if (input.from_omnibox_focus() ||
494 (input.type() == metrics::OmniboxInputType::INVALID) ||
495 (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
496 return;
498 // Do some fixup on the user input before matching against it, so we provide
499 // good results for local file paths, input with spaces, etc.
500 const FixupReturn fixup_return(FixupUserInput(input));
501 if (!fixup_return.first)
502 return;
503 url::Parsed parts;
504 url_fixer::SegmentURL(fixup_return.second, &parts);
505 AutocompleteInput fixed_up_input(input);
506 fixed_up_input.UpdateText(fixup_return.second, base::string16::npos, parts);
508 // Create a match for what the user typed.
509 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text());
510 AutocompleteMatch what_you_typed_match(SuggestExactInput(
511 fixed_up_input, fixed_up_input.canonicalized_url(), trim_http));
512 what_you_typed_match.relevance = CalculateRelevance(WHAT_YOU_TYPED, 0);
514 // Add the WYT match as a fallback in case we can't get the history service or
515 // URL DB; otherwise, we'll replace this match lower down. Don't do this for
516 // queries, though -- while we can sometimes mark up a match for them, it's
517 // not what the user wants, and just adds noise.
518 if (fixed_up_input.type() != metrics::OmniboxInputType::QUERY)
519 matches_.push_back(what_you_typed_match);
521 // We'll need the history service to run both passes, so try to obtain it.
522 history::HistoryService* const history_service =
523 client()->GetHistoryService();
524 if (!history_service)
525 return;
527 // Get the default search provider and search terms data now since we have to
528 // retrieve these on the UI thread, and the second pass runs on the history
529 // thread. |template_url_service| can be NULL when testing.
530 TemplateURLService* template_url_service = client()->GetTemplateURLService();
531 TemplateURL* default_search_provider = template_url_service ?
532 template_url_service->GetDefaultSearchProvider() : NULL;
534 // Create the data structure for the autocomplete passes. We'll save this off
535 // onto the |params_| member for later deletion below if we need to run pass
536 // 2.
537 scoped_ptr<HistoryURLProviderParams> params(new HistoryURLProviderParams(
538 fixed_up_input, trim_http, what_you_typed_match,
539 client()->GetAcceptLanguages(), default_search_provider,
540 client()->GetSearchTermsData()));
541 // Note that we use the non-fixed-up input here, since fixup may strip
542 // trailing whitespace.
543 params->prevent_inline_autocomplete = PreventInlineAutocomplete(input);
545 // Pass 1: Get the in-memory URL database, and use it to find and promote
546 // the inline autocomplete match, if any.
547 history::URLDatabase* url_db = history_service->InMemoryDatabase();
548 // url_db can be NULL if it hasn't finished initializing (or failed to
549 // initialize). In this case all we can do is fall back on the second
550 // pass.
552 // TODO(pkasting): We should just block here until this loads. Any time
553 // someone unloads the history backend, we'll get inconsistent inline
554 // autocomplete behavior here.
555 if (url_db) {
556 DoAutocomplete(NULL, url_db, params.get());
557 matches_.clear();
558 PromoteMatchesIfNecessary(*params);
559 // NOTE: We don't reset |params| here since at least the |promote_type|
560 // field on it will be read by the second pass -- see comments in
561 // DoAutocomplete().
564 // Pass 2: Ask the history service to call us back on the history thread,
565 // where we can read the full on-disk DB.
566 if (input.want_asynchronous_matches()) {
567 done_ = false;
568 params_ = params.release(); // This object will be destroyed in
569 // QueryComplete() once we're done with it.
570 history_service->ScheduleAutocomplete(
571 base::Bind(&HistoryURLProvider::ExecuteWithDB, this, params_));
575 void HistoryURLProvider::Stop(bool clear_cached_results,
576 bool due_to_user_inactivity) {
577 done_ = true;
579 if (params_)
580 params_->cancel_flag.Set();
583 AutocompleteMatch HistoryURLProvider::SuggestExactInput(
584 const AutocompleteInput& input,
585 const GURL& destination_url,
586 bool trim_http) {
587 // The FormattedStringWithEquivalentMeaning() call below requires callers to
588 // be on the main thread.
589 DCHECK(thread_checker_.CalledOnValidThread());
591 AutocompleteMatch match(this, 0, false,
592 AutocompleteMatchType::URL_WHAT_YOU_TYPED);
594 if (destination_url.is_valid()) {
595 match.destination_url = destination_url;
597 // Trim off "http://" if the user didn't type it.
598 DCHECK(!trim_http ||
599 !AutocompleteInput::HasHTTPScheme(input.text()));
600 base::string16 display_string(
601 net::FormatUrl(destination_url, std::string(),
602 net::kFormatUrlOmitAll & ~net::kFormatUrlOmitHTTP,
603 net::UnescapeRule::SPACES, NULL, NULL, NULL));
604 const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0;
605 match.fill_into_edit =
606 AutocompleteInput::FormattedStringWithEquivalentMeaning(
607 destination_url, display_string, client()->GetSchemeClassifier());
608 // The what-you-typed match is generally only allowed to be default for
609 // URL inputs. (It's also allowed to be default for UNKNOWN inputs
610 // where the destination is a known intranet site. In this case,
611 // |allowed_to_be_default_match| is revised in FixupExactSuggestion().)
612 match.allowed_to_be_default_match =
613 (input.type() == metrics::OmniboxInputType::URL) ||
614 !OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs();
615 // NOTE: Don't set match.inline_autocompletion to something non-empty here;
616 // it's surprising and annoying.
618 // Try to highlight "innermost" match location. If we fix up "w" into
619 // "www.w.com", we want to highlight the fifth character, not the first.
620 // This relies on match.destination_url being the non-prefix-trimmed version
621 // of match.contents.
622 match.contents = display_string;
623 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(
624 base::UTF8ToUTF16(destination_url.spec()), input.text());
625 // It's possible for match.destination_url to not contain the user's input
626 // at all (so |best_prefix| is NULL), for example if the input is
627 // "view-source:x" and |destination_url| has an inserted "http://" in the
628 // middle.
629 if (best_prefix == NULL) {
630 AutocompleteMatch::ClassifyMatchInString(input.text(),
631 match.contents,
632 ACMatchClassification::URL,
633 &match.contents_class);
634 } else {
635 AutocompleteMatch::ClassifyLocationInString(
636 best_prefix->prefix.length() - offset, input.text().length(),
637 match.contents.length(), ACMatchClassification::URL,
638 &match.contents_class);
642 return match;
645 void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams* params,
646 history::HistoryBackend* backend,
647 history::URLDatabase* db) {
648 // We may get called with a NULL database if it couldn't be properly
649 // initialized.
650 if (!db) {
651 params->failed = true;
652 } else if (!params->cancel_flag.IsSet()) {
653 base::TimeTicks beginning_time = base::TimeTicks::Now();
655 DoAutocomplete(backend, db, params);
657 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime",
658 base::TimeTicks::Now() - beginning_time);
661 // Return the results (if any) to the main thread.
662 params->message_loop->task_runner()->PostTask(
663 FROM_HERE, base::Bind(&HistoryURLProvider::QueryComplete, this, params));
666 HistoryURLProvider::~HistoryURLProvider() {
667 // Note: This object can get leaked on shutdown if there are pending
668 // requests on the database (which hold a reference to us). Normally, these
669 // messages get flushed for each thread. We do a round trip from main, to
670 // history, back to main while holding a reference. If the main thread
671 // completes before the history thread, the message to delegate back to the
672 // main thread will not run and the reference will leak. Therefore, don't do
673 // anything on destruction.
676 // static
677 int HistoryURLProvider::CalculateRelevance(MatchType match_type,
678 int match_number) {
679 switch (match_type) {
680 case INLINE_AUTOCOMPLETE:
681 return kScoreForBestInlineableResult;
683 case UNVISITED_INTRANET:
684 return kScoreForUnvisitedIntranetResult;
686 case WHAT_YOU_TYPED:
687 return kScoreForWhatYouTypedResult;
689 default: // NORMAL
690 return kBaseScoreForNonInlineableResult + match_number;
694 // static
695 ACMatchClassifications HistoryURLProvider::ClassifyDescription(
696 const base::string16& input_text,
697 const base::string16& description) {
698 base::string16 clean_description =
699 bookmarks::CleanUpTitleForMatching(description);
700 TermMatches description_matches(SortAndDeoverlapMatches(
701 MatchTermInString(input_text, clean_description, 0)));
702 WordStarts description_word_starts;
703 String16VectorFromString16(clean_description, false,
704 &description_word_starts);
705 // If HistoryURL retrieves any matches (and hence we reach this code), we
706 // are guaranteed that the beginning of input_text must be a word break.
707 WordStarts offsets(1, 0u);
708 description_matches = ScoredHistoryMatch::FilterTermMatchesByWordStarts(
709 description_matches, offsets, description_word_starts, 0,
710 std::string::npos);
711 return SpansFromTermMatch(
712 description_matches, clean_description.length(), false);
715 void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend,
716 history::URLDatabase* db,
717 HistoryURLProviderParams* params) {
718 // Get the matching URLs from the DB.
719 params->matches.clear();
720 history::URLRows url_matches;
721 const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes();
722 for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end();
723 ++i) {
724 if (params->cancel_flag.IsSet())
725 return; // Canceled in the middle of a query, give up.
727 // We only need kMaxMatches results in the end, but before we get there we
728 // need to promote lower-quality matches that are prefixes of higher-quality
729 // matches, and remove lower-quality redirects. So we ask for more results
730 // than we need, of every prefix type, in hopes this will give us far more
731 // than enough to work with. CullRedirects() will then reduce the list to
732 // the best kMaxMatches results.
733 db->AutocompleteForPrefix(
734 base::UTF16ToUTF8(i->prefix + params->input.text()), kMaxMatches * 2,
735 !backend, &url_matches);
736 for (history::URLRows::const_iterator j(url_matches.begin());
737 j != url_matches.end(); ++j) {
738 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(
739 base::UTF8ToUTF16(j->url().spec()), base::string16());
740 DCHECK(best_prefix);
741 params->matches.push_back(history::HistoryMatch(
742 *j, i->prefix.length(), !i->num_components,
743 i->num_components >= best_prefix->num_components));
747 // Create sorted list of suggestions.
748 CullPoorMatches(params);
749 SortAndDedupMatches(&params->matches);
751 // Try to create a shorter suggestion from the best match.
752 // We consider the what you typed match eligible for display when it's
753 // navigable and there's a reasonable chance the user intended to do
754 // something other than search. We use a variety of heuristics to determine
755 // this, e.g. whether the user explicitly typed a scheme, or if omnibox
756 // searching has been disabled by policy. In the cases where we've parsed as
757 // UNKNOWN, we'll still show an accidental search infobar if need be.
758 VisitClassifier classifier(this, params->input, db);
759 params->have_what_you_typed_match =
760 (params->input.type() != metrics::OmniboxInputType::QUERY) &&
761 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) ||
762 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) ||
763 !params->trim_http ||
764 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0) ||
765 !params->default_search_provider);
766 const bool have_shorter_suggestion_suitable_for_inline_autocomplete =
767 PromoteOrCreateShorterSuggestion(db, params);
769 // Check whether what the user typed appears in history.
770 const bool can_check_history_for_exact_match =
771 // Checking what_you_typed_match.destination_url.is_valid() tells us
772 // whether SuggestExactInput() succeeded in constructing a valid match.
773 params->what_you_typed_match.destination_url.is_valid() &&
774 // Additionally, in the case where the user has typed "foo.com" and
775 // visited (but not typed) "foo/", and the input is "foo", the first pass
776 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the
777 // second pass can suggest the exact input as a better URL. Since we need
778 // both passes to agree, and since during the first pass there's no way to
779 // know about "foo/", ensure that if the promote type was set to
780 // FRONT_HISTORY_MATCH during the first pass, the second pass will not
781 // consider the exact suggestion to be in history and therefore will not
782 // suggest the exact input as a better match. (Note that during the first
783 // pass, this conditional will always succeed since |promote_type| is
784 // initialized to NEITHER.)
785 (params->promote_type != HistoryURLProviderParams::FRONT_HISTORY_MATCH);
786 params->exact_suggestion_is_in_history = can_check_history_for_exact_match &&
787 FixupExactSuggestion(db, classifier, params);
789 // If we succeeded in fixing up the exact match based on the user's history,
790 // we should treat it as the best match regardless of input type. If not,
791 // then we check whether there's an inline autocompletion we can create from
792 // this input, so we can promote that as the best match.
793 if (params->exact_suggestion_is_in_history) {
794 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH;
795 } else if (!params->matches.empty() &&
796 (have_shorter_suggestion_suitable_for_inline_autocomplete ||
797 CanPromoteMatchForInlineAutocomplete(params->matches[0]))) {
798 // Note that we promote this inline-autocompleted match even when
799 // params->prevent_inline_autocomplete is true. This is safe because in
800 // this case the match will be marked as "not allowed to be default", and
801 // a non-inlined match that is "allowed to be default" will be reordered
802 // above it by the controller/AutocompleteResult. We ensure there is such
803 // a match in two ways:
804 // * If params->have_what_you_typed_match is true, we force the
805 // what-you-typed match to be added in this case. See comments in
806 // PromoteMatchesIfNecessary().
807 // * Otherwise, we should have some sort of QUERY or UNKNOWN input that
808 // the SearchProvider will provide a defaultable WYT match for.
809 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH;
810 } else {
811 // Failed to promote any URLs. Use the What You Typed match, if we have it.
812 params->promote_type = params->have_what_you_typed_match ?
813 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH :
814 HistoryURLProviderParams::NEITHER;
817 const size_t max_results =
818 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0);
819 if (backend) {
820 // Remove redirects and trim list to size. We want to provide up to
821 // kMaxMatches results plus the What You Typed result, if it was added to
822 // params->matches above.
823 CullRedirects(backend, &params->matches, max_results);
824 } else if (params->matches.size() > max_results) {
825 // Simply trim the list to size.
826 params->matches.resize(max_results);
830 void HistoryURLProvider::PromoteMatchesIfNecessary(
831 const HistoryURLProviderParams& params) {
832 if (params.promote_type == HistoryURLProviderParams::NEITHER)
833 return;
834 if (params.promote_type == HistoryURLProviderParams::FRONT_HISTORY_MATCH) {
835 matches_.push_back(
836 HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE,
837 CalculateRelevance(INLINE_AUTOCOMPLETE, 0)));
839 // There are two cases where we need to add the what-you-typed-match:
840 // * If params.promote_type is WHAT_YOU_TYPED_MATCH, we're being explicitly
841 // directed to.
842 // * If params.have_what_you_typed_match is true, then params.promote_type
843 // can't be NEITHER (see code near the end of DoAutocomplete()), so if
844 // it's not WHAT_YOU_TYPED_MATCH, it must be FRONT_HISTORY_MATCH, and
845 // we'll have promoted the history match above. If
846 // params.prevent_inline_autocomplete is also true, then this match
847 // will be marked "not allowed to be default", and we need to add the
848 // what-you-typed match to ensure there's a legal default match for the
849 // controller/AutocompleteResult to promote. (If
850 // params.have_what_you_typed_match is false, the SearchProvider should
851 // take care of adding this defaultable match.)
852 if ((params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) ||
853 (params.prevent_inline_autocomplete &&
854 params.have_what_you_typed_match)) {
855 matches_.push_back(params.what_you_typed_match);
859 void HistoryURLProvider::QueryComplete(
860 HistoryURLProviderParams* params_gets_deleted) {
861 // Ensure |params_gets_deleted| gets deleted on exit.
862 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted);
864 // If the user hasn't already started another query, clear our member pointer
865 // so we can't write into deleted memory.
866 if (params_ == params_gets_deleted)
867 params_ = NULL;
869 // Don't send responses for queries that have been canceled.
870 if (params->cancel_flag.IsSet())
871 return; // Already set done_ when we canceled, no need to set it again.
873 // Don't modify |matches_| if the query failed, since it might have a default
874 // match in it, whereas |params->matches| will be empty.
875 if (!params->failed) {
876 matches_.clear();
877 PromoteMatchesIfNecessary(*params);
879 // Determine relevance of highest scoring match, if any.
880 int relevance = matches_.empty() ?
881 CalculateRelevance(NORMAL,
882 static_cast<int>(params->matches.size() - 1)) :
883 matches_[0].relevance;
885 // Convert the history matches to autocomplete matches. If we promoted the
886 // first match, skip over it.
887 const size_t first_match =
888 (params->exact_suggestion_is_in_history ||
889 (params->promote_type ==
890 HistoryURLProviderParams::FRONT_HISTORY_MATCH)) ? 1 : 0;
891 for (size_t i = first_match; i < params->matches.size(); ++i) {
892 // All matches score one less than the previous match.
893 --relevance;
894 // The experimental scoring must not change the top result's score.
895 if (!matches_.empty()) {
896 relevance = CalculateRelevanceScoreUsingScoringParams(
897 params->matches[i], relevance, scoring_params_);
899 matches_.push_back(HistoryMatchToACMatch(*params, i, NORMAL, relevance));
903 done_ = true;
904 listener_->OnProviderUpdate(true);
907 bool HistoryURLProvider::FixupExactSuggestion(
908 history::URLDatabase* db,
909 const VisitClassifier& classifier,
910 HistoryURLProviderParams* params) const {
911 MatchType type = INLINE_AUTOCOMPLETE;
912 switch (classifier.type()) {
913 case VisitClassifier::INVALID:
914 return false;
915 case VisitClassifier::UNVISITED_INTRANET:
916 type = UNVISITED_INTRANET;
917 break;
918 default:
919 DCHECK_EQ(VisitClassifier::VISITED, classifier.type());
920 // We have data for this match, use it.
921 params->what_you_typed_match.deletable = true;
922 params->what_you_typed_match.description = classifier.url_row().title();
923 RecordAdditionalInfoFromUrlRow(classifier.url_row(),
924 &params->what_you_typed_match);
925 params->what_you_typed_match.description_class = ClassifyDescription(
926 params->input.text(), params->what_you_typed_match.description);
927 if (!classifier.url_row().typed_count()) {
928 // If we reach here, we must be in the second pass, and we must not have
929 // this row's data available during the first pass. That means we
930 // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to
931 // maintain the ordering between passes consistent, we need to score it
932 // the same way here.
933 type = CanFindIntranetURL(db, params->input) ?
934 UNVISITED_INTRANET : WHAT_YOU_TYPED;
936 break;
939 if (OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs()) {
940 const GURL& url = params->what_you_typed_match.destination_url;
941 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
942 // If the what-you-typed result looks like a single word (which can be
943 // interpreted as an intranet address) followed by a pound sign ("#"),
944 // leave the score for the url-what-you-typed result as is and also
945 // don't mark it as allowed to be the default match. It will likely be
946 // outscored by a search query from the SearchProvider or, if not, the
947 // search query default match will in any case--which is allowed to be the
948 // default match--will be reordered to be first. This test fixes cases
949 // such as "c#" and "c# foo" where the user has visited an intranet site
950 // "c". We want the search-what-you-typed score to beat the
951 // URL-what-you-typed score in this case. Most of the below test tries to
952 // make sure that this code does not trigger if the user did anything to
953 // indicate the desired match is a URL. For instance, "c/# foo" will not
954 // pass the test because that will be classified as input type URL. The
955 // parsed.CountCharactersBefore() in the test looks for the presence of a
956 // reference fragment in the URL by checking whether the position differs
957 // included the delimiter (pound sign) versus not including the delimiter.
958 // (One cannot simply check url.ref() because it will not distinguish
959 // between the input "c" and the input "c#", both of which will have empty
960 // reference fragments.)
961 if ((type == UNVISITED_INTRANET) &&
962 (params->input.type() != metrics::OmniboxInputType::URL) &&
963 url.username().empty() && url.password().empty() &&
964 url.port().empty() && (url.path() == "/") && url.query().empty() &&
965 (parsed.CountCharactersBefore(url::Parsed::REF, true) !=
966 parsed.CountCharactersBefore(url::Parsed::REF, false))) {
967 return false;
971 params->what_you_typed_match.allowed_to_be_default_match = true;
972 params->what_you_typed_match.relevance = CalculateRelevance(type, 0);
974 // If there are any other matches, then don't promote this match here, in
975 // hopes the caller will be able to inline autocomplete a better suggestion.
976 // DoAutocomplete() will fall back on this match if inline autocompletion
977 // fails. This matches how we react to never-visited URL inputs in the non-
978 // intranet case.
979 if (type == UNVISITED_INTRANET && !params->matches.empty())
980 return false;
982 // Put it on the front of the HistoryMatches for redirect culling.
983 CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false,
984 &params->matches, true, true);
985 return true;
988 bool HistoryURLProvider::CanFindIntranetURL(
989 history::URLDatabase* db,
990 const AutocompleteInput& input) const {
991 // Normally passing the first two conditions below ought to guarantee the
992 // third condition, but because FixupUserInput() can run and modify the
993 // input's text and parts between Parse() and here, it seems better to be
994 // paranoid and check.
995 if ((input.type() != metrics::OmniboxInputType::UNKNOWN) ||
996 !base::LowerCaseEqualsASCII(input.scheme(), url::kHttpScheme) ||
997 !input.parts().host.is_nonempty())
998 return false;
999 const std::string host(base::UTF16ToUTF8(
1000 input.text().substr(input.parts().host.begin, input.parts().host.len)));
1001 const size_t registry_length =
1002 net::registry_controlled_domains::GetRegistryLength(
1003 host,
1004 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
1005 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
1006 return registry_length == 0 && db->IsTypedHost(host);
1009 bool HistoryURLProvider::PromoteOrCreateShorterSuggestion(
1010 history::URLDatabase* db,
1011 HistoryURLProviderParams* params) {
1012 if (params->matches.empty())
1013 return false; // No matches, nothing to do.
1015 // Determine the base URL from which to search, and whether that URL could
1016 // itself be added as a match. We can add the base iff it's not "effectively
1017 // the same" as any "what you typed" match.
1018 const history::HistoryMatch& match = params->matches[0];
1019 GURL search_base = ConvertToHostOnly(match, params->input.text());
1020 bool can_add_search_base_to_matches = !params->have_what_you_typed_match;
1021 if (search_base.is_empty()) {
1022 // Search from what the user typed when we couldn't reduce the best match
1023 // to a host. Careful: use a substring of |match| here, rather than the
1024 // first match in |params|, because they might have different prefixes. If
1025 // the user typed "google.com", params->what_you_typed_match will hold
1026 // "http://google.com/", but |match| might begin with
1027 // "http://www.google.com/".
1028 // TODO: this should be cleaned up, and is probably incorrect for IDN.
1029 std::string new_match = match.url_info.url().possibly_invalid_spec().
1030 substr(0, match.input_location + params->input.text().length());
1031 search_base = GURL(new_match);
1032 if (search_base.is_empty())
1033 return false; // Can't construct a URL from which to start a search.
1034 } else if (!can_add_search_base_to_matches) {
1035 can_add_search_base_to_matches =
1036 (search_base != params->what_you_typed_match.destination_url);
1038 if (search_base == match.url_info.url())
1039 return false; // Couldn't shorten |match|, so no URLs to search over.
1041 // Search the DB for short URLs between our base and |match|.
1042 history::URLRow info(search_base);
1043 bool promote = true;
1044 // A short URL is only worth suggesting if it's been visited at least a third
1045 // as often as the longer URL.
1046 const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1;
1047 // For stability between the in-memory and on-disk autocomplete passes, when
1048 // the long URL has been typed before, only suggest shorter URLs that have
1049 // also been typed. Otherwise, the on-disk pass could suggest a shorter URL
1050 // (which hasn't been typed) that the in-memory pass doesn't know about,
1051 // thereby making the top match, and thus the behavior of inline
1052 // autocomplete, unstable.
1053 const int min_typed_count = match.url_info.typed_count() ? 1 : 0;
1054 if (!db->FindShortestURLFromBase(search_base.possibly_invalid_spec(),
1055 match.url_info.url().possibly_invalid_spec(), min_visit_count,
1056 min_typed_count, can_add_search_base_to_matches, &info)) {
1057 if (!can_add_search_base_to_matches)
1058 return false; // Couldn't find anything and can't add the search base.
1060 // Try to get info on the search base itself. Promote it to the top if the
1061 // original best match isn't good enough to autocomplete.
1062 db->GetRowForURL(search_base, &info);
1063 promote = match.url_info.typed_count() <= 1;
1066 // Promote or add the desired URL to the list of matches.
1067 const bool ensure_can_inline =
1068 promote && CanPromoteMatchForInlineAutocomplete(match);
1069 return CreateOrPromoteMatch(info, match.input_location, match.match_in_scheme,
1070 &params->matches, true, promote) &&
1071 ensure_can_inline;
1074 void HistoryURLProvider::CullPoorMatches(
1075 HistoryURLProviderParams* params) const {
1076 const base::Time& threshold(history::AutocompleteAgeThreshold());
1077 for (history::HistoryMatches::iterator i(params->matches.begin());
1078 i != params->matches.end(); ) {
1079 if (RowQualifiesAsSignificant(i->url_info, threshold) &&
1080 (!params->default_search_provider ||
1081 !params->default_search_provider->IsSearchURL(
1082 i->url_info.url(), *params->search_terms_data))) {
1083 ++i;
1084 } else {
1085 i = params->matches.erase(i);
1090 void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend,
1091 history::HistoryMatches* matches,
1092 size_t max_results) const {
1093 for (size_t source = 0;
1094 (source < matches->size()) && (source < max_results); ) {
1095 const GURL& url = (*matches)[source].url_info.url();
1096 // TODO(brettw) this should go away when everything uses GURL.
1097 history::RedirectList redirects;
1098 backend->QueryRedirectsFrom(url, &redirects);
1099 if (!redirects.empty()) {
1100 // Remove all but the first occurrence of any of these redirects in the
1101 // search results. We also must add the URL we queried for, since it may
1102 // not be the first match and we'd want to remove it.
1104 // For example, when A redirects to B and our matches are [A, X, B],
1105 // we'll get B as the redirects from, and we want to remove the second
1106 // item of that pair, removing B. If A redirects to B and our matches are
1107 // [B, X, A], we'll want to remove A instead.
1108 redirects.push_back(url);
1109 source = RemoveSubsequentMatchesOf(matches, source, redirects);
1110 } else {
1111 // Advance to next item.
1112 source++;
1116 if (matches->size() > max_results)
1117 matches->resize(max_results);
1120 size_t HistoryURLProvider::RemoveSubsequentMatchesOf(
1121 history::HistoryMatches* matches,
1122 size_t source_index,
1123 const std::vector<GURL>& remove) const {
1124 size_t next_index = source_index + 1; // return value = item after source
1126 // Find the first occurrence of any URL in the redirect chain. We want to
1127 // keep this one since it is rated the highest.
1128 history::HistoryMatches::iterator first(std::find_first_of(
1129 matches->begin(), matches->end(), remove.begin(), remove.end(),
1130 history::HistoryMatch::EqualsGURL));
1131 DCHECK(first != matches->end()) << "We should have always found at least the "
1132 "original URL.";
1134 // Find any following occurrences of any URL in the redirect chain, these
1135 // should be deleted.
1136 for (history::HistoryMatches::iterator next(std::find_first_of(first + 1,
1137 matches->end(), remove.begin(), remove.end(),
1138 history::HistoryMatch::EqualsGURL));
1139 next != matches->end(); next = std::find_first_of(next, matches->end(),
1140 remove.begin(), remove.end(), history::HistoryMatch::EqualsGURL)) {
1141 // Remove this item. When we remove an item before the source index, we
1142 // need to shift it to the right and remember that so we can return it.
1143 next = matches->erase(next);
1144 if (static_cast<size_t>(next - matches->begin()) < next_index)
1145 --next_index;
1147 return next_index;
1150 AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch(
1151 const HistoryURLProviderParams& params,
1152 size_t match_number,
1153 MatchType match_type,
1154 int relevance) {
1155 // The FormattedStringWithEquivalentMeaning() call below requires callers to
1156 // be on the main thread.
1157 DCHECK(thread_checker_.CalledOnValidThread());
1159 const history::HistoryMatch& history_match = params.matches[match_number];
1160 const history::URLRow& info = history_match.url_info;
1161 AutocompleteMatch match(this, relevance,
1162 !!info.visit_count(), AutocompleteMatchType::HISTORY_URL);
1163 match.typed_count = info.typed_count();
1164 match.destination_url = info.url();
1165 DCHECK(match.destination_url.is_valid());
1166 size_t inline_autocomplete_offset =
1167 history_match.input_location + params.input.text().length();
1168 std::string languages = (match_type == WHAT_YOU_TYPED) ?
1169 std::string() : params.languages;
1170 const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
1171 ~((params.trim_http && !history_match.match_in_scheme) ?
1172 0 : net::kFormatUrlOmitHTTP);
1173 match.fill_into_edit =
1174 AutocompleteInput::FormattedStringWithEquivalentMeaning(
1175 info.url(), net::FormatUrl(info.url(), languages, format_types,
1176 net::UnescapeRule::SPACES, NULL, NULL,
1177 &inline_autocomplete_offset),
1178 client()->GetSchemeClassifier());
1179 if (!params.prevent_inline_autocomplete &&
1180 (inline_autocomplete_offset != base::string16::npos)) {
1181 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1182 match.inline_autocompletion =
1183 match.fill_into_edit.substr(inline_autocomplete_offset);
1185 // The latter part of the test effectively asks "is the inline completion
1186 // empty?" (i.e., is this match effectively the what-you-typed match?).
1187 match.allowed_to_be_default_match = !params.prevent_inline_autocomplete ||
1188 ((inline_autocomplete_offset != base::string16::npos) &&
1189 (inline_autocomplete_offset >= match.fill_into_edit.length()));
1191 size_t match_start = history_match.input_location;
1192 match.contents = net::FormatUrl(info.url(), languages,
1193 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1194 if ((match_start != base::string16::npos) &&
1195 (inline_autocomplete_offset != base::string16::npos) &&
1196 (inline_autocomplete_offset != match_start)) {
1197 DCHECK(inline_autocomplete_offset > match_start);
1198 AutocompleteMatch::ClassifyLocationInString(match_start,
1199 inline_autocomplete_offset - match_start, match.contents.length(),
1200 ACMatchClassification::URL, &match.contents_class);
1201 } else {
1202 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
1203 match.contents.length(), ACMatchClassification::URL,
1204 &match.contents_class);
1206 match.description = info.title();
1207 match.description_class =
1208 ClassifyDescription(params.input.text(), match.description);
1209 RecordAdditionalInfoFromUrlRow(info, &match);
1210 return match;