Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / autocomplete / zero_suggest_provider.h
blob0156753c9ffbd445a5e4d06690d8c11d9c899385
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 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 "components/history/core/browser/history_types.h"
16 #include "components/metrics/proto/omnibox_event.pb.h"
17 #include "components/omnibox/base_search_provider.h"
18 #include "components/omnibox/search_provider.h"
19 #include "net/url_request/url_fetcher_delegate.h"
21 class AutocompleteProviderListener;
22 class Profile;
23 class TemplateURLService;
25 namespace base {
26 class ListValue;
27 class Value;
30 namespace net {
31 class URLFetcher;
34 namespace user_prefs {
35 class PrefRegistrySyncable;
38 // Autocomplete provider for searches based on the current URL.
40 // The controller will call Start() with |on_focus| set when the user focuses
41 // the omnibox. After construction, the autocomplete controller repeatedly calls
42 // Start() with some user input, each time expecting to receive an updated set
43 // of matches.
45 // TODO(jered): Consider deleting this class and building this functionality
46 // into SearchProvider after dogfood and after we break the association between
47 // omnibox text and suggestions.
48 class ZeroSuggestProvider : public BaseSearchProvider,
49 public net::URLFetcherDelegate {
50 public:
51 // Creates and returns an instance of this provider.
52 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
53 TemplateURLService* template_url_service,
54 Profile* profile);
56 // Registers a preference used to cache zero suggest results.
57 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
59 // AutocompleteProvider:
60 void Start(const AutocompleteInput& input,
61 bool minimal_changes,
62 bool called_due_to_focus) override;
63 void Stop(bool clear_cached_results,
64 bool due_to_user_inactivity) override;
65 void DeleteMatch(const AutocompleteMatch& match) override;
66 void AddProviderInfo(ProvidersInfo* provider_info) const override;
68 // Sets |field_trial_triggered_| to false.
69 void ResetSession() override;
71 private:
72 ZeroSuggestProvider(AutocompleteProviderListener* listener,
73 TemplateURLService* template_url_service,
74 Profile* profile);
76 ~ZeroSuggestProvider() override;
78 // BaseSearchProvider:
79 const TemplateURL* GetTemplateURL(bool is_keyword) const override;
80 const AutocompleteInput GetInput(bool is_keyword) const override;
81 bool ShouldAppendExtraParams(
82 const SearchSuggestionParser::SuggestResult& result) const override;
83 void RecordDeletionResult(bool success) override;
85 // net::URLFetcherDelegate:
86 void OnURLFetchComplete(const net::URLFetcher* source) override;
88 // Optionally, cache the received |json_data| and return true if we want
89 // to stop processing results at this point. The |parsed_data| is the parsed
90 // version of |json_data| used to determine if we received an empty result.
91 bool StoreSuggestionResponse(const std::string& json_data,
92 const base::Value& parsed_data);
94 // Adds AutocompleteMatches for each of the suggestions in |results| to
95 // |map|.
96 void AddSuggestResultsToMap(
97 const SearchSuggestionParser::SuggestResults& results,
98 MatchMap* map);
100 // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
101 AutocompleteMatch NavigationToMatch(
102 const SearchSuggestionParser::NavigationResult& navigation);
104 // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
105 void Run(const GURL& suggest_url);
107 // Converts the parsed results to a set of AutocompleteMatches and adds them
108 // to |matches_|. Also update the histograms for how many results were
109 // received.
110 void ConvertResultsToAutocompleteMatches();
112 // Returns an AutocompleteMatch for the current URL. The match should be in
113 // the top position so that pressing enter has the effect of reloading the
114 // page.
115 AutocompleteMatch MatchForCurrentURL();
117 // When the user is in the Most Visited field trial, we ask the TopSites
118 // service for the most visited URLs during Run(). It calls back to this
119 // function to return those |urls|.
120 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
122 // Returns the relevance score for the verbatim result.
123 int GetVerbatimRelevance() const;
125 // Whether we can show zero suggest without sending |current_page_url| to
126 // |suggest_url| search provider. Also checks that other conditions for
127 // non-contextual zero suggest are satisfied.
128 bool ShouldShowNonContextualZeroSuggest(const GURL& suggest_url,
129 const GURL& current_page_url) const;
131 // Checks whether we have a set of zero suggest results cached, and if so
132 // populates |matches_| with cached results.
133 void MaybeUseCachedSuggestions();
135 AutocompleteProviderListener* listener_;
136 Profile* profile_;
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 // Whether we are waiting for a most visited visited urls callback to run.
164 bool waiting_for_most_visited_urls_request_;
166 // For callbacks that may be run after destruction.
167 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
169 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
172 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_