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 <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
24 #include "chrome/browser/autocomplete/autocomplete_input.h" | 24 #include "chrome/browser/autocomplete/autocomplete_input.h" |
msw
2014/02/03 22:53:14
nit: you can optionally remove includes here that
Maria
2014/02/04 03:16:39
Done.
| |
25 #include "chrome/browser/autocomplete/autocomplete_match.h" | 25 #include "chrome/browser/autocomplete/autocomplete_match.h" |
26 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 26 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
27 #include "chrome/browser/autocomplete/base_search_provider.h" | |
27 #include "chrome/browser/history/history_types.h" | 28 #include "chrome/browser/history/history_types.h" |
28 #include "chrome/browser/search_engines/template_url.h" | 29 #include "chrome/browser/search_engines/template_url.h" |
29 #include "net/url_request/url_fetcher_delegate.h" | 30 #include "net/url_request/url_fetcher_delegate.h" |
30 | 31 |
31 class Profile; | 32 class Profile; |
32 class SearchProviderTest; | 33 class SearchProviderTest; |
33 class SuggestionDeletionHandler; | 34 class SuggestionDeletionHandler; |
34 class TemplateURLService; | 35 class TemplateURLService; |
35 | 36 |
36 namespace base { | 37 namespace base { |
37 class Value; | 38 class Value; |
38 } | 39 } |
39 | 40 |
40 namespace net { | 41 namespace net { |
41 class URLFetcher; | 42 class URLFetcher; |
42 } | 43 } |
43 | 44 |
44 // Autocomplete provider for searches and suggestions from a search engine. | 45 // Autocomplete provider for searches and suggestions from a search engine. |
45 // | 46 // |
46 // After construction, the autocomplete controller repeatedly calls Start() | 47 // After construction, the autocomplete controller repeatedly calls Start() |
47 // with some user input, each time expecting to receive a small set of the best | 48 // with some user input, each time expecting to receive a small set of the best |
48 // matches (either synchronously or asynchronously). | 49 // matches (either synchronously or asynchronously). |
49 // | 50 // |
50 // Initially the provider creates a match that searches for the current input | 51 // Initially the provider creates a match that searches for the current input |
51 // text. It also starts a task to query the Suggest servers. When that data | 52 // text. It also starts a task to query the Suggest servers. When that data |
52 // comes back, the provider creates and returns matches for the best | 53 // comes back, the provider creates and returns matches for the best |
53 // suggestions. | 54 // suggestions. |
54 class SearchProvider : public AutocompleteProvider, | 55 class SearchProvider : public BaseSearchProvider { |
55 public net::URLFetcherDelegate { | |
56 public: | 56 public: |
57 // ID used in creating URLFetcher for default provider's suggest results. | 57 // ID used in creating URLFetcher for default provider's suggest results. |
58 static const int kDefaultProviderURLFetcherID; | 58 static const int kDefaultProviderURLFetcherID; |
59 | 59 |
60 // ID used in creating URLFetcher for keyword provider's suggest results. | 60 // ID used in creating URLFetcher for keyword provider's suggest results. |
61 static const int kKeywordProviderURLFetcherID; | 61 static const int kKeywordProviderURLFetcherID; |
62 | 62 |
63 // ID used in creating URLFetcher for deleting suggestion results. | 63 // ID used in creating URLFetcher for deleting suggestion results. |
64 static const int kDeletionURLFetcherID; | 64 static const int kDeletionURLFetcherID; |
65 | 65 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 TemplateURLService* template_url_service_; | 146 TemplateURLService* template_url_service_; |
147 | 147 |
148 // Cached across the life of a query so we behave consistently even if the | 148 // Cached across the life of a query so we behave consistently even if the |
149 // user changes their default while the query is running. | 149 // user changes their default while the query is running. |
150 base::string16 default_provider_; | 150 base::string16 default_provider_; |
151 base::string16 keyword_provider_; | 151 base::string16 keyword_provider_; |
152 | 152 |
153 DISALLOW_COPY_AND_ASSIGN(Providers); | 153 DISALLOW_COPY_AND_ASSIGN(Providers); |
154 }; | 154 }; |
155 | 155 |
156 // The Result classes are intermediate representations of AutocompleteMatches, | |
157 // simply containing relevance-ranked search and navigation suggestions. | |
158 // They may be cached to provide some synchronous matches while requests for | |
159 // new suggestions from updated input are in flight. | |
160 // TODO(msw) Extend these classes to generate their corresponding matches and | |
161 // other requisite data, in order to consolidate and simplify the | |
162 // highly fragmented SearchProvider logic for each Result type. | |
163 class Result { | |
164 public: | |
165 Result(bool from_keyword_provider, | |
166 int relevance, | |
167 bool relevance_from_server); | |
168 virtual ~Result(); | |
169 | |
170 bool from_keyword_provider() const { return from_keyword_provider_; } | |
171 | |
172 const base::string16& match_contents() const { return match_contents_; } | |
173 const ACMatchClassifications& match_contents_class() const { | |
174 return match_contents_class_; | |
175 } | |
176 | |
177 int relevance() const { return relevance_; } | |
178 void set_relevance(int relevance) { relevance_ = relevance; } | |
179 | |
180 bool relevance_from_server() const { return relevance_from_server_; } | |
181 void set_relevance_from_server(bool relevance_from_server) { | |
182 relevance_from_server_ = relevance_from_server; | |
183 } | |
184 | |
185 // Returns if this result is inlineable against the current input |input|. | |
186 // Non-inlineable results are stale. | |
187 virtual bool IsInlineable(const base::string16& input) const = 0; | |
188 | |
189 // Returns the default relevance value for this result (which may | |
190 // be left over from a previous omnibox input) given the current | |
191 // input and whether the current input caused a keyword provider | |
192 // to be active. | |
193 virtual int CalculateRelevance(const AutocompleteInput& input, | |
194 bool keyword_provider_requested) const = 0; | |
195 | |
196 protected: | |
197 // The contents to be displayed and its style info. | |
198 base::string16 match_contents_; | |
199 ACMatchClassifications match_contents_class_; | |
200 | |
201 // True if the result came from the keyword provider. | |
202 bool from_keyword_provider_; | |
203 | |
204 // The relevance score. | |
205 int relevance_; | |
206 | |
207 private: | |
208 // Whether this result's relevance score was fully or partly calculated | |
209 // based on server information, and thus is assumed to be more accurate. | |
210 // This is ultimately used in | |
211 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments | |
212 // there. | |
213 bool relevance_from_server_; | |
214 }; | |
215 | |
216 class SuggestResult : public Result { | |
217 public: | |
218 SuggestResult(const base::string16& suggestion, | |
219 AutocompleteMatchType::Type type, | |
220 const base::string16& match_contents, | |
221 const base::string16& annotation, | |
222 const std::string& suggest_query_params, | |
223 const std::string& deletion_url, | |
224 bool from_keyword_provider, | |
225 int relevance, | |
226 bool relevance_from_server, | |
227 bool should_prefetch, | |
228 const base::string16& input_text); | |
229 virtual ~SuggestResult(); | |
230 | |
231 const base::string16& suggestion() const { return suggestion_; } | |
232 AutocompleteMatchType::Type type() const { return type_; } | |
233 const base::string16& annotation() const { return annotation_; } | |
234 const std::string& suggest_query_params() const { | |
235 return suggest_query_params_; | |
236 } | |
237 const std::string& deletion_url() const { return deletion_url_; } | |
238 bool should_prefetch() const { return should_prefetch_; } | |
239 | |
240 // Fills in |match_contents_class_| to reflect how |match_contents_| should | |
241 // be displayed and bolded against the current |input_text|. If | |
242 // |allow_bolding_all| is false and |match_contents_class_| would have all | |
243 // of |match_contents_| bolded, do nothing. | |
244 void ClassifyMatchContents(const bool allow_bolding_all, | |
245 const base::string16& input_text); | |
246 | |
247 // Result: | |
248 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; | |
249 virtual int CalculateRelevance( | |
250 const AutocompleteInput& input, | |
251 bool keyword_provider_requested) const OVERRIDE; | |
252 | |
253 private: | |
254 // The search terms to be used for this suggestion. | |
255 base::string16 suggestion_; | |
256 | |
257 AutocompleteMatchType::Type type_; | |
258 | |
259 // Optional annotation for the |match_contents_| for disambiguation. | |
260 // This may be displayed in the autocomplete match contents, but is defined | |
261 // separately to facilitate different formatting. | |
262 base::string16 annotation_; | |
263 | |
264 // Optional additional parameters to be added to the search URL. | |
265 std::string suggest_query_params_; | |
266 | |
267 // Optional deletion URL provided with suggestions. Fetching this URL | |
268 // should result in some reasonable deletion behaviour on the server, | |
269 // e.g. deleting this term out of a user's server-side search history. | |
270 std::string deletion_url_; | |
271 | |
272 // Should this result be prefetched? | |
273 bool should_prefetch_; | |
274 }; | |
275 | |
276 class NavigationResult : public Result { | |
277 public: | |
278 // |provider| is necessary to use StringForURLDisplay() in order to | |
279 // compute |formatted_url_|. | |
280 NavigationResult(const AutocompleteProvider& provider, | |
281 const GURL& url, | |
282 const base::string16& description, | |
283 bool from_keyword_provider, | |
284 int relevance, | |
285 bool relevance_from_server, | |
286 const base::string16& input_text, | |
287 const std::string& languages); | |
288 virtual ~NavigationResult(); | |
289 | |
290 const GURL& url() const { return url_; } | |
291 const base::string16& description() const { return description_; } | |
292 const base::string16& formatted_url() const { return formatted_url_; } | |
293 | |
294 // Fills in |match_contents_| and |match_contents_class_| to reflect how | |
295 // the URL should be displayed and bolded against the current |input_text| | |
296 // and user |languages|. If |allow_bolding_nothing| is false and | |
297 // |match_contents_class_| would result in an entirely unbolded | |
298 // |match_contents_|, do nothing. | |
299 void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing, | |
300 const base::string16& input_text, | |
301 const std::string& languages); | |
302 | |
303 // Result: | |
304 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; | |
305 virtual int CalculateRelevance( | |
306 const AutocompleteInput& input, | |
307 bool keyword_provider_requested) const OVERRIDE; | |
308 | |
309 private: | |
310 // The suggested url for navigation. | |
311 GURL url_; | |
312 | |
313 // The properly formatted ("fixed up") URL string with equivalent meaning | |
314 // to the one in |url_|. | |
315 base::string16 formatted_url_; | |
316 | |
317 // The suggested navigational result description; generally the site name. | |
318 base::string16 description_; | |
319 }; | |
320 | |
321 class CompareScoredResults; | 156 class CompareScoredResults; |
322 | 157 |
323 typedef std::vector<SuggestResult> SuggestResults; | |
324 typedef std::vector<NavigationResult> NavigationResults; | |
325 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; | 158 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; |
326 typedef std::pair<base::string16, std::string> MatchKey; | |
327 typedef std::map<MatchKey, AutocompleteMatch> MatchMap; | |
328 typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers; | 159 typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers; |
329 | 160 |
330 // A simple structure bundling most of the information (including | |
331 // both SuggestResults and NavigationResults) returned by a call to | |
332 // the suggest server. | |
333 // | |
334 // This has to be declared after the typedefs since it relies on some of them. | |
335 struct Results { | |
336 Results(); | |
337 ~Results(); | |
338 | |
339 // Clears |suggest_results| and |navigation_results| and resets | |
340 // |verbatim_relevance| to -1 (implies unset). | |
341 void Clear(); | |
342 | |
343 // Returns whether any of the results (including verbatim) have | |
344 // server-provided scores. | |
345 bool HasServerProvidedScores() const; | |
346 | |
347 // Query suggestions sorted by relevance score. | |
348 SuggestResults suggest_results; | |
349 | |
350 // Navigational suggestions sorted by relevance score. | |
351 NavigationResults navigation_results; | |
352 | |
353 // The server supplied verbatim relevance scores. Negative values | |
354 // indicate that there is no suggested score; a value of 0 | |
355 // suppresses the verbatim result. | |
356 int verbatim_relevance; | |
357 | |
358 // The JSON metadata associated with this server response. | |
359 std::string metadata; | |
360 | |
361 private: | |
362 DISALLOW_COPY_AND_ASSIGN(Results); | |
363 }; | |
364 | |
365 // Returns an AutocompleteMatch with the given |autocomplete_provider| | 161 // Returns an AutocompleteMatch with the given |autocomplete_provider| |
366 // for the search |suggestion|, which represents a search via |template_url|. | 162 // for the search |suggestion|, which represents a search via |template_url|. |
367 // If |template_url| is NULL, returns a match with an invalid destination URL. | 163 // If |template_url| is NULL, returns a match with an invalid destination URL. |
368 // | 164 // |
369 // |input_text| is the original user input. This is used to highlight | 165 // |input_text| is the original user input. This is used to highlight |
370 // portions of the match contents to distinguish locally-typed text from | 166 // portions of the match contents to distinguish locally-typed text from |
371 // suggested text. | 167 // suggested text. |
372 // | 168 // |
373 // |input| is necessary for various other details, like whether we should | 169 // |input| is necessary for various other details, like whether we should |
374 // allow inline autocompletion and what the transition type should be. | 170 // allow inline autocompletion and what the transition type should be. |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
692 // If true, search history query suggestions will score low enough that | 488 // If true, search history query suggestions will score low enough that |
693 // they will not be inlined. | 489 // they will not be inlined. |
694 bool prevent_search_history_inlining_; | 490 bool prevent_search_history_inlining_; |
695 | 491 |
696 GURL current_page_url_; | 492 GURL current_page_url_; |
697 | 493 |
698 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 494 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
699 }; | 495 }; |
700 | 496 |
701 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 497 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
OLD | NEW |