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/autocomplete_provider_listener.h" | 15 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
16 #include "chrome/browser/autocomplete/url_prefix.h" | 16 #include "chrome/browser/autocomplete/url_prefix.h" |
| 17 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" |
| 18 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.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" |
25 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 27 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
26 #include "chrome/browser/search_engines/template_url_service.h" | 28 #include "chrome/browser/search_engines/template_url_service.h" |
(...skipping 879 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 BitmapFetcherService* image_service = |
| 950 BitmapFetcherServiceFactory::GetForBrowserContext(profile_); |
| 951 DCHECK(image_service); |
| 952 |
| 953 for (size_t line = 0; line < lines->GetSize(); ++line) { |
| 954 const base::DictionaryValue* imageLine = NULL; |
| 955 lines->GetDictionary(line, &imageLine); |
| 956 if (!imageLine) |
| 957 continue; |
| 958 const base::DictionaryValue* imageData = NULL; |
| 959 imageLine->GetDictionary("i", &imageData); |
| 960 if (!imageData) |
| 961 continue; |
| 962 std::string imageUrl; |
| 963 imageData->GetString("d", &imageUrl); |
| 964 image_service->Prefetch(GURL(imageUrl)); |
| 965 } |
| 966 } |
| 967 |
938 void BaseSearchProvider::SortResults(bool is_keyword, | 968 void BaseSearchProvider::SortResults(bool is_keyword, |
939 const base::ListValue* relevances, | 969 const base::ListValue* relevances, |
940 Results* results) { | 970 Results* results) { |
941 } | 971 } |
942 | 972 |
943 bool BaseSearchProvider::StoreSuggestionResponse( | 973 bool BaseSearchProvider::StoreSuggestionResponse( |
944 const std::string& json_data, | 974 const std::string& json_data, |
945 const base::Value& parsed_data) { | 975 const base::Value& parsed_data) { |
946 return false; | 976 return false; |
947 } | 977 } |
(...skipping 18 matching lines...) Expand all Loading... |
966 } | 996 } |
967 | 997 |
968 void BaseSearchProvider::OnDeletionComplete( | 998 void BaseSearchProvider::OnDeletionComplete( |
969 bool success, SuggestionDeletionHandler* handler) { | 999 bool success, SuggestionDeletionHandler* handler) { |
970 RecordDeletionResult(success); | 1000 RecordDeletionResult(success); |
971 SuggestionDeletionHandlers::iterator it = std::find( | 1001 SuggestionDeletionHandlers::iterator it = std::find( |
972 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 1002 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
973 DCHECK(it != deletion_handlers_.end()); | 1003 DCHECK(it != deletion_handlers_.end()); |
974 deletion_handlers_.erase(it); | 1004 deletion_handlers_.erase(it); |
975 } | 1005 } |
OLD | NEW |