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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; | 144 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; |
142 | 145 |
143 // Removes non-inlineable results until either the top result can inline | 146 // Removes non-inlineable results until either the top result can inline |
144 // autocomplete the current input or verbatim outscores the top result. | 147 // autocomplete the current input or verbatim outscores the top result. |
145 static void RemoveStaleResults( | 148 static void RemoveStaleResults( |
146 const base::string16& input, | 149 const base::string16& input, |
147 int verbatim_relevance, | 150 int verbatim_relevance, |
148 SearchSuggestionParser::SuggestResults* suggest_results, | 151 SearchSuggestionParser::SuggestResults* suggest_results, |
149 SearchSuggestionParser::NavigationResults* navigation_results); | 152 SearchSuggestionParser::NavigationResults* navigation_results); |
150 | 153 |
151 // Recalculates the match contents class of |results| to better display | |
152 // against the current input and user's language. | |
153 void UpdateMatchContentsClass(const base::string16& input_text, | |
154 SearchSuggestionParser::Results* results); | |
155 | |
156 // Calculates the relevance score for the keyword verbatim result (if the | 154 // Calculates the relevance score for the keyword verbatim result (if the |
157 // input matches one of the profile's keyword). | 155 // input matches one of the profile's keyword). |
158 static int CalculateRelevanceForKeywordVerbatim( | 156 static int CalculateRelevanceForKeywordVerbatim( |
159 metrics::OmniboxInputType::Type type, | 157 metrics::OmniboxInputType::Type type, |
160 bool prefer_keyword); | 158 bool prefer_keyword); |
161 | 159 |
162 // AutocompleteProvider: | 160 // AutocompleteProvider: |
163 virtual void Start(const AutocompleteInput& input, | 161 virtual void Start(const AutocompleteInput& input, |
164 bool minimal_changes) OVERRIDE; | 162 bool minimal_changes) OVERRIDE; |
165 | 163 |
166 // BaseSearchProvider: | 164 // BaseSearchProvider: |
167 virtual void SortResults(bool is_keyword, | |
168 SearchSuggestionParser::Results* results) OVERRIDE; | |
169 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; | 165 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; |
170 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; | 166 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; |
171 virtual SearchSuggestionParser::Results* GetResultsToFill( | |
172 bool is_keyword) OVERRIDE; | |
173 virtual bool ShouldAppendExtraParams( | 167 virtual bool ShouldAppendExtraParams( |
174 const SearchSuggestionParser::SuggestResult& result) const OVERRIDE; | 168 const SearchSuggestionParser::SuggestResult& result) const OVERRIDE; |
175 virtual void StopSuggest() OVERRIDE; | 169 virtual void StopSuggest() OVERRIDE; |
176 virtual void ClearAllResults() OVERRIDE; | 170 virtual void ClearAllResults() OVERRIDE; |
177 virtual int GetDefaultResultRelevance() const OVERRIDE; | |
178 virtual void RecordDeletionResult(bool success) OVERRIDE; | 171 virtual void RecordDeletionResult(bool success) OVERRIDE; |
179 virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE; | 172 |
180 virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE; | 173 // net::URLFetcherDelegate: |
181 virtual void UpdateMatches() OVERRIDE; | 174 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 175 |
| 176 // Recalculates the match contents class of |results| to better display |
| 177 // against the current input and user's language. |
| 178 void UpdateMatchContentsClass(const base::string16& input_text, |
| 179 SearchSuggestionParser::Results* results); |
| 180 |
| 181 // Called after ParseSuggestResults to rank the |results|. |
| 182 void SortResults(bool is_keyword, SearchSuggestionParser::Results* results); |
| 183 |
| 184 // Records UMA statistics about a suggest server response. |
| 185 void LogFetchComplete(bool success, bool is_keyword); |
| 186 |
| 187 // Updates |matches_| from the latest results; applies calculated relevances |
| 188 // if suggested relevances cause undesirable behavior. Updates |done_|. |
| 189 void UpdateMatches(); |
182 | 190 |
183 // Called when timer_ expires. | 191 // Called when timer_ expires. |
184 void Run(); | 192 void Run(); |
185 | 193 |
186 // Runs the history query, if necessary. The history query is synchronous. | 194 // Runs the history query, if necessary. The history query is synchronous. |
187 // This does not update |done_|. | 195 // This does not update |done_|. |
188 void DoHistoryQuery(bool minimal_changes); | 196 void DoHistoryQuery(bool minimal_changes); |
189 | 197 |
190 // Determines whether an asynchronous subcomponent query should run for the | 198 // Determines whether an asynchronous subcomponent query should run for the |
191 // current input. If so, starts it if necessary; otherwise stops it. | 199 // current input. If so, starts it if necessary; otherwise stops it. |
(...skipping 108 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 |