| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ | 5 #ifndef COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ |
| 6 #define COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ | 6 #define COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 14 #include "components/autocomplete/autocomplete_match.h" | 13 #include "components/autocomplete/autocomplete_match.h" |
| 15 #include "components/autocomplete/autocomplete_match_type.h" | 14 #include "components/autocomplete/autocomplete_match_type.h" |
| 16 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 17 | 16 |
| 18 class AutocompleteInput; | 17 class AutocompleteInput; |
| 19 class AutocompleteSchemeClassifier; | 18 class AutocompleteSchemeClassifier; |
| 20 | 19 |
| 21 namespace base { | 20 namespace base { |
| 22 class DictionaryValue; | 21 class DictionaryValue; |
| 23 class Value; | 22 class Value; |
| 24 } | 23 } |
| 25 | 24 |
| 25 namespace net { |
| 26 class URLFetcher; |
| 27 } |
| 28 |
| 26 class SearchSuggestionParser { | 29 class SearchSuggestionParser { |
| 27 public: | 30 public: |
| 28 // The Result classes are intermediate representations of AutocompleteMatches, | 31 // The Result classes are intermediate representations of AutocompleteMatches, |
| 29 // simply containing relevance-ranked search and navigation suggestions. | 32 // simply containing relevance-ranked search and navigation suggestions. |
| 30 // They may be cached to provide some synchronous matches while requests for | 33 // They may be cached to provide some synchronous matches while requests for |
| 31 // new suggestions from updated input are in flight. | 34 // new suggestions from updated input are in flight. |
| 32 // TODO(msw) Extend these classes to generate their corresponding matches and | 35 // TODO(msw) Extend these classes to generate their corresponding matches and |
| 33 // other requisite data, in order to consolidate and simplify the | 36 // other requisite data, in order to consolidate and simplify the |
| 34 // highly fragmented SearchProvider logic for each Result type. | 37 // highly fragmented SearchProvider logic for each Result type. |
| 35 class Result { | 38 class Result { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 // indicate that there is no suggested score; a value of 0 | 242 // indicate that there is no suggested score; a value of 0 |
| 240 // suppresses the verbatim result. | 243 // suppresses the verbatim result. |
| 241 int verbatim_relevance; | 244 int verbatim_relevance; |
| 242 | 245 |
| 243 // The JSON metadata associated with this server response. | 246 // The JSON metadata associated with this server response. |
| 244 std::string metadata; | 247 std::string metadata; |
| 245 | 248 |
| 246 // If the active suggest field trial (if any) has triggered. | 249 // If the active suggest field trial (if any) has triggered. |
| 247 bool field_trial_triggered; | 250 bool field_trial_triggered; |
| 248 | 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 |
| 249 private: | 258 private: |
| 250 DISALLOW_COPY_AND_ASSIGN(Results); | 259 DISALLOW_COPY_AND_ASSIGN(Results); |
| 251 }; | 260 }; |
| 252 | 261 |
| 253 typedef base::Callback<void(const GURL& image_url)> ImagePrefetchCallback; | 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); |
| 254 | 269 |
| 255 // Parses results from the suggest server and updates the appropriate suggest | 270 // Parses results from the suggest server and updates the appropriate suggest |
| 256 // and navigation result lists in |results|. |is_keyword_result| indicates | 271 // and navigation result lists in |results|. |is_keyword_result| indicates |
| 257 // whether the response was received from the keyword provider. | 272 // whether the response was received from the keyword provider. |
| 258 // Returns whether the appropriate result list members were updated. | 273 // Returns whether the appropriate result list members were updated. |
| 259 static bool ParseSuggestResults( | 274 static bool ParseSuggestResults( |
| 260 const base::Value& root_val, | 275 const base::Value& root_val, |
| 261 const AutocompleteInput& input, | 276 const AutocompleteInput& input, |
| 262 const AutocompleteSchemeClassifier& scheme_classifier, | 277 const AutocompleteSchemeClassifier& scheme_classifier, |
| 263 const ImagePrefetchCallback& image_prefetch_callback, | |
| 264 int default_result_relevance, | 278 int default_result_relevance, |
| 265 const std::string& languages, | 279 const std::string& languages, |
| 266 bool is_keyword_result, | 280 bool is_keyword_result, |
| 267 bool* relevances_from_server, | |
| 268 Results* results); | 281 Results* results); |
| 269 | 282 |
| 270 private: | 283 private: |
| 271 // Prefetches any images in Answers results. | 284 // Gets URLs of any images in Answers results. |
| 272 static void PrefetchAnswersImages( | 285 static void GetAnswersImageURLs(const base::DictionaryValue* answer_json, |
| 273 const base::DictionaryValue* answer_json, | 286 std::vector<GURL>* urls); |
| 274 const ImagePrefetchCallback& image_prefetch_callback); | |
| 275 | 287 |
| 276 DISALLOW_COPY_AND_ASSIGN(SearchSuggestionParser); | 288 DISALLOW_COPY_AND_ASSIGN(SearchSuggestionParser); |
| 277 }; | 289 }; |
| 278 | 290 |
| 279 #endif // COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ | 291 #endif // COMPONENTS_AUTOCOMPLETE_SEARCH_SUGGESTION_PARSER_H_ |
| OLD | NEW |