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_ |
(...skipping 21 matching lines...) Expand all Loading... | |
32 // Base functionality for receiving suggestions from a search engine. | 32 // Base functionality for receiving suggestions from a search engine. |
33 // This class is abstract and should only be used as a base for other | 33 // This class is abstract and should only be used as a base for other |
34 // autocomplete providers utilizing its functionality. | 34 // autocomplete providers utilizing its functionality. |
35 class BaseSearchProvider : public AutocompleteProvider, | 35 class BaseSearchProvider : public AutocompleteProvider, |
36 public net::URLFetcherDelegate { | 36 public net::URLFetcherDelegate { |
37 public: | 37 public: |
38 BaseSearchProvider(AutocompleteProviderListener* listener, | 38 BaseSearchProvider(AutocompleteProviderListener* listener, |
39 Profile* profile, | 39 Profile* profile, |
40 AutocompleteProvider::Type type); | 40 AutocompleteProvider::Type type); |
41 | 41 |
42 // Returns whether |match| is flagged as a query that should be prefetched. | |
Mark P
2014/02/12 23:35:49
Are only queries allowed to be prefetched, or do p
Maria
2014/02/13 21:07:36
This is only used to prefetch Google search page r
| |
43 static bool ShouldPrefetch(const AutocompleteMatch& match); | |
44 | |
42 // AutocompleteProvider: | 45 // AutocompleteProvider: |
43 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; | 46 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; |
44 | 47 |
45 bool field_trial_triggered_in_session() const { | 48 bool field_trial_triggered_in_session() const { |
46 return field_trial_triggered_in_session_; | 49 return field_trial_triggered_in_session_; |
47 } | 50 } |
48 | 51 |
49 protected: | 52 protected: |
53 // The following keys are used to record additional information on matches. | |
54 | |
55 // We annotate our AutocompleteMatches with whether their relevance scores | |
56 // were server-provided using this key in the |additional_info| field. | |
57 static const char kRelevanceFromServerKey[]; | |
58 | |
59 // Indicates whether the server said a match should be prefetched. | |
60 static const char kShouldPrefetchKey[]; | |
61 | |
62 // Used to store metadata from the server response, which is needed for | |
63 // prefetching. | |
64 static const char kSuggestMetadataKey[]; | |
65 | |
66 // Used to store a deletion request url for server-provided suggestions. | |
67 static const char kDeletionUrlKey[]; | |
68 | |
69 // These are the values for the above keys. | |
70 static const char kTrue[]; | |
71 static const char kFalse[]; | |
72 | |
50 virtual ~BaseSearchProvider(); | 73 virtual ~BaseSearchProvider(); |
51 | 74 |
52 // The Result classes are intermediate representations of AutocompleteMatches, | 75 // The Result classes are intermediate representations of AutocompleteMatches, |
53 // simply containing relevance-ranked search and navigation suggestions. | 76 // simply containing relevance-ranked search and navigation suggestions. |
54 // They may be cached to provide some synchronous matches while requests for | 77 // They may be cached to provide some synchronous matches while requests for |
55 // new suggestions from updated input are in flight. | 78 // new suggestions from updated input are in flight. |
56 // TODO(msw) Extend these classes to generate their corresponding matches and | 79 // TODO(msw) Extend these classes to generate their corresponding matches and |
57 // other requisite data, in order to consolidate and simplify the | 80 // other requisite data, in order to consolidate and simplify the |
58 // highly fragmented SearchProvider logic for each Result type. | 81 // highly fragmented SearchProvider logic for each Result type. |
59 class Result { | 82 class Result { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 std::string metadata; | 274 std::string metadata; |
252 | 275 |
253 private: | 276 private: |
254 DISALLOW_COPY_AND_ASSIGN(Results); | 277 DISALLOW_COPY_AND_ASSIGN(Results); |
255 }; | 278 }; |
256 | 279 |
257 // Returns an AutocompleteMatch with the given |autocomplete_provider| | 280 // Returns an AutocompleteMatch with the given |autocomplete_provider| |
258 // for the search |suggestion|, which represents a search via |template_url|. | 281 // for the search |suggestion|, which represents a search via |template_url|. |
259 // If |template_url| is NULL, returns a match with an invalid destination URL. | 282 // If |template_url| is NULL, returns a match with an invalid destination URL. |
260 // | 283 // |
261 // |input_text| is the original user input. This is used to highlight | 284 // |input| is the original user input. Text in the input is used to highlight |
262 // portions of the match contents to distinguish locally-typed text from | 285 // portions of the match contents to distinguish locally-typed text from |
263 // suggested text. | 286 // suggested text. |
264 // | 287 // |
265 // |input| is necessary for various other details, like whether we should | 288 // |input| is also necessary for various other details, like whether we should |
266 // allow inline autocompletion and what the transition type should be. | 289 // allow inline autocompletion and what the transition type should be. |
267 // |accepted_suggestion| and |omnibox_start_margin| are used along with | 290 // |accepted_suggestion| and |omnibox_start_margin| are used to generate |
268 // |input_text| to generate Assisted Query Stats. | 291 // Assisted Query Stats. |
269 // |append_extra_query_params| should be set if |template_url| is the default | 292 // |append_extra_query_params| should be set if |template_url| is the default |
270 // search engine, so the destination URL will contain any | 293 // search engine, so the destination URL will contain any |
271 // command-line-specified query params. | 294 // command-line-specified query params. |
272 static AutocompleteMatch CreateSearchSuggestion( | 295 static AutocompleteMatch CreateSearchSuggestion( |
273 AutocompleteProvider* autocomplete_provider, | 296 AutocompleteProvider* autocomplete_provider, |
274 const AutocompleteInput& input, | 297 const AutocompleteInput& input, |
275 const base::string16& input_text, | |
276 const SuggestResult& suggestion, | 298 const SuggestResult& suggestion, |
277 const TemplateURL* template_url, | 299 const TemplateURL* template_url, |
278 int accepted_suggestion, | 300 int accepted_suggestion, |
279 int omnibox_start_margin, | 301 int omnibox_start_margin, |
280 bool append_extra_query_params); | 302 bool append_extra_query_params); |
msw
2014/02/13 02:43:50
With the new ShouldAppendExtraParams helper, this
Maria
2014/02/13 21:07:36
I don't think it makes sense to do that since Crea
msw
2014/02/13 22:47:18
Hmm, no. Leave it as-is for now, I think.
| |
281 | 303 |
282 // Parses JSON response received from the provider, stripping XSSI | 304 // Parses JSON response received from the provider, stripping XSSI |
283 // protection if needed. Returns the parsed data if successful, NULL | 305 // protection if needed. Returns the parsed data if successful, NULL |
284 // otherwise. | 306 // otherwise. |
285 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); | 307 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); |
286 | 308 |
287 // Returns whether we can send the URL of the current page in any suggest | 309 // Returns whether we can send the URL of the current page in any suggest |
288 // requests. Doing this requires that all the following hold: | 310 // requests. Doing this requires that all the following hold: |
289 // * The user has suggest enabled in their settings and is not in incognito | 311 // * The user has suggest enabled in their settings and is not in incognito |
290 // mode. (Incognito disables suggest entirely.) | 312 // mode. (Incognito disables suggest entirely.) |
(...skipping 15 matching lines...) Expand all Loading... | |
306 // especially if we want to support suggest providers other than Google, we | 328 // especially if we want to support suggest providers other than Google, we |
307 // may change this to be a standalone setting or part of some explicit | 329 // may change this to be a standalone setting or part of some explicit |
308 // general opt-in. | 330 // general opt-in. |
309 static bool CanSendURL( | 331 static bool CanSendURL( |
310 const GURL& current_page_url, | 332 const GURL& current_page_url, |
311 const GURL& suggest_url, | 333 const GURL& suggest_url, |
312 const TemplateURL* template_url, | 334 const TemplateURL* template_url, |
313 AutocompleteInput::PageClassification page_classification, | 335 AutocompleteInput::PageClassification page_classification, |
314 Profile* profile); | 336 Profile* profile); |
315 | 337 |
338 // Creates an AutocompleteMatch from |result| to search |template_url| for | |
339 // |input| text. Adds the created match to |map|; if such a match already | |
Mark P
2014/02/12 23:35:49
This doesn't sound right. Shouldn't it create a m
Maria
2014/02/13 21:07:36
Done.
| |
340 // exists, whichever one has lower relevance is eliminated. | |
341 // |metadata| and |accepted_suggestion| are used for generating an | |
342 // AutocompleteMatch. | |
343 void AddMatchToMap(const SuggestResult& result, | |
344 const AutocompleteInput& input, | |
345 const TemplateURL* template_url, | |
346 const std::string& metadata, | |
347 int accepted_suggestion, | |
348 MatchMap* map); | |
349 | |
350 // Returns whether the destination URL for an AutocompleteMatch corresponding | |
Mark P
2014/02/12 23:35:49
I find the phrase "for an AutocompleteMatch" here
Maria
2014/02/13 21:07:36
Done.
| |
351 // to the given |result| should contain command-line-specified query params. | |
352 virtual bool ShouldAppendExtraParams(const SuggestResult& result) const = 0; | |
353 | |
316 // Whether a field trial, if any, has triggered in the most recent | 354 // Whether a field trial, if any, has triggered in the most recent |
317 // autocomplete query. This field is set to true only if the suggestion | 355 // autocomplete query. This field is set to true only if the suggestion |
318 // provider has completed and the response contained | 356 // provider has completed and the response contained |
319 // '"google:fieldtrialtriggered":true'. | 357 // '"google:fieldtrialtriggered":true'. |
320 bool field_trial_triggered_; | 358 bool field_trial_triggered_; |
321 | 359 |
322 // Same as above except that it is maintained across the current Omnibox | 360 // Same as above except that it is maintained across the current Omnibox |
323 // session. | 361 // session. |
324 bool field_trial_triggered_in_session_; | 362 bool field_trial_triggered_in_session_; |
325 | 363 |
326 private: | 364 private: |
327 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); | 365 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); |
328 }; | 366 }; |
329 | 367 |
330 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ | 368 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ |
OLD | NEW |