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 // |
11 // For more information on the autocomplete system in general, including how | 11 // For more information on the autocomplete system in general, including how |
12 // the autocomplete controller and autocomplete providers work, see | 12 // the autocomplete controller and autocomplete providers work, see |
13 // chrome/browser/autocomplete.h. | 13 // chrome/browser/autocomplete.h. |
14 | 14 |
15 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 15 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
16 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 16 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
17 #pragma once | 17 #pragma once |
18 | 18 |
19 #include <map> | 19 #include <map> |
20 #include <string> | 20 #include <string> |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 #include "chrome/browser/autocomplete/autocomplete.h" | 25 #include "chrome/browser/autocomplete/autocomplete.h" |
26 #include "chrome/browser/autocomplete/autocomplete_match.h" | 26 #include "chrome/browser/autocomplete/autocomplete_match.h" |
27 #include "chrome/browser/history/history_types.h" | 27 #include "chrome/browser/history/history_types.h" |
28 #include "chrome/browser/search_engines/template_url.h" | 28 #include "chrome/browser/search_engines/template_url.h" |
29 #include "chrome/browser/search_engines/template_url_id.h" | 29 #include "chrome/browser/search_engines/template_url_id.h" |
30 #include "content/public/common/url_fetcher_delegate.h" | 30 #include "net/url_request/url_fetcher_delegate.h" |
31 | 31 |
32 class Profile; | 32 class Profile; |
33 class TemplateURLService; | 33 class TemplateURLService; |
34 | 34 |
35 namespace base { | 35 namespace base { |
36 class Value; | 36 class Value; |
37 } | 37 } |
38 | 38 |
| 39 namespace net { |
| 40 class URLFetcher; |
| 41 } |
| 42 |
39 // Autocomplete provider for searches and suggestions from a search engine. | 43 // Autocomplete provider for searches and suggestions from a search engine. |
40 // | 44 // |
41 // After construction, the autocomplete controller repeatedly calls Start() | 45 // After construction, the autocomplete controller repeatedly calls Start() |
42 // with some user input, each time expecting to receive a small set of the best | 46 // with some user input, each time expecting to receive a small set of the best |
43 // matches (either synchronously or asynchronously). | 47 // matches (either synchronously or asynchronously). |
44 // | 48 // |
45 // Initially the provider creates a match that searches for the current input | 49 // Initially the provider creates a match that searches for the current input |
46 // text. It also starts a task to query the Suggest servers. When that data | 50 // text. It also starts a task to query the Suggest servers. When that data |
47 // comes back, the provider creates and returns matches for the best | 51 // comes back, the provider creates and returns matches for the best |
48 // suggestions. | 52 // suggestions. |
49 class SearchProvider : public AutocompleteProvider, | 53 class SearchProvider : public AutocompleteProvider, |
50 public content::URLFetcherDelegate { | 54 public net::URLFetcherDelegate { |
51 public: | 55 public: |
52 SearchProvider(ACProviderListener* listener, Profile* profile); | 56 SearchProvider(ACProviderListener* listener, Profile* profile); |
53 | 57 |
54 #if defined(UNIT_TEST) | 58 #if defined(UNIT_TEST) |
55 static void set_query_suggest_immediately(bool value) { | 59 static void set_query_suggest_immediately(bool value) { |
56 query_suggest_immediately_ = value; | 60 query_suggest_immediately_ = value; |
57 } | 61 } |
58 #endif | 62 #endif |
59 | 63 |
60 // Marks the instant query as done. If |input_text| is non-empty this changes | 64 // Marks the instant query as done. If |input_text| is non-empty this changes |
61 // the 'search what you typed' results text to |input_text| + |suggest_text|. | 65 // the 'search what you typed' results text to |input_text| + |suggest_text|. |
62 // |input_text| is the text the user input into the edit. |input_text| differs | 66 // |input_text| is the text the user input into the edit. |input_text| differs |
63 // from |input_.text()| if the input contained whitespace. | 67 // from |input_.text()| if the input contained whitespace. |
64 // | 68 // |
65 // This method also marks the search provider as no longer needing to wait for | 69 // This method also marks the search provider as no longer needing to wait for |
66 // the instant result. | 70 // the instant result. |
67 void FinalizeInstantQuery(const string16& input_text, | 71 void FinalizeInstantQuery(const string16& input_text, |
68 const string16& suggest_text); | 72 const string16& suggest_text); |
69 | 73 |
70 // AutocompleteProvider | 74 // AutocompleteProvider |
71 virtual void Start(const AutocompleteInput& input, | 75 virtual void Start(const AutocompleteInput& input, |
72 bool minimal_changes) OVERRIDE; | 76 bool minimal_changes) OVERRIDE; |
73 virtual void Stop() OVERRIDE; | 77 virtual void Stop() OVERRIDE; |
74 | 78 |
75 // Adds search-provider-specific information to omnibox event logs. | 79 // Adds search-provider-specific information to omnibox event logs. |
76 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; | 80 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; |
77 | 81 |
78 // content::URLFetcherDelegate | 82 // net::URLFetcherDelegate |
79 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 83 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
80 | 84 |
81 // ID used in creating URLFetcher for default provider's suggest results. | 85 // ID used in creating URLFetcher for default provider's suggest results. |
82 static const int kDefaultProviderURLFetcherID; | 86 static const int kDefaultProviderURLFetcherID; |
83 | 87 |
84 // ID used in creating URLFetcher for keyword provider's suggest results. | 88 // ID used in creating URLFetcher for keyword provider's suggest results. |
85 static const int kKeywordProviderURLFetcherID; | 89 static const int kKeywordProviderURLFetcherID; |
86 | 90 |
87 private: | 91 private: |
88 virtual ~SearchProvider(); | 92 virtual ~SearchProvider(); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 217 |
214 // Stops the suggest query. | 218 // Stops the suggest query. |
215 // NOTE: This does not update |done_|. Callers must do so. | 219 // NOTE: This does not update |done_|. Callers must do so. |
216 void StopSuggest(); | 220 void StopSuggest(); |
217 | 221 |
218 // Clears the current results. | 222 // Clears the current results. |
219 void ClearResults(); | 223 void ClearResults(); |
220 | 224 |
221 // Creates a URLFetcher requesting suggest results from the specified | 225 // Creates a URLFetcher requesting suggest results from the specified |
222 // |suggestions_url|. The caller owns the returned URLFetcher. | 226 // |suggestions_url|. The caller owns the returned URLFetcher. |
223 content::URLFetcher* CreateSuggestFetcher( | 227 net::URLFetcher* CreateSuggestFetcher( |
224 int id, | 228 int id, |
225 const TemplateURLRef& suggestions_url, | 229 const TemplateURLRef& suggestions_url, |
226 const string16& text); | 230 const string16& text); |
227 | 231 |
228 // Parses the results from the Suggest server and stores up to kMaxMatches of | 232 // Parses the results from the Suggest server and stores up to kMaxMatches of |
229 // them in |suggest_results|. Returns whether parsing succeeded. | 233 // them in |suggest_results|. Returns whether parsing succeeded. |
230 bool ParseSuggestResults(base::Value* root_val, | 234 bool ParseSuggestResults(base::Value* root_val, |
231 bool is_keyword, | 235 bool is_keyword, |
232 const string16& input_text, | 236 const string16& input_text, |
233 SuggestResults* suggest_results); | 237 SuggestResults* suggest_results); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 int suggest_results_pending_; | 323 int suggest_results_pending_; |
320 | 324 |
321 // A timer to start a query to the suggest server after the user has stopped | 325 // A timer to start a query to the suggest server after the user has stopped |
322 // typing for long enough. | 326 // typing for long enough. |
323 base::OneShotTimer<SearchProvider> timer_; | 327 base::OneShotTimer<SearchProvider> timer_; |
324 | 328 |
325 // The time at which we sent a query to the suggest server. | 329 // The time at which we sent a query to the suggest server. |
326 base::TimeTicks time_suggest_request_sent_; | 330 base::TimeTicks time_suggest_request_sent_; |
327 | 331 |
328 // The fetcher that retrieves suggest results for the keyword from the server. | 332 // The fetcher that retrieves suggest results for the keyword from the server. |
329 scoped_ptr<content::URLFetcher> keyword_fetcher_; | 333 scoped_ptr<net::URLFetcher> keyword_fetcher_; |
330 | 334 |
331 // The fetcher that retrieves suggest results for the default engine from the | 335 // The fetcher that retrieves suggest results for the default engine from the |
332 // server. | 336 // server. |
333 scoped_ptr<content::URLFetcher> default_fetcher_; | 337 scoped_ptr<net::URLFetcher> default_fetcher_; |
334 | 338 |
335 // Suggestions returned by the Suggest server for the input text. | 339 // Suggestions returned by the Suggest server for the input text. |
336 SuggestResults keyword_suggest_results_; | 340 SuggestResults keyword_suggest_results_; |
337 SuggestResults default_suggest_results_; | 341 SuggestResults default_suggest_results_; |
338 | 342 |
339 // Navigational suggestions returned by the server. | 343 // Navigational suggestions returned by the server. |
340 NavigationResults keyword_navigation_results_; | 344 NavigationResults keyword_navigation_results_; |
341 NavigationResults default_navigation_results_; | 345 NavigationResults default_navigation_results_; |
342 | 346 |
343 // Whether suggest_results_ is valid. | 347 // Whether suggest_results_ is valid. |
344 bool have_suggest_results_; | 348 bool have_suggest_results_; |
345 | 349 |
346 // Has FinalizeInstantQuery been invoked since the last |Start|? | 350 // Has FinalizeInstantQuery been invoked since the last |Start|? |
347 bool instant_finalized_; | 351 bool instant_finalized_; |
348 | 352 |
349 // The |suggest_text| parameter passed to FinalizeInstantQuery. | 353 // The |suggest_text| parameter passed to FinalizeInstantQuery. |
350 string16 default_provider_suggest_text_; | 354 string16 default_provider_suggest_text_; |
351 | 355 |
352 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 356 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
353 }; | 357 }; |
354 | 358 |
355 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 359 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
OLD | NEW |