OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 #include "chrome/browser/search_engines/template_url_service_factory.h" | 38 #include "chrome/browser/search_engines/template_url_service_factory.h" |
39 #include "chrome/browser/sync/profile_sync_service.h" | 39 #include "chrome/browser/sync/profile_sync_service.h" |
40 #include "chrome/browser/sync/profile_sync_service_factory.h" | 40 #include "chrome/browser/sync/profile_sync_service_factory.h" |
41 #include "chrome/browser/ui/browser.h" | 41 #include "chrome/browser/ui/browser.h" |
42 #include "chrome/browser/ui/browser_finder.h" | 42 #include "chrome/browser/ui/browser_finder.h" |
43 #include "chrome/browser/ui/browser_instant_controller.h" | 43 #include "chrome/browser/ui/browser_instant_controller.h" |
44 #include "chrome/browser/ui/search/instant_controller.h" | 44 #include "chrome/browser/ui/search/instant_controller.h" |
45 #include "chrome/common/net/url_fixer_upper.h" | 45 #include "chrome/common/net/url_fixer_upper.h" |
46 #include "chrome/common/pref_names.h" | 46 #include "chrome/common/pref_names.h" |
47 #include "chrome/common/url_constants.h" | 47 #include "chrome/common/url_constants.h" |
48 #include "content/public/browser/user_metrics.h" | |
48 #include "grit/generated_resources.h" | 49 #include "grit/generated_resources.h" |
49 #include "net/base/escape.h" | 50 #include "net/base/escape.h" |
50 #include "net/base/load_flags.h" | 51 #include "net/base/load_flags.h" |
51 #include "net/base/net_util.h" | 52 #include "net/base/net_util.h" |
52 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 53 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
53 #include "net/http/http_request_headers.h" | 54 #include "net/http/http_request_headers.h" |
54 #include "net/http/http_response_headers.h" | 55 #include "net/http/http_response_headers.h" |
55 #include "net/url_request/url_fetcher.h" | 56 #include "net/url_request/url_fetcher.h" |
56 #include "net/url_request/url_request_status.h" | 57 #include "net/url_request/url_request_status.h" |
57 #include "ui/base/l10n/l10n_util.h" | 58 #include "ui/base/l10n/l10n_util.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 } | 169 } |
169 | 170 |
170 if (!annotation.empty() && (annotation_start >= match_contents_start)) | 171 if (!annotation.empty() && (annotation_start >= match_contents_start)) |
171 match->contents_class.push_back(ACMatchClassification( | 172 match->contents_class.push_back(ACMatchClassification( |
172 match_contents_end, ACMatchClassification::DIM)); | 173 match_contents_end, ACMatchClassification::DIM)); |
173 } | 174 } |
174 | 175 |
175 } // namespace | 176 } // namespace |
176 | 177 |
177 | 178 |
179 // SuggestionDeletionHandler ------------------------------------------------- | |
180 | |
181 // This class handles making requests to the server in order to delete | |
182 // personalized suggestions. | |
183 class SuggestionDeletionHandler : public net::URLFetcherDelegate { | |
184 public: | |
185 SuggestionDeletionHandler(); | |
186 virtual ~SuggestionDeletionHandler(); | |
187 | |
188 typedef base::Callback<void(bool, SuggestionDeletionHandler*)> | |
189 DeletionCompletedCallback; | |
190 | |
191 // Kicks off the deletion request to the server. | |
192 void StartRequest( | |
193 const std::string& deletion_url, | |
194 Profile* profile, | |
195 const DeletionCompletedCallback& callback); | |
196 | |
197 private: | |
198 // net::URLFetcherDelegate: | |
199 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
200 | |
201 scoped_ptr<net::URLFetcher> deletion_fetcher_; | |
202 DeletionCompletedCallback callback_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(SuggestionDeletionHandler); | |
205 }; | |
206 | |
207 | |
208 SuggestionDeletionHandler::SuggestionDeletionHandler() { | |
209 }; | |
210 | |
211 SuggestionDeletionHandler::~SuggestionDeletionHandler() { | |
212 }; | |
213 | |
214 void SuggestionDeletionHandler::StartRequest( | |
215 const std::string& deletion_url, | |
216 Profile* profile, | |
217 const DeletionCompletedCallback& callback) { | |
218 callback_ = callback; | |
219 GURL url(deletion_url); | |
220 DCHECK(url.is_valid()); | |
221 | |
222 deletion_fetcher_.reset(net::URLFetcher::Create( | |
223 SearchProvider::kDeletionURLFetcherID, | |
224 url, | |
225 net::URLFetcher::GET, | |
226 this)); | |
227 deletion_fetcher_->SetRequestContext(profile->GetRequestContext()); | |
228 deletion_fetcher_->Start(); | |
229 }; | |
230 | |
231 void SuggestionDeletionHandler::OnURLFetchComplete( | |
232 const net::URLFetcher* source) { | |
233 DCHECK(source == deletion_fetcher_.get()); | |
234 callback_.Run( | |
235 source->GetStatus().is_success() && (source->GetResponseCode() == 200), | |
236 this); | |
237 }; | |
238 | |
239 | |
178 // SearchProvider::Providers -------------------------------------------------- | 240 // SearchProvider::Providers -------------------------------------------------- |
179 | 241 |
180 SearchProvider::Providers::Providers(TemplateURLService* template_url_service) | 242 SearchProvider::Providers::Providers(TemplateURLService* template_url_service) |
181 : template_url_service_(template_url_service) { | 243 : template_url_service_(template_url_service) { |
182 } | 244 } |
183 | 245 |
184 const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { | 246 const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { |
185 return default_provider_.empty() ? NULL : | 247 return default_provider_.empty() ? NULL : |
186 template_url_service_->GetTemplateURLForKeyword(default_provider_); | 248 template_url_service_->GetTemplateURLForKeyword(default_provider_); |
187 } | 249 } |
(...skipping 18 matching lines...) Expand all Loading... | |
206 } | 268 } |
207 | 269 |
208 | 270 |
209 // SearchProvider::SuggestResult ---------------------------------------------- | 271 // SearchProvider::SuggestResult ---------------------------------------------- |
210 | 272 |
211 SearchProvider::SuggestResult::SuggestResult( | 273 SearchProvider::SuggestResult::SuggestResult( |
212 const string16& suggestion, | 274 const string16& suggestion, |
213 const string16& match_contents, | 275 const string16& match_contents, |
214 const string16& annotation, | 276 const string16& annotation, |
215 const std::string& suggest_query_params, | 277 const std::string& suggest_query_params, |
278 const std::string& deletion_url, | |
216 bool from_keyword_provider, | 279 bool from_keyword_provider, |
217 int relevance, | 280 int relevance, |
218 bool relevance_from_server, | 281 bool relevance_from_server, |
219 bool should_prefetch) | 282 bool should_prefetch) |
220 : Result(from_keyword_provider, relevance, relevance_from_server), | 283 : Result(from_keyword_provider, relevance, relevance_from_server), |
221 suggestion_(suggestion), | 284 suggestion_(suggestion), |
222 match_contents_(match_contents), | 285 match_contents_(match_contents), |
223 annotation_(annotation), | 286 annotation_(annotation), |
224 suggest_query_params_(suggest_query_params), | 287 suggest_query_params_(suggest_query_params), |
288 deletion_url_(deletion_url), | |
225 should_prefetch_(should_prefetch) { | 289 should_prefetch_(should_prefetch) { |
226 } | 290 } |
227 | 291 |
228 SearchProvider::SuggestResult::~SuggestResult() { | 292 SearchProvider::SuggestResult::~SuggestResult() { |
229 } | 293 } |
230 | 294 |
231 bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const { | 295 bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const { |
232 return StartsWith(suggestion_, input, false); | 296 return StartsWith(suggestion_, input, false); |
233 } | 297 } |
234 | 298 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 | 383 |
320 return false; | 384 return false; |
321 } | 385 } |
322 | 386 |
323 | 387 |
324 // SearchProvider ------------------------------------------------------------- | 388 // SearchProvider ------------------------------------------------------------- |
325 | 389 |
326 // static | 390 // static |
327 const int SearchProvider::kDefaultProviderURLFetcherID = 1; | 391 const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
328 const int SearchProvider::kKeywordProviderURLFetcherID = 2; | 392 const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
393 const int SearchProvider::kDeletionURLFetcherID = 3; | |
329 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; | 394 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
330 const char SearchProvider::kRelevanceFromServerKey[] = "relevance_from_server"; | 395 const char SearchProvider::kRelevanceFromServerKey[] = "relevance_from_server"; |
331 const char SearchProvider::kShouldPrefetchKey[] = "should_prefetch"; | 396 const char SearchProvider::kShouldPrefetchKey[] = "should_prefetch"; |
332 const char SearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; | 397 const char SearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; |
398 const char SearchProvider::kDeletionUrlKey[] = "deletion_url"; | |
333 const char SearchProvider::kTrue[] = "true"; | 399 const char SearchProvider::kTrue[] = "true"; |
334 const char SearchProvider::kFalse[] = "false"; | 400 const char SearchProvider::kFalse[] = "false"; |
335 | 401 |
336 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, | 402 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
337 Profile* profile) | 403 Profile* profile) |
338 : AutocompleteProvider(listener, profile, | 404 : AutocompleteProvider(listener, profile, |
339 AutocompleteProvider::TYPE_SEARCH), | 405 AutocompleteProvider::TYPE_SEARCH), |
340 providers_(TemplateURLServiceFactory::GetForProfile(profile)), | 406 providers_(TemplateURLServiceFactory::GetForProfile(profile)), |
341 suggest_results_pending_(0), | 407 suggest_results_pending_(0), |
342 field_trial_triggered_(false), | 408 field_trial_triggered_(false), |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
427 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { | 493 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { |
428 if (field_trial_triggered_) | 494 if (field_trial_triggered_) |
429 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); | 495 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
430 if (field_trial_triggered_in_session_) { | 496 if (field_trial_triggered_in_session_) { |
431 new_entry.mutable_field_trial_triggered_in_session()->Add( | 497 new_entry.mutable_field_trial_triggered_in_session()->Add( |
432 field_trial_hashes[i]); | 498 field_trial_hashes[i]); |
433 } | 499 } |
434 } | 500 } |
435 } | 501 } |
436 | 502 |
503 void SearchProvider::DeleteMatch(const AutocompleteMatch& match) { | |
504 // TODO(mariakhomenko): Add support for deleting search history suggestions. | |
505 DCHECK(match.deletable); | |
506 | |
507 deletion_handlers_.push_back(new SuggestionDeletionHandler()); | |
508 deletion_handlers_.back()->StartRequest( | |
509 match.GetAdditionalInfo(SearchProvider::kDeletionUrlKey), | |
510 profile_, | |
511 base::Bind(&SearchProvider::OnDeletionComplete, base::Unretained(this))); | |
512 | |
513 // Immediately update the list of matches to show the match was deleted, | |
514 // regardless of whether the server request actually succeeds. | |
515 DeleteMatchFromMatches(match); | |
516 } | |
517 | |
437 void SearchProvider::ResetSession() { | 518 void SearchProvider::ResetSession() { |
438 field_trial_triggered_in_session_ = false; | 519 field_trial_triggered_in_session_ = false; |
439 } | 520 } |
440 | 521 |
441 SearchProvider::~SearchProvider() { | 522 SearchProvider::~SearchProvider() { |
442 } | 523 } |
443 | 524 |
444 // static | 525 // static |
445 void SearchProvider::RemoveStaleResults(const string16& input, | 526 void SearchProvider::RemoveStaleResults(const string16& input, |
446 int verbatim_relevance, | 527 int verbatim_relevance, |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
631 elapsed_time); | 712 elapsed_time); |
632 } | 713 } |
633 } | 714 } |
634 | 715 |
635 bool results_updated = false; | 716 bool results_updated = false; |
636 if (request_succeeded) { | 717 if (request_succeeded) { |
637 const net::HttpResponseHeaders* const response_headers = | 718 const net::HttpResponseHeaders* const response_headers = |
638 source->GetResponseHeaders(); | 719 source->GetResponseHeaders(); |
639 std::string json_data; | 720 std::string json_data; |
640 source->GetResponseAsString(&json_data); | 721 source->GetResponseAsString(&json_data); |
722 | |
641 // JSON is supposed to be UTF-8, but some suggest service providers send | 723 // JSON is supposed to be UTF-8, but some suggest service providers send |
642 // JSON files in non-UTF-8 encodings. The actual encoding is usually | 724 // JSON files in non-UTF-8 encodings. The actual encoding is usually |
643 // specified in the Content-Type header field. | 725 // specified in the Content-Type header field. |
644 if (response_headers) { | 726 if (response_headers) { |
645 std::string charset; | 727 std::string charset; |
646 if (response_headers->GetCharset(&charset)) { | 728 if (response_headers->GetCharset(&charset)) { |
647 string16 data_16; | 729 string16 data_16; |
648 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. | 730 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
649 if (base::CodepageToUTF16(json_data, charset.c_str(), | 731 if (base::CodepageToUTF16(json_data, charset.c_str(), |
650 base::OnStringConversionError::FAIL, | 732 base::OnStringConversionError::FAIL, |
(...skipping 20 matching lines...) Expand all Loading... | |
671 break; | 753 break; |
672 } | 754 } |
673 } | 755 } |
674 } | 756 } |
675 | 757 |
676 UpdateMatches(); | 758 UpdateMatches(); |
677 if (done_ || results_updated) | 759 if (done_ || results_updated) |
678 listener_->OnProviderUpdate(results_updated); | 760 listener_->OnProviderUpdate(results_updated); |
679 } | 761 } |
680 | 762 |
763 void SearchProvider::OnDeletionComplete(bool success, | |
764 SuggestionDeletionHandler* handler) { | |
765 if (success) { | |
766 content::RecordAction( | |
767 content::UserMetricsAction("Omnibox.ServerSuggestDelete.Success")); | |
768 } else { | |
769 content::RecordAction( | |
770 content::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure")); | |
771 } | |
772 | |
773 ScopedVector<SuggestionDeletionHandler>::iterator it = std::find( | |
774 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | |
775 DCHECK(it != deletion_handlers_.end()); | |
776 deletion_handlers_.erase(it); | |
777 } | |
778 | |
779 void SearchProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) { | |
780 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { | |
781 // Find the desired match to delete by checking the type and contents. | |
782 // We can't check the destination URL, because the autocomplete controller | |
783 // may have reformulated that. Not that while checking for matching | |
784 // contents works for personalized suggestions, if more match types gain | |
785 // deletion support, this algorithm may need to be re-examined. | |
786 if (i->contents == match.contents && i->type == match.type) { | |
787 matches_.erase(i); | |
788 break; | |
789 } | |
790 } | |
791 listener_->OnProviderUpdate(true); | |
792 } | |
793 | |
681 void SearchProvider::Run() { | 794 void SearchProvider::Run() { |
682 // Start a new request with the current input. | 795 // Start a new request with the current input. |
683 suggest_results_pending_ = 0; | 796 suggest_results_pending_ = 0; |
684 time_suggest_request_sent_ = base::TimeTicks::Now(); | 797 time_suggest_request_sent_ = base::TimeTicks::Now(); |
685 | 798 |
686 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, | 799 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
687 providers_.GetDefaultProviderURL(), input_)); | 800 providers_.GetDefaultProviderURL(), input_)); |
688 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, | 801 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
689 providers_.GetKeywordProviderURL(), keyword_input_)); | 802 providers_.GetKeywordProviderURL(), keyword_input_)); |
690 | 803 |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
928 NavigationResults* list) { | 1041 NavigationResults* list) { |
929 for (size_t i = 0; i < list->size(); ++i) { | 1042 for (size_t i = 0; i < list->size(); ++i) { |
930 NavigationResult& result = (*list)[i]; | 1043 NavigationResult& result = (*list)[i]; |
931 result.set_relevance( | 1044 result.set_relevance( |
932 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + | 1045 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
933 (list->size() - i - 1)); | 1046 (list->size() - i - 1)); |
934 result.set_relevance_from_server(false); | 1047 result.set_relevance_from_server(false); |
935 } | 1048 } |
936 } | 1049 } |
937 | 1050 |
938 bool SearchProvider::CanSendURL( | |
939 const GURL& current_page_url, | |
940 const GURL& suggest_url, | |
941 const TemplateURL* template_url, | |
942 AutocompleteInput::PageClassification page_classification, | |
943 Profile* profile) { | |
944 if (!current_page_url.is_valid()) | |
945 return false; | |
946 | |
947 // TODO(hfung): Show Most Visited on NTP with appropriate verbatim | |
948 // description when the user actively focuses on the omnibox as discussed in | |
949 // crbug/305366 if Most Visited (or something similar) will launch. | |
950 if ((page_classification == | |
951 AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) || | |
952 (page_classification == | |
953 AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS)) | |
954 return false; | |
955 | |
956 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search | |
957 // provider. | |
958 if ((current_page_url.scheme() != content::kHttpScheme) && | |
959 ((current_page_url.scheme() != content::kHttpsScheme) || | |
960 !net::registry_controlled_domains::SameDomainOrHost( | |
961 current_page_url, suggest_url, | |
962 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) | |
963 return false; | |
964 | |
965 // Make sure we are sending the suggest request through HTTPS to prevent | |
966 // exposing the current page URL to networks before the search provider. | |
967 if (!suggest_url.SchemeIs(content::kHttpsScheme)) | |
968 return false; | |
969 | |
970 // Don't run if there's no profile or in incognito mode. | |
971 if (profile == NULL || profile->IsOffTheRecord()) | |
972 return false; | |
973 | |
974 // Don't run if we can't get preferences or search suggest is not enabled. | |
975 PrefService* prefs = profile->GetPrefs(); | |
976 if (!prefs->GetBoolean(prefs::kSearchSuggestEnabled)) | |
977 return false; | |
978 | |
979 // Only make the request if we know that the provider supports zero suggest | |
980 // (currently only the prepopulated Google provider). | |
981 if (template_url == NULL || !template_url->SupportsReplacement() || | |
982 TemplateURLPrepopulateData::GetEngineType(*template_url) != | |
983 SEARCH_ENGINE_GOOGLE) | |
984 return false; | |
985 | |
986 // Check field trials and settings allow sending the URL on suggest requests. | |
987 ProfileSyncService* service = | |
988 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | |
989 browser_sync::SyncPrefs sync_prefs(prefs); | |
990 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || | |
991 service == NULL || | |
992 !service->IsSyncEnabledAndLoggedIn() || | |
993 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | |
994 syncer::PROXY_TABS) || | |
995 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | |
996 return false; | |
997 | |
998 return true; | |
999 } | |
1000 | |
1001 net::URLFetcher* SearchProvider::CreateSuggestFetcher( | 1051 net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
1002 int id, | 1052 int id, |
1003 const TemplateURL* template_url, | 1053 const TemplateURL* template_url, |
1004 const AutocompleteInput& input) { | 1054 const AutocompleteInput& input) { |
1005 if (!template_url || template_url->suggestions_url().empty()) | 1055 if (!template_url || template_url->suggestions_url().empty()) |
1006 return NULL; | 1056 return NULL; |
1007 | 1057 |
1008 // Bail if the suggestion URL is invalid with the given replacements. | 1058 // Bail if the suggestion URL is invalid with the given replacements. |
1009 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); | 1059 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
1010 search_term_args.cursor_position = input.cursor_position(); | 1060 search_term_args.cursor_position = input.cursor_position(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1127 results->navigation_results.push_back(NavigationResult( | 1177 results->navigation_results.push_back(NavigationResult( |
1128 *this, url, title, is_keyword, relevance, true)); | 1178 *this, url, title, is_keyword, relevance, true)); |
1129 } | 1179 } |
1130 } else { | 1180 } else { |
1131 bool should_prefetch = static_cast<int>(index) == prefetch_index; | 1181 bool should_prefetch = static_cast<int>(index) == prefetch_index; |
1132 DictionaryValue* suggestion_detail = NULL; | 1182 DictionaryValue* suggestion_detail = NULL; |
1133 string16 match_contents = suggestion; | 1183 string16 match_contents = suggestion; |
1134 string16 disambiguating_query; | 1184 string16 disambiguating_query; |
1135 string16 annotation; | 1185 string16 annotation; |
1136 std::string suggest_query_params; | 1186 std::string suggest_query_params; |
1187 std::string deletion_url; | |
1137 if (suggestion_details && (type == "ENTITY") && | 1188 if (suggestion_details && (type == "ENTITY") && |
1138 suggestion_details->GetDictionary(index, &suggestion_detail) && | 1189 suggestion_details->GetDictionary(index, &suggestion_detail) && |
1139 suggestion_detail) { | 1190 suggestion_detail) { |
1140 suggestion_detail->GetString("a", &annotation); | 1191 suggestion_detail->GetString("a", &annotation); |
1141 if (suggestion_detail->GetString("dq", &disambiguating_query) && | 1192 if (suggestion_detail->GetString("dq", &disambiguating_query) && |
1142 !disambiguating_query.empty()) | 1193 !disambiguating_query.empty()) |
1143 suggestion = disambiguating_query; | 1194 suggestion = disambiguating_query; |
1144 suggestion_detail->GetString("q", &suggest_query_params); | 1195 suggestion_detail->GetString("q", &suggest_query_params); |
1196 } else if (suggestion_details && (type == "PERSONALIZED_QUERY") && | |
1197 suggestion_details->GetDictionary(index, &suggestion_detail) && | |
Peter Kasting
2013/11/28 00:44:17
Nit: Instead of duplicating this GetDictionary() c
| |
1198 suggestion_detail) { | |
1199 suggestion_detail->GetString("du", &deletion_url); | |
1145 } | 1200 } |
1146 // TODO(kochi): Improve calculator suggestion presentation. | 1201 // TODO(kochi): Improve calculator suggestion presentation. |
1147 results->suggest_results.push_back(SuggestResult( | 1202 results->suggest_results.push_back(SuggestResult( |
1148 suggestion, match_contents, annotation, suggest_query_params, | 1203 suggestion, match_contents, annotation, suggest_query_params, |
1149 is_keyword, relevance, true, should_prefetch)); | 1204 deletion_url, is_keyword, relevance, true, should_prefetch)); |
1150 } | 1205 } |
1151 } | 1206 } |
1152 | 1207 |
1153 // Ignore suggested scores for non-keyword matches in keyword mode; if the | 1208 // Ignore suggested scores for non-keyword matches in keyword mode; if the |
1154 // server is allowed to score these, it could interfere with the user's | 1209 // server is allowed to score these, it could interfere with the user's |
1155 // ability to get good keyword results. | 1210 // ability to get good keyword results. |
1156 const bool abandon_suggested_scores = | 1211 const bool abandon_suggested_scores = |
1157 !is_keyword && !providers_.keyword_provider().empty(); | 1212 !is_keyword && !providers_.keyword_provider().empty(); |
1158 // Apply calculated relevance scores to suggestions if a valid list was | 1213 // Apply calculated relevance scores to suggestions if a valid list was |
1159 // not provided or we're abandoning suggested scores entirely. | 1214 // not provided or we're abandoning suggested scores entirely. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1199 relevance_from_server, | 1254 relevance_from_server, |
1200 false, | 1255 false, |
1201 std::string(), | 1256 std::string(), |
1202 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 1257 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
1203 false, | 1258 false, |
1204 input_.text(), | 1259 input_.text(), |
1205 string16(), | 1260 string16(), |
1206 input_.text(), | 1261 input_.text(), |
1207 did_not_accept_default_suggestion, | 1262 did_not_accept_default_suggestion, |
1208 std::string(), | 1263 std::string(), |
1264 std::string(), | |
1209 &map); | 1265 &map); |
1210 } | 1266 } |
1211 if (!keyword_input_.text().empty()) { | 1267 if (!keyword_input_.text().empty()) { |
1212 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 1268 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
1213 // We only create the verbatim search query match for a keyword | 1269 // We only create the verbatim search query match for a keyword |
1214 // if it's not an extension keyword. Extension keywords are handled | 1270 // if it's not an extension keyword. Extension keywords are handled |
1215 // in KeywordProvider::Start(). (Extensions are complicated...) | 1271 // in KeywordProvider::Start(). (Extensions are complicated...) |
1216 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond | 1272 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
1217 // to the keyword verbatim search query. Do not create other matches | 1273 // to the keyword verbatim search query. Do not create other matches |
1218 // of type SEARCH_OTHER_ENGINE. | 1274 // of type SEARCH_OTHER_ENGINE. |
1219 if (keyword_url && | 1275 if (keyword_url && |
1220 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 1276 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
1221 bool keyword_relevance_from_server; | 1277 bool keyword_relevance_from_server; |
1222 const int keyword_verbatim_relevance = | 1278 const int keyword_verbatim_relevance = |
1223 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); | 1279 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); |
1224 if (keyword_verbatim_relevance > 0) { | 1280 if (keyword_verbatim_relevance > 0) { |
1225 AddMatchToMap(keyword_input_.text(), | 1281 AddMatchToMap(keyword_input_.text(), |
1226 keyword_verbatim_relevance, | 1282 keyword_verbatim_relevance, |
1227 keyword_relevance_from_server, | 1283 keyword_relevance_from_server, |
1228 false, | 1284 false, |
1229 std::string(), | 1285 std::string(), |
1230 AutocompleteMatchType::SEARCH_OTHER_ENGINE, | 1286 AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
1231 true, | 1287 true, |
1232 keyword_input_.text(), | 1288 keyword_input_.text(), |
1233 string16(), | 1289 string16(), |
1234 keyword_input_.text(), | 1290 keyword_input_.text(), |
1235 did_not_accept_keyword_suggestion, | 1291 did_not_accept_keyword_suggestion, |
1236 std::string(), | 1292 std::string(), |
1293 std::string(), | |
1237 &map); | 1294 &map); |
1238 } | 1295 } |
1239 } | 1296 } |
1240 } | 1297 } |
1241 AddHistoryResultsToMap(keyword_history_results_, true, | 1298 AddHistoryResultsToMap(keyword_history_results_, true, |
1242 did_not_accept_keyword_suggestion, &map); | 1299 did_not_accept_keyword_suggestion, &map); |
1243 AddHistoryResultsToMap(default_history_results_, false, | 1300 AddHistoryResultsToMap(default_history_results_, false, |
1244 did_not_accept_default_suggestion, &map); | 1301 did_not_accept_default_suggestion, &map); |
1245 | 1302 |
1246 AddSuggestResultsToMap(keyword_results_.suggest_results, | 1303 AddSuggestResultsToMap(keyword_results_.suggest_results, |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1535 false, | 1592 false, |
1536 false, | 1593 false, |
1537 std::string(), | 1594 std::string(), |
1538 AutocompleteMatchType::SEARCH_HISTORY, | 1595 AutocompleteMatchType::SEARCH_HISTORY, |
1539 is_keyword, | 1596 is_keyword, |
1540 i->suggestion(), | 1597 i->suggestion(), |
1541 string16(), | 1598 string16(), |
1542 i->suggestion(), | 1599 i->suggestion(), |
1543 did_not_accept_suggestion, | 1600 did_not_accept_suggestion, |
1544 std::string(), | 1601 std::string(), |
1602 std::string(), | |
1545 map); | 1603 map); |
1546 } | 1604 } |
1547 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", | 1605 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", |
1548 base::TimeTicks::Now() - start_time); | 1606 base::TimeTicks::Now() - start_time); |
1549 } | 1607 } |
1550 | 1608 |
1551 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( | 1609 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
1552 const HistoryResults& results, | 1610 const HistoryResults& results, |
1553 bool base_prevent_inline_autocomplete, | 1611 bool base_prevent_inline_autocomplete, |
1554 bool input_multiple_words, | 1612 bool input_multiple_words, |
(...skipping 30 matching lines...) Expand all Loading... | |
1585 classifier->Classify(i->term, false, false, &match, NULL); | 1643 classifier->Classify(i->term, false, false, &match, NULL); |
1586 prevent_inline_autocomplete = | 1644 prevent_inline_autocomplete = |
1587 !AutocompleteMatch::IsSearchType(match.type); | 1645 !AutocompleteMatch::IsSearchType(match.type); |
1588 } | 1646 } |
1589 | 1647 |
1590 int relevance = CalculateRelevanceForHistory( | 1648 int relevance = CalculateRelevanceForHistory( |
1591 i->time, is_keyword, !prevent_inline_autocomplete, | 1649 i->time, is_keyword, !prevent_inline_autocomplete, |
1592 prevent_search_history_inlining); | 1650 prevent_search_history_inlining); |
1593 scored_results.push_back( | 1651 scored_results.push_back( |
1594 SuggestResult(i->term, string16(), string16(), std::string(), | 1652 SuggestResult(i->term, string16(), string16(), std::string(), |
1595 is_keyword, relevance, false, false)); | 1653 std::string(), is_keyword, relevance, false, false)); |
1596 } | 1654 } |
1597 | 1655 |
1598 // History returns results sorted for us. However, we may have docked some | 1656 // History returns results sorted for us. However, we may have docked some |
1599 // results' scores, so things are no longer in order. Do a stable sort to get | 1657 // results' scores, so things are no longer in order. Do a stable sort to get |
1600 // things back in order without otherwise disturbing results with equal | 1658 // things back in order without otherwise disturbing results with equal |
1601 // scores, then force the scores to be unique, so that the order in which | 1659 // scores, then force the scores to be unique, so that the order in which |
1602 // they're shown is deterministic. | 1660 // they're shown is deterministic. |
1603 std::stable_sort(scored_results.begin(), scored_results.end(), | 1661 std::stable_sort(scored_results.begin(), scored_results.end(), |
1604 CompareScoredResults()); | 1662 CompareScoredResults()); |
1605 int last_relevance = 0; | 1663 int last_relevance = 0; |
(...skipping 18 matching lines...) Expand all Loading... | |
1624 results[i].relevance_from_server(), | 1682 results[i].relevance_from_server(), |
1625 results[i].should_prefetch(), | 1683 results[i].should_prefetch(), |
1626 metadata, | 1684 metadata, |
1627 AutocompleteMatchType::SEARCH_SUGGEST, | 1685 AutocompleteMatchType::SEARCH_SUGGEST, |
1628 is_keyword, | 1686 is_keyword, |
1629 results[i].match_contents(), | 1687 results[i].match_contents(), |
1630 results[i].annotation(), | 1688 results[i].annotation(), |
1631 results[i].suggestion(), | 1689 results[i].suggestion(), |
1632 i, | 1690 i, |
1633 results[i].suggest_query_params(), | 1691 results[i].suggest_query_params(), |
1692 results[i].deletion_url(), | |
1634 map); | 1693 map); |
1635 } | 1694 } |
1636 } | 1695 } |
1637 | 1696 |
1638 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { | 1697 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { |
1639 // Use the suggested verbatim relevance score if it is non-negative (valid), | 1698 // Use the suggested verbatim relevance score if it is non-negative (valid), |
1640 // if inline autocomplete isn't prevented (always show verbatim on backspace), | 1699 // if inline autocomplete isn't prevented (always show verbatim on backspace), |
1641 // and if it won't suppress verbatim, leaving no default provider matches. | 1700 // and if it won't suppress verbatim, leaving no default provider matches. |
1642 // Otherwise, if the default provider returned no matches and was still able | 1701 // Otherwise, if the default provider returned no matches and was still able |
1643 // to suppress verbatim, the user would have no search/nav matches and may be | 1702 // to suppress verbatim, the user would have no search/nav matches and may be |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1748 bool relevance_from_server, | 1807 bool relevance_from_server, |
1749 bool should_prefetch, | 1808 bool should_prefetch, |
1750 const std::string& metadata, | 1809 const std::string& metadata, |
1751 AutocompleteMatch::Type type, | 1810 AutocompleteMatch::Type type, |
1752 bool is_keyword, | 1811 bool is_keyword, |
1753 const string16& match_contents, | 1812 const string16& match_contents, |
1754 const string16& annotation, | 1813 const string16& annotation, |
1755 const string16& query_string, | 1814 const string16& query_string, |
1756 int accepted_suggestion, | 1815 int accepted_suggestion, |
1757 const std::string& suggest_query_params, | 1816 const std::string& suggest_query_params, |
1817 const std::string& deletion_url, | |
1758 MatchMap* map) { | 1818 MatchMap* map) { |
1759 // On non-mobile, ask the instant controller for the appropriate start margin. | 1819 // On non-mobile, ask the instant controller for the appropriate start margin. |
1760 // On mobile the start margin is unused, so leave the value as default there. | 1820 // On mobile the start margin is unused, so leave the value as default there. |
1761 int omnibox_start_margin = chrome::kDisableStartMargin; | 1821 int omnibox_start_margin = chrome::kDisableStartMargin; |
1762 #if !defined(OS_ANDROID) && !defined(IOS) | 1822 #if !defined(OS_ANDROID) && !defined(IOS) |
1763 if (chrome::IsInstantExtendedAPIEnabled()) { | 1823 if (chrome::IsInstantExtendedAPIEnabled()) { |
1764 Browser* browser = | 1824 Browser* browser = |
1765 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop()); | 1825 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop()); |
1766 if (browser && browser->instant_controller() && | 1826 if (browser && browser->instant_controller() && |
1767 browser->instant_controller()->instant()) { | 1827 browser->instant_controller()->instant()) { |
(...skipping 12 matching lines...) Expand all Loading... | |
1780 !is_keyword || providers_.default_provider().empty()); | 1840 !is_keyword || providers_.default_provider().empty()); |
1781 if (!match.destination_url.is_valid()) | 1841 if (!match.destination_url.is_valid()) |
1782 return; | 1842 return; |
1783 match.search_terms_args->bookmark_bar_pinned = | 1843 match.search_terms_args->bookmark_bar_pinned = |
1784 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | 1844 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
1785 match.RecordAdditionalInfo(kRelevanceFromServerKey, | 1845 match.RecordAdditionalInfo(kRelevanceFromServerKey, |
1786 relevance_from_server ? kTrue : kFalse); | 1846 relevance_from_server ? kTrue : kFalse); |
1787 match.RecordAdditionalInfo(kShouldPrefetchKey, | 1847 match.RecordAdditionalInfo(kShouldPrefetchKey, |
1788 should_prefetch ? kTrue : kFalse); | 1848 should_prefetch ? kTrue : kFalse); |
1789 | 1849 |
1850 if (!deletion_url.empty() && GURL(deletion_url).is_valid()) { | |
1851 match.RecordAdditionalInfo(kDeletionUrlKey, deletion_url); | |
1852 match.deletable = true; | |
1853 } | |
1854 | |
1790 // Metadata is needed only for prefetching queries. | 1855 // Metadata is needed only for prefetching queries. |
1791 if (should_prefetch) | 1856 if (should_prefetch) |
1792 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 1857 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
1793 | 1858 |
1794 // Try to add |match| to |map|. If a match for |query_string| is already in | 1859 // Try to add |match| to |map|. If a match for |query_string| is already in |
1795 // |map|, replace it if |match| is more relevant. | 1860 // |map|, replace it if |match| is more relevant. |
1796 // NOTE: Keep this ToLower() call in sync with url_database.cc. | 1861 // NOTE: Keep this ToLower() call in sync with url_database.cc. |
1797 MatchKey match_key( | 1862 MatchKey match_key( |
1798 std::make_pair(base::i18n::ToLower(query_string), | 1863 std::make_pair(base::i18n::ToLower(query_string), |
1799 match.search_terms_args->suggest_query_params)); | 1864 match.search_terms_args->suggest_query_params)); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1945 it->set_relevance(max_query_relevance); | 2010 it->set_relevance(max_query_relevance); |
1946 it->set_relevance_from_server(relevance_from_server); | 2011 it->set_relevance_from_server(relevance_from_server); |
1947 } | 2012 } |
1948 } | 2013 } |
1949 | 2014 |
1950 void SearchProvider::UpdateDone() { | 2015 void SearchProvider::UpdateDone() { |
1951 // We're done when the timer isn't running, there are no suggest queries | 2016 // We're done when the timer isn't running, there are no suggest queries |
1952 // pending, and we're not waiting on Instant. | 2017 // pending, and we're not waiting on Instant. |
1953 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); | 2018 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
1954 } | 2019 } |
2020 | |
2021 bool SearchProvider::CanSendURL( | |
2022 const GURL& current_page_url, | |
2023 const GURL& suggest_url, | |
2024 const TemplateURL* template_url, | |
2025 AutocompleteInput::PageClassification page_classification, | |
2026 Profile* profile) { | |
2027 if (!current_page_url.is_valid()) | |
2028 return false; | |
2029 | |
2030 // TODO(hfung): Show Most Visited on NTP with appropriate verbatim | |
2031 // description when the user actively focuses on the omnibox as discussed in | |
2032 // crbug/305366 if Most Visited (or something similar) will launch. | |
2033 if ((page_classification == | |
2034 AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) || | |
2035 (page_classification == | |
2036 AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS)) | |
2037 return false; | |
2038 | |
2039 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search | |
2040 // provider. | |
2041 if ((current_page_url.scheme() != content::kHttpScheme) && | |
2042 ((current_page_url.scheme() != content::kHttpsScheme) || | |
2043 !net::registry_controlled_domains::SameDomainOrHost( | |
2044 current_page_url, suggest_url, | |
2045 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) | |
2046 return false; | |
2047 | |
2048 // Make sure we are sending the suggest request through HTTPS to prevent | |
2049 // exposing the current page URL to networks before the search provider. | |
2050 if (!suggest_url.SchemeIs(content::kHttpsScheme)) | |
2051 return false; | |
2052 | |
2053 // Don't run if there's no profile or in incognito mode. | |
2054 if (profile == NULL || profile->IsOffTheRecord()) | |
2055 return false; | |
2056 | |
2057 // Don't run if we can't get preferences or search suggest is not enabled. | |
2058 PrefService* prefs = profile->GetPrefs(); | |
2059 if (!prefs->GetBoolean(prefs::kSearchSuggestEnabled)) | |
2060 return false; | |
2061 | |
2062 // Only make the request if we know that the provider supports zero suggest | |
2063 // (currently only the prepopulated Google provider). | |
2064 if (template_url == NULL || !template_url->SupportsReplacement() || | |
2065 TemplateURLPrepopulateData::GetEngineType(*template_url) != | |
2066 SEARCH_ENGINE_GOOGLE) | |
2067 return false; | |
2068 | |
2069 // Check field trials and settings allow sending the URL on suggest requests. | |
2070 ProfileSyncService* service = | |
2071 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | |
2072 browser_sync::SyncPrefs sync_prefs(prefs); | |
2073 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || | |
2074 service == NULL || | |
2075 !service->IsSyncEnabledAndLoggedIn() || | |
2076 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | |
2077 syncer::PROXY_TABS) || | |
2078 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | |
2079 return false; | |
2080 | |
2081 return true; | |
2082 } | |
OLD | NEW |