• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 //
5 // This file contains the Search autocomplete provider.  This provider is
6 // responsible for all autocomplete entries that start with "Search <engine>
7 // for ...", including searching for the current input string, search
8 // history, and search suggestions.  An instance of it gets created and
9 // managed by the autocomplete controller.
10 
11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_
12 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "base/basictypes.h"
19 #include "base/compiler_specific.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/time/time.h"
23 #include "base/timer/timer.h"
24 #include "chrome/browser/autocomplete/autocomplete_input.h"
25 #include "chrome/browser/autocomplete/autocomplete_match.h"
26 #include "chrome/browser/autocomplete/autocomplete_provider.h"
27 #include "chrome/browser/history/history_types.h"
28 #include "chrome/browser/search_engines/template_url.h"
29 #include "net/url_request/url_fetcher_delegate.h"
30 
31 class Profile;
32 class SearchProviderTest;
33 class SuggestionDeletionHandler;
34 class TemplateURLService;
35 
36 namespace base {
37 class Value;
38 }
39 
40 namespace net {
41 class URLFetcher;
42 }
43 
44 // Autocomplete provider for searches and suggestions from a search engine.
45 //
46 // 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 // matches (either synchronously or asynchronously).
49 //
50 // 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 // comes back, the provider creates and returns matches for the best
53 // suggestions.
54 class SearchProvider : public AutocompleteProvider,
55                        public net::URLFetcherDelegate {
56  public:
57   // ID used in creating URLFetcher for default provider's suggest results.
58   static const int kDefaultProviderURLFetcherID;
59 
60   // ID used in creating URLFetcher for keyword provider's suggest results.
61   static const int kKeywordProviderURLFetcherID;
62 
63   // ID used in creating URLFetcher for deleting suggestion results.
64   static const int kDeletionURLFetcherID;
65 
66   SearchProvider(AutocompleteProviderListener* listener, Profile* profile);
67 
68   // Returns an AutocompleteMatch with the given |autocomplete_provider|,
69   // |relevance|, and |type|, which represents a search via |template_url| for
70   // |query_string|.  If |template_url| is NULL, returns a match with an invalid
71   // destination URL.
72   //
73   // |input_text| is the original user input, which may differ from
74   // |query_string|; e.g. the user typed "foo" and got a search suggestion of
75   // "food", which we're now marking up.  This is used to highlight portions of
76   // the match contents to distinguish locally-typed text from suggested text.
77   //
78   // |input| and |is_keyword| are necessary for various other details, like
79   // whether we should allow inline autocompletion and what the transition type
80   // should be.  |accepted_suggestion| and |omnibox_start_margin| are used along
81   // with |input_text| to generate Assisted Query Stats.
82   // |append_extra_query_params| should be set if |template_url| is the default
83   // search engine, so the destination URL will contain any
84   // command-line-specified query params.
85   static AutocompleteMatch CreateSearchSuggestion(
86       AutocompleteProvider* autocomplete_provider,
87       const AutocompleteInput& input,
88       const base::string16& input_text,
89       int relevance,
90       AutocompleteMatch::Type type,
91       bool is_keyword,
92       const base::string16& match_contents,
93       const base::string16& annotation,
94       const TemplateURL* template_url,
95       const base::string16& query_string,
96       const std::string& suggest_query_params,
97       int accepted_suggestion,
98       int omnibox_start_margin,
99       bool append_extra_query_params);
100 
101   // Returns whether the SearchProvider previously flagged |match| as a query
102   // that should be prefetched.
103   static bool ShouldPrefetch(const AutocompleteMatch& match);
104 
105   // Extracts the suggest response metadata which SearchProvider previously
106   // stored for |match|.
107   static std::string GetSuggestMetadata(const AutocompleteMatch& match);
108 
109   // AutocompleteProvider:
110   virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE;
111   virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
112   virtual void ResetSession() OVERRIDE;
113 
field_trial_triggered_in_session()114   bool field_trial_triggered_in_session() const {
115     return field_trial_triggered_in_session_;
116   }
117 
118   // This URL may be sent with suggest requests; see comments on CanSendURL().
set_current_page_url(const GURL & current_page_url)119   void set_current_page_url(const GURL& current_page_url) {
120     current_page_url_ = current_page_url;
121   }
122 
123  protected:
124   virtual ~SearchProvider();
125 
126  private:
127   // TODO(hfung): Remove ZeroSuggestProvider as a friend class after
128   // refactoring common code to a new base class.
129   friend class SearchProviderTest;
130   friend class ZeroSuggestProvider;
131   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, CanSendURL);
132   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInline);
133   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineDomainClassify);
134   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineSchemeSubstring);
135   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, RemoveStaleResultsTest);
136   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestRelevanceExperiment);
137   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch);
138   FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, GetDestinationURL);
139   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, ClearPrefetchedResults);
140   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, SetPrefetchQuery);
141 
142   // Manages the providers (TemplateURLs) used by SearchProvider. Two providers
143   // may be used:
144   // . The default provider. This corresponds to the user's default search
145   //   engine. This is always used, except for the rare case of no default
146   //   engine.
147   // . The keyword provider. This is used if the user has typed in a keyword.
148   class Providers {
149    public:
150     explicit Providers(TemplateURLService* template_url_service);
151 
152     // Returns true if the specified providers match the two providers cached
153     // by this class.
equal(const base::string16 & default_provider,const base::string16 & keyword_provider)154     bool equal(const base::string16& default_provider,
155                const base::string16& keyword_provider) const {
156       return (default_provider == default_provider_) &&
157           (keyword_provider == keyword_provider_);
158     }
159 
160     // Resets the cached providers.
set(const base::string16 & default_provider,const base::string16 & keyword_provider)161     void set(const base::string16& default_provider,
162              const base::string16& keyword_provider) {
163       default_provider_ = default_provider;
164       keyword_provider_ = keyword_provider;
165     }
166 
template_url_service()167     TemplateURLService* template_url_service() { return template_url_service_; }
default_provider()168     const base::string16& default_provider() const { return default_provider_; }
keyword_provider()169     const base::string16& keyword_provider() const { return keyword_provider_; }
170 
171     // NOTE: These may return NULL even if the provider members are nonempty!
172     const TemplateURL* GetDefaultProviderURL() const;
173     const TemplateURL* GetKeywordProviderURL() const;
174 
175     // Returns true if there is a valid keyword provider.
has_keyword_provider()176     bool has_keyword_provider() const { return !keyword_provider_.empty(); }
177 
178    private:
179     TemplateURLService* template_url_service_;
180 
181     // Cached across the life of a query so we behave consistently even if the
182     // user changes their default while the query is running.
183     base::string16 default_provider_;
184     base::string16 keyword_provider_;
185 
186     DISALLOW_COPY_AND_ASSIGN(Providers);
187   };
188 
189   // The Result classes are intermediate representations of AutocompleteMatches,
190   // simply containing relevance-ranked search and navigation suggestions.
191   // They may be cached to provide some synchronous matches while requests for
192   // new suggestions from updated input are in flight.
193   // TODO(msw) Extend these classes to generate their corresponding matches and
194   //           other requisite data, in order to consolidate and simplify the
195   //           highly fragmented SearchProvider logic for each Result type.
196   class Result {
197    public:
198     Result(bool from_keyword_provider,
199            int relevance,
200            bool relevance_from_server);
201     virtual ~Result();
202 
from_keyword_provider()203     bool from_keyword_provider() const { return from_keyword_provider_; }
204 
relevance()205     int relevance() const { return relevance_; }
set_relevance(int relevance)206     void set_relevance(int relevance) { relevance_ = relevance; }
207 
relevance_from_server()208     bool relevance_from_server() const { return relevance_from_server_; }
set_relevance_from_server(bool relevance_from_server)209     void set_relevance_from_server(bool relevance_from_server) {
210       relevance_from_server_ = relevance_from_server;
211     }
212 
213     // Returns if this result is inlineable against the current input |input|.
214     // Non-inlineable results are stale.
215     virtual bool IsInlineable(const base::string16& input) const = 0;
216 
217     // Returns the default relevance value for this result (which may
218     // be left over from a previous omnibox input) given the current
219     // input and whether the current input caused a keyword provider
220     // to be active.
221     virtual int CalculateRelevance(const AutocompleteInput& input,
222                                    bool keyword_provider_requested) const = 0;
223 
224    protected:
225     // True if the result came from the keyword provider.
226     bool from_keyword_provider_;
227 
228     // The relevance score.
229     int relevance_;
230 
231    private:
232     // Whether this result's relevance score was fully or partly calculated
233     // based on server information, and thus is assumed to be more accurate.
234     // This is ultimately used in
235     // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments
236     // there.
237     bool relevance_from_server_;
238   };
239 
240   class SuggestResult : public Result {
241    public:
242     SuggestResult(const base::string16& suggestion,
243                   AutocompleteMatchType::Type type,
244                   const base::string16& match_contents,
245                   const base::string16& annotation,
246                   const std::string& suggest_query_params,
247                   const std::string& deletion_url,
248                   bool from_keyword_provider,
249                   int relevance,
250                   bool relevance_from_server,
251                   bool should_prefetch);
252     virtual ~SuggestResult();
253 
suggestion()254     const base::string16& suggestion() const { return suggestion_; }
type()255     AutocompleteMatchType::Type type() const { return type_; }
match_contents()256     const base::string16& match_contents() const { return match_contents_; }
annotation()257     const base::string16& annotation() const { return annotation_; }
suggest_query_params()258     const std::string& suggest_query_params() const {
259       return suggest_query_params_;
260     }
deletion_url()261     const std::string& deletion_url() const { return deletion_url_; }
should_prefetch()262     bool should_prefetch() const { return should_prefetch_; }
263 
264     // Result:
265     virtual bool IsInlineable(const base::string16& input) const OVERRIDE;
266     virtual int CalculateRelevance(
267         const AutocompleteInput& input,
268         bool keyword_provider_requested) const OVERRIDE;
269 
270    private:
271     // The search terms to be used for this suggestion.
272     base::string16 suggestion_;
273 
274     AutocompleteMatchType::Type type_;
275 
276     // The contents to be displayed in the autocomplete match.
277     base::string16 match_contents_;
278 
279     // Optional annotation for the |match_contents_| for disambiguation.
280     // This may be displayed in the autocomplete match contents, but is defined
281     // separately to facilitate different formatting.
282     base::string16 annotation_;
283 
284     // Optional additional parameters to be added to the search URL.
285     std::string suggest_query_params_;
286 
287     // Optional deletion URL provided with suggestions. Fetching this URL
288     // should result in some reasonable deletion behaviour on the server,
289     // e.g. deleting this term out of a user's server-side search history.
290     std::string deletion_url_;
291 
292     // Should this result be prefetched?
293     bool should_prefetch_;
294   };
295 
296   class NavigationResult : public Result {
297    public:
298     // |provider| is necessary to use StringForURLDisplay() in order to
299     // compute |formatted_url_|.
300     NavigationResult(const AutocompleteProvider& provider,
301                      const GURL& url,
302                      const base::string16& description,
303                      bool from_keyword_provider,
304                      int relevance,
305                      bool relevance_from_server);
306     virtual ~NavigationResult();
307 
url()308     const GURL& url() const { return url_; }
description()309     const base::string16& description() const { return description_; }
formatted_url()310     const base::string16& formatted_url() const { return formatted_url_; }
311 
312     // Result:
313     virtual bool IsInlineable(const base::string16& input) const OVERRIDE;
314     virtual int CalculateRelevance(
315         const AutocompleteInput& input,
316         bool keyword_provider_requested) const OVERRIDE;
317 
318    private:
319     // The suggested url for navigation.
320     GURL url_;
321 
322     // The properly formatted ("fixed up") URL string with equivalent meaning
323     // to the one in |url_|.
324     base::string16 formatted_url_;
325 
326     // The suggested navigational result description; generally the site name.
327     base::string16 description_;
328   };
329 
330   class CompareScoredResults;
331 
332   typedef std::vector<SuggestResult> SuggestResults;
333   typedef std::vector<NavigationResult> NavigationResults;
334   typedef std::vector<history::KeywordSearchTermVisit> HistoryResults;
335   typedef std::pair<base::string16, std::string> MatchKey;
336   typedef std::map<MatchKey, AutocompleteMatch> MatchMap;
337   typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers;
338 
339   // A simple structure bundling most of the information (including
340   // both SuggestResults and NavigationResults) returned by a call to
341   // the suggest server.
342   //
343   // This has to be declared after the typedefs since it relies on some of them.
344   struct Results {
345     Results();
346     ~Results();
347 
348     // Clears |suggest_results| and |navigation_results| and resets
349     // |verbatim_relevance| to -1 (implies unset).
350     void Clear();
351 
352     // Returns whether any of the results (including verbatim) have
353     // server-provided scores.
354     bool HasServerProvidedScores() const;
355 
356     // Query suggestions sorted by relevance score.
357     SuggestResults suggest_results;
358 
359     // Navigational suggestions sorted by relevance score.
360     NavigationResults navigation_results;
361 
362     // The server supplied verbatim relevance scores. Negative values
363     // indicate that there is no suggested score; a value of 0
364     // suppresses the verbatim result.
365     int verbatim_relevance;
366 
367     // The JSON metadata associated with this server response.
368     std::string metadata;
369 
370    private:
371     DISALLOW_COPY_AND_ASSIGN(Results);
372   };
373 
374   // Removes non-inlineable results until either the top result can inline
375   // autocomplete the current input or verbatim outscores the top result.
376   static void RemoveStaleResults(const base::string16& input,
377                                  int verbatim_relevance,
378                                  SuggestResults* suggest_results,
379                                  NavigationResults* navigation_results);
380 
381   // Calculates the relevance score for the keyword verbatim result (if the
382   // input matches one of the profile's keyword).
383   static int CalculateRelevanceForKeywordVerbatim(AutocompleteInput::Type type,
384                                                   bool prefer_keyword);
385 
386   // AutocompleteProvider:
387   virtual void Start(const AutocompleteInput& input,
388                      bool minimal_changes) OVERRIDE;
389   virtual void Stop(bool clear_cached_results) OVERRIDE;
390 
391   // net::URLFetcherDelegate:
392   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
393 
394   // This gets called when we have requested a suggestion deletion from the
395   // server to handle the results of the deletion.
396   void OnDeletionComplete(bool success,
397                           SuggestionDeletionHandler* handler);
398 
399   // Records in UMA whether the deletion request resulted in success.
400   // This is virtual so test code can override it to check that we
401   // correctly handle the request result.
402   virtual void RecordDeletionResult(bool success);
403 
404   // Removes the deleted match from the list of |matches_|.
405   void DeleteMatchFromMatches(const AutocompleteMatch& match);
406 
407   // Called when timer_ expires.
408   void Run();
409 
410   // Runs the history query, if necessary. The history query is synchronous.
411   // This does not update |done_|.
412   void DoHistoryQuery(bool minimal_changes);
413 
414   // Determines whether an asynchronous subcomponent query should run for the
415   // current input.  If so, starts it if necessary; otherwise stops it.
416   // NOTE: This function does not update |done_|.  Callers must do so.
417   void StartOrStopSuggestQuery(bool minimal_changes);
418 
419   // Returns true when the current query can be sent to the Suggest service.
420   // This will be false e.g. when Suggest is disabled, the query contains
421   // potentially private data, etc.
422   bool IsQuerySuitableForSuggest() const;
423 
424   // Stops the suggest query.
425   // NOTE: This does not update |done_|.  Callers must do so.
426   void StopSuggest();
427 
428   // Clears the current results.
429   void ClearAllResults();
430 
431   // Removes stale results for both default and keyword providers.  See comments
432   // on RemoveStaleResults().
433   void RemoveAllStaleResults();
434 
435   // Apply calculated relevance scores to the current results.
436   void ApplyCalculatedRelevance();
437   void ApplyCalculatedSuggestRelevance(SuggestResults* list);
438   void ApplyCalculatedNavigationRelevance(NavigationResults* list);
439 
440   // Starts a new URLFetcher requesting suggest results from |template_url|;
441   // callers own the returned URLFetcher, which is NULL for invalid providers.
442   net::URLFetcher* CreateSuggestFetcher(int id,
443                                         const TemplateURL* template_url,
444                                         const AutocompleteInput& input);
445 
446   // Parses JSON response received from the provider, stripping XSSI
447   // protection if needed. Returns the parsed data if successful, NULL
448   // otherwise.
449   static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data);
450 
451   // Parses results from the suggest server and updates the appropriate suggest
452   // and navigation result lists, depending on whether |is_keyword| is true.
453   // Returns whether the appropriate result list members were updated.
454   bool ParseSuggestResults(base::Value* root_val, bool is_keyword);
455 
456   // Converts the parsed results to a set of AutocompleteMatches, |matches_|.
457   void ConvertResultsToAutocompleteMatches();
458 
459   // Returns an iterator to the first match in |matches_| which might
460   // be chosen as default.  If
461   // |autocomplete_result_will_reorder_for_default_match| is false,
462   // this simply means the first match; otherwise, it means the first
463   // match for which the |allowed_to_be_default_match| member is true.
464   ACMatches::const_iterator FindTopMatch(
465     bool autocomplete_result_will_reorder_for_default_match) const;
466 
467   // Checks if suggested relevances violate certain expected constraints.
468   // See UpdateMatches() for the use and explanation of these constraints.
469   bool IsTopMatchNavigationInKeywordMode(
470       bool autocomplete_result_will_reorder_for_default_match) const;
471   bool HasKeywordDefaultMatchInKeywordMode() const;
472   bool IsTopMatchScoreTooLow(
473       bool autocomplete_result_will_reorder_for_default_match) const;
474   bool IsTopMatchSearchWithURLInput(
475       bool autocomplete_result_will_reorder_for_default_match) const;
476   bool HasValidDefaultMatch(
477       bool autocomplete_result_will_reorder_for_default_match) const;
478 
479   // Updates |matches_| from the latest results; applies calculated relevances
480   // if suggested relevances cause undesriable behavior. Updates |done_|.
481   void UpdateMatches();
482 
483   // Converts an appropriate number of navigation results in
484   // |navigation_results| to matches and adds them to |matches|.
485   void AddNavigationResultsToMatches(
486       const NavigationResults& navigation_results,
487       ACMatches* matches);
488 
489   // Adds a match for each result in |results| to |map|. |is_keyword| indicates
490   // whether the results correspond to the keyword provider or default provider.
491   void AddHistoryResultsToMap(const HistoryResults& results,
492                               bool is_keyword,
493                               int did_not_accept_suggestion,
494                               MatchMap* map);
495 
496   // Calculates relevance scores for all |results|.
497   SuggestResults ScoreHistoryResults(const HistoryResults& results,
498                                      bool base_prevent_inline_autocomplete,
499                                      bool input_multiple_words,
500                                      const base::string16& input_text,
501                                      bool is_keyword);
502 
503   // Adds matches for |results| to |map|.
504   void AddSuggestResultsToMap(const SuggestResults& results,
505                               const std::string& metadata,
506                               MatchMap* map);
507 
508   // Gets the relevance score for the verbatim result.  This value may be
509   // provided by the suggest server or calculated locally; if
510   // |relevance_from_server| is non-NULL, it will be set to indicate which of
511   // those is true.
512   int GetVerbatimRelevance(bool* relevance_from_server) const;
513 
514   // Calculates the relevance score for the verbatim result from the
515   // default search engine.  This version takes into account context:
516   // i.e., whether the user has entered a keyword-based search or not.
517   int CalculateRelevanceForVerbatim() const;
518 
519   // Calculates the relevance score for the verbatim result from the default
520   // search engine *ignoring* whether the input is a keyword-based search
521   // or not.  This function should only be used to determine the minimum
522   // relevance score that the best result from this provider should have.
523   // For normal use, prefer the above function.
524   int CalculateRelevanceForVerbatimIgnoringKeywordModeState() const;
525 
526   // Gets the relevance score for the keyword verbatim result.
527   // |relevance_from_server| is handled as in GetVerbatimRelevance().
528   // TODO(mpearson): Refactor so this duplication isn't necessary or
529   // restructure so one static function takes all the parameters it needs
530   // (rather than looking at internal state).
531   int GetKeywordVerbatimRelevance(bool* relevance_from_server) const;
532 
533   // |time| is the time at which this query was last seen.  |is_keyword|
534   // indicates whether the results correspond to the keyword provider or default
535   // provider. |use_aggressive_method| says whether this function can use a
536   // method that gives high scores (1200+) rather than one that gives lower
537   // scores.  When using the aggressive method, scores may exceed 1300
538   // unless |prevent_search_history_inlining| is set.
539   int CalculateRelevanceForHistory(const base::Time& time,
540                                    bool is_keyword,
541                                    bool use_aggressive_method,
542                                    bool prevent_search_history_inlining) const;
543 
544   // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with
545   // the supplied details.  Adds this match to |map|; if such a match already
546   // exists, whichever one has lower relevance is eliminated.
547   void AddMatchToMap(const SuggestResult& result,
548                      const base::string16& input_text,
549                      const std::string& metadata,
550                      int accepted_suggestion,
551                      MatchMap* map);
552 
553   // Returns an AutocompleteMatch for a navigational suggestion.
554   AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
555 
556   // Resets the scores of all |keyword_navigation_results_| matches to
557   // be below that of the top keyword query match (the verbatim match
558   // as expressed by |keyword_verbatim_relevance_| or keyword query
559   // suggestions stored in |keyword_suggest_results_|).  If there
560   // are no keyword suggestions and keyword verbatim is suppressed,
561   // then drops the suggested relevance scores for the navsuggestions
562   // and drops the request to suppress verbatim, thereby introducing the
563   // keyword verbatim match which will naturally outscore the navsuggestions.
564   void DemoteKeywordNavigationMatchesPastTopQuery();
565 
566   // Updates the value of |done_| from the internal state.
567   void UpdateDone();
568 
569   // Returns whether we can send the URL of the current page in any suggest
570   // requests.  Doing this requires that all the following hold:
571   // * The user has suggest enabled in their settings and is not in incognito
572   //   mode.  (Incognito disables suggest entirely.)
573   // * The current URL is HTTP, or HTTPS with the same domain as the suggest
574   //   server.  Non-HTTP[S] URLs (e.g. FTP/file URLs) may contain sensitive
575   //   information.  HTTPS URLs may also contain sensitive information, but if
576   //   they're on the same domain as the suggest server, then the relevant
577   //   entity could have already seen/logged this data.
578   // * The suggest request is sent over HTTPS.  This avoids leaking the current
579   //   page URL in world-readable network traffic.
580   // * The user's suggest provider is Google.  We might want to allow other
581   //   providers to see this data someday, but for now this has only been
582   //   implemented for Google.  Also see next bullet.
583   // * The user is OK in principle with sending URLs of current pages to their
584   //   provider.  Today, there is no explicit setting that controls this, but if
585   //   the user has tab sync enabled and tab sync is unencrypted, then they're
586   //   already sending this data to Google for sync purposes.  Thus we use this
587   //   setting as a proxy for "it's OK to send such data".  In the future,
588   //   especially if we want to support suggest providers other than Google, we
589   //   may change this to be a standalone setting or part of some explicit
590   //   general opt-in.
591   static bool CanSendURL(
592       const GURL& current_page_url,
593       const GURL& suggest_url,
594       const TemplateURL* template_url,
595       AutocompleteInput::PageClassification page_classification,
596       Profile* profile);
597 
598   // The amount of time to wait before sending a new suggest request after the
599   // previous one.  Non-const because some unittests modify this value.
600   static int kMinimumTimeBetweenSuggestQueriesMs;
601 
602   // The following keys are used to record additional information on matches.
603 
604   // We annotate our AutocompleteMatches with whether their relevance scores
605   // were server-provided using this key in the |additional_info| field.
606   static const char kRelevanceFromServerKey[];
607 
608   // Indicates whether the server said a match should be prefetched.
609   static const char kShouldPrefetchKey[];
610 
611   // Used to store metadata from the server response, which is needed for
612   // prefetching.
613   static const char kSuggestMetadataKey[];
614 
615   // Used to store a deletion request url for server-provided suggestions.
616   static const char kDeletionUrlKey[];
617 
618   // These are the values for the above keys.
619   static const char kTrue[];
620   static const char kFalse[];
621 
622   // Maintains the TemplateURLs used.
623   Providers providers_;
624 
625   // The user's input.
626   AutocompleteInput input_;
627 
628   // Input when searching against the keyword provider.
629   AutocompleteInput keyword_input_;
630 
631   // Searches in the user's history that begin with the input text.
632   HistoryResults keyword_history_results_;
633   HistoryResults default_history_results_;
634 
635   // Number of suggest results that haven't yet arrived. If greater than 0 it
636   // indicates one of the URLFetchers is still running.
637   int suggest_results_pending_;
638 
639   // A timer to start a query to the suggest server after the user has stopped
640   // typing for long enough.
641   base::OneShotTimer<SearchProvider> timer_;
642 
643   // The time at which we sent a query to the suggest server.
644   base::TimeTicks time_suggest_request_sent_;
645 
646   // Fetchers used to retrieve results for the keyword and default providers.
647   scoped_ptr<net::URLFetcher> keyword_fetcher_;
648   scoped_ptr<net::URLFetcher> default_fetcher_;
649 
650   // Results from the default and keyword search providers.
651   Results default_results_;
652   Results keyword_results_;
653 
654   // Each deletion handler in this vector corresponds to an outstanding request
655   // that a server delete a personalized suggestion. Making this a ScopedVector
656   // causes us to auto-cancel all such requests on shutdown.
657   SuggestionDeletionHandlers deletion_handlers_;
658 
659   // Whether a field trial, if any, has triggered in the most recent
660   // autocomplete query.  This field is set to false in Start() and may be set
661   // to true if either the default provider or keyword provider has completed
662   // and their corresponding suggest response contained
663   // '"google:fieldtrialtriggered":true'.
664   // If the autocomplete query has not returned, this field is set to false.
665   bool field_trial_triggered_;
666 
667   // Same as above except that it is maintained across the current Omnibox
668   // session.
669   bool field_trial_triggered_in_session_;
670 
671   // If true, search history query suggestions will score low enough that
672   // they will not be inlined.
673   bool prevent_search_history_inlining_;
674 
675   GURL current_page_url_;
676 
677   DISALLOW_COPY_AND_ASSIGN(SearchProvider);
678 };
679 
680 #endif  // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_
681