1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/base_search_provider.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <memory>
#include "base/feature_list.h"
#include "base/i18n/case_conversion.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/omnibox/browser/suggestion_answer.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "components/variations/net/variations_http_headers.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "third_party/metrics_proto/omnibox_input_type.pb.h"
#include "url/gurl.h"
// SuggestionDeletionHandler -------------------------------------------------
// This class handles making requests to the server in order to delete
// personalized suggestions.
class SuggestionDeletionHandler {
public:
typedef base::Callback<void(bool, SuggestionDeletionHandler*)>
DeletionCompletedCallback;
SuggestionDeletionHandler(AutocompleteProviderClient* client,
const std::string& deletion_url,
const DeletionCompletedCallback& callback);
~SuggestionDeletionHandler();
private:
// Callback from SimpleURLLoader
void OnURLLoadComplete(const network::SimpleURLLoader* source,
std::unique_ptr<std::string> response_body);
std::unique_ptr<network::SimpleURLLoader> deletion_fetcher_;
DeletionCompletedCallback callback_;
DISALLOW_COPY_AND_ASSIGN(SuggestionDeletionHandler);
};
SuggestionDeletionHandler::SuggestionDeletionHandler(
AutocompleteProviderClient* client,
const std::string& deletion_url,
const DeletionCompletedCallback& callback)
: callback_(callback) {
GURL url(deletion_url);
DCHECK(url.is_valid());
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("omnibox_suggest_deletion", R"(
semantics {
sender: "Omnibox"
description:
"When users attempt to delete server-provided personalized search "
"or navigation suggestions from the omnibox dropdown, Chrome sends "
"a message to the server requesting deletion of the suggestion."
trigger:
"A user attempt to delete a server-provided omnibox suggestion, "
"for which the server provided a custom deletion URL."
data:
"No user data is explicitly sent with the request, but because the "
"requested URL is provided by the server for each specific "
"suggestion, it necessarily uniquely identifies the suggestion the "
"user is attempting to delete."
destination: WEBSITE
}
policy {
cookies_allowed: YES
cookies_store: "user"
setting:
"Since this can only be triggered on seeing server-provided "
"suggestions in the omnibox dropdown, whether it is enabled is the "
"same as whether those suggestions are enabled.\n"
"Users can control this feature via the 'Use a prediction service "
"to help complete searches and URLs typed in the address bar' "
"setting under 'Privacy'. The feature is enabled by default."
chrome_policy {
SearchSuggestEnabled {
policy_options {mode: MANDATORY}
SearchSuggestEnabled: false
}
}
})");
auto request = std::make_unique<network::ResourceRequest>();
request->url = url;
variations::AppendVariationHeadersUnknownSignedIn(
request->url,
client->IsOffTheRecord() ? variations::InIncognito::kYes
: variations::InIncognito::kNo,
&request->headers);
// TODO(https://crbug.com/808498) re-add data use measurement once
// SimpleURLLoader supports it.
// data_use_measurement::DataUseUserData::OMNIBOX
deletion_fetcher_ =
network::SimpleURLLoader::Create(std::move(request), traffic_annotation);
deletion_fetcher_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
client->GetURLLoaderFactory().get(),
base::BindOnce(&SuggestionDeletionHandler::OnURLLoadComplete,
base::Unretained(this), deletion_fetcher_.get()));
}
SuggestionDeletionHandler::~SuggestionDeletionHandler() {
}
void SuggestionDeletionHandler::OnURLLoadComplete(
const network::SimpleURLLoader* source,
std::unique_ptr<std::string> response_body) {
DCHECK(source == deletion_fetcher_.get());
const bool ok = source->NetError() == net::OK &&
(source->ResponseInfo() && source->ResponseInfo()->headers &&
source->ResponseInfo()->headers->response_code() == 200);
callback_.Run(ok, this);
}
// BaseSearchProvider ---------------------------------------------------------
BaseSearchProvider::BaseSearchProvider(AutocompleteProvider::Type type,
AutocompleteProviderClient* client)
: AutocompleteProvider(type),
client_(client),
field_trial_triggered_(false),
field_trial_triggered_in_session_(false) {
}
// static
bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) {
return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue;
}
// static
AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion(
const base::string16& suggestion,
AutocompleteMatchType::Type type,
bool from_keyword_provider,
const TemplateURL* template_url,
const SearchTermsData& search_terms_data) {
// These calls use a number of default values. For instance, they assume
// that if this match is from a keyword provider, then the user is in keyword
// mode. They also assume the caller knows what it's doing and we set
// this match to look as if it was received/created synchronously.
SearchSuggestionParser::SuggestResult suggest_result(
suggestion, type, /*subtype_identifier=*/0, from_keyword_provider,
/*relevance=*/0, /*relevance_from_server=*/false,
/*input_text=*/base::string16());
suggest_result.set_received_after_last_keystroke(false);
return CreateSearchSuggestion(nullptr, AutocompleteInput(),
from_keyword_provider, suggest_result,
template_url, search_terms_data, 0, false);
}
void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) {
DCHECK(match.deletable);
if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) {
deletion_handlers_.push_back(std::make_unique<SuggestionDeletionHandler>(
client(), match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey),
base::Bind(&BaseSearchProvider::OnDeletionComplete,
base::Unretained(this))));
}
const TemplateURL* template_url =
match.GetTemplateURL(client_->GetTemplateURLService(), false);
// This may be nullptr if the template corresponding to the keyword has been
// deleted or there is no keyword set.
if (template_url != nullptr) {
client_->DeleteMatchingURLsForKeywordFromHistory(template_url->id(),
match.contents);
}
// Immediately update the list of matches to show the match was deleted,
// regardless of whether the server request actually succeeds.
DeleteMatchFromMatches(match);
}
void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
new_entry.set_provider(AsOmniboxEventProviderType());
new_entry.set_provider_done(done_);
std::vector<uint32_t> field_trial_hashes;
OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes);
for (size_t i = 0; i < field_trial_hashes.size(); ++i) {
if (field_trial_triggered_)
new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]);
if (field_trial_triggered_in_session_) {
new_entry.mutable_field_trial_triggered_in_session()->Add(
field_trial_hashes[i]);
}
}
}
// static
const char BaseSearchProvider::kRelevanceFromServerKey[] =
"relevance_from_server";
const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch";
const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata";
const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url";
const char BaseSearchProvider::kTrue[] = "true";
const char BaseSearchProvider::kFalse[] = "false";
BaseSearchProvider::~BaseSearchProvider() {}
// static
bool BaseSearchProvider::IsNTPPage(
metrics::OmniboxEventProto::PageClassification classification) {
using OEP = metrics::OmniboxEventProto;
return (classification == OEP::NTP) ||
(classification == OEP::OBSOLETE_INSTANT_NTP) ||
(classification == OEP::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) ||
(classification == OEP::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS);
}
// static
AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion(
AutocompleteProvider* autocomplete_provider,
const AutocompleteInput& input,
const bool in_keyword_mode,
const SearchSuggestionParser::SuggestResult& suggestion,
const TemplateURL* template_url,
const SearchTermsData& search_terms_data,
int accepted_suggestion,
bool append_extra_query_params_from_command_line) {
AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false,
suggestion.type());
if (!template_url)
return match;
match.keyword = template_url->keyword();
match.image_dominant_color = suggestion.image_dominant_color();
match.image_url = suggestion.image_url();
match.contents = suggestion.match_contents();
match.contents_class = suggestion.match_contents_class();
match.answer_contents = suggestion.answer_contents();
match.answer_type = suggestion.answer_type();
match.answer = suggestion.answer();
match.subtype_identifier = suggestion.subtype_identifier();
if (suggestion.type() == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) {
match.RecordAdditionalInfo(kACMatchPropertySuggestionText,
base::UTF16ToUTF8(suggestion.suggestion()));
match.RecordAdditionalInfo(
kACMatchPropertyContentsPrefix,
base::UTF16ToUTF8(suggestion.match_contents_prefix()));
match.RecordAdditionalInfo(
kACMatchPropertyContentsStartIndex,
static_cast<int>(
suggestion.suggestion().length() - match.contents.length()));
}
if (!suggestion.annotation().empty()) {
match.description = suggestion.annotation();
AutocompleteMatch::AddLastClassificationIfNecessary(
&match.description_class, 0, ACMatchClassification::NONE);
}
const base::string16 input_lower = base::i18n::ToLower(input.text());
// suggestion.match_contents() should have already been collapsed.
match.allowed_to_be_default_match =
(!in_keyword_mode || suggestion.from_keyword_provider()) &&
(base::CollapseWhitespace(input_lower, false) ==
base::i18n::ToLower(suggestion.match_contents()));
if (suggestion.from_keyword_provider())
match.fill_into_edit.append(match.keyword + base::char16(' '));
// We only allow inlinable navsuggestions that were received before the
// last keystroke because we don't want asynchronous inline autocompletions.
if (!input.prevent_inline_autocomplete() &&
!suggestion.received_after_last_keystroke() &&
(!in_keyword_mode || suggestion.from_keyword_provider()) &&
base::StartsWith(
base::i18n::ToLower(suggestion.suggestion()), input_lower,
base::CompareCase::SENSITIVE)) {
match.inline_autocompletion =
suggestion.suggestion().substr(input.text().length());
match.allowed_to_be_default_match = true;
}
match.fill_into_edit.append(suggestion.suggestion());
const TemplateURLRef& search_url = template_url->url_ref();
DCHECK(search_url.SupportsReplacement(search_terms_data));
match.search_terms_args.reset(
new TemplateURLRef::SearchTermsArgs(suggestion.suggestion()));
match.search_terms_args->original_query = input.text();
match.search_terms_args->accepted_suggestion = accepted_suggestion;
match.search_terms_args->additional_query_params =
suggestion.additional_query_params();
match.search_terms_args->append_extra_query_params_from_command_line =
append_extra_query_params_from_command_line;
// This is the destination URL sans assisted query stats. This must be set
// so the AutocompleteController can properly de-dupe; the controller will
// eventually overwrite it before it reaches the user.
match.destination_url = GURL(search_url.ReplaceSearchTerms(
*match.search_terms_args, search_terms_data));
// Search results don't look like URLs.
match.transition = suggestion.from_keyword_provider() ?
ui::PAGE_TRANSITION_KEYWORD : ui::PAGE_TRANSITION_GENERATED;
return match;
}
// static
bool BaseSearchProvider::CanSendURL(
const GURL& current_page_url,
const GURL& suggest_url,
const TemplateURL* template_url,
metrics::OmniboxEventProto::PageClassification page_classification,
const SearchTermsData& search_terms_data,
AutocompleteProviderClient* client) {
// Make sure we are sending the suggest request through a cryptographically
// secure channel to prevent exposing the current page URL or personalized
// results without encryption.
if (!suggest_url.SchemeIsCryptographic())
return false;
// Don't run if in incognito mode.
if (client->IsOffTheRecord())
return false;
// Don't run if we can't get preferences or search suggest is not enabled.
if (!client->SearchSuggestEnabled())
return false;
// Only make the request if we know that the provider supports sending zero
// suggest. (Currently only the prepopulated Google provider supports it.)
if (template_url == nullptr ||
!template_url->SupportsReplacement(search_terms_data) ||
template_url->GetEngineType(search_terms_data) != SEARCH_ENGINE_GOOGLE)
return false;
if (!current_page_url.is_valid())
return false;
// Don't bother sending the URL of an NTP page; it's not useful. The server
// already gets equivalent information in the form of the current page
// classification.
if (IsNTPPage(page_classification))
return false;
// Only allow HTTP URLs or HTTPS URLs.
const bool scheme_allowed = (current_page_url.scheme() == url::kHttpScheme) ||
(current_page_url.scheme() == url::kHttpsScheme);
if (!scheme_allowed)
return false;
if (!client->IsPersonalizedUrlDataCollectionActive())
return false;
return true;
}
void BaseSearchProvider::SetDeletionURL(const std::string& deletion_url,
AutocompleteMatch* match) {
if (deletion_url.empty())
return;
TemplateURLService* template_url_service = client_->GetTemplateURLService();
if (!template_url_service)
return;
GURL url =
template_url_service->GetDefaultSearchProvider()->GenerateSearchURL(
template_url_service->search_terms_data());
url = url.GetOrigin().Resolve(deletion_url);
if (url.is_valid()) {
match->RecordAdditionalInfo(BaseSearchProvider::kDeletionUrlKey,
url.spec());
match->deletable = true;
}
}
void BaseSearchProvider::AddMatchToMap(
const SearchSuggestionParser::SuggestResult& result,
const std::string& metadata,
int accepted_suggestion,
bool mark_as_deletable,
bool in_keyword_mode,
MatchMap* map) {
AutocompleteMatch match = CreateSearchSuggestion(
this, GetInput(result.from_keyword_provider()), in_keyword_mode, result,
GetTemplateURL(result.from_keyword_provider()),
client_->GetTemplateURLService()->search_terms_data(),
accepted_suggestion, ShouldAppendExtraParams(result));
if (!match.destination_url.is_valid())
return;
match.RecordAdditionalInfo(kRelevanceFromServerKey,
result.relevance_from_server() ? kTrue : kFalse);
match.RecordAdditionalInfo(kShouldPrefetchKey,
result.should_prefetch() ? kTrue : kFalse);
SetDeletionURL(result.deletion_url(), &match);
if (mark_as_deletable)
match.deletable = true;
// Metadata is needed only for prefetching queries.
if (result.should_prefetch())
match.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
// Try to add |match| to |map|. If a match for this suggestion is
// already in |map|, replace it if |match| is more relevant.
// NOTE: Keep this ToLower() call in sync with url_database.cc.
MatchKey match_key(
std::make_pair(base::i18n::ToLower(result.suggestion()),
match.search_terms_args->additional_query_params));
const std::pair<MatchMap::iterator, bool> i(
map->insert(std::make_pair(match_key, match)));
bool should_prefetch = result.should_prefetch();
if (!i.second) {
// NOTE: We purposefully do a direct relevance comparison here instead of
// using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items
// added first" rather than "items alphabetically first" when the scores
// are equal. The only case this matters is when a user has results with
// the same score that differ only by capitalization; because the history
// system returns results sorted by recency, this means we'll pick the most
// recent such result even if the precision of our relevance score is too
// low to distinguish the two.
if (match.relevance > i.first->second.relevance) {
match.duplicate_matches.insert(match.duplicate_matches.end(),
i.first->second.duplicate_matches.begin(),
i.first->second.duplicate_matches.end());
i.first->second.duplicate_matches.clear();
match.duplicate_matches.push_back(i.first->second);
i.first->second = match;
} else {
i.first->second.duplicate_matches.push_back(match);
if (match.keyword == i.first->second.keyword) {
// Old and new matches are from the same search provider. It is okay to
// record one match's prefetch data onto a different match (for the same
// query string) for the following reasons:
// 1. Because the suggest server only sends down a query string from
// which we construct a URL, rather than sending a full URL, and because
// we construct URLs from query strings in the same way every time, the
// URLs for the two matches will be the same. Therefore, we won't end up
// prefetching something the server didn't intend.
// 2. Presumably the server sets the prefetch bit on a match it things
// is sufficiently relevant that the user is likely to choose it.
// Surely setting the prefetch bit on a match of even higher relevance
// won't violate this assumption.
should_prefetch |= ShouldPrefetch(i.first->second);
i.first->second.RecordAdditionalInfo(kShouldPrefetchKey,
should_prefetch ? kTrue : kFalse);
if (should_prefetch)
i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
}
}
// Copy over answer data from lower-ranking item, if necessary.
// This depends on the lower-ranking item always being added last - see
// use of push_back above.
AutocompleteMatch& more_relevant_match = i.first->second;
const AutocompleteMatch& less_relevant_match =
more_relevant_match.duplicate_matches.back();
if (less_relevant_match.answer && !more_relevant_match.answer) {
more_relevant_match.answer_type = less_relevant_match.answer_type;
more_relevant_match.answer_contents = less_relevant_match.answer_contents;
more_relevant_match.answer = less_relevant_match.answer;
}
}
}
bool BaseSearchProvider::ParseSuggestResults(
const base::Value& root_val,
int default_result_relevance,
bool is_keyword_result,
SearchSuggestionParser::Results* results) {
if (!SearchSuggestionParser::ParseSuggestResults(
root_val, GetInput(is_keyword_result), client_->GetSchemeClassifier(),
default_result_relevance, is_keyword_result, results))
return false;
field_trial_triggered_ |= results->field_trial_triggered;
field_trial_triggered_in_session_ |= results->field_trial_triggered;
return true;
}
void BaseSearchProvider::DeleteMatchFromMatches(
const AutocompleteMatch& match) {
for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
// Find the desired match to delete by checking the type and contents.
// We can't check the destination URL, because the autocomplete controller
// may have reformulated that. Not that while checking for matching
// contents works for personalized suggestions, if more match types gain
// deletion support, this algorithm may need to be re-examined.
if (i->contents == match.contents && i->type == match.type) {
matches_.erase(i);
break;
}
}
}
void BaseSearchProvider::OnDeletionComplete(
bool success, SuggestionDeletionHandler* handler) {
RecordDeletionResult(success);
base::EraseIf(
deletion_handlers_,
[handler](const std::unique_ptr<SuggestionDeletionHandler>& elem) {
return elem.get() == handler;
});
}
|