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
|
// 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/autocomplete_result.h"
#include <algorithm>
#include <iterator>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "components/metrics/proto/omnibox_event.pb.h"
#include "components/metrics/proto/omnibox_input_type.pb.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/match_compare.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/omnibox/browser/omnibox_switches.h"
#include "components/search/search.h"
#include "components/url_formatter/url_fixer.h"
// static
const size_t AutocompleteResult::kMaxMatches = 6;
void AutocompleteResult::Selection::Clear() {
destination_url = GURL();
provider_affinity = NULL;
is_history_what_you_typed_match = false;
}
AutocompleteResult::AutocompleteResult() {
// Reserve space for the max number of matches we'll show.
matches_.reserve(kMaxMatches);
// It's probably safe to do this in the initializer list, but there's little
// penalty to doing it here and it ensures our object is fully constructed
// before calling member functions.
default_match_ = end();
}
AutocompleteResult::~AutocompleteResult() {}
void AutocompleteResult::CopyOldMatches(
const AutocompleteInput& input,
const AutocompleteResult& old_matches,
TemplateURLService* template_url_service) {
if (old_matches.empty())
return;
if (empty()) {
// If we've got no matches we can copy everything from the last result.
CopyFrom(old_matches);
for (ACMatches::iterator i(begin()); i != end(); ++i)
i->from_previous = true;
return;
}
// In hopes of providing a stable popup we try to keep the number of matches
// per provider consistent. Other schemes (such as blindly copying the most
// relevant matches) typically result in many successive 'What You Typed'
// results filling all the matches, which looks awful.
//
// Instead of starting with the current matches and then adding old matches
// until we hit our overall limit, we copy enough old matches so that each
// provider has at least as many as before, and then use SortAndCull() to
// clamp globally. This way, old high-relevance matches will starve new
// low-relevance matches, under the assumption that the new matches will
// ultimately be similar. If the assumption holds, this prevents seeing the
// new low-relevance match appear and then quickly get pushed off the bottom;
// if it doesn't, then once the providers are done and we expire the old
// matches, the new ones will all become visible, so we won't have lost
// anything permanently.
ProviderToMatches matches_per_provider, old_matches_per_provider;
BuildProviderToMatches(&matches_per_provider);
old_matches.BuildProviderToMatches(&old_matches_per_provider);
for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin());
i != old_matches_per_provider.end(); ++i) {
MergeMatchesByProvider(input.current_page_classification(),
i->second, matches_per_provider[i->first]);
}
SortAndCull(input, template_url_service);
}
void AutocompleteResult::AppendMatches(const AutocompleteInput& input,
const ACMatches& matches) {
for (const auto& i : matches) {
#ifndef NDEBUG
DCHECK_EQ(AutocompleteMatch::SanitizeString(i.contents), i.contents);
DCHECK_EQ(AutocompleteMatch::SanitizeString(i.description),
i.description);
#endif
matches_.push_back(i);
if (!AutocompleteMatch::IsSearchType(i.type)) {
const OmniboxFieldTrial::EmphasizeTitlesCondition condition(
OmniboxFieldTrial::GetEmphasizeTitlesConditionForInput(input.type()));
bool emphasize = false;
switch (condition) {
case OmniboxFieldTrial::EMPHASIZE_WHEN_NONEMPTY:
emphasize = !i.description.empty();
break;
case OmniboxFieldTrial::EMPHASIZE_WHEN_TITLE_MATCHES:
emphasize = !i.description.empty() &&
AutocompleteMatch::HasMatchStyle(i.description_class);
break;
case OmniboxFieldTrial::EMPHASIZE_WHEN_ONLY_TITLE_MATCHES:
emphasize = !i.description.empty() &&
AutocompleteMatch::HasMatchStyle(i.description_class) &&
!AutocompleteMatch::HasMatchStyle(i.contents_class);
break;
case OmniboxFieldTrial::EMPHASIZE_NEVER:
break;
default:
NOTREACHED();
}
matches_.back().swap_contents_and_description = emphasize;
}
}
default_match_ = end();
alternate_nav_url_ = GURL();
}
void AutocompleteResult::SortAndCull(
const AutocompleteInput& input,
TemplateURLService* template_url_service) {
for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i)
i->ComputeStrippedDestinationURL(input, template_url_service);
SortAndDedupMatches(input.current_page_classification(), &matches_);
// Sort and trim to the most relevant kMaxMatches matches.
size_t max_num_matches = std::min(kMaxMatches, matches_.size());
CompareWithDemoteByType<AutocompleteMatch> comparing_object(
input.current_page_classification());
std::sort(matches_.begin(), matches_.end(), comparing_object);
if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match) {
// Top match is not allowed to be the default match. Find the most
// relevant legal match and shift it to the front.
for (AutocompleteResult::iterator it = matches_.begin() + 1;
it != matches_.end(); ++it) {
if (it->allowed_to_be_default_match) {
std::rotate(matches_.begin(), it, it + 1);
break;
}
}
}
// In the process of trimming, drop all matches with a demoted relevance
// score of 0.
size_t num_matches;
for (num_matches = 0u; (num_matches < max_num_matches) &&
(comparing_object.GetDemotedRelevance(*match_at(num_matches)) > 0);
++num_matches) {}
matches_.resize(num_matches);
default_match_ = matches_.begin();
if (default_match_ != matches_.end()) {
const base::string16 debug_info =
base::ASCIIToUTF16("fill_into_edit=") +
default_match_->fill_into_edit +
base::ASCIIToUTF16(", provider=") +
((default_match_->provider != NULL)
? base::ASCIIToUTF16(default_match_->provider->GetName())
: base::string16()) +
base::ASCIIToUTF16(", input=") +
input.text();
// We should only get here with an empty omnibox for automatic suggestions
// on focus on the NTP; in these cases hitting enter should do nothing, so
// there should be no default match. Otherwise, we're doing automatic
// suggestions for the currently visible URL (and hitting enter should
// reload it), or the user is typing; in either of these cases, there should
// be a default match.
DCHECK_NE(input.text().empty(), default_match_->allowed_to_be_default_match)
<< debug_info;
// For navigable default matches, make sure the destination type is what the
// user would expect given the input.
if (default_match_->allowed_to_be_default_match &&
default_match_->destination_url.is_valid()) {
if (AutocompleteMatch::IsSearchType(default_match_->type)) {
// We shouldn't get query matches for URL inputs.
DCHECK_NE(metrics::OmniboxInputType::URL, input.type()) << debug_info;
} else {
// If the user explicitly typed a scheme, the default match should
// have the same scheme.
if ((input.type() == metrics::OmniboxInputType::URL) &&
input.parts().scheme.is_nonempty()) {
const std::string& in_scheme = base::UTF16ToUTF8(input.scheme());
const std::string& dest_scheme =
default_match_->destination_url.scheme();
DCHECK(url_formatter::IsEquivalentScheme(in_scheme, dest_scheme))
<< debug_info;
}
}
}
}
// Set the alternate nav URL.
alternate_nav_url_ = (default_match_ == matches_.end()) ?
GURL() : ComputeAlternateNavUrl(input, *default_match_);
}
bool AutocompleteResult::HasCopiedMatches() const {
for (ACMatches::const_iterator i(begin()); i != end(); ++i) {
if (i->from_previous)
return true;
}
return false;
}
size_t AutocompleteResult::size() const {
return matches_.size();
}
bool AutocompleteResult::empty() const {
return matches_.empty();
}
AutocompleteResult::const_iterator AutocompleteResult::begin() const {
return matches_.begin();
}
AutocompleteResult::iterator AutocompleteResult::begin() {
return matches_.begin();
}
AutocompleteResult::const_iterator AutocompleteResult::end() const {
return matches_.end();
}
AutocompleteResult::iterator AutocompleteResult::end() {
return matches_.end();
}
// Returns the match at the given index.
const AutocompleteMatch& AutocompleteResult::match_at(size_t index) const {
DCHECK_LT(index, matches_.size());
return matches_[index];
}
AutocompleteMatch* AutocompleteResult::match_at(size_t index) {
DCHECK_LT(index, matches_.size());
return &matches_[index];
}
bool AutocompleteResult::TopMatchIsStandaloneVerbatimMatch() const {
if (empty() || !match_at(0).IsVerbatimType())
return false;
// Skip any copied matches, under the assumption that they'll be expired and
// disappear. We don't want this disappearance to cause the visibility of the
// top match to change.
for (const_iterator i(begin() + 1); i != end(); ++i) {
if (!i->from_previous)
return !i->IsVerbatimType();
}
return true;
}
void AutocompleteResult::Reset() {
matches_.clear();
default_match_ = end();
}
void AutocompleteResult::Swap(AutocompleteResult* other) {
const size_t default_match_offset = default_match_ - begin();
const size_t other_default_match_offset =
other->default_match_ - other->begin();
matches_.swap(other->matches_);
default_match_ = begin() + other_default_match_offset;
other->default_match_ = other->begin() + default_match_offset;
alternate_nav_url_.Swap(&(other->alternate_nav_url_));
}
#ifndef NDEBUG
void AutocompleteResult::Validate() const {
for (const_iterator i(begin()); i != end(); ++i)
i->Validate();
}
#endif
// static
GURL AutocompleteResult::ComputeAlternateNavUrl(
const AutocompleteInput& input,
const AutocompleteMatch& match) {
return ((input.type() == metrics::OmniboxInputType::UNKNOWN) &&
(AutocompleteMatch::IsSearchType(match.type)) &&
!ui::PageTransitionCoreTypeIs(match.transition,
ui::PAGE_TRANSITION_KEYWORD) &&
(input.canonicalized_url() != match.destination_url)) ?
input.canonicalized_url() : GURL();
}
void AutocompleteResult::SortAndDedupMatches(
metrics::OmniboxEventProto::PageClassification page_classification,
ACMatches* matches) {
// Sort matches such that duplicate matches are consecutive.
std::sort(matches->begin(), matches->end(),
DestinationSort<AutocompleteMatch>(page_classification));
// Set duplicate_matches for the first match before erasing duplicate
// matches.
for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) {
for (int j = 1; (i + j != matches->end()) &&
AutocompleteMatch::DestinationsEqual(*i, *(i + j));
++j) {
AutocompleteMatch& dup_match(*(i + j));
i->duplicate_matches.insert(i->duplicate_matches.end(),
dup_match.duplicate_matches.begin(),
dup_match.duplicate_matches.end());
dup_match.duplicate_matches.clear();
i->duplicate_matches.push_back(dup_match);
}
}
// Erase duplicate matches.
matches->erase(std::unique(matches->begin(), matches->end(),
&AutocompleteMatch::DestinationsEqual),
matches->end());
}
void AutocompleteResult::CopyFrom(const AutocompleteResult& rhs) {
if (this == &rhs)
return;
matches_ = rhs.matches_;
// Careful! You can't just copy iterators from another container, you have to
// reconstruct them.
default_match_ = (rhs.default_match_ == rhs.end()) ?
end() : (begin() + (rhs.default_match_ - rhs.begin()));
alternate_nav_url_ = rhs.alternate_nav_url_;
}
void AutocompleteResult::BuildProviderToMatches(
ProviderToMatches* provider_to_matches) const {
for (ACMatches::const_iterator i(begin()); i != end(); ++i)
(*provider_to_matches)[i->provider].push_back(*i);
}
// static
bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match,
const ACMatches& matches) {
for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) {
if (i->destination_url == match.destination_url)
return true;
}
return false;
}
void AutocompleteResult::MergeMatchesByProvider(
metrics::OmniboxEventProto::PageClassification page_classification,
const ACMatches& old_matches,
const ACMatches& new_matches) {
if (new_matches.size() >= old_matches.size())
return;
// Prevent old matches from this provider from outranking new ones and
// becoming the default match by capping old matches' scores to be less than
// the highest-scoring allowed-to-be-default match from this provider.
ACMatches::const_iterator i = std::find_if(
new_matches.begin(), new_matches.end(),
[] (const AutocompleteMatch& m) {
return m.allowed_to_be_default_match;
});
// If the provider doesn't have any matches that are allowed-to-be-default,
// cap scores below the global allowed-to-be-default match.
// AutocompleteResult maintains the invariant that the first item in
// |matches_| is always such a match.
if (i == new_matches.end())
i = matches_.begin();
DCHECK(i->allowed_to_be_default_match);
const int max_relevance = i->relevance - 1;
// Because the goal is a visibly-stable popup, rather than one that preserves
// the highest-relevance matches, we copy in the lowest-relevance matches
// first. This means that within each provider's "group" of matches, any
// synchronous matches (which tend to have the highest scores) will
// "overwrite" the initial matches from that provider's previous results,
// minimally disturbing the rest of the matches.
size_t delta = old_matches.size() - new_matches.size();
for (ACMatches::const_reverse_iterator i(old_matches.rbegin());
i != old_matches.rend() && delta > 0; ++i) {
if (!HasMatchByDestination(*i, new_matches)) {
AutocompleteMatch match = *i;
match.relevance = std::min(max_relevance, match.relevance);
match.from_previous = true;
matches_.push_back(match);
delta--;
}
}
}
|