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 "components/omnibox/base_search_provider.h" | 5 #include "components/omnibox/base_search_provider.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "components/metrics/proto/omnibox_event.pb.h" | 10 #include "components/metrics/proto/omnibox_event.pb.h" |
11 #include "components/metrics/proto/omnibox_input_type.pb.h" | 11 #include "components/metrics/proto/omnibox_input_type.pb.h" |
12 #include "components/omnibox/autocomplete_provider_delegate.h" | 12 #include "components/omnibox/autocomplete_provider_client.h" |
13 #include "components/omnibox/autocomplete_provider_listener.h" | 13 #include "components/omnibox/autocomplete_provider_listener.h" |
14 #include "components/omnibox/omnibox_field_trial.h" | 14 #include "components/omnibox/omnibox_field_trial.h" |
15 #include "components/search_engines/template_url.h" | 15 #include "components/search_engines/template_url.h" |
16 #include "components/search_engines/template_url_prepopulate_data.h" | 16 #include "components/search_engines/template_url_prepopulate_data.h" |
17 #include "components/search_engines/template_url_service.h" | 17 #include "components/search_engines/template_url_service.h" |
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
19 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 // BaseSearchProvider --------------------------------------------------------- | 78 // BaseSearchProvider --------------------------------------------------------- |
79 | 79 |
80 // static | 80 // static |
81 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; | 81 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; |
82 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; | 82 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; |
83 const int BaseSearchProvider::kDeletionURLFetcherID = 3; | 83 const int BaseSearchProvider::kDeletionURLFetcherID = 3; |
84 | 84 |
85 BaseSearchProvider::BaseSearchProvider( | 85 BaseSearchProvider::BaseSearchProvider( |
86 TemplateURLService* template_url_service, | 86 TemplateURLService* template_url_service, |
87 scoped_ptr<AutocompleteProviderDelegate> delegate, | 87 scoped_ptr<AutocompleteProviderClient> client, |
88 AutocompleteProvider::Type type) | 88 AutocompleteProvider::Type type) |
89 : AutocompleteProvider(type), | 89 : AutocompleteProvider(type), |
90 template_url_service_(template_url_service), | 90 template_url_service_(template_url_service), |
91 delegate_(delegate.Pass()), | 91 client_(client.Pass()), |
92 field_trial_triggered_(false), | 92 field_trial_triggered_(false), |
93 field_trial_triggered_in_session_(false) { | 93 field_trial_triggered_in_session_(false) { |
94 } | 94 } |
95 | 95 |
96 // static | 96 // static |
97 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { | 97 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
98 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; | 98 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
99 } | 99 } |
100 | 100 |
101 // static | 101 // static |
(...skipping 12 matching lines...) Expand all Loading... |
114 base::string16(), base::string16(), std::string(), std::string(), | 114 base::string16(), base::string16(), std::string(), std::string(), |
115 from_keyword_provider, 0, false, false, base::string16()), | 115 from_keyword_provider, 0, false, false, base::string16()), |
116 template_url, search_terms_data, 0, false); | 116 template_url, search_terms_data, 0, false); |
117 } | 117 } |
118 | 118 |
119 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { | 119 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { |
120 DCHECK(match.deletable); | 120 DCHECK(match.deletable); |
121 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { | 121 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { |
122 deletion_handlers_.push_back(new SuggestionDeletionHandler( | 122 deletion_handlers_.push_back(new SuggestionDeletionHandler( |
123 match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey), | 123 match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey), |
124 delegate_->RequestContext(), | 124 client_->RequestContext(), |
125 base::Bind(&BaseSearchProvider::OnDeletionComplete, | 125 base::Bind(&BaseSearchProvider::OnDeletionComplete, |
126 base::Unretained(this)))); | 126 base::Unretained(this)))); |
127 } | 127 } |
128 | 128 |
129 TemplateURL* template_url = | 129 TemplateURL* template_url = |
130 match.GetTemplateURL(template_url_service_, false); | 130 match.GetTemplateURL(template_url_service_, false); |
131 // This may be NULL if the template corresponding to the keyword has been | 131 // This may be NULL if the template corresponding to the keyword has been |
132 // deleted or there is no keyword set. | 132 // deleted or there is no keyword set. |
133 if (template_url != NULL) { | 133 if (template_url != NULL) { |
134 delegate_->DeleteMatchingURLsForKeywordFromHistory(template_url->id(), | 134 client_->DeleteMatchingURLsForKeywordFromHistory(template_url->id(), |
135 match.contents); | 135 match.contents); |
136 } | 136 } |
137 | 137 |
138 // Immediately update the list of matches to show the match was deleted, | 138 // Immediately update the list of matches to show the match was deleted, |
139 // regardless of whether the server request actually succeeds. | 139 // regardless of whether the server request actually succeeds. |
140 DeleteMatchFromMatches(match); | 140 DeleteMatchFromMatches(match); |
141 } | 141 } |
142 | 142 |
143 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { | 143 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
144 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); | 144 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
145 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); | 145 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 266 |
267 return match; | 267 return match; |
268 } | 268 } |
269 | 269 |
270 // static | 270 // static |
271 bool BaseSearchProvider::ZeroSuggestEnabled( | 271 bool BaseSearchProvider::ZeroSuggestEnabled( |
272 const GURL& suggest_url, | 272 const GURL& suggest_url, |
273 const TemplateURL* template_url, | 273 const TemplateURL* template_url, |
274 OmniboxEventProto::PageClassification page_classification, | 274 OmniboxEventProto::PageClassification page_classification, |
275 const SearchTermsData& search_terms_data, | 275 const SearchTermsData& search_terms_data, |
276 AutocompleteProviderDelegate* delegate) { | 276 AutocompleteProviderClient* client) { |
277 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial()) | 277 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial()) |
278 return false; | 278 return false; |
279 | 279 |
280 // Make sure we are sending the suggest request through HTTPS to prevent | 280 // Make sure we are sending the suggest request through HTTPS to prevent |
281 // exposing the current page URL or personalized results without encryption. | 281 // exposing the current page URL or personalized results without encryption. |
282 if (!suggest_url.SchemeIs(url::kHttpsScheme)) | 282 if (!suggest_url.SchemeIs(url::kHttpsScheme)) |
283 return false; | 283 return false; |
284 | 284 |
285 // Don't show zero suggest on the NTP. | 285 // Don't show zero suggest on the NTP. |
286 // TODO(hfung): Experiment with showing MostVisited zero suggest on NTP | 286 // TODO(hfung): Experiment with showing MostVisited zero suggest on NTP |
287 // under the conditions described in crbug.com/305366. | 287 // under the conditions described in crbug.com/305366. |
288 if ((page_classification == | 288 if ((page_classification == |
289 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) || | 289 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) || |
290 (page_classification == | 290 (page_classification == |
291 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS)) | 291 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS)) |
292 return false; | 292 return false; |
293 | 293 |
294 // Don't run if in incognito mode. | 294 // Don't run if in incognito mode. |
295 if (delegate->IsOffTheRecord()) | 295 if (client->IsOffTheRecord()) |
296 return false; | 296 return false; |
297 | 297 |
298 // Don't run if we can't get preferences or search suggest is not enabled. | 298 // Don't run if we can't get preferences or search suggest is not enabled. |
299 if (!delegate->SearchSuggestEnabled()) | 299 if (!client->SearchSuggestEnabled()) |
300 return false; | 300 return false; |
301 | 301 |
302 // Only make the request if we know that the provider supports zero suggest | 302 // Only make the request if we know that the provider supports zero suggest |
303 // (currently only the prepopulated Google provider). | 303 // (currently only the prepopulated Google provider). |
304 if (template_url == NULL || | 304 if (template_url == NULL || |
305 !template_url->SupportsReplacement(search_terms_data) || | 305 !template_url->SupportsReplacement(search_terms_data) || |
306 TemplateURLPrepopulateData::GetEngineType( | 306 TemplateURLPrepopulateData::GetEngineType( |
307 *template_url, search_terms_data) != SEARCH_ENGINE_GOOGLE) | 307 *template_url, search_terms_data) != SEARCH_ENGINE_GOOGLE) |
308 return false; | 308 return false; |
309 | 309 |
310 return true; | 310 return true; |
311 } | 311 } |
312 | 312 |
313 // static | 313 // static |
314 bool BaseSearchProvider::CanSendURL( | 314 bool BaseSearchProvider::CanSendURL( |
315 const GURL& current_page_url, | 315 const GURL& current_page_url, |
316 const GURL& suggest_url, | 316 const GURL& suggest_url, |
317 const TemplateURL* template_url, | 317 const TemplateURL* template_url, |
318 OmniboxEventProto::PageClassification page_classification, | 318 OmniboxEventProto::PageClassification page_classification, |
319 const SearchTermsData& search_terms_data, | 319 const SearchTermsData& search_terms_data, |
320 AutocompleteProviderDelegate* delegate) { | 320 AutocompleteProviderClient* client) { |
321 if (!ZeroSuggestEnabled(suggest_url, template_url, page_classification, | 321 if (!ZeroSuggestEnabled(suggest_url, template_url, page_classification, |
322 search_terms_data, delegate)) | 322 search_terms_data, client)) |
323 return false; | 323 return false; |
324 | 324 |
325 if (!current_page_url.is_valid()) | 325 if (!current_page_url.is_valid()) |
326 return false; | 326 return false; |
327 | 327 |
328 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search | 328 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search |
329 // provider. | 329 // provider. |
330 if ((current_page_url.scheme() != url::kHttpScheme) && | 330 if ((current_page_url.scheme() != url::kHttpScheme) && |
331 ((current_page_url.scheme() != url::kHttpsScheme) || | 331 ((current_page_url.scheme() != url::kHttpsScheme) || |
332 !net::registry_controlled_domains::SameDomainOrHost( | 332 !net::registry_controlled_domains::SameDomainOrHost( |
333 current_page_url, suggest_url, | 333 current_page_url, suggest_url, |
334 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) | 334 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) |
335 return false; | 335 return false; |
336 | 336 |
337 if (!delegate->TabSyncEnabledAndUnencrypted()) | 337 if (!client->TabSyncEnabledAndUnencrypted()) |
338 return false; | 338 return false; |
339 | 339 |
340 return true; | 340 return true; |
341 } | 341 } |
342 | 342 |
343 void BaseSearchProvider::AddMatchToMap( | 343 void BaseSearchProvider::AddMatchToMap( |
344 const SearchSuggestionParser::SuggestResult& result, | 344 const SearchSuggestionParser::SuggestResult& result, |
345 const std::string& metadata, | 345 const std::string& metadata, |
346 int accepted_suggestion, | 346 int accepted_suggestion, |
347 bool mark_as_deletable, | 347 bool mark_as_deletable, |
348 bool in_keyword_mode, | 348 bool in_keyword_mode, |
349 MatchMap* map) { | 349 MatchMap* map) { |
350 AutocompleteMatch match = CreateSearchSuggestion( | 350 AutocompleteMatch match = CreateSearchSuggestion( |
351 this, GetInput(result.from_keyword_provider()), in_keyword_mode, result, | 351 this, GetInput(result.from_keyword_provider()), in_keyword_mode, result, |
352 GetTemplateURL(result.from_keyword_provider()), | 352 GetTemplateURL(result.from_keyword_provider()), |
353 template_url_service_->search_terms_data(), accepted_suggestion, | 353 template_url_service_->search_terms_data(), accepted_suggestion, |
354 ShouldAppendExtraParams(result)); | 354 ShouldAppendExtraParams(result)); |
355 if (!match.destination_url.is_valid()) | 355 if (!match.destination_url.is_valid()) |
356 return; | 356 return; |
357 match.search_terms_args->bookmark_bar_pinned = delegate_->ShowBookmarkBar(); | 357 match.search_terms_args->bookmark_bar_pinned = client_->ShowBookmarkBar(); |
358 match.RecordAdditionalInfo(kRelevanceFromServerKey, | 358 match.RecordAdditionalInfo(kRelevanceFromServerKey, |
359 result.relevance_from_server() ? kTrue : kFalse); | 359 result.relevance_from_server() ? kTrue : kFalse); |
360 match.RecordAdditionalInfo(kShouldPrefetchKey, | 360 match.RecordAdditionalInfo(kShouldPrefetchKey, |
361 result.should_prefetch() ? kTrue : kFalse); | 361 result.should_prefetch() ? kTrue : kFalse); |
362 SetDeletionURL(result.deletion_url(), &match); | 362 SetDeletionURL(result.deletion_url(), &match); |
363 if (mark_as_deletable) | 363 if (mark_as_deletable) |
364 match.deletable = true; | 364 match.deletable = true; |
365 // Metadata is needed only for prefetching queries. | 365 // Metadata is needed only for prefetching queries. |
366 if (result.should_prefetch()) | 366 if (result.should_prefetch()) |
367 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 367 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 bool BaseSearchProvider::ParseSuggestResults( | 420 bool BaseSearchProvider::ParseSuggestResults( |
421 const base::Value& root_val, | 421 const base::Value& root_val, |
422 int default_result_relevance, | 422 int default_result_relevance, |
423 bool is_keyword_result, | 423 bool is_keyword_result, |
424 SearchSuggestionParser::Results* results) { | 424 SearchSuggestionParser::Results* results) { |
425 if (!SearchSuggestionParser::ParseSuggestResults( | 425 if (!SearchSuggestionParser::ParseSuggestResults( |
426 root_val, GetInput(is_keyword_result), | 426 root_val, GetInput(is_keyword_result), |
427 delegate_->SchemeClassifier(), default_result_relevance, | 427 client_->SchemeClassifier(), default_result_relevance, |
428 delegate_->AcceptLanguages(), is_keyword_result, results)) | 428 client_->AcceptLanguages(), is_keyword_result, results)) |
429 return false; | 429 return false; |
430 | 430 |
431 for (std::vector<GURL>::const_iterator it = | 431 for (std::vector<GURL>::const_iterator it = |
432 results->answers_image_urls.begin(); | 432 results->answers_image_urls.begin(); |
433 it != results->answers_image_urls.end(); ++it) | 433 it != results->answers_image_urls.end(); ++it) |
434 delegate_->PrefetchImage(*it); | 434 client_->PrefetchImage(*it); |
435 | 435 |
436 field_trial_triggered_ |= results->field_trial_triggered; | 436 field_trial_triggered_ |= results->field_trial_triggered; |
437 field_trial_triggered_in_session_ |= results->field_trial_triggered; | 437 field_trial_triggered_in_session_ |= results->field_trial_triggered; |
438 return true; | 438 return true; |
439 } | 439 } |
440 | 440 |
441 void BaseSearchProvider::DeleteMatchFromMatches( | 441 void BaseSearchProvider::DeleteMatchFromMatches( |
442 const AutocompleteMatch& match) { | 442 const AutocompleteMatch& match) { |
443 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { | 443 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { |
444 // Find the desired match to delete by checking the type and contents. | 444 // Find the desired match to delete by checking the type and contents. |
445 // We can't check the destination URL, because the autocomplete controller | 445 // We can't check the destination URL, because the autocomplete controller |
446 // may have reformulated that. Not that while checking for matching | 446 // may have reformulated that. Not that while checking for matching |
447 // contents works for personalized suggestions, if more match types gain | 447 // contents works for personalized suggestions, if more match types gain |
448 // deletion support, this algorithm may need to be re-examined. | 448 // deletion support, this algorithm may need to be re-examined. |
449 if (i->contents == match.contents && i->type == match.type) { | 449 if (i->contents == match.contents && i->type == match.type) { |
450 matches_.erase(i); | 450 matches_.erase(i); |
451 break; | 451 break; |
452 } | 452 } |
453 } | 453 } |
454 } | 454 } |
455 | 455 |
456 void BaseSearchProvider::OnDeletionComplete( | 456 void BaseSearchProvider::OnDeletionComplete( |
457 bool success, SuggestionDeletionHandler* handler) { | 457 bool success, SuggestionDeletionHandler* handler) { |
458 RecordDeletionResult(success); | 458 RecordDeletionResult(success); |
459 SuggestionDeletionHandlers::iterator it = std::find( | 459 SuggestionDeletionHandlers::iterator it = std::find( |
460 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 460 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
461 DCHECK(it != deletion_handlers_.end()); | 461 DCHECK(it != deletion_handlers_.end()); |
462 deletion_handlers_.erase(it); | 462 deletion_handlers_.erase(it); |
463 } | 463 } |
OLD | NEW |