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 #include "chrome/browser/autocomplete/base_search_provider.h" | 5 #include "chrome/browser/autocomplete/base_search_provider.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/autocomplete/answers_image_service.h" | |
16 #include "chrome/browser/autocomplete/answers_image_service_factory.h" | |
15 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
16 #include "chrome/browser/autocomplete/url_prefix.h" | 18 #include "chrome/browser/autocomplete/url_prefix.h" |
17 #include "chrome/browser/history/history_service.h" | 19 #include "chrome/browser/history/history_service.h" |
18 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
19 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 21 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/search/instant_service.h" | 23 #include "chrome/browser/search/instant_service.h" |
22 #include "chrome/browser/search/instant_service_factory.h" | 24 #include "chrome/browser/search/instant_service_factory.h" |
23 #include "chrome/browser/search/search.h" | 25 #include "chrome/browser/search/search.h" |
24 #include "chrome/browser/search_engines/template_url.h" | 26 #include "chrome/browser/search_engines/template_url.h" |
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
906 suggestion_detail->GetString("mp", &match_contents_prefix); | 908 suggestion_detail->GetString("mp", &match_contents_prefix); |
907 // Error correction for bad data from server. | 909 // Error correction for bad data from server. |
908 if (match_contents.empty()) | 910 if (match_contents.empty()) |
909 match_contents = suggestion; | 911 match_contents = suggestion; |
910 suggestion_detail->GetString("a", &annotation); | 912 suggestion_detail->GetString("a", &annotation); |
911 suggestion_detail->GetString("q", &suggest_query_params); | 913 suggestion_detail->GetString("q", &suggest_query_params); |
912 | 914 |
913 // Extract Answers, if provided. | 915 // Extract Answers, if provided. |
914 const base::DictionaryValue* answer_json = NULL; | 916 const base::DictionaryValue* answer_json = NULL; |
915 if (suggestion_detail->GetDictionary("ansa", &answer_json)) { | 917 if (suggestion_detail->GetDictionary("ansa", &answer_json)) { |
918 PrefetchAnswersImages(answer_json); | |
916 std::string contents; | 919 std::string contents; |
917 base::JSONWriter::Write(answer_json, &contents); | 920 base::JSONWriter::Write(answer_json, &contents); |
918 answer_contents = base::UTF8ToUTF16(contents); | 921 answer_contents = base::UTF8ToUTF16(contents); |
919 suggestion_detail->GetString("ansb", &answer_type); | 922 suggestion_detail->GetString("ansb", &answer_type); |
920 } | 923 } |
921 } | 924 } |
922 } | 925 } |
923 | 926 |
924 bool should_prefetch = static_cast<int>(index) == prefetch_index; | 927 bool should_prefetch = static_cast<int>(index) == prefetch_index; |
925 // TODO(kochi): Improve calculator suggestion presentation. | 928 // TODO(kochi): Improve calculator suggestion presentation. |
926 results->suggest_results.push_back(SuggestResult( | 929 results->suggest_results.push_back(SuggestResult( |
927 base::CollapseWhitespace(suggestion, false), match_type, | 930 base::CollapseWhitespace(suggestion, false), match_type, |
928 base::CollapseWhitespace(match_contents, false), | 931 base::CollapseWhitespace(match_contents, false), |
929 match_contents_prefix, annotation, answer_contents, answer_type, | 932 match_contents_prefix, annotation, answer_contents, answer_type, |
930 suggest_query_params, deletion_url, is_keyword_result, relevance, | 933 suggest_query_params, deletion_url, is_keyword_result, relevance, |
931 relevances != NULL, should_prefetch, trimmed_input)); | 934 relevances != NULL, should_prefetch, trimmed_input)); |
932 } | 935 } |
933 } | 936 } |
934 SortResults(is_keyword_result, relevances, results); | 937 SortResults(is_keyword_result, relevances, results); |
935 return true; | 938 return true; |
936 } | 939 } |
937 | 940 |
941 void BaseSearchProvider::PrefetchAnswersImages( | |
942 const base::DictionaryValue* answer_json) { | |
943 DCHECK(answer_json); | |
944 const base::ListValue* lines = NULL; | |
945 answer_json->GetList("l", &lines); | |
946 if (!lines || lines->GetSize() == 0) | |
947 return; | |
948 | |
949 // TODO(groby): Do we want prefetch in incognito? | |
Peter Kasting
2014/06/05 23:42:47
We don't currently send suggest requests in incogn
groby-ooo-7-16
2014/06/14 00:22:08
Done.
| |
950 AnswersImageService* image_service = | |
951 AnswersImageServiceFactory::GetForBrowserContext(profile_); | |
952 DCHECK(image_service); | |
953 | |
954 for (size_t line = 0; line < lines->GetSize(); ++line) { | |
955 const base::DictionaryValue* imageLine = NULL; | |
956 lines->GetDictionary(line, &imageLine); | |
957 if (!imageLine) | |
958 continue; | |
959 const base::DictionaryValue* imageData = NULL; | |
960 imageLine->GetDictionary("i", &imageData); | |
961 if (!imageData) | |
962 continue; | |
963 std::string imageUrl; | |
964 imageData->GetString("d", &imageUrl); | |
965 | |
966 // Not having an URL is technically a malformed reply, but crashing seems | |
967 // overly harsh as response. | |
Peter Kasting
2014/06/05 23:42:47
This comment doesn't seem necessary -- clearly we
groby-ooo-7-16
2014/06/14 00:22:08
Removed.
| |
968 if (imageUrl.empty()) | |
969 continue; | |
970 LOG(ERROR) << "Prefetching image for " << imageUrl; | |
Peter Kasting
2014/06/05 23:42:47
I don't think you should check this line in.
groby-ooo-7-16
2014/06/14 00:22:08
Done.
| |
971 image_service->Prefetch(GURL(imageUrl)); | |
Peter Kasting
2014/06/05 23:42:47
What if GURL(imageUrl) is invalid? This can happe
groby-ooo-7-16
2014/06/14 00:22:08
Done.
| |
972 } | |
973 } | |
974 | |
938 void BaseSearchProvider::SortResults(bool is_keyword, | 975 void BaseSearchProvider::SortResults(bool is_keyword, |
939 const base::ListValue* relevances, | 976 const base::ListValue* relevances, |
940 Results* results) { | 977 Results* results) { |
941 } | 978 } |
942 | 979 |
943 bool BaseSearchProvider::StoreSuggestionResponse( | 980 bool BaseSearchProvider::StoreSuggestionResponse( |
944 const std::string& json_data, | 981 const std::string& json_data, |
945 const base::Value& parsed_data) { | 982 const base::Value& parsed_data) { |
946 return false; | 983 return false; |
947 } | 984 } |
(...skipping 18 matching lines...) Expand all Loading... | |
966 } | 1003 } |
967 | 1004 |
968 void BaseSearchProvider::OnDeletionComplete( | 1005 void BaseSearchProvider::OnDeletionComplete( |
969 bool success, SuggestionDeletionHandler* handler) { | 1006 bool success, SuggestionDeletionHandler* handler) { |
970 RecordDeletionResult(success); | 1007 RecordDeletionResult(success); |
971 SuggestionDeletionHandlers::iterator it = std::find( | 1008 SuggestionDeletionHandlers::iterator it = std::find( |
972 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 1009 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
973 DCHECK(it != deletion_handlers_.end()); | 1010 DCHECK(it != deletion_handlers_.end()); |
974 deletion_handlers_.erase(it); | 1011 deletion_handlers_.erase(it); |
975 } | 1012 } |
OLD | NEW |