Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // This file contains the Search autocomplete provider. This provider is | 5 // This file contains the Search autocomplete provider. This provider is |
| 6 // responsible for all autocomplete entries that start with "Search <engine> | 6 // responsible for all autocomplete entries that start with "Search <engine> |
| 7 // for ...", including searching for the current input string, search | 7 // for ...", including searching for the current input string, search |
| 8 // history, and search suggestions. An instance of it gets created and | 8 // history, and search suggestions. An instance of it gets created and |
| 9 // managed by the autocomplete controller. | 9 // managed by the autocomplete controller. |
| 10 | 10 |
| 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| 12 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 12 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 19 #include "chrome/browser/autocomplete/base_search_provider.h" | 19 #include "chrome/browser/autocomplete/base_search_provider.h" |
| 20 #include "components/metrics/proto/omnibox_input_type.pb.h" | 20 #include "components/metrics/proto/omnibox_input_type.pb.h" |
| 21 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | |
| 22 | 23 |
| 24 class AutocompleteProviderListener; | |
| 23 class AutocompleteResult; | 25 class AutocompleteResult; |
| 24 class Profile; | 26 class Profile; |
| 25 class SearchProviderTest; | 27 class SearchProviderTest; |
| 26 class TemplateURLService; | 28 class TemplateURLService; |
| 27 | 29 |
| 28 namespace history { | 30 namespace history { |
| 29 struct KeywordSearchTermVisit; | 31 struct KeywordSearchTermVisit; |
| 30 } | 32 } |
| 31 | 33 |
| 32 namespace net { | 34 namespace net { |
| 33 class URLFetcher; | 35 class URLFetcher; |
| 34 } | 36 } |
| 35 | 37 |
| 36 // Autocomplete provider for searches and suggestions from a search engine. | 38 // Autocomplete provider for searches and suggestions from a search engine. |
| 37 // | 39 // |
| 38 // After construction, the autocomplete controller repeatedly calls Start() | 40 // After construction, the autocomplete controller repeatedly calls Start() |
| 39 // with some user input, each time expecting to receive a small set of the best | 41 // with some user input, each time expecting to receive a small set of the best |
| 40 // matches (either synchronously or asynchronously). | 42 // matches (either synchronously or asynchronously). |
| 41 // | 43 // |
| 42 // Initially the provider creates a match that searches for the current input | 44 // Initially the provider creates a match that searches for the current input |
| 43 // text. It also starts a task to query the Suggest servers. When that data | 45 // text. It also starts a task to query the Suggest servers. When that data |
| 44 // comes back, the provider creates and returns matches for the best | 46 // comes back, the provider creates and returns matches for the best |
| 45 // suggestions. | 47 // suggestions. |
| 46 class SearchProvider : public BaseSearchProvider { | 48 class SearchProvider : public BaseSearchProvider, |
| 49 public net::URLFetcherDelegate { | |
| 47 public: | 50 public: |
| 48 SearchProvider(AutocompleteProviderListener* listener, | 51 SearchProvider(AutocompleteProviderListener* listener, |
| 49 TemplateURLService* template_url_service, | 52 TemplateURLService* template_url_service, |
| 50 Profile* profile); | 53 Profile* profile); |
| 51 | 54 |
| 52 // Extracts the suggest response metadata which SearchProvider previously | 55 // Extracts the suggest response metadata which SearchProvider previously |
| 53 // stored for |match|. | 56 // stored for |match|. |
| 54 static std::string GetSuggestMetadata(const AutocompleteMatch& match); | 57 static std::string GetSuggestMetadata(const AutocompleteMatch& match); |
| 55 | 58 |
| 56 // Answers prefetch handling - register displayed answers. Takes the top | 59 // Answers prefetch handling - register displayed answers. Takes the top |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 // Calculates the relevance score for the keyword verbatim result (if the | 159 // Calculates the relevance score for the keyword verbatim result (if the |
| 157 // input matches one of the profile's keyword). | 160 // input matches one of the profile's keyword). |
| 158 static int CalculateRelevanceForKeywordVerbatim( | 161 static int CalculateRelevanceForKeywordVerbatim( |
| 159 metrics::OmniboxInputType::Type type, | 162 metrics::OmniboxInputType::Type type, |
| 160 bool prefer_keyword); | 163 bool prefer_keyword); |
| 161 | 164 |
| 162 // AutocompleteProvider: | 165 // AutocompleteProvider: |
| 163 virtual void Start(const AutocompleteInput& input, | 166 virtual void Start(const AutocompleteInput& input, |
| 164 bool minimal_changes) OVERRIDE; | 167 bool minimal_changes) OVERRIDE; |
| 165 | 168 |
| 169 // Called after ParseSuggestResults to rank the |results|. | |
| 170 void SortResults(bool is_keyword, SearchSuggestionParser::Results* results); | |
|
Peter Kasting
2014/08/08 17:33:23
Nit: Move this down below the virtual functions (a
hashimoto
2014/08/11 05:15:04
Done.
| |
| 171 | |
| 166 // BaseSearchProvider: | 172 // BaseSearchProvider: |
| 167 virtual void SortResults(bool is_keyword, | |
| 168 SearchSuggestionParser::Results* results) OVERRIDE; | |
| 169 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; | 173 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; |
| 170 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; | 174 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; |
| 171 virtual SearchSuggestionParser::Results* GetResultsToFill( | |
| 172 bool is_keyword) OVERRIDE; | |
| 173 virtual bool ShouldAppendExtraParams( | 175 virtual bool ShouldAppendExtraParams( |
| 174 const SearchSuggestionParser::SuggestResult& result) const OVERRIDE; | 176 const SearchSuggestionParser::SuggestResult& result) const OVERRIDE; |
| 175 virtual void StopSuggest() OVERRIDE; | 177 virtual void StopSuggest() OVERRIDE; |
| 176 virtual void ClearAllResults() OVERRIDE; | 178 virtual void ClearAllResults() OVERRIDE; |
| 177 virtual int GetDefaultResultRelevance() const OVERRIDE; | |
| 178 virtual void RecordDeletionResult(bool success) OVERRIDE; | 179 virtual void RecordDeletionResult(bool success) OVERRIDE; |
| 179 virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE; | 180 |
| 180 virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE; | 181 // Records UMA statistics about a suggest server response. |
| 181 virtual void UpdateMatches() OVERRIDE; | 182 void LogFetchComplete(bool success, bool is_keyword); |
| 183 | |
| 184 // Updates |matches_| from the latest results; applies calculated relevances | |
| 185 // if suggested relevances cause undesriable behavior. Updates |done_|. | |
|
Peter Kasting
2014/08/08 17:33:24
Nit undesirable
hashimoto
2014/08/11 05:15:04
Done.
| |
| 186 void UpdateMatches(); | |
| 182 | 187 |
| 183 // Called when timer_ expires. | 188 // Called when timer_ expires. |
| 184 void Run(); | 189 void Run(); |
| 185 | 190 |
| 186 // Runs the history query, if necessary. The history query is synchronous. | 191 // Runs the history query, if necessary. The history query is synchronous. |
| 187 // This does not update |done_|. | 192 // This does not update |done_|. |
| 188 void DoHistoryQuery(bool minimal_changes); | 193 void DoHistoryQuery(bool minimal_changes); |
| 189 | 194 |
| 190 // Determines whether an asynchronous subcomponent query should run for the | 195 // Determines whether an asynchronous subcomponent query should run for the |
| 191 // current input. If so, starts it if necessary; otherwise stops it. | 196 // current input. If so, starts it if necessary; otherwise stops it. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 207 SearchSuggestionParser::SuggestResults* list); | 212 SearchSuggestionParser::SuggestResults* list); |
| 208 void ApplyCalculatedNavigationRelevance( | 213 void ApplyCalculatedNavigationRelevance( |
| 209 SearchSuggestionParser::NavigationResults* list); | 214 SearchSuggestionParser::NavigationResults* list); |
| 210 | 215 |
| 211 // Starts a new URLFetcher requesting suggest results from |template_url|; | 216 // Starts a new URLFetcher requesting suggest results from |template_url|; |
| 212 // callers own the returned URLFetcher, which is NULL for invalid providers. | 217 // callers own the returned URLFetcher, which is NULL for invalid providers. |
| 213 net::URLFetcher* CreateSuggestFetcher(int id, | 218 net::URLFetcher* CreateSuggestFetcher(int id, |
| 214 const TemplateURL* template_url, | 219 const TemplateURL* template_url, |
| 215 const AutocompleteInput& input); | 220 const AutocompleteInput& input); |
| 216 | 221 |
| 222 // net::URLFetcherDelegate: | |
| 223 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
|
Peter Kasting
2014/08/08 17:33:23
Nit: Move this up to be with the other virtual fun
hashimoto
2014/08/11 05:15:04
Done.
| |
| 224 | |
| 217 // Converts the parsed results to a set of AutocompleteMatches, |matches_|. | 225 // Converts the parsed results to a set of AutocompleteMatches, |matches_|. |
| 218 void ConvertResultsToAutocompleteMatches(); | 226 void ConvertResultsToAutocompleteMatches(); |
| 219 | 227 |
| 220 // Returns an iterator to the first match in |matches_| which might | 228 // Returns an iterator to the first match in |matches_| which might |
| 221 // be chosen as default. | 229 // be chosen as default. |
| 222 ACMatches::const_iterator FindTopMatch() const; | 230 ACMatches::const_iterator FindTopMatch() const; |
| 223 | 231 |
| 224 // Checks if suggested relevances violate certain expected constraints. | 232 // Checks if suggested relevances violate certain expected constraints. |
| 225 // See UpdateMatches() for the use and explanation of these constraints. | 233 // See UpdateMatches() for the use and explanation of these constraints. |
| 226 bool HasKeywordDefaultMatchInKeywordMode() const; | 234 bool HasKeywordDefaultMatchInKeywordMode() const; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 std::string GetSessionToken(); | 308 std::string GetSessionToken(); |
| 301 | 309 |
| 302 // Answers prefetch handling - finds previously displayed answer matching the | 310 // Answers prefetch handling - finds previously displayed answer matching the |
| 303 // current |input| and sets |prefetch_data_|. | 311 // current |input| and sets |prefetch_data_|. |
| 304 void DoAnswersQuery(const AutocompleteInput& input); | 312 void DoAnswersQuery(const AutocompleteInput& input); |
| 305 | 313 |
| 306 // The amount of time to wait before sending a new suggest request after the | 314 // The amount of time to wait before sending a new suggest request after the |
| 307 // previous one. Non-const because some unittests modify this value. | 315 // previous one. Non-const because some unittests modify this value. |
| 308 static int kMinimumTimeBetweenSuggestQueriesMs; | 316 static int kMinimumTimeBetweenSuggestQueriesMs; |
| 309 | 317 |
| 318 AutocompleteProviderListener* listener_; | |
| 319 | |
| 320 // The number of suggest results that haven't yet arrived. If it's greater | |
| 321 // than 0, it indicates that one of the URLFetchers is still running. | |
| 322 int suggest_results_pending_; | |
| 323 | |
| 310 // Maintains the TemplateURLs used. | 324 // Maintains the TemplateURLs used. |
| 311 Providers providers_; | 325 Providers providers_; |
| 312 | 326 |
| 313 // The user's input. | 327 // The user's input. |
| 314 AutocompleteInput input_; | 328 AutocompleteInput input_; |
| 315 | 329 |
| 316 // Input when searching against the keyword provider. | 330 // Input when searching against the keyword provider. |
| 317 AutocompleteInput keyword_input_; | 331 AutocompleteInput keyword_input_; |
| 318 | 332 |
| 319 // Searches in the user's history that begin with the input text. | 333 // Searches in the user's history that begin with the input text. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 342 base::TimeTicks token_expiration_time_; | 356 base::TimeTicks token_expiration_time_; |
| 343 | 357 |
| 344 // Answers prefetch management. | 358 // Answers prefetch management. |
| 345 AnswersQueryData prefetch_data_; // Data to use for query prefetching. | 359 AnswersQueryData prefetch_data_; // Data to use for query prefetching. |
| 346 AnswersQueryData last_answer_seen_; // Last answer seen. | 360 AnswersQueryData last_answer_seen_; // Last answer seen. |
| 347 | 361 |
| 348 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 362 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
| 349 }; | 363 }; |
| 350 | 364 |
| 351 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 365 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| OLD | NEW |