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 "components/omnibox/search_provider.h" | 5 #include "components/omnibox/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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 const SearchSuggestionParser::Result& b) { | 110 const SearchSuggestionParser::Result& b) { |
111 // Sort in descending relevance order. | 111 // Sort in descending relevance order. |
112 return a.relevance() > b.relevance(); | 112 return a.relevance() > b.relevance(); |
113 } | 113 } |
114 }; | 114 }; |
115 | 115 |
116 | 116 |
117 // SearchProvider ------------------------------------------------------------- | 117 // SearchProvider ------------------------------------------------------------- |
118 | 118 |
119 // static | 119 // static |
120 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; | 120 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
Mark P
2014/10/21 21:28:22
Shall we delete this? It's unused now.
Bart N.
2014/10/22 01:19:08
Moved to OFT.
| |
121 | 121 |
122 SearchProvider::SearchProvider( | 122 SearchProvider::SearchProvider( |
123 AutocompleteProviderListener* listener, | 123 AutocompleteProviderListener* listener, |
124 TemplateURLService* template_url_service, | 124 TemplateURLService* template_url_service, |
125 scoped_ptr<AutocompleteProviderClient> client) | 125 scoped_ptr<AutocompleteProviderClient> client) |
126 : BaseSearchProvider(template_url_service, client.Pass(), | 126 : BaseSearchProvider(template_url_service, client.Pass(), |
127 AutocompleteProvider::TYPE_SEARCH), | 127 AutocompleteProvider::TYPE_SEARCH), |
128 listener_(listener), | 128 listener_(listener), |
129 suggest_results_pending_(0), | 129 suggest_results_pending_(0), |
130 providers_(template_url_service), | 130 providers_(template_url_service), |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
572 } | 572 } |
573 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 573 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
574 if (keyword_url) { | 574 if (keyword_url) { |
575 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), | 575 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), |
576 keyword_input_.text(), | 576 keyword_input_.text(), |
577 num_matches, | 577 num_matches, |
578 &raw_keyword_history_results_); | 578 &raw_keyword_history_results_); |
579 } | 579 } |
580 } | 580 } |
581 | 581 |
582 base::TimeDelta SearchProvider::GetSuggestQueryDelay() { | |
583 bool since_the_last_request = true; | |
Mark P
2014/10/21 21:28:22
I suggest you don't initialize these two variables
Bart N.
2014/10/22 01:19:08
Done.
| |
584 int polling_delay_ms = 100; | |
585 OmniboxFieldTrial::GetSuggestPollingStrategy(&since_the_last_request, | |
586 &polling_delay_ms); | |
587 | |
588 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(polling_delay_ms)); | |
589 if (!since_the_last_request) | |
590 return delay; | |
591 | |
592 base::TimeDelta time_since_last_suggest_request = | |
593 base::TimeTicks::Now() - time_suggest_request_sent_; | |
594 return std::max(base::TimeDelta(), delay - time_since_last_suggest_request); | |
Mark P
2014/10/21 21:28:22
You shouldn't need this max. TimeTicks is suppose
Bart N.
2014/10/22 01:19:08
What if time_since_last_suggest_request is more th
Mark P
2014/10/22 20:33:06
Well, negative delays shouldn't wait in any case.
Bart N.
2014/10/22 22:04:56
Acknowledged.
| |
595 } | |
596 | |
582 void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { | 597 void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
583 if (!IsQuerySuitableForSuggest()) { | 598 if (!IsQuerySuitableForSuggest()) { |
584 StopSuggest(); | 599 StopSuggest(); |
585 ClearAllResults(); | 600 ClearAllResults(); |
586 return; | 601 return; |
587 } | 602 } |
588 | 603 |
604 if (OmniboxFieldTrial::DisableResultsCaching()) | |
605 ClearAllResults(); | |
Mark P
2014/10/21 21:28:22
For your experiment, do you also want to clear cac
Bart N.
2014/10/22 01:19:08
I think so. Do you know why it is not cleared alre
Mark P
2014/10/22 20:33:06
Well, my understanding of how answers work is that
Bart N.
2014/10/22 22:04:56
Acknowledged.
| |
606 | |
589 // For the minimal_changes case, if we finished the previous query and still | 607 // For the minimal_changes case, if we finished the previous query and still |
590 // have its results, or are allowed to keep running it, just do that, rather | 608 // have its results, or are allowed to keep running it, just do that, rather |
591 // than starting a new query. | 609 // than starting a new query. |
592 if (minimal_changes && | 610 if (minimal_changes && |
593 (!default_results_.suggest_results.empty() || | 611 (!default_results_.suggest_results.empty() || |
594 !default_results_.navigation_results.empty() || | 612 !default_results_.navigation_results.empty() || |
595 !keyword_results_.suggest_results.empty() || | 613 !keyword_results_.suggest_results.empty() || |
596 !keyword_results_.navigation_results.empty() || | 614 !keyword_results_.navigation_results.empty() || |
597 (!done_ && input_.want_asynchronous_matches()))) | 615 (!done_ && input_.want_asynchronous_matches()))) |
598 return; | 616 return; |
599 | 617 |
600 // We can't keep running any previous query, so halt it. | 618 // We can't keep running any previous query, so halt it. |
601 StopSuggest(); | 619 StopSuggest(); |
602 | 620 |
603 UpdateAllOldResults(minimal_changes); | 621 UpdateAllOldResults(minimal_changes); |
604 | 622 |
605 // Update the content classifications of remaining results so they look good | 623 // Update the content classifications of remaining results so they look good |
606 // against the current input. | 624 // against the current input. |
607 UpdateMatchContentsClass(input_.text(), &default_results_); | 625 UpdateMatchContentsClass(input_.text(), &default_results_); |
608 if (!keyword_input_.text().empty()) | 626 if (!keyword_input_.text().empty()) |
609 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_); | 627 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_); |
610 | 628 |
611 // We can't start a new query if we're only allowed synchronous results. | 629 // We can't start a new query if we're only allowed synchronous results. |
612 if (!input_.want_asynchronous_matches()) | 630 if (!input_.want_asynchronous_matches()) |
613 return; | 631 return; |
614 | 632 |
615 // To avoid flooding the suggest server, don't send a query until at | 633 // Kick off a timer that will start the URL fetch if it completes before |
616 // least 100 ms since the last query. | 634 // the user types another character. Requests may be delayed to avoid |
617 base::TimeTicks next_suggest_time(time_suggest_request_sent_ + | 635 // flooding the server with requests that are likely to be thrown away later |
618 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs)); | 636 // anyway. |
619 base::TimeTicks now(base::TimeTicks::Now()); | 637 const base::TimeDelta delay = GetSuggestQueryDelay(); |
620 if (now >= next_suggest_time) { | 638 if (delay <= base::TimeDelta()) { |
621 Run(); | 639 Run(); |
622 return; | 640 return; |
623 } | 641 } |
624 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run); | 642 timer_.Start(FROM_HERE, delay, this, &SearchProvider::Run); |
625 } | 643 } |
626 | 644 |
627 bool SearchProvider::IsQuerySuitableForSuggest() const { | 645 bool SearchProvider::IsQuerySuitableForSuggest() const { |
628 // Don't run Suggest in incognito mode, if the engine doesn't support it, or | 646 // Don't run Suggest in incognito mode, if the engine doesn't support it, or |
629 // if the user has disabled it. | 647 // if the user has disabled it. |
630 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); | 648 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
631 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 649 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
632 if (client_->IsOffTheRecord() || | 650 if (client_->IsOffTheRecord() || |
633 ((!default_url || default_url->suggestions_url().empty()) && | 651 ((!default_url || default_url->suggestions_url().empty()) && |
634 (!keyword_url || keyword_url->suggestions_url().empty())) || | 652 (!keyword_url || keyword_url->suggestions_url().empty())) || |
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1431 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) | 1449 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
1432 matches.push_back(i->second); | 1450 matches.push_back(i->second); |
1433 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); | 1451 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); |
1434 | 1452 |
1435 // If there is a top scoring entry, find the corresponding answer. | 1453 // If there is a top scoring entry, find the corresponding answer. |
1436 if (!matches.empty()) | 1454 if (!matches.empty()) |
1437 return answers_cache_.GetTopAnswerEntry(matches[0].contents); | 1455 return answers_cache_.GetTopAnswerEntry(matches[0].contents); |
1438 | 1456 |
1439 return AnswersQueryData(); | 1457 return AnswersQueryData(); |
1440 } | 1458 } |
OLD | NEW |