Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / components / omnibox / search_suggestion_parser.h
blobe86affb486a80816faf364463bb6dbf00fc2d662
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.
5 #ifndef COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_
6 #define COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_piece.h"
14 #include "components/omnibox/autocomplete_match.h"
15 #include "components/omnibox/autocomplete_match_type.h"
16 #include "components/omnibox/suggestion_answer.h"
17 #include "url/gurl.h"
19 class AutocompleteInput;
20 class AutocompleteSchemeClassifier;
22 namespace base {
23 class DictionaryValue;
24 class Value;
27 namespace net {
28 class URLFetcher;
31 class SearchSuggestionParser {
32 public:
33 // The Result classes are intermediate representations of AutocompleteMatches,
34 // simply containing relevance-ranked search and navigation suggestions.
35 // They may be cached to provide some synchronous matches while requests for
36 // new suggestions from updated input are in flight.
37 // TODO(msw) Extend these classes to generate their corresponding matches and
38 // other requisite data, in order to consolidate and simplify the
39 // highly fragmented SearchProvider logic for each Result type.
40 class Result {
41 public:
42 Result(bool from_keyword_provider,
43 int relevance,
44 bool relevance_from_server,
45 AutocompleteMatchType::Type type,
46 const std::string& deletion_url);
47 virtual ~Result();
49 bool from_keyword_provider() const { return from_keyword_provider_; }
51 const base::string16& match_contents() const { return match_contents_; }
52 const ACMatchClassifications& match_contents_class() const {
53 return match_contents_class_;
56 AutocompleteMatchType::Type type() const { return type_; }
57 int relevance() const { return relevance_; }
58 void set_relevance(int relevance) { relevance_ = relevance; }
59 bool received_after_last_keystroke() const {
60 return received_after_last_keystroke_;
62 void set_received_after_last_keystroke(
63 bool received_after_last_keystroke) {
64 received_after_last_keystroke_ = received_after_last_keystroke;
67 bool relevance_from_server() const { return relevance_from_server_; }
68 void set_relevance_from_server(bool relevance_from_server) {
69 relevance_from_server_ = relevance_from_server;
72 const std::string& deletion_url() const { return deletion_url_; }
74 // Returns the default relevance value for this result (which may
75 // be left over from a previous omnibox input) given the current
76 // input and whether the current input caused a keyword provider
77 // to be active.
78 virtual int CalculateRelevance(const AutocompleteInput& input,
79 bool keyword_provider_requested) const = 0;
81 protected:
82 // The contents to be displayed and its style info.
83 base::string16 match_contents_;
84 ACMatchClassifications match_contents_class_;
86 // True if the result came from the keyword provider.
87 bool from_keyword_provider_;
89 AutocompleteMatchType::Type type_;
91 // The relevance score.
92 int relevance_;
94 private:
95 // Whether this result's relevance score was fully or partly calculated
96 // based on server information, and thus is assumed to be more accurate.
97 // This is ultimately used in
98 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments
99 // there.
100 bool relevance_from_server_;
102 // Whether this result was received asynchronously after the last
103 // keystroke, otherwise it must have come from prior cached results
104 // or from a synchronous provider.
105 bool received_after_last_keystroke_;
107 // Optional deletion URL provided with suggestions. Fetching this URL
108 // should result in some reasonable deletion behaviour on the server,
109 // e.g. deleting this term out of a user's server-side search history.
110 std::string deletion_url_;
113 class SuggestResult : public Result {
114 public:
115 SuggestResult(const base::string16& suggestion,
116 AutocompleteMatchType::Type type,
117 const base::string16& match_contents,
118 const base::string16& match_contents_prefix,
119 const base::string16& annotation,
120 const base::string16& answer_contents,
121 const base::string16& answer_type,
122 scoped_ptr<SuggestionAnswer> answer,
123 const std::string& suggest_query_params,
124 const std::string& deletion_url,
125 bool from_keyword_provider,
126 int relevance,
127 bool relevance_from_server,
128 bool should_prefetch,
129 const base::string16& input_text);
130 SuggestResult(const SuggestResult& result);
131 ~SuggestResult() override;
133 SuggestResult& operator=(const SuggestResult& rhs);
135 const base::string16& suggestion() const { return suggestion_; }
136 const base::string16& match_contents_prefix() const {
137 return match_contents_prefix_;
139 const base::string16& annotation() const { return annotation_; }
140 const std::string& suggest_query_params() const {
141 return suggest_query_params_;
144 const base::string16& answer_contents() const { return answer_contents_; }
145 const base::string16& answer_type() const { return answer_type_; }
146 const SuggestionAnswer* answer() const { return answer_.get(); }
148 bool should_prefetch() const { return should_prefetch_; }
150 // Fills in |match_contents_class_| to reflect how |match_contents_| should
151 // be displayed and bolded against the current |input_text|. If
152 // |allow_bolding_all| is false and |match_contents_class_| would have all
153 // of |match_contents_| bolded, do nothing.
154 void ClassifyMatchContents(const bool allow_bolding_all,
155 const base::string16& input_text);
157 // Result:
158 int CalculateRelevance(const AutocompleteInput& input,
159 bool keyword_provider_requested) const override;
161 private:
162 // The search terms to be used for this suggestion.
163 base::string16 suggestion_;
165 // The contents to be displayed as prefix of match contents.
166 // Used for postfix suggestions to display a leading ellipsis (or some
167 // equivalent character) to indicate omitted text.
168 // Only used to pass this information to about:omnibox's "Additional Info".
169 base::string16 match_contents_prefix_;
171 // Optional annotation for the |match_contents_| for disambiguation.
172 // This may be displayed in the autocomplete match contents, but is defined
173 // separately to facilitate different formatting.
174 base::string16 annotation_;
176 // Optional additional parameters to be added to the search URL.
177 std::string suggest_query_params_;
179 // TODO(jdonnelly): Remove the following two properties once the downstream
180 // clients are using the SuggestionAnswer.
181 // Optional formatted Answers result.
182 base::string16 answer_contents_;
184 // Type of optional formatted Answers result.
185 base::string16 answer_type_;
187 // Optional short answer to the input that produced this suggestion.
188 scoped_ptr<SuggestionAnswer> answer_;
190 // Should this result be prefetched?
191 bool should_prefetch_;
194 class NavigationResult : public Result {
195 public:
196 NavigationResult(const AutocompleteSchemeClassifier& scheme_classifier,
197 const GURL& url,
198 AutocompleteMatchType::Type type,
199 const base::string16& description,
200 const std::string& deletion_url,
201 bool from_keyword_provider,
202 int relevance,
203 bool relevance_from_server,
204 const base::string16& input_text,
205 const std::string& languages);
206 ~NavigationResult() override;
208 const GURL& url() const { return url_; }
209 const base::string16& description() const { return description_; }
210 const base::string16& formatted_url() const { return formatted_url_; }
212 // Fills in |match_contents_| and |match_contents_class_| to reflect how
213 // the URL should be displayed and bolded against the current |input_text|
214 // and user |languages|. If |allow_bolding_nothing| is false and
215 // |match_contents_class_| would result in an entirely unbolded
216 // |match_contents_|, do nothing.
217 void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing,
218 const base::string16& input_text,
219 const std::string& languages);
221 // Result:
222 int CalculateRelevance(const AutocompleteInput& input,
223 bool keyword_provider_requested) const override;
225 private:
226 // The suggested url for navigation.
227 GURL url_;
229 // The properly formatted ("fixed up") URL string with equivalent meaning
230 // to the one in |url_|.
231 base::string16 formatted_url_;
233 // The suggested navigational result description; generally the site name.
234 base::string16 description_;
237 typedef std::vector<SuggestResult> SuggestResults;
238 typedef std::vector<NavigationResult> NavigationResults;
240 // A simple structure bundling most of the information (including
241 // both SuggestResults and NavigationResults) returned by a call to
242 // the suggest server.
244 // This has to be declared after the typedefs since it relies on some of them.
245 struct Results {
246 Results();
247 ~Results();
249 // Clears |suggest_results| and |navigation_results| and resets
250 // |verbatim_relevance| to -1 (implies unset).
251 void Clear();
253 // Returns whether any of the results (including verbatim) have
254 // server-provided scores.
255 bool HasServerProvidedScores() const;
257 // Query suggestions sorted by relevance score.
258 SuggestResults suggest_results;
260 // Navigational suggestions sorted by relevance score.
261 NavigationResults navigation_results;
263 // The server supplied verbatim relevance scores. Negative values
264 // indicate that there is no suggested score; a value of 0
265 // suppresses the verbatim result.
266 int verbatim_relevance;
268 // The JSON metadata associated with this server response.
269 std::string metadata;
271 // If the active suggest field trial (if any) has triggered.
272 bool field_trial_triggered;
274 // If the relevance values of the results are from the server.
275 bool relevances_from_server;
277 // URLs of any images in Answers results.
278 SuggestionAnswer::URLs answers_image_urls;
280 private:
281 DISALLOW_COPY_AND_ASSIGN(Results);
284 // Extracts JSON data fetched by |source| and converts it to UTF-8.
285 static std::string ExtractJsonData(const net::URLFetcher* source);
287 // Parses JSON response received from the provider, stripping XSSI
288 // protection if needed. Returns the parsed data if successful, NULL
289 // otherwise.
290 static scoped_ptr<base::Value> DeserializeJsonData(
291 base::StringPiece json_data);
293 // Parses results from the suggest server and updates the appropriate suggest
294 // and navigation result lists in |results|. |is_keyword_result| indicates
295 // whether the response was received from the keyword provider.
296 // Returns whether the appropriate result list members were updated.
297 static bool ParseSuggestResults(
298 const base::Value& root_val,
299 const AutocompleteInput& input,
300 const AutocompleteSchemeClassifier& scheme_classifier,
301 int default_result_relevance,
302 const std::string& languages,
303 bool is_keyword_result,
304 Results* results);
306 private:
307 DISALLOW_COPY_AND_ASSIGN(SearchSuggestionParser);
310 #endif // COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_