Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / zero_suggest_provider.h
blob904aa6d7fa4f498a4c4d16aee1ebd7f05f8510f9
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 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 COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
10 #define COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/history/core/browser/history_types.h"
16 #include "components/metrics/proto/omnibox_event.pb.h"
17 #include "components/omnibox/browser/base_search_provider.h"
18 #include "components/omnibox/browser/search_provider.h"
19 #include "net/url_request/url_fetcher_delegate.h"
21 class AutocompleteProviderListener;
23 namespace base {
24 class ListValue;
25 class Value;
28 namespace net {
29 class URLFetcher;
32 namespace user_prefs {
33 class PrefRegistrySyncable;
36 // Autocomplete provider for searches based on the current URL.
38 // The controller will call Start() with |on_focus| set when the user focuses
39 // the omnibox. After construction, the autocomplete controller repeatedly calls
40 // Start() with some user input, each time expecting to receive an updated set
41 // of matches.
43 // TODO(jered): Consider deleting this class and building this functionality
44 // into SearchProvider after dogfood and after we break the association between
45 // omnibox text and suggestions.
46 class ZeroSuggestProvider : public BaseSearchProvider,
47 public net::URLFetcherDelegate {
48 public:
49 // Creates and returns an instance of this provider.
50 static ZeroSuggestProvider* Create(AutocompleteProviderClient* client,
51 AutocompleteProviderListener* listener);
53 // Registers a preference used to cache zero suggest results.
54 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
56 // AutocompleteProvider:
57 void Start(const AutocompleteInput& input, bool minimal_changes) override;
58 void Stop(bool clear_cached_results,
59 bool due_to_user_inactivity) override;
60 void DeleteMatch(const AutocompleteMatch& match) override;
61 void AddProviderInfo(ProvidersInfo* provider_info) const override;
63 // Sets |field_trial_triggered_| to false.
64 void ResetSession() override;
66 private:
67 ZeroSuggestProvider(AutocompleteProviderClient* client,
68 AutocompleteProviderListener* listener);
70 ~ZeroSuggestProvider() override;
72 // BaseSearchProvider:
73 const TemplateURL* GetTemplateURL(bool is_keyword) const override;
74 const AutocompleteInput GetInput(bool is_keyword) const override;
75 bool ShouldAppendExtraParams(
76 const SearchSuggestionParser::SuggestResult& result) const override;
77 void RecordDeletionResult(bool success) override;
79 // net::URLFetcherDelegate:
80 void OnURLFetchComplete(const net::URLFetcher* source) override;
82 // Optionally, cache the received |json_data| and return true if we want
83 // to stop processing results at this point. The |parsed_data| is the parsed
84 // version of |json_data| used to determine if we received an empty result.
85 bool StoreSuggestionResponse(const std::string& json_data,
86 const base::Value& parsed_data);
88 // Adds AutocompleteMatches for each of the suggestions in |results| to
89 // |map|.
90 void AddSuggestResultsToMap(
91 const SearchSuggestionParser::SuggestResults& results,
92 MatchMap* map);
94 // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
95 AutocompleteMatch NavigationToMatch(
96 const SearchSuggestionParser::NavigationResult& navigation);
98 // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
99 void Run(const GURL& suggest_url);
101 // Converts the parsed results to a set of AutocompleteMatches and adds them
102 // to |matches_|. Also update the histograms for how many results were
103 // received.
104 void ConvertResultsToAutocompleteMatches();
106 // Returns an AutocompleteMatch for the current URL. The match should be in
107 // the top position so that pressing enter has the effect of reloading the
108 // page.
109 AutocompleteMatch MatchForCurrentURL();
111 // When the user is in the Most Visited field trial, we ask the TopSites
112 // service for the most visited URLs during Run(). It calls back to this
113 // function to return those |urls|.
114 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
116 // Whether we can show zero suggest without sending |current_page_url| to
117 // |suggest_url| search provider. Also checks that other conditions for
118 // non-contextual zero suggest are satisfied.
119 bool ShouldShowNonContextualZeroSuggest(const GURL& suggest_url,
120 const GURL& current_page_url) const;
122 // Checks whether we have a set of zero suggest results cached, and if so
123 // populates |matches_| with cached results.
124 void MaybeUseCachedSuggestions();
126 AutocompleteProviderListener* listener_;
128 // The URL for which a suggestion fetch is pending.
129 std::string current_query_;
131 // The type of page the user is viewing (a search results page doing search
132 // term replacement, an arbitrary URL, etc.).
133 metrics::OmniboxEventProto::PageClassification current_page_classification_;
135 // Copy of OmniboxEditModel::permanent_text_.
136 base::string16 permanent_text_;
138 // Fetcher used to retrieve results.
139 scoped_ptr<net::URLFetcher> fetcher_;
141 // Suggestion for the current URL.
142 AutocompleteMatch current_url_match_;
144 // Contains suggest and navigation results as well as relevance parsed from
145 // the response for the most recent zero suggest input URL.
146 SearchSuggestionParser::Results results_;
148 // Whether we are currently showing cached zero suggest results.
149 bool results_from_cache_;
151 history::MostVisitedURLList most_visited_urls_;
153 // Whether we are waiting for a most visited visited urls callback to run.
154 bool waiting_for_most_visited_urls_request_;
156 // For callbacks that may be run after destruction.
157 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
159 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
162 #endif // COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_