OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/base_search_provider.h" | 5 #include "chrome/browser/autocomplete/base_search_provider.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 field_trial_triggered_(false), | 103 field_trial_triggered_(false), |
104 field_trial_triggered_in_session_(false) { | 104 field_trial_triggered_in_session_(false) { |
105 } | 105 } |
106 | 106 |
107 // static | 107 // static |
108 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { | 108 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
109 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; | 109 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
110 } | 110 } |
111 | 111 |
112 // static | 112 // static |
113 // This wrapper uses a number of default values that may or may not be | |
msw
2014/08/15 19:06:06
This comment is valuable, but belongs integrated w
Mark P
2014/08/15 19:14:49
Done.
| |
114 // appropriate for your needs. For instance, it assumes that if this match | |
115 // is from a keyword provider than the user is in keyword mode. | |
116 // See warning by this function's declaration in the header file. | |
113 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( | 117 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( |
114 const base::string16& suggestion, | 118 const base::string16& suggestion, |
115 AutocompleteMatchType::Type type, | 119 AutocompleteMatchType::Type type, |
116 bool from_keyword_provider, | 120 bool from_keyword_provider, |
117 const TemplateURL* template_url, | 121 const TemplateURL* template_url, |
118 const SearchTermsData& search_terms_data) { | 122 const SearchTermsData& search_terms_data) { |
119 return CreateSearchSuggestion( | 123 return CreateSearchSuggestion( |
120 NULL, AutocompleteInput(), SearchSuggestionParser::SuggestResult( | 124 NULL, AutocompleteInput(), from_keyword_provider, |
125 SearchSuggestionParser::SuggestResult( | |
121 suggestion, type, suggestion, base::string16(), base::string16(), | 126 suggestion, type, suggestion, base::string16(), base::string16(), |
122 base::string16(), base::string16(), std::string(), std::string(), | 127 base::string16(), base::string16(), std::string(), std::string(), |
123 from_keyword_provider, 0, false, false, base::string16()), | 128 from_keyword_provider, 0, false, false, base::string16()), |
124 template_url, search_terms_data, 0, false); | 129 template_url, search_terms_data, 0, false); |
125 } | 130 } |
126 | 131 |
127 void BaseSearchProvider::Stop(bool clear_cached_results) { | 132 void BaseSearchProvider::Stop(bool clear_cached_results) { |
128 StopSuggest(); | 133 StopSuggest(); |
129 done_ = true; | 134 done_ = true; |
130 | 135 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 match->RecordAdditionalInfo(BaseSearchProvider::kDeletionUrlKey, | 206 match->RecordAdditionalInfo(BaseSearchProvider::kDeletionUrlKey, |
202 url.spec()); | 207 url.spec()); |
203 match->deletable = true; | 208 match->deletable = true; |
204 } | 209 } |
205 } | 210 } |
206 | 211 |
207 // static | 212 // static |
208 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( | 213 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( |
209 AutocompleteProvider* autocomplete_provider, | 214 AutocompleteProvider* autocomplete_provider, |
210 const AutocompleteInput& input, | 215 const AutocompleteInput& input, |
216 const bool in_keyword_mode, | |
211 const SearchSuggestionParser::SuggestResult& suggestion, | 217 const SearchSuggestionParser::SuggestResult& suggestion, |
212 const TemplateURL* template_url, | 218 const TemplateURL* template_url, |
213 const SearchTermsData& search_terms_data, | 219 const SearchTermsData& search_terms_data, |
214 int accepted_suggestion, | 220 int accepted_suggestion, |
215 bool append_extra_query_params) { | 221 bool append_extra_query_params) { |
216 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, | 222 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, |
217 suggestion.type()); | 223 suggestion.type()); |
218 | 224 |
219 if (!template_url) | 225 if (!template_url) |
220 return match; | 226 return match; |
(...skipping 12 matching lines...) Expand all Loading... | |
233 kACMatchPropertyContentsStartIndex, | 239 kACMatchPropertyContentsStartIndex, |
234 static_cast<int>( | 240 static_cast<int>( |
235 suggestion.suggestion().length() - match.contents.length())); | 241 suggestion.suggestion().length() - match.contents.length())); |
236 } | 242 } |
237 | 243 |
238 if (!suggestion.annotation().empty()) | 244 if (!suggestion.annotation().empty()) |
239 match.description = suggestion.annotation(); | 245 match.description = suggestion.annotation(); |
240 | 246 |
241 // suggestion.match_contents() should have already been collapsed. | 247 // suggestion.match_contents() should have already been collapsed. |
242 match.allowed_to_be_default_match = | 248 match.allowed_to_be_default_match = |
249 (!in_keyword_mode || suggestion.from_keyword_provider()) && | |
243 (base::CollapseWhitespace(input.text(), false) == | 250 (base::CollapseWhitespace(input.text(), false) == |
244 suggestion.match_contents()); | 251 suggestion.match_contents()); |
245 | 252 |
246 // When the user forced a query, we need to make sure all the fill_into_edit | 253 // When the user forced a query, we need to make sure all the fill_into_edit |
247 // values preserve that property. Otherwise, if the user starts editing a | 254 // values preserve that property. Otherwise, if the user starts editing a |
248 // suggestion, non-Search results will suddenly appear. | 255 // suggestion, non-Search results will suddenly appear. |
249 if (input.type() == metrics::OmniboxInputType::FORCED_QUERY) | 256 if (input.type() == metrics::OmniboxInputType::FORCED_QUERY) |
250 match.fill_into_edit.assign(base::ASCIIToUTF16("?")); | 257 match.fill_into_edit.assign(base::ASCIIToUTF16("?")); |
251 if (suggestion.from_keyword_provider()) | 258 if (suggestion.from_keyword_provider()) |
252 match.fill_into_edit.append(match.keyword + base::char16(' ')); | 259 match.fill_into_edit.append(match.keyword + base::char16(' ')); |
253 if (!input.prevent_inline_autocomplete() && | 260 if (!input.prevent_inline_autocomplete() && |
261 (!in_keyword_mode || suggestion.from_keyword_provider()) && | |
254 StartsWith(suggestion.suggestion(), input.text(), false)) { | 262 StartsWith(suggestion.suggestion(), input.text(), false)) { |
255 match.inline_autocompletion = | 263 match.inline_autocompletion = |
256 suggestion.suggestion().substr(input.text().length()); | 264 suggestion.suggestion().substr(input.text().length()); |
257 match.allowed_to_be_default_match = true; | 265 match.allowed_to_be_default_match = true; |
258 } | 266 } |
259 match.fill_into_edit.append(suggestion.suggestion()); | 267 match.fill_into_edit.append(suggestion.suggestion()); |
260 | 268 |
261 const TemplateURLRef& search_url = template_url->url_ref(); | 269 const TemplateURLRef& search_url = template_url->url_ref(); |
262 DCHECK(search_url.SupportsReplacement(search_terms_data)); | 270 DCHECK(search_url.SupportsReplacement(search_terms_data)); |
263 match.search_terms_args.reset( | 271 match.search_terms_args.reset( |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 return false; | 371 return false; |
364 | 372 |
365 return true; | 373 return true; |
366 } | 374 } |
367 | 375 |
368 void BaseSearchProvider::AddMatchToMap( | 376 void BaseSearchProvider::AddMatchToMap( |
369 const SearchSuggestionParser::SuggestResult& result, | 377 const SearchSuggestionParser::SuggestResult& result, |
370 const std::string& metadata, | 378 const std::string& metadata, |
371 int accepted_suggestion, | 379 int accepted_suggestion, |
372 bool mark_as_deletable, | 380 bool mark_as_deletable, |
381 bool in_keyword_mode, | |
373 MatchMap* map) { | 382 MatchMap* map) { |
374 AutocompleteMatch match = CreateSearchSuggestion( | 383 AutocompleteMatch match = CreateSearchSuggestion( |
375 this, GetInput(result.from_keyword_provider()), result, | 384 this, GetInput(result.from_keyword_provider()), in_keyword_mode, result, |
376 GetTemplateURL(result.from_keyword_provider()), | 385 GetTemplateURL(result.from_keyword_provider()), |
377 template_url_service_->search_terms_data(), accepted_suggestion, | 386 template_url_service_->search_terms_data(), accepted_suggestion, |
378 ShouldAppendExtraParams(result)); | 387 ShouldAppendExtraParams(result)); |
379 if (!match.destination_url.is_valid()) | 388 if (!match.destination_url.is_valid()) |
380 return; | 389 return; |
381 match.search_terms_args->bookmark_bar_pinned = | 390 match.search_terms_args->bookmark_bar_pinned = |
382 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | 391 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
383 match.RecordAdditionalInfo(kRelevanceFromServerKey, | 392 match.RecordAdditionalInfo(kRelevanceFromServerKey, |
384 result.relevance_from_server() ? kTrue : kFalse); | 393 result.relevance_from_server() ? kTrue : kFalse); |
385 match.RecordAdditionalInfo(kShouldPrefetchKey, | 394 match.RecordAdditionalInfo(kShouldPrefetchKey, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 } | 496 } |
488 | 497 |
489 void BaseSearchProvider::OnDeletionComplete( | 498 void BaseSearchProvider::OnDeletionComplete( |
490 bool success, SuggestionDeletionHandler* handler) { | 499 bool success, SuggestionDeletionHandler* handler) { |
491 RecordDeletionResult(success); | 500 RecordDeletionResult(success); |
492 SuggestionDeletionHandlers::iterator it = std::find( | 501 SuggestionDeletionHandlers::iterator it = std::find( |
493 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 502 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
494 DCHECK(it != deletion_handlers_.end()); | 503 DCHECK(it != deletion_handlers_.end()); |
495 deletion_handlers_.erase(it); | 504 deletion_handlers_.erase(it); |
496 } | 505 } |
OLD | NEW |