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.
5 // This file contains the zero-suggest autocomplete provider. This experimental
6 // provider is invoked when the user focuses in the omnibox prior to editing,
7 // and generates search query suggestions based on the current URL.
9 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
10 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "chrome/browser/autocomplete/base_search_provider.h"
16 #include "chrome/browser/autocomplete/search_provider.h"
17 #include "chrome/browser/history/history_types.h"
18 #include "components/metrics/proto/omnibox_event.pb.h"
19 #include "net/url_request/url_fetcher_delegate.h"
21 class AutocompleteProviderListener;
22 class TemplateURLService;
33 namespace user_prefs {
34 class PrefRegistrySyncable;
37 // Autocomplete provider for searches based on the current URL.
39 // The controller will call StartZeroSuggest when the user focuses in the
40 // omnibox. After construction, the autocomplete controller repeatedly calls
41 // Start() with some user input, each time expecting to receive an updated
44 // TODO(jered): Consider deleting this class and building this functionality
45 // into SearchProvider after dogfood and after we break the association between
46 // omnibox text and suggestions.
47 class ZeroSuggestProvider : public BaseSearchProvider,
48 public net::URLFetcherDelegate {
50 // Creates and returns an instance of this provider.
51 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
52 TemplateURLService* template_url_service,
55 // Registers a preference used to cache zero suggest results.
56 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
58 // AutocompleteProvider:
59 virtual void Start(const AutocompleteInput& input,
60 bool minimal_changes) OVERRIDE;
61 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
63 // Sets |field_trial_triggered_| to false.
64 virtual void ResetSession() OVERRIDE;
67 // BaseSearchProvider:
68 virtual void ModifyProviderInfo(
69 metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE;
72 ZeroSuggestProvider(AutocompleteProviderListener* listener,
73 TemplateURLService* template_url_service,
76 virtual ~ZeroSuggestProvider();
78 // BaseSearchProvider:
79 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE;
80 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE;
81 virtual bool ShouldAppendExtraParams(
82 const SearchSuggestionParser::SuggestResult& result) const OVERRIDE;
83 virtual void StopSuggest() OVERRIDE;
84 virtual void ClearAllResults() OVERRIDE;
85 virtual void RecordDeletionResult(bool success) OVERRIDE;
87 // net::URLFetcherDelegate:
88 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
90 // Optionally, cache the received |json_data| and return true if we want
91 // to stop processing results at this point. The |parsed_data| is the parsed
92 // version of |json_data| used to determine if we received an empty result.
93 bool StoreSuggestionResponse(const std::string& json_data,
94 const base::Value& parsed_data);
96 // Adds AutocompleteMatches for each of the suggestions in |results| to
98 void AddSuggestResultsToMap(
99 const SearchSuggestionParser::SuggestResults& results,
102 // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
103 AutocompleteMatch NavigationToMatch(
104 const SearchSuggestionParser::NavigationResult& navigation);
106 // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
107 void Run(const GURL& suggest_url);
109 // Converts the parsed results to a set of AutocompleteMatches and adds them
110 // to |matches_|. Also update the histograms for how many results were
112 void ConvertResultsToAutocompleteMatches();
114 // Returns an AutocompleteMatch for the current URL. The match should be in
115 // the top position so that pressing enter has the effect of reloading the
117 AutocompleteMatch MatchForCurrentURL();
119 // When the user is in the Most Visited field trial, we ask the TopSites
120 // service for the most visited URLs during Run(). It calls back to this
121 // function to return those |urls|.
122 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
124 // Returns the relevance score for the verbatim result.
125 int GetVerbatimRelevance() const;
127 // Whether we can show zero suggest on |current_page_url| without
128 // sending |current_page_url| as a parameter to the server at |suggest_url|.
129 bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url,
130 const GURL& current_page_url) const;
132 // Checks whether we have a set of zero suggest results cached, and if so
133 // populates |matches_| with cached results.
134 void MaybeUseCachedSuggestions();
136 AutocompleteProviderListener* listener_;
138 // The URL for which a suggestion fetch is pending.
139 std::string current_query_;
141 // The type of page the user is viewing (a search results page doing search
142 // term replacement, an arbitrary URL, etc.).
143 metrics::OmniboxEventProto::PageClassification current_page_classification_;
145 // Copy of OmniboxEditModel::permanent_text_.
146 base::string16 permanent_text_;
148 // Fetcher used to retrieve results.
149 scoped_ptr<net::URLFetcher> fetcher_;
151 // Suggestion for the current URL.
152 AutocompleteMatch current_url_match_;
154 // Contains suggest and navigation results as well as relevance parsed from
155 // the response for the most recent zero suggest input URL.
156 SearchSuggestionParser::Results results_;
158 // Whether we are currently showing cached zero suggest results.
159 bool results_from_cache_;
161 history::MostVisitedURLList most_visited_urls_;
163 // For callbacks that may be run after destruction.
164 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
166 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
169 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_