Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / components / omnibox / base_search_provider.cc
bloba7957fedb24a5ce0128709e354f55d7f43d30350
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
3 // found in the LICENSE file.
5 #include "components/omnibox/base_search_provider.h"
7 #include "base/i18n/case_conversion.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/metrics/proto/omnibox_event.pb.h"
11 #include "components/metrics/proto/omnibox_input_type.pb.h"
12 #include "components/omnibox/autocomplete_provider_client.h"
13 #include "components/omnibox/autocomplete_provider_listener.h"
14 #include "components/omnibox/omnibox_field_trial.h"
15 #include "components/omnibox/suggestion_answer.h"
16 #include "components/search_engines/template_url.h"
17 #include "components/search_engines/template_url_prepopulate_data.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_fetcher_delegate.h"
22 #include "url/gurl.h"
24 using metrics::OmniboxEventProto;
26 // SuggestionDeletionHandler -------------------------------------------------
28 // This class handles making requests to the server in order to delete
29 // personalized suggestions.
30 class SuggestionDeletionHandler : public net::URLFetcherDelegate {
31 public:
32 typedef base::Callback<void(bool, SuggestionDeletionHandler*)>
33 DeletionCompletedCallback;
35 SuggestionDeletionHandler(
36 const std::string& deletion_url,
37 net::URLRequestContextGetter* request_context,
38 const DeletionCompletedCallback& callback);
40 ~SuggestionDeletionHandler() override;
42 private:
43 // net::URLFetcherDelegate:
44 void OnURLFetchComplete(const net::URLFetcher* source) override;
46 scoped_ptr<net::URLFetcher> deletion_fetcher_;
47 DeletionCompletedCallback callback_;
49 DISALLOW_COPY_AND_ASSIGN(SuggestionDeletionHandler);
52 SuggestionDeletionHandler::SuggestionDeletionHandler(
53 const std::string& deletion_url,
54 net::URLRequestContextGetter* request_context,
55 const DeletionCompletedCallback& callback) : callback_(callback) {
56 GURL url(deletion_url);
57 DCHECK(url.is_valid());
59 deletion_fetcher_.reset(net::URLFetcher::Create(
60 BaseSearchProvider::kDeletionURLFetcherID,
61 url,
62 net::URLFetcher::GET,
63 this));
64 deletion_fetcher_->SetRequestContext(request_context);
65 deletion_fetcher_->Start();
68 SuggestionDeletionHandler::~SuggestionDeletionHandler() {
71 void SuggestionDeletionHandler::OnURLFetchComplete(
72 const net::URLFetcher* source) {
73 DCHECK(source == deletion_fetcher_.get());
74 callback_.Run(
75 source->GetStatus().is_success() && (source->GetResponseCode() == 200),
76 this);
79 // BaseSearchProvider ---------------------------------------------------------
81 // static
82 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1;
83 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2;
84 const int BaseSearchProvider::kDeletionURLFetcherID = 3;
86 BaseSearchProvider::BaseSearchProvider(
87 TemplateURLService* template_url_service,
88 scoped_ptr<AutocompleteProviderClient> client,
89 AutocompleteProvider::Type type)
90 : AutocompleteProvider(type),
91 template_url_service_(template_url_service),
92 client_(client.Pass()),
93 field_trial_triggered_(false),
94 field_trial_triggered_in_session_(false) {
97 // static
98 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) {
99 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue;
102 // static
103 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion(
104 const base::string16& suggestion,
105 AutocompleteMatchType::Type type,
106 bool from_keyword_provider,
107 const TemplateURL* template_url,
108 const SearchTermsData& search_terms_data) {
109 // These calls use a number of default values. For instance, they assume
110 // that if this match is from a keyword provider, then the user is in keyword
111 // mode. They also assume the caller knows what it's doing and we set
112 // this match to look as if it was received/created synchronously.
113 SearchSuggestionParser::SuggestResult suggest_result(
114 suggestion, type, suggestion, base::string16(), base::string16(),
115 base::string16(), base::string16(), nullptr, std::string(),
116 std::string(), from_keyword_provider, 0, false, false, base::string16());
117 suggest_result.set_received_after_last_keystroke(false);
118 return CreateSearchSuggestion(
119 NULL, AutocompleteInput(), from_keyword_provider, suggest_result,
120 template_url, search_terms_data, 0, false);
123 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) {
124 DCHECK(match.deletable);
125 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) {
126 deletion_handlers_.push_back(new SuggestionDeletionHandler(
127 match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey),
128 client_->RequestContext(),
129 base::Bind(&BaseSearchProvider::OnDeletionComplete,
130 base::Unretained(this))));
133 TemplateURL* template_url =
134 match.GetTemplateURL(template_url_service_, false);
135 // This may be NULL if the template corresponding to the keyword has been
136 // deleted or there is no keyword set.
137 if (template_url != NULL) {
138 client_->DeleteMatchingURLsForKeywordFromHistory(template_url->id(),
139 match.contents);
142 // Immediately update the list of matches to show the match was deleted,
143 // regardless of whether the server request actually succeeds.
144 DeleteMatchFromMatches(match);
147 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
148 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
149 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
150 new_entry.set_provider(AsOmniboxEventProviderType());
151 new_entry.set_provider_done(done_);
152 std::vector<uint32> field_trial_hashes;
153 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes);
154 for (size_t i = 0; i < field_trial_hashes.size(); ++i) {
155 if (field_trial_triggered_)
156 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]);
157 if (field_trial_triggered_in_session_) {
158 new_entry.mutable_field_trial_triggered_in_session()->Add(
159 field_trial_hashes[i]);
164 // static
165 const char BaseSearchProvider::kRelevanceFromServerKey[] =
166 "relevance_from_server";
167 const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch";
168 const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata";
169 const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url";
170 const char BaseSearchProvider::kTrue[] = "true";
171 const char BaseSearchProvider::kFalse[] = "false";
173 BaseSearchProvider::~BaseSearchProvider() {}
175 void BaseSearchProvider::SetDeletionURL(const std::string& deletion_url,
176 AutocompleteMatch* match) {
177 if (deletion_url.empty())
178 return;
179 if (!template_url_service_)
180 return;
181 GURL url =
182 template_url_service_->GetDefaultSearchProvider()->GenerateSearchURL(
183 template_url_service_->search_terms_data());
184 url = url.GetOrigin().Resolve(deletion_url);
185 if (url.is_valid()) {
186 match->RecordAdditionalInfo(BaseSearchProvider::kDeletionUrlKey,
187 url.spec());
188 match->deletable = true;
192 // static
193 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion(
194 AutocompleteProvider* autocomplete_provider,
195 const AutocompleteInput& input,
196 const bool in_keyword_mode,
197 const SearchSuggestionParser::SuggestResult& suggestion,
198 const TemplateURL* template_url,
199 const SearchTermsData& search_terms_data,
200 int accepted_suggestion,
201 bool append_extra_query_params) {
202 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false,
203 suggestion.type());
205 if (!template_url)
206 return match;
207 match.keyword = template_url->keyword();
208 match.contents = suggestion.match_contents();
209 match.contents_class = suggestion.match_contents_class();
210 match.answer_contents = suggestion.answer_contents();
211 match.answer_type = suggestion.answer_type();
212 match.answer = SuggestionAnswer::copy(suggestion.answer());
213 if (suggestion.type() == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) {
214 match.RecordAdditionalInfo(
215 kACMatchPropertyInputText, base::UTF16ToUTF8(input.text()));
216 match.RecordAdditionalInfo(
217 kACMatchPropertyContentsPrefix,
218 base::UTF16ToUTF8(suggestion.match_contents_prefix()));
219 match.RecordAdditionalInfo(
220 kACMatchPropertyContentsStartIndex,
221 static_cast<int>(
222 suggestion.suggestion().length() - match.contents.length()));
225 if (!suggestion.annotation().empty()) {
226 match.description = suggestion.annotation();
227 AutocompleteMatch::AddLastClassificationIfNecessary(
228 &match.description_class, 0, ACMatchClassification::NONE);
231 // suggestion.match_contents() should have already been collapsed.
232 match.allowed_to_be_default_match =
233 (!in_keyword_mode || suggestion.from_keyword_provider()) &&
234 (base::CollapseWhitespace(input.text(), false) ==
235 suggestion.match_contents());
237 // When the user forced a query, we need to make sure all the fill_into_edit
238 // values preserve that property. Otherwise, if the user starts editing a
239 // suggestion, non-Search results will suddenly appear.
240 if (input.type() == metrics::OmniboxInputType::FORCED_QUERY)
241 match.fill_into_edit.assign(base::ASCIIToUTF16("?"));
242 if (suggestion.from_keyword_provider())
243 match.fill_into_edit.append(match.keyword + base::char16(' '));
244 // We only allow inlinable navsuggestions that were received before the
245 // last keystroke because we don't want asynchronous inline autocompletions.
246 if (!input.prevent_inline_autocomplete() &&
247 !suggestion.received_after_last_keystroke() &&
248 (!in_keyword_mode || suggestion.from_keyword_provider()) &&
249 StartsWith(suggestion.suggestion(), input.text(), false)) {
250 match.inline_autocompletion =
251 suggestion.suggestion().substr(input.text().length());
252 match.allowed_to_be_default_match = true;
254 match.fill_into_edit.append(suggestion.suggestion());
256 const TemplateURLRef& search_url = template_url->url_ref();
257 DCHECK(search_url.SupportsReplacement(search_terms_data));
258 match.search_terms_args.reset(
259 new TemplateURLRef::SearchTermsArgs(suggestion.suggestion()));
260 match.search_terms_args->original_query = input.text();
261 match.search_terms_args->accepted_suggestion = accepted_suggestion;
262 match.search_terms_args->enable_omnibox_start_margin = true;
263 match.search_terms_args->suggest_query_params =
264 suggestion.suggest_query_params();
265 match.search_terms_args->append_extra_query_params =
266 append_extra_query_params;
267 // This is the destination URL sans assisted query stats. This must be set
268 // so the AutocompleteController can properly de-dupe; the controller will
269 // eventually overwrite it before it reaches the user.
270 match.destination_url =
271 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get(),
272 search_terms_data));
274 // Search results don't look like URLs.
275 match.transition = suggestion.from_keyword_provider() ?
276 ui::PAGE_TRANSITION_KEYWORD : ui::PAGE_TRANSITION_GENERATED;
278 return match;
281 // static
282 bool BaseSearchProvider::ZeroSuggestEnabled(
283 const GURL& suggest_url,
284 const TemplateURL* template_url,
285 OmniboxEventProto::PageClassification page_classification,
286 const SearchTermsData& search_terms_data,
287 AutocompleteProviderClient* client) {
288 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial())
289 return false;
291 // Make sure we are sending the suggest request through HTTPS to prevent
292 // exposing the current page URL or personalized results without encryption.
293 if (!suggest_url.SchemeIs(url::kHttpsScheme))
294 return false;
296 // Don't show zero suggest on the NTP.
297 // TODO(hfung): Experiment with showing MostVisited zero suggest on NTP
298 // under the conditions described in crbug.com/305366.
299 if ((page_classification ==
300 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) ||
301 (page_classification ==
302 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS))
303 return false;
305 // Don't run if in incognito mode.
306 if (client->IsOffTheRecord())
307 return false;
309 // Don't run if we can't get preferences or search suggest is not enabled.
310 if (!client->SearchSuggestEnabled())
311 return false;
313 // Only make the request if we know that the provider supports zero suggest
314 // (currently only the prepopulated Google provider).
315 if (template_url == NULL ||
316 !template_url->SupportsReplacement(search_terms_data) ||
317 TemplateURLPrepopulateData::GetEngineType(
318 *template_url, search_terms_data) != SEARCH_ENGINE_GOOGLE)
319 return false;
321 return true;
324 // static
325 bool BaseSearchProvider::CanSendURL(
326 const GURL& current_page_url,
327 const GURL& suggest_url,
328 const TemplateURL* template_url,
329 OmniboxEventProto::PageClassification page_classification,
330 const SearchTermsData& search_terms_data,
331 AutocompleteProviderClient* client) {
332 if (!ZeroSuggestEnabled(suggest_url, template_url, page_classification,
333 search_terms_data, client))
334 return false;
336 if (!current_page_url.is_valid())
337 return false;
339 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search
340 // provider.
341 if ((current_page_url.scheme() != url::kHttpScheme) &&
342 ((current_page_url.scheme() != url::kHttpsScheme) ||
343 !net::registry_controlled_domains::SameDomainOrHost(
344 current_page_url, suggest_url,
345 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)))
346 return false;
348 if (!client->TabSyncEnabledAndUnencrypted())
349 return false;
351 return true;
354 void BaseSearchProvider::AddMatchToMap(
355 const SearchSuggestionParser::SuggestResult& result,
356 const std::string& metadata,
357 int accepted_suggestion,
358 bool mark_as_deletable,
359 bool in_keyword_mode,
360 MatchMap* map) {
361 AutocompleteMatch match = CreateSearchSuggestion(
362 this, GetInput(result.from_keyword_provider()), in_keyword_mode, result,
363 GetTemplateURL(result.from_keyword_provider()),
364 template_url_service_->search_terms_data(), accepted_suggestion,
365 ShouldAppendExtraParams(result));
366 if (!match.destination_url.is_valid())
367 return;
368 match.search_terms_args->bookmark_bar_pinned = client_->ShowBookmarkBar();
369 match.RecordAdditionalInfo(kRelevanceFromServerKey,
370 result.relevance_from_server() ? kTrue : kFalse);
371 match.RecordAdditionalInfo(kShouldPrefetchKey,
372 result.should_prefetch() ? kTrue : kFalse);
373 SetDeletionURL(result.deletion_url(), &match);
374 if (mark_as_deletable)
375 match.deletable = true;
376 // Metadata is needed only for prefetching queries.
377 if (result.should_prefetch())
378 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
380 // Try to add |match| to |map|. If a match for this suggestion is
381 // already in |map|, replace it if |match| is more relevant.
382 // NOTE: Keep this ToLower() call in sync with url_database.cc.
383 MatchKey match_key(
384 std::make_pair(base::i18n::ToLower(result.suggestion()),
385 match.search_terms_args->suggest_query_params));
386 const std::pair<MatchMap::iterator, bool> i(
387 map->insert(std::make_pair(match_key, match)));
389 bool should_prefetch = result.should_prefetch();
390 if (!i.second) {
391 // NOTE: We purposefully do a direct relevance comparison here instead of
392 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items
393 // added first" rather than "items alphabetically first" when the scores
394 // are equal. The only case this matters is when a user has results with
395 // the same score that differ only by capitalization; because the history
396 // system returns results sorted by recency, this means we'll pick the most
397 // recent such result even if the precision of our relevance score is too
398 // low to distinguish the two.
399 if (match.relevance > i.first->second.relevance) {
400 match.duplicate_matches.insert(match.duplicate_matches.end(),
401 i.first->second.duplicate_matches.begin(),
402 i.first->second.duplicate_matches.end());
403 i.first->second.duplicate_matches.clear();
404 match.duplicate_matches.push_back(i.first->second);
405 i.first->second = match;
406 } else {
407 i.first->second.duplicate_matches.push_back(match);
408 if (match.keyword == i.first->second.keyword) {
409 // Old and new matches are from the same search provider. It is okay to
410 // record one match's prefetch data onto a different match (for the same
411 // query string) for the following reasons:
412 // 1. Because the suggest server only sends down a query string from
413 // which we construct a URL, rather than sending a full URL, and because
414 // we construct URLs from query strings in the same way every time, the
415 // URLs for the two matches will be the same. Therefore, we won't end up
416 // prefetching something the server didn't intend.
417 // 2. Presumably the server sets the prefetch bit on a match it things
418 // is sufficiently relevant that the user is likely to choose it.
419 // Surely setting the prefetch bit on a match of even higher relevance
420 // won't violate this assumption.
421 should_prefetch |= ShouldPrefetch(i.first->second);
422 i.first->second.RecordAdditionalInfo(kShouldPrefetchKey,
423 should_prefetch ? kTrue : kFalse);
424 if (should_prefetch)
425 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
428 // Copy over answer data from lower-ranking item, if necessary.
429 // This depends on the lower-ranking item always being added last - see
430 // use of push_back above.
431 AutocompleteMatch& more_relevant_match = i.first->second;
432 const AutocompleteMatch& less_relevant_match =
433 more_relevant_match.duplicate_matches.back();
434 if (less_relevant_match.answer && !more_relevant_match.answer) {
435 more_relevant_match.answer_type = less_relevant_match.answer_type;
436 more_relevant_match.answer_contents = less_relevant_match.answer_contents;
437 more_relevant_match.answer =
438 SuggestionAnswer::copy(less_relevant_match.answer.get());
443 bool BaseSearchProvider::ParseSuggestResults(
444 const base::Value& root_val,
445 int default_result_relevance,
446 bool is_keyword_result,
447 SearchSuggestionParser::Results* results) {
448 if (!SearchSuggestionParser::ParseSuggestResults(
449 root_val, GetInput(is_keyword_result),
450 client_->SchemeClassifier(), default_result_relevance,
451 client_->AcceptLanguages(), is_keyword_result, results))
452 return false;
454 for (const GURL& url : results->answers_image_urls)
455 client_->PrefetchImage(url);
457 field_trial_triggered_ |= results->field_trial_triggered;
458 field_trial_triggered_in_session_ |= results->field_trial_triggered;
459 return true;
462 void BaseSearchProvider::DeleteMatchFromMatches(
463 const AutocompleteMatch& match) {
464 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
465 // Find the desired match to delete by checking the type and contents.
466 // We can't check the destination URL, because the autocomplete controller
467 // may have reformulated that. Not that while checking for matching
468 // contents works for personalized suggestions, if more match types gain
469 // deletion support, this algorithm may need to be re-examined.
470 if (i->contents == match.contents && i->type == match.type) {
471 matches_.erase(i);
472 break;
477 void BaseSearchProvider::OnDeletionComplete(
478 bool success, SuggestionDeletionHandler* handler) {
479 RecordDeletionResult(success);
480 SuggestionDeletionHandlers::iterator it = std::find(
481 deletion_handlers_.begin(), deletion_handlers_.end(), handler);
482 DCHECK(it != deletion_handlers_.end());
483 deletion_handlers_.erase(it);