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/base64.h" | 10 #include "base/base64.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 | 125 |
126 | 126 |
127 // SearchProvider ------------------------------------------------------------- | 127 // SearchProvider ------------------------------------------------------------- |
128 | 128 |
129 // static | 129 // static |
130 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; | 130 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
131 | 131 |
132 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, | 132 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
133 TemplateURLService* template_url_service, | 133 TemplateURLService* template_url_service, |
134 Profile* profile) | 134 Profile* profile) |
135 : BaseSearchProvider(listener, template_url_service, profile, | 135 : BaseSearchProvider(template_url_service, profile, |
136 AutocompleteProvider::TYPE_SEARCH), | 136 AutocompleteProvider::TYPE_SEARCH), |
137 listener_(listener), | |
138 suggest_results_pending_(0), | |
137 providers_(template_url_service) { | 139 providers_(template_url_service) { |
138 } | 140 } |
139 | 141 |
140 // static | 142 // static |
141 std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) { | 143 std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) { |
142 return match.GetAdditionalInfo(kSuggestMetadataKey); | 144 return match.GetAdditionalInfo(kSuggestMetadataKey); |
143 } | 145 } |
144 | 146 |
145 void SearchProvider::ResetSession() { | 147 void SearchProvider::ResetSession() { |
146 field_trial_triggered_in_session_ = false; | 148 field_trial_triggered_in_session_ = false; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 | 296 |
295 const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const { | 297 const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const { |
296 return is_keyword ? providers_.GetKeywordProviderURL() | 298 return is_keyword ? providers_.GetKeywordProviderURL() |
297 : providers_.GetDefaultProviderURL(); | 299 : providers_.GetDefaultProviderURL(); |
298 } | 300 } |
299 | 301 |
300 const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const { | 302 const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const { |
301 return is_keyword ? keyword_input_ : input_; | 303 return is_keyword ? keyword_input_ : input_; |
302 } | 304 } |
303 | 305 |
304 SearchSuggestionParser::Results* SearchProvider::GetResultsToFill( | |
305 bool is_keyword) { | |
306 return is_keyword ? &keyword_results_ : &default_results_; | |
307 } | |
308 | |
309 bool SearchProvider::ShouldAppendExtraParams( | 306 bool SearchProvider::ShouldAppendExtraParams( |
310 const SearchSuggestionParser::SuggestResult& result) const { | 307 const SearchSuggestionParser::SuggestResult& result) const { |
311 return !result.from_keyword_provider() || | 308 return !result.from_keyword_provider() || |
312 providers_.default_provider().empty(); | 309 providers_.default_provider().empty(); |
313 } | 310 } |
314 | 311 |
315 void SearchProvider::StopSuggest() { | 312 void SearchProvider::StopSuggest() { |
316 // Increment the appropriate field in the histogram by the number of | 313 // Increment the appropriate field in the histogram by the number of |
317 // pending requests that were invalidated. | 314 // pending requests that were invalidated. |
318 for (int i = 0; i < suggest_results_pending_; ++i) | 315 for (int i = 0; i < suggest_results_pending_; ++i) |
319 LogOmniboxSuggestRequest(REQUEST_INVALIDATED); | 316 LogOmniboxSuggestRequest(REQUEST_INVALIDATED); |
320 suggest_results_pending_ = 0; | 317 suggest_results_pending_ = 0; |
321 timer_.Stop(); | 318 timer_.Stop(); |
322 // Stop any in-progress URL fetches. | 319 // Stop any in-progress URL fetches. |
323 keyword_fetcher_.reset(); | 320 keyword_fetcher_.reset(); |
324 default_fetcher_.reset(); | 321 default_fetcher_.reset(); |
325 } | 322 } |
326 | 323 |
327 void SearchProvider::ClearAllResults() { | 324 void SearchProvider::ClearAllResults() { |
328 keyword_results_.Clear(); | 325 keyword_results_.Clear(); |
329 default_results_.Clear(); | 326 default_results_.Clear(); |
330 } | 327 } |
331 | 328 |
332 int SearchProvider::GetDefaultResultRelevance() const { | |
333 return -1; | |
334 } | |
335 | |
336 void SearchProvider::RecordDeletionResult(bool success) { | 329 void SearchProvider::RecordDeletionResult(bool success) { |
337 if (success) { | 330 if (success) { |
338 content::RecordAction( | 331 content::RecordAction( |
339 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success")); | 332 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success")); |
340 } else { | 333 } else { |
341 content::RecordAction( | 334 content::RecordAction( |
342 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure")); | 335 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure")); |
343 } | 336 } |
344 } | 337 } |
345 | 338 |
(...skipping 13 matching lines...) Expand all Loading... | |
359 if (success) { | 352 if (success) { |
360 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", | 353 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
361 elapsed_time); | 354 elapsed_time); |
362 } else { | 355 } else { |
363 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", | 356 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
364 elapsed_time); | 357 elapsed_time); |
365 } | 358 } |
366 } | 359 } |
367 } | 360 } |
368 | 361 |
369 bool SearchProvider::IsKeywordFetcher(const net::URLFetcher* fetcher) const { | |
370 return fetcher == keyword_fetcher_.get(); | |
371 } | |
372 | |
373 void SearchProvider::UpdateMatches() { | 362 void SearchProvider::UpdateMatches() { |
374 ConvertResultsToAutocompleteMatches(); | 363 ConvertResultsToAutocompleteMatches(); |
375 | 364 |
376 // Check constraints that may be violated by suggested relevances. | 365 // Check constraints that may be violated by suggested relevances. |
377 if (!matches_.empty() && | 366 if (!matches_.empty() && |
378 (default_results_.HasServerProvidedScores() || | 367 (default_results_.HasServerProvidedScores() || |
379 keyword_results_.HasServerProvidedScores())) { | 368 keyword_results_.HasServerProvidedScores())) { |
380 // These blocks attempt to repair undesirable behavior by suggested | 369 // These blocks attempt to repair undesirable behavior by suggested |
381 // relevances with minimal impact, preserving other suggested relevances. | 370 // relevances with minimal impact, preserving other suggested relevances. |
382 | 371 |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 674 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
686 // Add Chrome experiment state to the request headers. | 675 // Add Chrome experiment state to the request headers. |
687 net::HttpRequestHeaders headers; | 676 net::HttpRequestHeaders headers; |
688 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( | 677 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
689 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); | 678 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
690 fetcher->SetExtraRequestHeaders(headers.ToString()); | 679 fetcher->SetExtraRequestHeaders(headers.ToString()); |
691 fetcher->Start(); | 680 fetcher->Start(); |
692 return fetcher; | 681 return fetcher; |
693 } | 682 } |
694 | 683 |
684 void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) { | |
685 DCHECK(!done_); | |
686 suggest_results_pending_--; | |
Peter Kasting
2014/08/08 17:33:23
Nit: Predecrement
hashimoto
2014/08/11 05:15:04
Done.
| |
687 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. | |
688 | |
689 const bool is_keyword = source == keyword_fetcher_.get(); | |
690 | |
691 // Ensure the request succeeded and that the provider used is still available. | |
692 // A verbatim match cannot be generated without this provider, causing errors. | |
693 const bool request_succeeded = | |
694 source->GetStatus().is_success() && (source->GetResponseCode() == 200) && | |
695 GetTemplateURL(is_keyword); | |
696 | |
697 LogFetchComplete(request_succeeded, is_keyword); | |
698 | |
699 bool results_updated = false; | |
700 if (request_succeeded) { | |
701 scoped_ptr<base::Value> data(SearchSuggestionParser::DeserializeJsonData( | |
702 SearchSuggestionParser::ExtractJsonData(source))); | |
703 if (data) { | |
704 SearchSuggestionParser::Results* results = | |
705 is_keyword ? &keyword_results_ : &default_results_; | |
706 results_updated = ParseSuggestResults(*data, -1, is_keyword, results); | |
707 if (results_updated) | |
708 SortResults(is_keyword, results); | |
709 } | |
710 } | |
711 UpdateMatches(); | |
712 if (done_ || results_updated) | |
713 listener_->OnProviderUpdate(results_updated); | |
714 } | |
715 | |
695 void SearchProvider::ConvertResultsToAutocompleteMatches() { | 716 void SearchProvider::ConvertResultsToAutocompleteMatches() { |
696 // Convert all the results to matches and add them to a map, so we can keep | 717 // Convert all the results to matches and add them to a map, so we can keep |
697 // the most relevant match for each result. | 718 // the most relevant match for each result. |
698 base::TimeTicks start_time(base::TimeTicks::Now()); | 719 base::TimeTicks start_time(base::TimeTicks::Now()); |
699 MatchMap map; | 720 MatchMap map; |
700 const base::Time no_time; | 721 const base::Time no_time; |
701 int did_not_accept_keyword_suggestion = | 722 int did_not_accept_keyword_suggestion = |
702 keyword_results_.suggest_results.empty() ? | 723 keyword_results_.suggest_results.empty() ? |
703 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : | 724 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
704 TemplateURLRef::NO_SUGGESTION_CHOSEN; | 725 TemplateURLRef::NO_SUGGESTION_CHOSEN; |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1241 last_answer_seen_.query_type = match->answer_type; | 1262 last_answer_seen_.query_type = match->answer_type; |
1242 } | 1263 } |
1243 | 1264 |
1244 void SearchProvider::DoAnswersQuery(const AutocompleteInput& input) { | 1265 void SearchProvider::DoAnswersQuery(const AutocompleteInput& input) { |
1245 // If the query text starts with trimmed input, this is valid prefetch data. | 1266 // If the query text starts with trimmed input, this is valid prefetch data. |
1246 prefetch_data_ = StartsWith(last_answer_seen_.full_query_text, | 1267 prefetch_data_ = StartsWith(last_answer_seen_.full_query_text, |
1247 base::CollapseWhitespace(input.text(), false), | 1268 base::CollapseWhitespace(input.text(), false), |
1248 false) ? | 1269 false) ? |
1249 last_answer_seen_ : AnswersQueryData(); | 1270 last_answer_seen_ : AnswersQueryData(); |
1250 } | 1271 } |
OLD | NEW |