OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 class contains common functionality for search-based autocomplete | 5 // This class contains common functionality for search-based autocomplete |
6 // providers. Search provider and zero suggest provider both use it for common | 6 // providers. Search provider and zero suggest provider both use it for common |
7 // functionality. | 7 // functionality. |
8 | 8 |
9 #ifndef CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ | 9 #ifndef CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ |
10 #define CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ | 10 #define CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ |
11 | 11 |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
19 #include "components/metrics/proto/omnibox_event.pb.h" | 19 #include "components/metrics/proto/omnibox_event.pb.h" |
20 #include "components/omnibox/autocomplete_input.h" | 20 #include "components/omnibox/autocomplete_input.h" |
21 #include "components/omnibox/autocomplete_match.h" | 21 #include "components/omnibox/autocomplete_match.h" |
22 #include "components/omnibox/autocomplete_provider.h" | 22 #include "components/omnibox/autocomplete_provider.h" |
23 #include "components/omnibox/search_suggestion_parser.h" | 23 #include "components/omnibox/search_suggestion_parser.h" |
24 #include "net/url_request/url_fetcher_delegate.h" | |
25 | 24 |
26 class AutocompleteProviderListener; | |
27 class GURL; | 25 class GURL; |
28 class Profile; | 26 class Profile; |
29 class SearchTermsData; | 27 class SearchTermsData; |
30 class SuggestionDeletionHandler; | 28 class SuggestionDeletionHandler; |
31 class TemplateURL; | 29 class TemplateURL; |
32 class TemplateURLService; | 30 class TemplateURLService; |
33 | 31 |
34 namespace base { | 32 namespace base { |
35 class DictionaryValue; | 33 class DictionaryValue; |
36 class ListValue; | 34 class ListValue; |
37 class Value; | 35 class Value; |
38 } | 36 } |
39 | 37 |
40 // Base functionality for receiving suggestions from a search engine. | 38 // Base functionality for receiving suggestions from a search engine. |
41 // This class is abstract and should only be used as a base for other | 39 // This class is abstract and should only be used as a base for other |
42 // autocomplete providers utilizing its functionality. | 40 // autocomplete providers utilizing its functionality. |
43 class BaseSearchProvider : public AutocompleteProvider, | 41 class BaseSearchProvider : public AutocompleteProvider { |
44 public net::URLFetcherDelegate { | |
45 public: | 42 public: |
46 // ID used in creating URLFetcher for default provider's suggest results. | 43 // ID used in creating URLFetcher for default provider's suggest results. |
47 static const int kDefaultProviderURLFetcherID; | 44 static const int kDefaultProviderURLFetcherID; |
48 | 45 |
49 // ID used in creating URLFetcher for keyword provider's suggest results. | 46 // ID used in creating URLFetcher for keyword provider's suggest results. |
50 static const int kKeywordProviderURLFetcherID; | 47 static const int kKeywordProviderURLFetcherID; |
51 | 48 |
52 // ID used in creating URLFetcher for deleting suggestion results. | 49 // ID used in creating URLFetcher for deleting suggestion results. |
53 static const int kDeletionURLFetcherID; | 50 static const int kDeletionURLFetcherID; |
54 | 51 |
55 BaseSearchProvider(AutocompleteProviderListener* listener, | 52 BaseSearchProvider(TemplateURLService* template_url_service, |
56 TemplateURLService* template_url_service, | |
57 Profile* profile, | 53 Profile* profile, |
58 AutocompleteProvider::Type type); | 54 AutocompleteProvider::Type type); |
59 | 55 |
60 // Returns whether |match| is flagged as a query that should be prefetched. | 56 // Returns whether |match| is flagged as a query that should be prefetched. |
61 static bool ShouldPrefetch(const AutocompleteMatch& match); | 57 static bool ShouldPrefetch(const AutocompleteMatch& match); |
62 | 58 |
63 // Returns a simpler AutocompleteMatch suitable for persistence like in | 59 // Returns a simpler AutocompleteMatch suitable for persistence like in |
64 // ShortcutsDatabase. | 60 // ShortcutsDatabase. |
65 // NOTE: Use with care. Most likely you want the other CreateSearchSuggestion | 61 // NOTE: Use with care. Most likely you want the other CreateSearchSuggestion |
66 // with protected access. | 62 // with protected access. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // may change this to be a standalone setting or part of some explicit | 161 // may change this to be a standalone setting or part of some explicit |
166 // general opt-in. | 162 // general opt-in. |
167 static bool CanSendURL( | 163 static bool CanSendURL( |
168 const GURL& current_page_url, | 164 const GURL& current_page_url, |
169 const GURL& suggest_url, | 165 const GURL& suggest_url, |
170 const TemplateURL* template_url, | 166 const TemplateURL* template_url, |
171 metrics::OmniboxEventProto::PageClassification page_classification, | 167 metrics::OmniboxEventProto::PageClassification page_classification, |
172 const SearchTermsData& search_terms_data, | 168 const SearchTermsData& search_terms_data, |
173 Profile* profile); | 169 Profile* profile); |
174 | 170 |
175 // net::URLFetcherDelegate: | |
176 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
177 | |
178 // If the |deletion_url| is valid, then set |match.deletable| to true and | 171 // If the |deletion_url| is valid, then set |match.deletable| to true and |
179 // save the |deletion_url| into the |match|'s additional info under | 172 // save the |deletion_url| into the |match|'s additional info under |
180 // the key |kDeletionUrlKey|. | 173 // the key |kDeletionUrlKey|. |
181 void SetDeletionURL(const std::string& deletion_url, | 174 void SetDeletionURL(const std::string& deletion_url, |
182 AutocompleteMatch* match); | 175 AutocompleteMatch* match); |
183 | 176 |
184 // Creates an AutocompleteMatch from |result| to search for the query in | 177 // Creates an AutocompleteMatch from |result| to search for the query in |
185 // |result|. Adds the created match to |map|; if such a match | 178 // |result|. Adds the created match to |map|; if such a match |
186 // already exists, whichever one has lower relevance is eliminated. | 179 // already exists, whichever one has lower relevance is eliminated. |
187 // |metadata| and |accepted_suggestion| are used for generating an | 180 // |metadata| and |accepted_suggestion| are used for generating an |
188 // AutocompleteMatch. | 181 // AutocompleteMatch. |
189 // |mark_as_deletable| indicates whether the match should be marked deletable. | 182 // |mark_as_deletable| indicates whether the match should be marked deletable. |
190 // NOTE: Any result containing a deletion URL is always marked deletable. | 183 // NOTE: Any result containing a deletion URL is always marked deletable. |
191 void AddMatchToMap(const SearchSuggestionParser::SuggestResult& result, | 184 void AddMatchToMap(const SearchSuggestionParser::SuggestResult& result, |
192 const std::string& metadata, | 185 const std::string& metadata, |
193 int accepted_suggestion, | 186 int accepted_suggestion, |
194 bool mark_as_deletable, | 187 bool mark_as_deletable, |
195 MatchMap* map); | 188 MatchMap* map); |
196 | 189 |
197 // Parses results from the suggest server and updates the appropriate suggest | 190 // Parses results from the suggest server and updates the appropriate suggest |
198 // and navigation result lists in |results|. |is_keyword_result| indicates | 191 // and navigation result lists in |results|. |default_result_relevance| is |
199 // whether the response was received from the keyword provider. | 192 // the relevance to use if it was not explicitly set by the server. |
| 193 // |is_keyword_result| indicates whether the response was received from the |
| 194 // keyword provider. |
200 // Returns whether the appropriate result list members were updated. | 195 // Returns whether the appropriate result list members were updated. |
201 bool ParseSuggestResults(const base::Value& root_val, | 196 bool ParseSuggestResults(const base::Value& root_val, |
| 197 int default_result_relevance, |
202 bool is_keyword_result, | 198 bool is_keyword_result, |
203 SearchSuggestionParser::Results* results); | 199 SearchSuggestionParser::Results* results); |
204 | 200 |
205 // Called at the end of ParseSuggestResults to rank the |results|. | |
206 virtual void SortResults(bool is_keyword, | |
207 SearchSuggestionParser::Results* results); | |
208 | |
209 // Optionally, cache the received |json_data| and return true if we want | |
210 // to stop processing results at this point. The |parsed_data| is the parsed | |
211 // version of |json_data| used to determine if we received an empty result. | |
212 virtual bool StoreSuggestionResponse(const std::string& json_data, | |
213 const base::Value& parsed_data); | |
214 | |
215 // Returns the TemplateURL corresponding to the keyword or default | 201 // Returns the TemplateURL corresponding to the keyword or default |
216 // provider based on the value of |is_keyword|. | 202 // provider based on the value of |is_keyword|. |
217 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const = 0; | 203 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const = 0; |
218 | 204 |
219 // Returns the AutocompleteInput for keyword provider or default provider | 205 // Returns the AutocompleteInput for keyword provider or default provider |
220 // based on the value of |is_keyword|. | 206 // based on the value of |is_keyword|. |
221 virtual const AutocompleteInput GetInput(bool is_keyword) const = 0; | 207 virtual const AutocompleteInput GetInput(bool is_keyword) const = 0; |
222 | 208 |
223 // Returns a pointer to a Results object, which will hold suggest results. | |
224 virtual SearchSuggestionParser::Results* GetResultsToFill( | |
225 bool is_keyword) = 0; | |
226 | |
227 // Returns whether the destination URL corresponding to the given |result| | 209 // Returns whether the destination URL corresponding to the given |result| |
228 // should contain command-line-specified query params. | 210 // should contain command-line-specified query params. |
229 virtual bool ShouldAppendExtraParams( | 211 virtual bool ShouldAppendExtraParams( |
230 const SearchSuggestionParser::SuggestResult& result) const = 0; | 212 const SearchSuggestionParser::SuggestResult& result) const = 0; |
231 | 213 |
232 // Stops the suggest query. | 214 // Stops the suggest query. |
233 // NOTE: This does not update |done_|. Callers must do so. | 215 // NOTE: This does not update |done_|. Callers must do so. |
234 virtual void StopSuggest() = 0; | 216 virtual void StopSuggest() = 0; |
235 | 217 |
236 // Clears the current results. | 218 // Clears the current results. |
237 virtual void ClearAllResults() = 0; | 219 virtual void ClearAllResults() = 0; |
238 | 220 |
239 // Returns the relevance to use if it was not explicitly set by the server. | |
240 virtual int GetDefaultResultRelevance() const = 0; | |
241 | |
242 // Records in UMA whether the deletion request resulted in success. | 221 // Records in UMA whether the deletion request resulted in success. |
243 virtual void RecordDeletionResult(bool success) = 0; | 222 virtual void RecordDeletionResult(bool success) = 0; |
244 | 223 |
245 // Records UMA statistics about a suggest server response. | |
246 virtual void LogFetchComplete(bool succeeded, bool is_keyword) = 0; | |
247 | |
248 // Modify provider-specific UMA statistics. | 224 // Modify provider-specific UMA statistics. |
249 virtual void ModifyProviderInfo( | 225 virtual void ModifyProviderInfo( |
250 metrics::OmniboxEventProto_ProviderInfo* provider_info) const; | 226 metrics::OmniboxEventProto_ProviderInfo* provider_info) const; |
251 | 227 |
252 // Returns whether the |fetcher| is for the keyword provider. | |
253 virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const = 0; | |
254 | |
255 // Updates |matches_| from the latest results; applies calculated relevances | |
256 // if suggested relevances cause undesirable behavior. Updates |done_|. | |
257 virtual void UpdateMatches() = 0; | |
258 | |
259 AutocompleteProviderListener* listener_; | |
260 TemplateURLService* template_url_service_; | 228 TemplateURLService* template_url_service_; |
261 Profile* profile_; | 229 Profile* profile_; |
262 | 230 |
263 // Whether a field trial, if any, has triggered in the most recent | 231 // Whether a field trial, if any, has triggered in the most recent |
264 // autocomplete query. This field is set to true only if the suggestion | 232 // autocomplete query. This field is set to true only if the suggestion |
265 // provider has completed and the response contained | 233 // provider has completed and the response contained |
266 // '"google:fieldtrialtriggered":true'. | 234 // '"google:fieldtrialtriggered":true'. |
267 bool field_trial_triggered_; | 235 bool field_trial_triggered_; |
268 | 236 |
269 // Same as above except that it is maintained across the current Omnibox | 237 // Same as above except that it is maintained across the current Omnibox |
270 // session. | 238 // session. |
271 bool field_trial_triggered_in_session_; | 239 bool field_trial_triggered_in_session_; |
272 | 240 |
273 // The number of suggest results that haven't yet arrived. If it's greater | |
274 // than 0, it indicates that one of the URLFetchers is still running. | |
275 int suggest_results_pending_; | |
276 | |
277 private: | 241 private: |
278 friend class SearchProviderTest; | 242 friend class SearchProviderTest; |
279 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch); | 243 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch); |
280 | 244 |
281 // Removes the deleted |match| from the list of |matches_|. | 245 // Removes the deleted |match| from the list of |matches_|. |
282 void DeleteMatchFromMatches(const AutocompleteMatch& match); | 246 void DeleteMatchFromMatches(const AutocompleteMatch& match); |
283 | 247 |
284 // This gets called when we have requested a suggestion deletion from the | 248 // This gets called when we have requested a suggestion deletion from the |
285 // server to handle the results of the deletion. It will be called after the | 249 // server to handle the results of the deletion. It will be called after the |
286 // deletion request completes. | 250 // deletion request completes. |
287 void OnDeletionComplete(bool success, | 251 void OnDeletionComplete(bool success, |
288 SuggestionDeletionHandler* handler); | 252 SuggestionDeletionHandler* handler); |
289 | 253 |
290 // Each deletion handler in this vector corresponds to an outstanding request | 254 // Each deletion handler in this vector corresponds to an outstanding request |
291 // that a server delete a personalized suggestion. Making this a ScopedVector | 255 // that a server delete a personalized suggestion. Making this a ScopedVector |
292 // causes us to auto-cancel all such requests on shutdown. | 256 // causes us to auto-cancel all such requests on shutdown. |
293 SuggestionDeletionHandlers deletion_handlers_; | 257 SuggestionDeletionHandlers deletion_handlers_; |
294 | 258 |
295 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); | 259 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); |
296 }; | 260 }; |
297 | 261 |
298 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ | 262 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ |
OLD | NEW |