| 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 non-keyword autocomplete entries that start with | 6 // responsible for all non-keyword autocomplete entries that start with |
| 7 // "Search <engine> for ...", including searching for the current input string, | 7 // "Search <engine> for ...", including searching for the current input string, |
| 8 // search history, and search suggestions. An instance of it gets created and | 8 // search 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 // |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 TemplateURL cached_default_provider_; | 151 TemplateURL cached_default_provider_; |
| 152 TemplateURL cached_keyword_provider_; | 152 TemplateURL cached_keyword_provider_; |
| 153 | 153 |
| 154 // TODO(pkasting): http://b/1162970 We shouldn't need these. | 154 // TODO(pkasting): http://b/1162970 We shouldn't need these. |
| 155 const TemplateURL* default_provider_; | 155 const TemplateURL* default_provider_; |
| 156 const TemplateURL* keyword_provider_; | 156 const TemplateURL* keyword_provider_; |
| 157 | 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(Providers); | 158 DISALLOW_COPY_AND_ASSIGN(Providers); |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 struct NavigationResult { | 161 struct SuggestResult { |
| 162 NavigationResult(const GURL& url, const string16& site_name) | 162 SuggestResult(const string16& suggestion, |
| 163 : url(url), | 163 bool has_suggested_relevance, |
| 164 site_name(site_name) { | 164 int relevance); |
| 165 } | |
| 166 | 165 |
| 167 // The URL. | 166 string16 suggestion; |
| 168 GURL url; | |
| 169 | 167 |
| 170 // Name for the site. | 168 bool has_suggested_relevance; |
| 171 string16 site_name; | 169 int relevance; |
| 172 }; | 170 }; |
| 173 | 171 |
| 174 typedef std::vector<string16> SuggestResults; | 172 struct NavigationResult { |
| 173 NavigationResult(const GURL& url, |
| 174 const string16& site_name, |
| 175 bool has_suggested_relevance, |
| 176 int relevance); |
| 177 |
| 178 GURL url; |
| 179 string16 site_name; |
| 180 |
| 181 bool has_suggested_relevance; |
| 182 int relevance; |
| 183 }; |
| 184 |
| 185 typedef std::vector<SuggestResult> SuggestResults; |
| 175 typedef std::vector<NavigationResult> NavigationResults; | 186 typedef std::vector<NavigationResult> NavigationResults; |
| 176 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; | 187 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; |
| 177 typedef std::map<string16, AutocompleteMatch> MatchMap; | 188 typedef std::map<string16, AutocompleteMatch> MatchMap; |
| 178 typedef std::pair<string16, int> ScoredTerm; | 189 typedef std::pair<string16, int> ScoredTerm; |
| 179 typedef std::vector<ScoredTerm> ScoredTerms; | 190 typedef std::vector<ScoredTerm> ScoredTerms; |
| 180 | 191 |
| 181 class CompareScoredTerms; | 192 class CompareScoredTerms; |
| 182 | 193 |
| 183 // Called when timer_ expires. | 194 // Called when timer_ expires. |
| 184 void Run(); | 195 void Run(); |
| 185 | 196 |
| 186 // Runs the history query, if necessary. The history query is synchronous. | 197 // Runs the history query, if necessary. The history query is synchronous. |
| 187 // This does not update |done_|. | 198 // This does not update |done_|. |
| 188 void DoHistoryQuery(bool minimal_changes); | 199 void DoHistoryQuery(bool minimal_changes); |
| 189 | 200 |
| 190 // Determines whether an asynchronous subcomponent query should run for the | 201 // Determines whether an asynchronous subcomponent query should run for the |
| 191 // current input. If so, starts it if necessary; otherwise stops it. | 202 // current input. If so, starts it if necessary; otherwise stops it. |
| 192 // NOTE: This function does not update |done_|. Callers must do so. | 203 // NOTE: This function does not update |done_|. Callers must do so. |
| 193 void StartOrStopSuggestQuery(bool minimal_changes); | 204 void StartOrStopSuggestQuery(bool minimal_changes); |
| 194 | 205 |
| 195 // Returns true when the current query can be sent to the Suggest service. | 206 // Returns true when the current query can be sent to the Suggest service. |
| 196 // This will be false e.g. when Suggest is disabled, the query contains | 207 // This will be false e.g. when Suggest is disabled, the query contains |
| 197 // potentially private data, etc. | 208 // potentially private data, etc. |
| 198 bool IsQuerySuitableForSuggest() const; | 209 bool IsQuerySuitableForSuggest() const; |
| 199 | 210 |
| 200 // Stops the suggest query. | 211 // Stops the suggest query. |
| 201 // NOTE: This does not update |done_|. Callers must do so. | 212 // NOTE: This does not update |done_|. Callers must do so. |
| 202 void StopSuggest(); | 213 void StopSuggest(); |
| 203 | 214 |
| 215 // Clears the current results. |
| 216 void ClearResults(); |
| 217 |
| 204 // Creates a URLFetcher requesting suggest results from the specified | 218 // Creates a URLFetcher requesting suggest results from the specified |
| 205 // |suggestions_url|. The caller owns the returned URLFetcher. | 219 // |suggestions_url|. The caller owns the returned URLFetcher. |
| 206 content::URLFetcher* CreateSuggestFetcher( | 220 content::URLFetcher* CreateSuggestFetcher( |
| 207 int id, | 221 int id, |
| 208 const TemplateURLRef& suggestions_url, | 222 const TemplateURLRef& suggestions_url, |
| 209 const string16& text); | 223 const string16& text); |
| 210 | 224 |
| 211 // Parses the results from the Suggest server and stores up to kMaxMatches of | 225 // Parses the results from the Suggest server and stores up to kMaxMatches of |
| 212 // them in server_results_. Returns whether parsing succeeded. | 226 // them in server_results_. Returns whether parsing succeeded. |
| 213 bool ParseSuggestResults(base::Value* root_val, | 227 bool ParseSuggestResults(base::Value* root_val, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // |time| is the time at which this query was last seen. |is_keyword| | 268 // |time| is the time at which this query was last seen. |is_keyword| |
| 255 // indicates whether the results correspond to the keyword provider or default | 269 // indicates whether the results correspond to the keyword provider or default |
| 256 // provider. |prevent_inline_autocomplete| is true if we should not inline | 270 // provider. |prevent_inline_autocomplete| is true if we should not inline |
| 257 // autocomplete this query. | 271 // autocomplete this query. |
| 258 int CalculateRelevanceForHistory(const base::Time& time, | 272 int CalculateRelevanceForHistory(const base::Time& time, |
| 259 bool is_keyword, | 273 bool is_keyword, |
| 260 bool prevent_inline_autocomplete) const; | 274 bool prevent_inline_autocomplete) const; |
| 261 // |result_number| is the index of the suggestion in the result set from the | 275 // |result_number| is the index of the suggestion in the result set from the |
| 262 // server; the best suggestion is suggestion number 0. |is_keyword| is true | 276 // server; the best suggestion is suggestion number 0. |is_keyword| is true |
| 263 // if the search is from the keyword provider. | 277 // if the search is from the keyword provider. |
| 264 int CalculateRelevanceForSuggestion(size_t num_results, | 278 int CalculateRelevanceForSuggestion(const SuggestResults& results, |
| 265 size_t result_number, | 279 size_t result_number, |
| 266 bool is_keyword) const; | 280 bool is_keyword) const; |
| 267 // |result_number| is same as above. |is_keyword| is true if the navigation | 281 // |result_number| is same as above. |is_keyword| is true if the navigation |
| 268 // result was suggested by the keyword provider. | 282 // result was suggested by the keyword provider. |
| 269 int CalculateRelevanceForNavigation(size_t num_results, | 283 int CalculateRelevanceForNavigation(const NavigationResults& results, |
| 270 size_t result_number, | 284 size_t result_number, |
| 271 bool is_keyword) const; | 285 bool is_keyword) const; |
| 272 | 286 |
| 273 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with | 287 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with |
| 274 // the supplied relevance. Adds this match to |map|; if such a match already | 288 // the supplied relevance. Adds this match to |map|; if such a match already |
| 275 // exists, whichever one has lower relevance is eliminated. | 289 // exists, whichever one has lower relevance is eliminated. |
| 276 void AddMatchToMap(const string16& query_string, | 290 void AddMatchToMap(const string16& query_string, |
| 277 const string16& input_text, | 291 const string16& input_text, |
| 278 int relevance, | 292 int relevance, |
| 279 AutocompleteMatch::Type type, | 293 AutocompleteMatch::Type type, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 293 // Should we query for suggest results immediately? This is normally false, | 307 // Should we query for suggest results immediately? This is normally false, |
| 294 // but may be set to true during testing. | 308 // but may be set to true during testing. |
| 295 static bool query_suggest_immediately_; | 309 static bool query_suggest_immediately_; |
| 296 | 310 |
| 297 // Maintains the TemplateURLs used. | 311 // Maintains the TemplateURLs used. |
| 298 Providers providers_; | 312 Providers providers_; |
| 299 | 313 |
| 300 // The user's input. | 314 // The user's input. |
| 301 AutocompleteInput input_; | 315 AutocompleteInput input_; |
| 302 | 316 |
| 317 // The suggested verbatim relevance for SEARCH_WHAT_YOU_TYPED. |
| 318 bool has_verbatim_relevance_; |
| 319 int verbatim_relevance_; |
| 320 |
| 303 // Input text when searching against the keyword provider. | 321 // Input text when searching against the keyword provider. |
| 304 string16 keyword_input_text_; | 322 string16 keyword_input_text_; |
| 305 | 323 |
| 306 // Searches in the user's history that begin with the input text. | 324 // Searches in the user's history that begin with the input text. |
| 307 HistoryResults keyword_history_results_; | 325 HistoryResults keyword_history_results_; |
| 308 HistoryResults default_history_results_; | 326 HistoryResults default_history_results_; |
| 309 | 327 |
| 310 // Number of suggest results that haven't yet arrived. If greater than 0 it | 328 // Number of suggest results that haven't yet arrived. If greater than 0 it |
| 311 // indicates either |timer_| or one of the URLFetchers is still running. | 329 // indicates either |timer_| or one of the URLFetchers is still running. |
| 312 int suggest_results_pending_; | 330 int suggest_results_pending_; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 336 // Has FinalizeInstantQuery been invoked since the last |Start|? | 354 // Has FinalizeInstantQuery been invoked since the last |Start|? |
| 337 bool instant_finalized_; | 355 bool instant_finalized_; |
| 338 | 356 |
| 339 // The |suggest_text| parameter passed to FinalizeInstantQuery. | 357 // The |suggest_text| parameter passed to FinalizeInstantQuery. |
| 340 string16 default_provider_suggest_text_; | 358 string16 default_provider_suggest_text_; |
| 341 | 359 |
| 342 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 360 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
| 343 }; | 361 }; |
| 344 | 362 |
| 345 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 363 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| OLD | NEW |