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