Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / base_search_provider.h
blobdcf1218a70dfc93b141657603f6680226b8c248b
1 // Copyright 2014 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 class contains common functionality for search-based autocomplete
6 // providers. Search provider and zero suggest provider both use it for common
7 // functionality.
9 #ifndef COMPONENTS_OMNIBOX_BROWSER_BASE_SEARCH_PROVIDER_H_
10 #define COMPONENTS_OMNIBOX_BROWSER_BASE_SEARCH_PROVIDER_H_
12 #include <map>
13 #include <string>
14 #include <utility>
15 #include <vector>
17 #include "base/memory/scoped_vector.h"
18 #include "base/strings/string16.h"
19 #include "components/metrics/proto/omnibox_event.pb.h"
20 #include "components/omnibox/browser/autocomplete_input.h"
21 #include "components/omnibox/browser/autocomplete_match.h"
22 #include "components/omnibox/browser/autocomplete_provider.h"
23 #include "components/omnibox/browser/search_suggestion_parser.h"
25 class AutocompleteProviderClient;
26 class GURL;
27 class SearchTermsData;
28 class SuggestionDeletionHandler;
29 class TemplateURL;
31 namespace base {
32 class DictionaryValue;
33 class ListValue;
34 class Value;
37 // Base functionality for receiving suggestions from a search engine.
38 // This class is abstract and should only be used as a base for other
39 // autocomplete providers utilizing its functionality.
40 class BaseSearchProvider : public AutocompleteProvider {
41 public:
42 // ID used in creating URLFetcher for default provider's suggest results.
43 static const int kDefaultProviderURLFetcherID;
45 // ID used in creating URLFetcher for keyword provider's suggest results.
46 static const int kKeywordProviderURLFetcherID;
48 // ID used in creating URLFetcher for deleting suggestion results.
49 static const int kDeletionURLFetcherID;
51 BaseSearchProvider(AutocompleteProvider::Type type,
52 AutocompleteProviderClient* client);
54 // Returns whether |match| is flagged as a query that should be prefetched.
55 static bool ShouldPrefetch(const AutocompleteMatch& match);
57 // Returns a simpler AutocompleteMatch suitable for persistence like in
58 // ShortcutsDatabase. This wrapper function uses a number of default values
59 // that may or may not be appropriate for your needs.
60 // NOTE: Use with care. Most likely you want the other CreateSearchSuggestion
61 // with protected access.
62 static AutocompleteMatch CreateSearchSuggestion(
63 const base::string16& suggestion,
64 AutocompleteMatchType::Type type,
65 bool from_keyword_provider,
66 const TemplateURL* template_url,
67 const SearchTermsData& search_terms_data);
69 // AutocompleteProvider:
70 void DeleteMatch(const AutocompleteMatch& match) override;
71 void AddProviderInfo(ProvidersInfo* provider_info) const override;
73 bool field_trial_triggered_in_session() const {
74 return field_trial_triggered_in_session_;
77 protected:
78 // The following keys are used to record additional information on matches.
80 // We annotate our AutocompleteMatches with whether their relevance scores
81 // were server-provided using this key in the |additional_info| field.
82 static const char kRelevanceFromServerKey[];
84 // Indicates whether the server said a match should be prefetched.
85 static const char kShouldPrefetchKey[];
87 // Used to store metadata from the server response, which is needed for
88 // prefetching.
89 static const char kSuggestMetadataKey[];
91 // Used to store a deletion request url for server-provided suggestions.
92 static const char kDeletionUrlKey[];
94 // These are the values for the above keys.
95 static const char kTrue[];
96 static const char kFalse[];
98 ~BaseSearchProvider() override;
100 typedef std::pair<base::string16, std::string> MatchKey;
101 typedef std::map<MatchKey, AutocompleteMatch> MatchMap;
102 typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers;
104 // Returns an AutocompleteMatch with the given |autocomplete_provider|
105 // for the search |suggestion|, which represents a search via |template_url|.
106 // If |template_url| is NULL, returns a match with an invalid destination URL.
108 // |input| is the original user input. Text in the input is used to highlight
109 // portions of the match contents to distinguish locally-typed text from
110 // suggested text.
112 // |input| is also necessary for various other details, like whether we should
113 // allow inline autocompletion and what the transition type should be.
114 // |in_keyword_mode| helps guarantee a non-keyword suggestion does not
115 // appear as the default match when the user is in keyword mode.
116 // |accepted_suggestion| is used to generate Assisted Query Stats.
117 // |append_extra_query_params| should be set if |template_url| is the default
118 // search engine, so the destination URL will contain any
119 // command-line-specified query params.
120 static AutocompleteMatch CreateSearchSuggestion(
121 AutocompleteProvider* autocomplete_provider,
122 const AutocompleteInput& input,
123 const bool in_keyword_mode,
124 const SearchSuggestionParser::SuggestResult& suggestion,
125 const TemplateURL* template_url,
126 const SearchTermsData& search_terms_data,
127 int accepted_suggestion,
128 bool append_extra_query_params);
130 // Returns whether the requirements for requesting zero suggest results
131 // are met. The requirements are
132 // * The user is enrolled in a zero suggest experiment.
133 // * The user is not on the NTP.
134 // * The suggest request is sent over HTTPS. This avoids leaking the current
135 // page URL or personal data in unencrypted network traffic.
136 // * The user has suggest enabled in their settings and is not in incognito
137 // mode. (Incognito disables suggest entirely.)
138 // * The user's suggest provider is Google. We might want to allow other
139 // providers to see this data someday, but for now this has only been
140 // implemented for Google.
141 static bool ZeroSuggestEnabled(
142 const GURL& suggest_url,
143 const TemplateURL* template_url,
144 metrics::OmniboxEventProto::PageClassification page_classification,
145 const SearchTermsData& search_terms_data,
146 const AutocompleteProviderClient* client);
148 // Returns whether we can send the URL of the current page in any suggest
149 // requests. Doing this requires that all the following hold:
150 // * ZeroSuggestEnabled() is true, so we meet the requirements above.
151 // * The current URL is HTTP, or HTTPS with the same domain as the suggest
152 // server. Non-HTTP[S] URLs (e.g. FTP/file URLs) may contain sensitive
153 // information. HTTPS URLs may also contain sensitive information, but if
154 // they're on the same domain as the suggest server, then the relevant
155 // entity could have already seen/logged this data.
156 // * The user is OK in principle with sending URLs of current pages to their
157 // provider. Today, there is no explicit setting that controls this, but if
158 // the user has tab sync enabled and tab sync is unencrypted, then they're
159 // already sending this data to Google for sync purposes. Thus we use this
160 // setting as a proxy for "it's OK to send such data". In the future,
161 // especially if we want to support suggest providers other than Google, we
162 // may change this to be a standalone setting or part of some explicit
163 // general opt-in.
164 static bool CanSendURL(
165 const GURL& current_page_url,
166 const GURL& suggest_url,
167 const TemplateURL* template_url,
168 metrics::OmniboxEventProto::PageClassification page_classification,
169 const SearchTermsData& search_terms_data,
170 AutocompleteProviderClient* client);
172 // If the |deletion_url| is valid, then set |match.deletable| to true and
173 // save the |deletion_url| into the |match|'s additional info under
174 // the key |kDeletionUrlKey|.
175 void SetDeletionURL(const std::string& deletion_url,
176 AutocompleteMatch* match);
178 // Creates an AutocompleteMatch from |result| to search for the query in
179 // |result|. Adds the created match to |map|; if such a match
180 // already exists, whichever one has lower relevance is eliminated.
181 // |metadata| and |accepted_suggestion| are used for generating an
182 // AutocompleteMatch.
183 // |mark_as_deletable| indicates whether the match should be marked deletable.
184 // |in_keyword_mode| helps guarantee a non-keyword suggestion does not
185 // appear as the default match when the user is in keyword mode.
186 // NOTE: Any result containing a deletion URL is always marked deletable.
187 void AddMatchToMap(const SearchSuggestionParser::SuggestResult& result,
188 const std::string& metadata,
189 int accepted_suggestion,
190 bool mark_as_deletable,
191 bool in_keyword_mode,
192 MatchMap* map);
194 // Parses results from the suggest server and updates the appropriate suggest
195 // and navigation result lists in |results|. |default_result_relevance| is
196 // the relevance to use if it was not explicitly set by the server.
197 // |is_keyword_result| indicates whether the response was received from the
198 // keyword provider.
199 // Returns whether the appropriate result list members were updated.
200 bool ParseSuggestResults(const base::Value& root_val,
201 int default_result_relevance,
202 bool is_keyword_result,
203 SearchSuggestionParser::Results* results);
205 // Returns the TemplateURL corresponding to the keyword or default
206 // provider based on the value of |is_keyword|.
207 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const = 0;
209 // Returns the AutocompleteInput for keyword provider or default provider
210 // based on the value of |is_keyword|.
211 virtual const AutocompleteInput GetInput(bool is_keyword) const = 0;
213 // Returns whether the destination URL corresponding to the given |result|
214 // should contain command-line-specified query params.
215 virtual bool ShouldAppendExtraParams(
216 const SearchSuggestionParser::SuggestResult& result) const = 0;
218 // Records in UMA whether the deletion request resulted in success.
219 virtual void RecordDeletionResult(bool success) = 0;
221 AutocompleteProviderClient* client() { return client_; }
222 const AutocompleteProviderClient* client() const { return client_; }
224 bool field_trial_triggered() const { return field_trial_triggered_; }
226 void set_field_trial_triggered(bool triggered) {
227 field_trial_triggered_ = triggered;
229 void set_field_trial_triggered_in_session(bool triggered) {
230 field_trial_triggered_in_session_ = triggered;
233 private:
234 friend class SearchProviderTest;
235 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch);
237 // Removes the deleted |match| from the list of |matches_|.
238 void DeleteMatchFromMatches(const AutocompleteMatch& match);
240 // This gets called when we have requested a suggestion deletion from the
241 // server to handle the results of the deletion. It will be called after the
242 // deletion request completes.
243 void OnDeletionComplete(bool success,
244 SuggestionDeletionHandler* handler);
246 AutocompleteProviderClient* client_;
248 // Whether a field trial, if any, has triggered in the most recent
249 // autocomplete query. This field is set to true only if the suggestion
250 // provider has completed and the response contained
251 // '"google:fieldtrialtriggered":true'.
252 bool field_trial_triggered_;
254 // Same as above except that it is maintained across the current Omnibox
255 // session.
256 bool field_trial_triggered_in_session_;
258 // Each deletion handler in this vector corresponds to an outstanding request
259 // that a server delete a personalized suggestion. Making this a ScopedVector
260 // causes us to auto-cancel all such requests on shutdown.
261 SuggestionDeletionHandlers deletion_handlers_;
263 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider);
266 #endif // COMPONENTS_OMNIBOX_BROWSER_BASE_SEARCH_PROVIDER_H_