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. To enable
8 // this provider, point --experimental-zero-suggest-url-prefix at an
9 // appropriate suggestion service.
11 // HUGE DISCLAIMER: This is just here for experimenting and will probably be
12 // deleted entirely as we revise how suggestions work with the omnibox.
14 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
15 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
17 #include "base/basictypes.h"
18 #include "base/compiler_specific.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "chrome/browser/autocomplete/base_search_provider.h"
21 #include "chrome/browser/autocomplete/search_provider.h"
23 class TemplateURLService;
34 // Autocomplete provider for searches based on the current URL.
36 // The controller will call StartZeroSuggest when the user focuses in the
37 // omnibox. After construction, the autocomplete controller repeatedly calls
38 // Start() with some user input, each time expecting to receive an updated
41 // TODO(jered): Consider deleting this class and building this functionality
42 // into SearchProvider after dogfood and after we break the association between
43 // omnibox text and suggestions.
44 class ZeroSuggestProvider : public BaseSearchProvider {
46 // Creates and returns an instance of this provider.
47 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
50 // AutocompleteProvider:
51 virtual void Start(const AutocompleteInput& input,
52 bool /*minimal_changes*/) OVERRIDE;
54 // Sets |field_trial_triggered_| to false.
55 virtual void ResetSession() OVERRIDE;
57 // net::URLFetcherDelegate
58 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
60 // Initiates a new fetch for the given |url| of classification
61 // |page_classification|. |permanent_text| is the omnibox text
62 // for the current page.
63 void StartZeroSuggest(
64 const GURL& curent_page_url,
65 AutocompleteInput::PageClassification page_classification,
66 const base::string16& permanent_text);
69 ZeroSuggestProvider(AutocompleteProviderListener* listener,
72 virtual ~ZeroSuggestProvider();
74 // BaseSearchProvider:
75 virtual const TemplateURL* GetTemplateURL(
76 const SuggestResult& result) const OVERRIDE;
77 virtual const AutocompleteInput GetInput(
78 const SuggestResult& result) const OVERRIDE;
79 virtual bool ShouldAppendExtraParams(
80 const SuggestResult& result) const OVERRIDE;
81 virtual void StopSuggest() OVERRIDE;
82 virtual void ClearAllResults() OVERRIDE;
84 // The 4 functions below (that take classes defined in SearchProvider as
85 // arguments) were copied and trimmed from SearchProvider.
86 // TODO(hfung): Refactor them into a new base class common to both
87 // ZeroSuggestProvider and SearchProvider.
89 // From the OpenSearch formatted response |root_val|, populate query
90 // suggestions into |suggest_results|, navigation suggestions into
91 // |navigation_results|, and the verbatim relevance score into
92 // |verbatim_relevance|.
93 void FillResults(const base::Value& root_val,
94 int* verbatim_relevance,
95 SuggestResults* suggest_results,
96 NavigationResults* navigation_results);
98 // Adds AutocompleteMatches for each of the suggestions in |results| to
100 void AddSuggestResultsToMap(const SuggestResults& results,
103 // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
104 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
106 // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
107 void Run(const GURL& suggest_url);
109 // Parses results from the zero-suggest server and updates results.
110 void ParseSuggestResults(const base::Value& root_val);
112 // Converts the parsed results to a set of AutocompleteMatches and adds them
113 // to |matches_|. Also update the histograms for how many results were
115 void ConvertResultsToAutocompleteMatches();
117 // Returns an AutocompleteMatch for the current URL. The match should be in
118 // the top position so that pressing enter has the effect of reloading the
120 AutocompleteMatch MatchForCurrentURL();
122 // When the user is in the Most Visited field trial, we ask the TopSites
123 // service for the most visited URLs during Run(). It calls back to this
124 // function to return those |urls|.
125 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
127 // Used to build default search engine URLs for suggested queries.
128 TemplateURLService* template_url_service_;
130 // The URL for which a suggestion fetch is pending.
131 std::string current_query_;
133 // The type of page the user is viewing (a search results page doing search
134 // term replacement, an arbitrary URL, etc.).
135 AutocompleteInput::PageClassification current_page_classification_;
137 // Copy of OmniboxEditModel::permanent_text_.
138 base::string16 permanent_text_;
140 // Fetcher used to retrieve results.
141 scoped_ptr<net::URLFetcher> fetcher_;
142 // Whether there's a pending request in flight.
143 bool have_pending_request_;
145 // Suggestion for the current URL.
146 AutocompleteMatch current_url_match_;
147 // Navigation suggestions for the most recent ZeroSuggest input URL.
148 NavigationResults navigation_results_;
149 // Query suggestions for the most recent ZeroSuggest input URL.
150 MatchMap query_matches_map_;
151 // The relevance score for the URL of the current page.
152 int verbatim_relevance_;
154 history::MostVisitedURLList most_visited_urls_;
156 // For callbacks that may be run after destruction.
157 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
159 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
162 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_