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