| 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_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_ | 5 #ifndef COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_ |
| 6 #define COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_ | 6 #define COMPONENTS_OMNIBOX_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/gtest_prod_util.h" | |
| 13 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 14 #include "components/omnibox/autocomplete_match.h" | 13 #include "components/omnibox/autocomplete_match.h" |
| 15 #include "components/omnibox/autocomplete_match_type.h" | 14 #include "components/omnibox/autocomplete_match_type.h" |
| 15 #include "components/omnibox/suggestion_answer.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 class AutocompleteInput; | 18 class AutocompleteInput; |
| 19 class AutocompleteSchemeClassifier; | 19 class AutocompleteSchemeClassifier; |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class DictionaryValue; | 22 class DictionaryValue; |
| 23 class Value; | 23 class Value; |
| 24 } | 24 } |
| 25 | 25 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 111 |
| 112 class SuggestResult : public Result { | 112 class SuggestResult : public Result { |
| 113 public: | 113 public: |
| 114 SuggestResult(const base::string16& suggestion, | 114 SuggestResult(const base::string16& suggestion, |
| 115 AutocompleteMatchType::Type type, | 115 AutocompleteMatchType::Type type, |
| 116 const base::string16& match_contents, | 116 const base::string16& match_contents, |
| 117 const base::string16& match_contents_prefix, | 117 const base::string16& match_contents_prefix, |
| 118 const base::string16& annotation, | 118 const base::string16& annotation, |
| 119 const base::string16& answer_contents, | 119 const base::string16& answer_contents, |
| 120 const base::string16& answer_type, | 120 const base::string16& answer_type, |
| 121 scoped_ptr<SuggestionAnswer> answer, |
| 121 const std::string& suggest_query_params, | 122 const std::string& suggest_query_params, |
| 122 const std::string& deletion_url, | 123 const std::string& deletion_url, |
| 123 bool from_keyword_provider, | 124 bool from_keyword_provider, |
| 124 int relevance, | 125 int relevance, |
| 125 bool relevance_from_server, | 126 bool relevance_from_server, |
| 126 bool should_prefetch, | 127 bool should_prefetch, |
| 127 const base::string16& input_text); | 128 const base::string16& input_text); |
| 129 SuggestResult(const SuggestResult& result); |
| 128 ~SuggestResult() override; | 130 ~SuggestResult() override; |
| 129 | 131 |
| 132 SuggestResult& operator=(const SuggestResult& rhs); |
| 133 |
| 130 const base::string16& suggestion() const { return suggestion_; } | 134 const base::string16& suggestion() const { return suggestion_; } |
| 131 const base::string16& match_contents_prefix() const { | 135 const base::string16& match_contents_prefix() const { |
| 132 return match_contents_prefix_; | 136 return match_contents_prefix_; |
| 133 } | 137 } |
| 134 const base::string16& annotation() const { return annotation_; } | 138 const base::string16& annotation() const { return annotation_; } |
| 135 const std::string& suggest_query_params() const { | 139 const std::string& suggest_query_params() const { |
| 136 return suggest_query_params_; | 140 return suggest_query_params_; |
| 137 } | 141 } |
| 138 | 142 |
| 139 const base::string16& answer_contents() const { return answer_contents_; } | 143 const base::string16& answer_contents() const { return answer_contents_; } |
| 140 const base::string16& answer_type() const { return answer_type_; } | 144 const base::string16& answer_type() const { return answer_type_; } |
| 145 const SuggestionAnswer* answer() const { return answer_.get(); } |
| 141 | 146 |
| 142 bool should_prefetch() const { return should_prefetch_; } | 147 bool should_prefetch() const { return should_prefetch_; } |
| 143 | 148 |
| 144 // Fills in |match_contents_class_| to reflect how |match_contents_| should | 149 // Fills in |match_contents_class_| to reflect how |match_contents_| should |
| 145 // be displayed and bolded against the current |input_text|. If | 150 // be displayed and bolded against the current |input_text|. If |
| 146 // |allow_bolding_all| is false and |match_contents_class_| would have all | 151 // |allow_bolding_all| is false and |match_contents_class_| would have all |
| 147 // of |match_contents_| bolded, do nothing. | 152 // of |match_contents_| bolded, do nothing. |
| 148 void ClassifyMatchContents(const bool allow_bolding_all, | 153 void ClassifyMatchContents(const bool allow_bolding_all, |
| 149 const base::string16& input_text); | 154 const base::string16& input_text); |
| 150 | 155 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 163 base::string16 match_contents_prefix_; | 168 base::string16 match_contents_prefix_; |
| 164 | 169 |
| 165 // Optional annotation for the |match_contents_| for disambiguation. | 170 // Optional annotation for the |match_contents_| for disambiguation. |
| 166 // This may be displayed in the autocomplete match contents, but is defined | 171 // This may be displayed in the autocomplete match contents, but is defined |
| 167 // separately to facilitate different formatting. | 172 // separately to facilitate different formatting. |
| 168 base::string16 annotation_; | 173 base::string16 annotation_; |
| 169 | 174 |
| 170 // Optional additional parameters to be added to the search URL. | 175 // Optional additional parameters to be added to the search URL. |
| 171 std::string suggest_query_params_; | 176 std::string suggest_query_params_; |
| 172 | 177 |
| 178 // TODO(jdonnelly): Remove the following two properties once the downstream |
| 179 // clients are using the SuggestionAnswer. |
| 173 // Optional formatted Answers result. | 180 // Optional formatted Answers result. |
| 174 base::string16 answer_contents_; | 181 base::string16 answer_contents_; |
| 175 | 182 |
| 176 // Type of optional formatted Answers result. | 183 // Type of optional formatted Answers result. |
| 177 base::string16 answer_type_; | 184 base::string16 answer_type_; |
| 178 | 185 |
| 186 // Optional short answer to the input that produced this suggestion. |
| 187 scoped_ptr<SuggestionAnswer> answer_; |
| 188 |
| 179 // Should this result be prefetched? | 189 // Should this result be prefetched? |
| 180 bool should_prefetch_; | 190 bool should_prefetch_; |
| 181 }; | 191 }; |
| 182 | 192 |
| 183 class NavigationResult : public Result { | 193 class NavigationResult : public Result { |
| 184 public: | 194 public: |
| 185 NavigationResult(const AutocompleteSchemeClassifier& scheme_classifier, | 195 NavigationResult(const AutocompleteSchemeClassifier& scheme_classifier, |
| 186 const GURL& url, | 196 const GURL& url, |
| 187 AutocompleteMatchType::Type type, | 197 AutocompleteMatchType::Type type, |
| 188 const base::string16& description, | 198 const base::string16& description, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // The JSON metadata associated with this server response. | 267 // The JSON metadata associated with this server response. |
| 258 std::string metadata; | 268 std::string metadata; |
| 259 | 269 |
| 260 // If the active suggest field trial (if any) has triggered. | 270 // If the active suggest field trial (if any) has triggered. |
| 261 bool field_trial_triggered; | 271 bool field_trial_triggered; |
| 262 | 272 |
| 263 // If the relevance values of the results are from the server. | 273 // If the relevance values of the results are from the server. |
| 264 bool relevances_from_server; | 274 bool relevances_from_server; |
| 265 | 275 |
| 266 // URLs of any images in Answers results. | 276 // URLs of any images in Answers results. |
| 267 std::vector<GURL> answers_image_urls; | 277 SuggestionAnswer::URLs answers_image_urls; |
| 268 | 278 |
| 269 private: | 279 private: |
| 270 DISALLOW_COPY_AND_ASSIGN(Results); | 280 DISALLOW_COPY_AND_ASSIGN(Results); |
| 271 }; | 281 }; |
| 272 | 282 |
| 273 // Extracts JSON data fetched by |source| and converts it to UTF-8. | 283 // Extracts JSON data fetched by |source| and converts it to UTF-8. |
| 274 static std::string ExtractJsonData(const net::URLFetcher* source); | 284 static std::string ExtractJsonData(const net::URLFetcher* source); |
| 275 | 285 |
| 276 // Parses JSON response received from the provider, stripping XSSI | 286 // Parses JSON response received from the provider, stripping XSSI |
| 277 // protection if needed. Returns the parsed data if successful, NULL | 287 // protection if needed. Returns the parsed data if successful, NULL |
| 278 // otherwise. | 288 // otherwise. |
| 279 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); | 289 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); |
| 280 | 290 |
| 281 // Parses results from the suggest server and updates the appropriate suggest | 291 // Parses results from the suggest server and updates the appropriate suggest |
| 282 // and navigation result lists in |results|. |is_keyword_result| indicates | 292 // and navigation result lists in |results|. |is_keyword_result| indicates |
| 283 // whether the response was received from the keyword provider. | 293 // whether the response was received from the keyword provider. |
| 284 // Returns whether the appropriate result list members were updated. | 294 // Returns whether the appropriate result list members were updated. |
| 285 static bool ParseSuggestResults( | 295 static bool ParseSuggestResults( |
| 286 const base::Value& root_val, | 296 const base::Value& root_val, |
| 287 const AutocompleteInput& input, | 297 const AutocompleteInput& input, |
| 288 const AutocompleteSchemeClassifier& scheme_classifier, | 298 const AutocompleteSchemeClassifier& scheme_classifier, |
| 289 int default_result_relevance, | 299 int default_result_relevance, |
| 290 const std::string& languages, | 300 const std::string& languages, |
| 291 bool is_keyword_result, | 301 bool is_keyword_result, |
| 292 Results* results); | 302 Results* results); |
| 293 | 303 |
| 294 private: | 304 private: |
| 295 FRIEND_TEST_ALL_PREFIXES(SearchSuggestionParser, | |
| 296 GetAnswersImageURLsWithoutImagelines); | |
| 297 FRIEND_TEST_ALL_PREFIXES(SearchSuggestionParser, | |
| 298 GetAnswersImageURLsWithValidImage); | |
| 299 | |
| 300 // Gets URLs of any images in Answers results. | |
| 301 static void GetAnswersImageURLs(const base::DictionaryValue* answer_json, | |
| 302 std::vector<GURL>* urls); | |
| 303 | |
| 304 DISALLOW_COPY_AND_ASSIGN(SearchSuggestionParser); | 305 DISALLOW_COPY_AND_ASSIGN(SearchSuggestionParser); |
| 305 }; | 306 }; |
| 306 | 307 |
| 307 #endif // COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_ | 308 #endif // COMPONENTS_OMNIBOX_SEARCH_SUGGESTION_PARSER_H_ |
| OLD | NEW |