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 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 public net::URLFetcherDelegate { | 53 public net::URLFetcherDelegate { |
54 public: | 54 public: |
55 // ID used in creating URLFetcher for default provider's suggest results. | 55 // ID used in creating URLFetcher for default provider's suggest results. |
56 static const int kDefaultProviderURLFetcherID; | 56 static const int kDefaultProviderURLFetcherID; |
57 | 57 |
58 // ID used in creating URLFetcher for keyword provider's suggest results. | 58 // ID used in creating URLFetcher for keyword provider's suggest results. |
59 static const int kKeywordProviderURLFetcherID; | 59 static const int kKeywordProviderURLFetcherID; |
60 | 60 |
61 SearchProvider(AutocompleteProviderListener* listener, Profile* profile); | 61 SearchProvider(AutocompleteProviderListener* listener, Profile* profile); |
62 | 62 |
63 // Returns an AutocompleteMatch with the given |autocomplete_provider|, | |
64 // |relevance|, and |type|, which represents a search via |template_url| for | |
65 // |query_string|. If |template_url| is NULL, returns a match with an invalid | |
66 // destination URL. | |
67 // | |
68 // |input_text| is the original user input, which may differ from | |
69 // |query_string|; e.g. the user typed "foo" and got a search suggestion of | |
70 // "food", which we're now marking up. This is used to highlight portions of | |
71 // the match contents to distinguish locally-typed text from suggested text. | |
72 // | |
73 // |input| and |is_keyword| are necessary for various other details, like | |
74 // whether we should allow inline autocompletion and what the transition type | |
75 // should be. |accepted_suggestion| and |omnibox_start_margin| are used along | |
76 // with |input_text| to generate Assisted Query Stats. | |
77 // |append_extra_query_params| should be set if |template_url| is the default | |
78 // search engine, so the destination URL will contain any | |
79 // command-line-specified query params. | |
80 static AutocompleteMatch CreateSearchSuggestion( | |
81 AutocompleteProvider* autocomplete_provider, | |
82 int relevance, | |
83 AutocompleteMatch::Type type, | |
84 const TemplateURL* template_url, | |
85 const string16& query_string, | |
86 const string16& input_text, | |
87 const AutocompleteInput& input, | |
88 bool is_keyword, | |
89 int accepted_suggestion, | |
90 int omnibox_start_margin, | |
91 bool append_extra_query_params); | |
92 | |
93 // AutocompleteProvider: | 63 // AutocompleteProvider: |
94 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; | 64 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; |
95 virtual void ResetSession() OVERRIDE; | 65 virtual void ResetSession() OVERRIDE; |
96 | 66 |
97 bool field_trial_triggered_in_session() const { | 67 bool field_trial_triggered_in_session() const { |
98 return field_trial_triggered_in_session_; | 68 return field_trial_triggered_in_session_; |
99 } | 69 } |
100 | 70 |
101 private: | 71 private: |
102 // TODO(hfung): Remove ZeroSuggestProvider as a friend class after | 72 // TODO(hfung): Remove ZeroSuggestProvider as a friend class after |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 bool from_keyword_provider() const { return from_keyword_provider_; } | 144 bool from_keyword_provider() const { return from_keyword_provider_; } |
175 | 145 |
176 int relevance() const { return relevance_; } | 146 int relevance() const { return relevance_; } |
177 void set_relevance(int relevance) { relevance_ = relevance; } | 147 void set_relevance(int relevance) { relevance_ = relevance; } |
178 | 148 |
179 bool relevance_from_server() const { return relevance_from_server_; } | 149 bool relevance_from_server() const { return relevance_from_server_; } |
180 void set_relevance_from_server(bool relevance_from_server) { | 150 void set_relevance_from_server(bool relevance_from_server) { |
181 relevance_from_server_ = relevance_from_server; | 151 relevance_from_server_ = relevance_from_server; |
182 } | 152 } |
183 | 153 |
154 const string16& contents() const { return contents_; } | |
155 const ACMatchClassifications& contents_class() const { | |
156 return contents_class_; | |
157 } | |
158 | |
184 // Returns if this result is inlineable against the current input |input|. | 159 // Returns if this result is inlineable against the current input |input|. |
185 // Non-inlineable results are stale. | 160 // Non-inlineable results are stale. |
186 virtual bool IsInlineable(const string16& input) const = 0; | 161 virtual bool IsInlineable(const string16& input) const = 0; |
187 | 162 |
188 // Returns the default relevance value for this result (which may | 163 // Returns the default relevance value for this result (which may |
189 // be left over from a previous omnibox input) given the current | 164 // be left over from a previous omnibox input) given the current |
190 // input and whether the current input caused a keyword provider | 165 // input and whether the current input caused a keyword provider |
191 // to be active. | 166 // to be active. |
192 virtual int CalculateRelevance(const AutocompleteInput& input, | 167 virtual int CalculateRelevance(const AutocompleteInput& input, |
193 bool keyword_provider_requested) const = 0; | 168 bool keyword_provider_requested) const = 0; |
194 | 169 |
195 protected: | 170 protected: |
196 // True if the result came from the keyword provider. | 171 // True if the result came from the keyword provider. |
197 bool from_keyword_provider_; | 172 bool from_keyword_provider_; |
198 | 173 |
199 // The relevance score. | 174 // The relevance score. |
200 int relevance_; | 175 int relevance_; |
201 | 176 |
177 // The string is loaded into the location bar when the item is selected | |
178 // by pressing the arrow keys. This may be different than a URL. For | |
179 // example, for search suggestions this would just be the search terms. | |
180 // string16 fill_into_edit_; | |
Mark P
2013/08/15 18:34:23
I abandoned this part, sorry that the lines are st
| |
181 | |
182 // The main text displayed in the address bar dropdown. | |
183 string16 contents_; | |
184 ACMatchClassifications contents_class_; | |
185 | |
202 private: | 186 private: |
203 // Whether this result's relevance score was fully or partly calculated | 187 // Whether this result's relevance score was fully or partly calculated |
204 // based on server information, and thus is assumed to be more accurate. | 188 // based on server information, and thus is assumed to be more accurate. |
205 // This is ultimately used in | 189 // This is ultimately used in |
206 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments | 190 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments |
207 // there. | 191 // there. |
208 bool relevance_from_server_; | 192 bool relevance_from_server_; |
209 }; | 193 }; |
210 | 194 |
211 class SuggestResult : public Result { | 195 class SuggestResult : public Result { |
212 public: | 196 public: |
197 // Most parameters are self-explanatory. |input_text| is used to | |
198 // determine highlighting (i.e., populate |contents_| and | |
199 // |contents_class_|). We determine highlighting at the time | |
200 // the SuggestResult is created in order to reduce flicker as | |
201 // additional characters are typed or deleted. | |
213 SuggestResult(const string16& suggestion, | 202 SuggestResult(const string16& suggestion, |
214 bool from_keyword_provider, | 203 bool from_keyword_provider, |
215 int relevance, | 204 int relevance, |
216 bool relevance_from_server); | 205 bool relevance_from_server, |
206 const string16& input_text); | |
217 virtual ~SuggestResult(); | 207 virtual ~SuggestResult(); |
218 | 208 |
219 const string16& suggestion() const { return suggestion_; } | 209 const string16& suggestion() const { return suggestion_; } |
220 | 210 |
221 // Result: | 211 // Result: |
222 virtual bool IsInlineable(const string16& input) const OVERRIDE; | 212 virtual bool IsInlineable(const string16& input) const OVERRIDE; |
223 virtual int CalculateRelevance( | 213 virtual int CalculateRelevance( |
224 const AutocompleteInput& input, | 214 const AutocompleteInput& input, |
225 bool keyword_provider_requested) const OVERRIDE; | 215 bool keyword_provider_requested) const OVERRIDE; |
226 | 216 |
227 private: | 217 private: |
228 // The search suggestion string. | 218 // The search suggestion string. |
229 string16 suggestion_; | 219 string16 suggestion_; |
230 }; | 220 }; |
231 | 221 |
232 class NavigationResult : public Result { | 222 class NavigationResult : public Result { |
233 public: | 223 public: |
234 // |provider| is necessary to use StringForURLDisplay() in order to | 224 // |provider| is necessary to use StringForURLDisplay() in order to |
235 // compute |formatted_url_|. | 225 // compute |formatted_url_|. |
226 // |input_text| and |languages| are used to determine | |
227 // highlighting (i.e., populate |contents_| and |contents_class_|). | |
228 // We determine highlighting at the time the NavigationResult is | |
229 // created in order to reduce flicker as additional characters are | |
230 // typed or deleted. | |
236 NavigationResult(const AutocompleteProvider& provider, | 231 NavigationResult(const AutocompleteProvider& provider, |
237 const GURL& url, | 232 const GURL& url, |
238 const string16& description, | 233 const string16& description, |
239 bool from_keyword_provider, | 234 bool from_keyword_provider, |
240 int relevance, | 235 int relevance, |
241 bool relevance_from_server); | 236 bool relevance_from_server, |
237 const string16& input_text, | |
238 const std::string& languages); | |
242 virtual ~NavigationResult(); | 239 virtual ~NavigationResult(); |
243 | 240 |
244 const GURL& url() const { return url_; } | 241 const GURL& url() const { return url_; } |
245 const string16& description() const { return description_; } | 242 const string16& description() const { return description_; } |
246 const string16& formatted_url() const { return formatted_url_; } | 243 const string16& formatted_url() const { return formatted_url_; } |
247 | 244 |
248 // Result: | 245 // Result: |
249 virtual bool IsInlineable(const string16& input) const OVERRIDE; | 246 virtual bool IsInlineable(const string16& input) const OVERRIDE; |
250 virtual int CalculateRelevance( | 247 virtual int CalculateRelevance( |
251 const AutocompleteInput& input, | 248 const AutocompleteInput& input, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 // indicate that there is no suggested score; a value of 0 | 294 // indicate that there is no suggested score; a value of 0 |
298 // suppresses the verbatim result. | 295 // suppresses the verbatim result. |
299 int verbatim_relevance; | 296 int verbatim_relevance; |
300 | 297 |
301 private: | 298 private: |
302 DISALLOW_COPY_AND_ASSIGN(Results); | 299 DISALLOW_COPY_AND_ASSIGN(Results); |
303 }; | 300 }; |
304 | 301 |
305 virtual ~SearchProvider(); | 302 virtual ~SearchProvider(); |
306 | 303 |
304 // Returns an AutocompleteMatch with the given |autocomplete_provider|, | |
305 // |relevance|, and |type|, which represents a search via |template_url| for | |
306 // |query_string|. If |template_url| is NULL, returns a match with an invalid | |
307 // destination URL. | |
308 // | |
309 // |input_text| is the original user input, which may differ from | |
310 // |query_string|; e.g. the user typed "foo" and got a search suggestion of | |
311 // "food", which we're now marking up. This is used to highlight portions of | |
312 // the match contents to distinguish locally-typed text from suggested text. | |
313 // | |
314 // |input| and |is_keyword| are necessary for various other details, like | |
315 // whether we should allow inline autocompletion and what the transition type | |
316 // should be. |accepted_suggestion| and |omnibox_start_margin| are used along | |
317 // with |input_text| to generate Assisted Query Stats. | |
318 // |append_extra_query_params| should be set if |template_url| is the default | |
319 // search engine, so the destination URL will contain any | |
320 // command-line-specified query params. | |
321 static AutocompleteMatch CreateSearchSuggestion( | |
Mark P
2013/08/15 18:34:23
FYI, this is an exact move except I changed some o
| |
322 AutocompleteProvider* autocomplete_provider, | |
323 const SuggestResult& result, | |
324 AutocompleteMatch::Type type, | |
325 const TemplateURL* template_url, | |
326 const string16& input_text, | |
327 const AutocompleteInput& input, | |
328 int accepted_suggestion, | |
329 int omnibox_start_margin, | |
330 bool append_extra_query_params); | |
331 | |
307 // Removes non-inlineable results until either the top result can inline | 332 // Removes non-inlineable results until either the top result can inline |
308 // autocomplete the current input or verbatim outscores the top result. | 333 // autocomplete the current input or verbatim outscores the top result. |
309 static void RemoveStaleResults(const string16& input, | 334 static void RemoveStaleResults(const string16& input, |
310 int verbatim_relevance, | 335 int verbatim_relevance, |
311 SuggestResults* suggest_results, | 336 SuggestResults* suggest_results, |
312 NavigationResults* navigation_results); | 337 NavigationResults* navigation_results); |
313 | 338 |
314 // Calculates the relevance score for the keyword verbatim result (if the | 339 // Calculates the relevance score for the keyword verbatim result (if the |
315 // input matches one of the profile's keyword). | 340 // input matches one of the profile's keyword). |
316 static int CalculateRelevanceForKeywordVerbatim(AutocompleteInput::Type type, | 341 static int CalculateRelevanceForKeywordVerbatim(AutocompleteInput::Type type, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 // scores. When using the aggressive method, scores may exceed 1300 | 463 // scores. When using the aggressive method, scores may exceed 1300 |
439 // unless |prevent_search_history_inlining| is set. | 464 // unless |prevent_search_history_inlining| is set. |
440 int CalculateRelevanceForHistory(const base::Time& time, | 465 int CalculateRelevanceForHistory(const base::Time& time, |
441 bool is_keyword, | 466 bool is_keyword, |
442 bool use_aggressive_method, | 467 bool use_aggressive_method, |
443 bool prevent_search_history_inlining) const; | 468 bool prevent_search_history_inlining) const; |
444 | 469 |
445 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with | 470 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with |
446 // the supplied relevance. Adds this match to |map|; if such a match already | 471 // the supplied relevance. Adds this match to |map|; if such a match already |
447 // exists, whichever one has lower relevance is eliminated. | 472 // exists, whichever one has lower relevance is eliminated. |
448 void AddMatchToMap(const string16& query_string, | 473 void AddMatchToMap(const SuggestResult& result, |
449 const string16& input_text, | 474 const string16& input_text, |
450 int relevance, | |
451 bool relevance_from_server, | |
452 AutocompleteMatch::Type type, | 475 AutocompleteMatch::Type type, |
453 int accepted_suggestion, | 476 int accepted_suggestion, |
454 bool is_keyword, | |
455 MatchMap* map); | 477 MatchMap* map); |
456 | 478 |
457 // Returns an AutocompleteMatch for a navigational suggestion. | 479 // Returns an AutocompleteMatch for a navigational suggestion. |
458 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); | 480 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); |
459 | 481 |
460 // Resets the scores of all |keyword_navigation_results_| matches to | 482 // Resets the scores of all |keyword_navigation_results_| matches to |
461 // be below that of the top keyword query match (the verbatim match | 483 // be below that of the top keyword query match (the verbatim match |
462 // as expressed by |keyword_verbatim_relevance_| or keyword query | 484 // as expressed by |keyword_verbatim_relevance_| or keyword query |
463 // suggestions stored in |keyword_suggest_results_|). If there | 485 // suggestions stored in |keyword_suggest_results_|). If there |
464 // are no keyword suggestions and keyword verbatim is suppressed, | 486 // are no keyword suggestions and keyword verbatim is suppressed, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 bool field_trial_triggered_in_session_; | 548 bool field_trial_triggered_in_session_; |
527 | 549 |
528 // If true, search history query suggestions will score low enough that | 550 // If true, search history query suggestions will score low enough that |
529 // they will not be inlined. | 551 // they will not be inlined. |
530 bool prevent_search_history_inlining_; | 552 bool prevent_search_history_inlining_; |
531 | 553 |
532 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 554 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
533 }; | 555 }; |
534 | 556 |
535 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 557 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
OLD | NEW |