| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 suggest_results_pending_(0), | 252 suggest_results_pending_(0), |
| 253 instant_finalized_(false), | 253 instant_finalized_(false), |
| 254 field_trial_triggered_(false), | 254 field_trial_triggered_(false), |
| 255 field_trial_triggered_in_session_(false), | 255 field_trial_triggered_in_session_(false), |
| 256 suppress_search_suggestions_(false), | 256 suppress_search_suggestions_(false), |
| 257 omnibox_start_margin_(-1) { | 257 omnibox_start_margin_(-1) { |
| 258 } | 258 } |
| 259 | 259 |
| 260 // static | 260 // static |
| 261 AutocompleteMatch SearchProvider::CreateSearchSuggestion( | 261 AutocompleteMatch SearchProvider::CreateSearchSuggestion( |
| 262 Profile* profile, | |
| 263 AutocompleteProvider* autocomplete_provider, | 262 AutocompleteProvider* autocomplete_provider, |
| 264 const AutocompleteInput& input, | 263 int relevance, |
| 264 AutocompleteMatch::Type type, |
| 265 const TemplateURL* template_url, |
| 265 const string16& query_string, | 266 const string16& query_string, |
| 266 const string16& input_text, | 267 const string16& input_text, |
| 267 int relevance, | 268 const AutocompleteInput& input, |
| 268 AutocompleteMatch::Type type, | 269 bool is_keyword, |
| 269 int accepted_suggestion, | 270 int accepted_suggestion, |
| 270 bool is_keyword, | 271 int omnibox_start_margin, |
| 271 const string16& keyword, | 272 bool append_extra_query_params) { |
| 272 int omnibox_start_margin) { | |
| 273 AutocompleteMatch match(autocomplete_provider, relevance, false, type); | 273 AutocompleteMatch match(autocomplete_provider, relevance, false, type); |
| 274 | 274 |
| 275 // Bail out now if we don't actually have a valid provider. | 275 if (!template_url) |
| 276 match.keyword = keyword; | |
| 277 const TemplateURL* provider_url = match.GetTemplateURL(profile, false); | |
| 278 if (provider_url == NULL) | |
| 279 return match; | 276 return match; |
| 277 match.keyword = template_url->keyword(); |
| 280 | 278 |
| 281 match.contents.assign(query_string); | 279 match.contents.assign(query_string); |
| 282 // We do intra-string highlighting for suggestions - the suggested segment | 280 // We do intra-string highlighting for suggestions - the suggested segment |
| 283 // will be highlighted, e.g. for input_text = "you" the suggestion may be | 281 // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 284 // "youtube", so we'll bold the "tube" section: you*tube*. | 282 // "youtube", so we'll bold the "tube" section: you*tube*. |
| 285 if (input_text != query_string) { | 283 if (input_text != query_string) { |
| 286 size_t input_position = match.contents.find(input_text); | 284 size_t input_position = match.contents.find(input_text); |
| 287 if (input_position == string16::npos) { | 285 if (input_position == string16::npos) { |
| 288 // The input text is not a substring of the query string, e.g. input | 286 // The input text is not a substring of the query string, e.g. input |
| 289 // text is "slasdot" and the query string is "slashdot", so we bold the | 287 // text is "slasdot" and the query string is "slashdot", so we bold the |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 match.fill_into_edit.assign(ASCIIToUTF16("?")); | 323 match.fill_into_edit.assign(ASCIIToUTF16("?")); |
| 326 if (is_keyword) | 324 if (is_keyword) |
| 327 match.fill_into_edit.append(match.keyword + char16(' ')); | 325 match.fill_into_edit.append(match.keyword + char16(' ')); |
| 328 if (!input.prevent_inline_autocomplete() && | 326 if (!input.prevent_inline_autocomplete() && |
| 329 StartsWith(query_string, input_text, false)) { | 327 StartsWith(query_string, input_text, false)) { |
| 330 match.inline_autocomplete_offset = | 328 match.inline_autocomplete_offset = |
| 331 match.fill_into_edit.length() + input_text.length(); | 329 match.fill_into_edit.length() + input_text.length(); |
| 332 } | 330 } |
| 333 match.fill_into_edit.append(query_string); | 331 match.fill_into_edit.append(query_string); |
| 334 | 332 |
| 335 const TemplateURLRef& search_url = provider_url->url_ref(); | 333 const TemplateURLRef& search_url = template_url->url_ref(); |
| 336 DCHECK(search_url.SupportsReplacement()); | 334 DCHECK(search_url.SupportsReplacement()); |
| 337 match.search_terms_args.reset( | 335 match.search_terms_args.reset( |
| 338 new TemplateURLRef::SearchTermsArgs(query_string)); | 336 new TemplateURLRef::SearchTermsArgs(query_string)); |
| 339 match.search_terms_args->original_query = input_text; | 337 match.search_terms_args->original_query = input_text; |
| 340 match.search_terms_args->accepted_suggestion = accepted_suggestion; | 338 match.search_terms_args->accepted_suggestion = accepted_suggestion; |
| 341 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; | 339 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; |
| 340 match.search_terms_args->append_extra_query_params = |
| 341 append_extra_query_params; |
| 342 // This is the destination URL sans assisted query stats. This must be set | 342 // This is the destination URL sans assisted query stats. This must be set |
| 343 // so the AutocompleteController can properly de-dupe; the controller will | 343 // so the AutocompleteController can properly de-dupe; the controller will |
| 344 // eventually overwrite it before it reaches the user. | 344 // eventually overwrite it before it reaches the user. |
| 345 match.destination_url = | 345 match.destination_url = |
| 346 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); | 346 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); |
| 347 | 347 |
| 348 // Search results don't look like URLs. | 348 // Search results don't look like URLs. |
| 349 match.transition = is_keyword ? | 349 match.transition = is_keyword ? |
| 350 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; | 350 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; |
| 351 | 351 |
| (...skipping 1178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1530 } | 1530 } |
| 1531 | 1531 |
| 1532 void SearchProvider::AddMatchToMap(const string16& query_string, | 1532 void SearchProvider::AddMatchToMap(const string16& query_string, |
| 1533 const string16& input_text, | 1533 const string16& input_text, |
| 1534 int relevance, | 1534 int relevance, |
| 1535 bool relevance_from_server, | 1535 bool relevance_from_server, |
| 1536 AutocompleteMatch::Type type, | 1536 AutocompleteMatch::Type type, |
| 1537 int accepted_suggestion, | 1537 int accepted_suggestion, |
| 1538 bool is_keyword, | 1538 bool is_keyword, |
| 1539 MatchMap* map) { | 1539 MatchMap* map) { |
| 1540 const string16& keyword = is_keyword ? | 1540 const TemplateURL* template_url = is_keyword ? |
| 1541 providers_.keyword_provider() : providers_.default_provider(); | 1541 providers_.GetKeywordProviderURL() : providers_.GetDefaultProviderURL(); |
| 1542 AutocompleteMatch match = CreateSearchSuggestion(profile_, this, input_, | 1542 AutocompleteMatch match = CreateSearchSuggestion(this, relevance, type, |
| 1543 query_string, input_text, relevance, type, accepted_suggestion, | 1543 template_url, query_string, input_text, input_, is_keyword, |
| 1544 is_keyword, keyword, omnibox_start_margin_); | 1544 accepted_suggestion, omnibox_start_margin_, |
| 1545 !is_keyword || providers_.default_provider().empty()); |
| 1545 if (!match.destination_url.is_valid()) | 1546 if (!match.destination_url.is_valid()) |
| 1546 return; | 1547 return; |
| 1547 match.RecordAdditionalInfo(kRelevanceFromServerKey, | 1548 match.RecordAdditionalInfo(kRelevanceFromServerKey, |
| 1548 relevance_from_server ? kTrue : kFalse); | 1549 relevance_from_server ? kTrue : kFalse); |
| 1549 | 1550 |
| 1550 // Try to add |match| to |map|. If a match for |query_string| is already in | 1551 // Try to add |match| to |map|. If a match for |query_string| is already in |
| 1551 // |map|, replace it if |match| is more relevant. | 1552 // |map|, replace it if |match| is more relevant. |
| 1552 // NOTE: Keep this ToLower() call in sync with url_database.cc. | 1553 // NOTE: Keep this ToLower() call in sync with url_database.cc. |
| 1553 const std::pair<MatchMap::iterator, bool> i( | 1554 const std::pair<MatchMap::iterator, bool> i( |
| 1554 map->insert(std::make_pair(base::i18n::ToLower(query_string), match))); | 1555 map->insert(std::make_pair(base::i18n::ToLower(query_string), match))); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1669 it->set_relevance_from_server(relevance_from_server); | 1670 it->set_relevance_from_server(relevance_from_server); |
| 1670 } | 1671 } |
| 1671 } | 1672 } |
| 1672 | 1673 |
| 1673 void SearchProvider::UpdateDone() { | 1674 void SearchProvider::UpdateDone() { |
| 1674 // We're done when the timer isn't running, there are no suggest queries | 1675 // We're done when the timer isn't running, there are no suggest queries |
| 1675 // pending, and we're not waiting on Instant. | 1676 // pending, and we're not waiting on Instant. |
| 1676 done_ = IsNonInstantSearchDone() && | 1677 done_ = IsNonInstantSearchDone() && |
| 1677 (instant_finalized_ || !chrome::IsInstantEnabled(profile_)); | 1678 (instant_finalized_ || !chrome::IsInstantEnabled(profile_)); |
| 1678 } | 1679 } |
| OLD | NEW |