#include <iterator>
#include "base/logging.h"
+#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_input.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/omnibox/omnibox_field_trial.h"
#include "chrome/browser/search/search.h"
#include "chrome/common/autocomplete_match_type.h"
+#include "components/metrics/proto/omnibox_event.pb.h"
+#include "components/metrics/proto/omnibox_input_type.pb.h"
+
+using metrics::OmniboxEventProto;
namespace {
class CompareWithDemoteByType {
public:
CompareWithDemoteByType(
- AutocompleteInput::PageClassification current_page_classification);
+ OmniboxEventProto::PageClassification current_page_classification);
// Returns the relevance score of |match| demoted appropriately by
// |demotions_by_type_|.
};
CompareWithDemoteByType::CompareWithDemoteByType(
- AutocompleteInput::PageClassification current_page_classification) {
+ OmniboxEventProto::PageClassification current_page_classification) {
OmniboxFieldTrial::GetDemotionsByType(current_page_classification,
&demotions_);
}
(demoted_relevance1 > demoted_relevance2);
}
+class DestinationSort {
+ public:
+ DestinationSort(
+ OmniboxEventProto::PageClassification current_page_classification);
+ bool operator()(const AutocompleteMatch& elem1,
+ const AutocompleteMatch& elem2);
+
+ private:
+ CompareWithDemoteByType demote_by_type_;
+};
+
+DestinationSort::DestinationSort(
+ OmniboxEventProto::PageClassification current_page_classification) :
+ demote_by_type_(current_page_classification) {}
+
+bool DestinationSort::operator()(const AutocompleteMatch& elem1,
+ const AutocompleteMatch& elem2) {
+ // Sort identical destination_urls together. Place the most relevant matches
+ // first, so that when we call std::unique(), these are the ones that get
+ // preserved.
+ if (AutocompleteMatch::DestinationsEqual(elem1, elem2) ||
+ (elem1.stripped_destination_url.is_empty() &&
+ elem2.stripped_destination_url.is_empty())) {
+ return demote_by_type_(elem1, elem2);
+ }
+ return elem1.stripped_destination_url < elem2.stripped_destination_url;
+}
+
+// Returns true if |match| is allowed to the default match taking into account
+// whether we're supposed to (and able to) demote all matches with inline
+// autocompletions.
+bool AllowedToBeDefaultMatchAccountingForDisableInliningExperiment(
+ const AutocompleteMatch& match,
+ const bool has_legal_default_match_without_completion) {
+ return match.allowed_to_be_default_match &&
+ (!OmniboxFieldTrial::DisableInlining() ||
+ !has_legal_default_match_without_completion ||
+ match.inline_autocompletion.empty());
+}
+
}; // namespace
// static
const size_t AutocompleteResult::kMaxMatches = 6;
-const int AutocompleteResult::kLowestDefaultScore = 1200;
void AutocompleteResult::Selection::Clear() {
destination_url = GURL();
for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i)
i->ComputeStrippedDestinationURL(profile);
- // Remove duplicates.
- std::sort(matches_.begin(), matches_.end(),
- &AutocompleteMatch::DestinationSortFunc);
- matches_.erase(std::unique(matches_.begin(), matches_.end(),
- &AutocompleteMatch::DestinationsEqual),
- matches_.end());
+ DedupMatchesByDestination(input.current_page_classification(), true,
+ &matches_);
+
+ // If the result set has at least one legal default match without an inline
+ // autocompletion, then in the disable inlining experiment it will be okay
+ // to demote all matches with inline autocompletions. On the other hand, if
+ // the experiment is active but there is no legal match without an inline
+ // autocompletion, then we'll pretend the experiment is not active and not
+ // demote the matches with an inline autocompletion. In other words, an
+ // alternate name for this variable is
+ // allowed_to_demote_matches_with_inline_autocompletion.
+ bool has_legal_default_match_without_completion = false;
+ for (AutocompleteResult::iterator it = matches_.begin();
+ (it != matches_.end()) && !has_legal_default_match_without_completion;
+ ++it) {
+ if (it->allowed_to_be_default_match && it->inline_autocompletion.empty())
+ has_legal_default_match_without_completion = true;
+ }
+ UMA_HISTOGRAM_BOOLEAN("Omnibox.HasLegalDefaultMatchWithoutCompletion",
+ has_legal_default_match_without_completion);
// Sort and trim to the most relevant kMaxMatches matches.
size_t max_num_matches = std::min(kMaxMatches, matches_.size());
CompareWithDemoteByType comparing_object(input.current_page_classification());
std::sort(matches_.begin(), matches_.end(), comparing_object);
- if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match &&
- OmniboxFieldTrial::ReorderForLegalDefaultMatch(
- input.current_page_classification())) {
+ if (!matches_.empty() &&
+ !AllowedToBeDefaultMatchAccountingForDisableInliningExperiment(
+ *matches_.begin(), has_legal_default_match_without_completion)) {
// 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) {
+ if (AllowedToBeDefaultMatchAccountingForDisableInliningExperiment(
+ *it, has_legal_default_match_without_completion)) {
std::rotate(matches_.begin(), it, it + 1);
break;
}
default_match_ = matches_.begin();
if (default_match_ != matches_.end()) {
- const string16 debug_info = ASCIIToUTF16("fill_into_edit=") +
- default_match_->fill_into_edit + ASCIIToUTF16(", provider=") +
- ((default_match_->provider != NULL) ?
- ASCIIToUTF16(default_match_->provider->GetName()) : string16()) +
- ASCIIToUTF16(", input=") + input.text();
+ 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();
DCHECK(default_match_->allowed_to_be_default_match) << debug_info;
- // We shouldn't get query matches for URL inputs, or non-query matches
- // for query inputs.
- if (AutocompleteMatch::IsSearchType(default_match_->type)) {
- DCHECK_NE(AutocompleteInput::URL, input.type()) << debug_info;
- } else {
- DCHECK_NE(AutocompleteInput::FORCED_QUERY, input.type()) << debug_info;
+ // If the default match is valid (i.e., not a prompt/placeholder), make
+ // sure the type of destination is what the user would expect given the
+ // input.
+ if (default_match_->destination_url.is_valid()) {
+ // We shouldn't get query matches for URL inputs, or non-query matches
+ // for query inputs.
+ if (AutocompleteMatch::IsSearchType(default_match_->type)) {
+ DCHECK_NE(metrics::OmniboxInputType::URL, input.type()) << debug_info;
+ } else {
+ DCHECK_NE(metrics::OmniboxInputType::FORCED_QUERY, input.type())
+ << debug_info;
+ }
}
}
}
bool AutocompleteResult::ShouldHideTopMatch() const {
- // Gate on our field trial flag.
- if (!chrome::ShouldHideTopVerbatimMatch())
- return false;
+ return chrome::ShouldHideTopVerbatimMatch() &&
+ TopMatchIsStandaloneVerbatimMatch();
+}
- // If we don't have a verbatim first match, show everything.
+bool AutocompleteResult::TopMatchIsStandaloneVerbatimMatch() const {
if (empty() || !match_at(0).IsVerbatimType())
return false;
- // If the verbatim first match is followed by another verbatim match, don't
- // hide anything, lest we cause user confusion.
- if ((size() > 1) && match_at(1).IsVerbatimType())
- return false;
-
- // Otherwise, it's safe to hide the verbatim first match.
+ // 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;
}
GURL AutocompleteResult::ComputeAlternateNavUrl(
const AutocompleteInput& input,
const AutocompleteMatch& match) {
- return ((input.type() == AutocompleteInput::UNKNOWN) &&
+ return ((input.type() == metrics::OmniboxInputType::UNKNOWN) &&
(AutocompleteMatch::IsSearchType(match.type)) &&
(match.transition != content::PAGE_TRANSITION_KEYWORD) &&
(input.canonicalized_url() != match.destination_url)) ?
input.canonicalized_url() : GURL();
}
+void AutocompleteResult::DedupMatchesByDestination(
+ OmniboxEventProto::PageClassification page_classification,
+ bool set_duplicate_matches,
+ ACMatches* matches) {
+ DestinationSort destination_sort(page_classification);
+ // Sort matches such that duplicate matches are consecutive.
+ std::sort(matches->begin(), matches->end(), destination_sort);
+
+ if (set_duplicate_matches) {
+ // 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;
alternate_nav_url_ = rhs.alternate_nav_url_;
}
-void AutocompleteResult::AddMatch(
- AutocompleteInput::PageClassification page_classification,
- const AutocompleteMatch& match) {
- DCHECK(default_match_ != end());
- DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents);
- DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description),
- match.description);
- CompareWithDemoteByType comparing_object(page_classification);
- ACMatches::iterator insertion_point =
- std::upper_bound(begin(), end(), match, comparing_object);
- matches_difference_type default_offset = default_match_ - begin();
- if ((insertion_point - begin()) <= default_offset)
- ++default_offset;
- matches_.insert(insertion_point, match);
- default_match_ = begin() + default_offset;
-}
-
void AutocompleteResult::BuildProviderToMatches(
ProviderToMatches* provider_to_matches) const {
for (ACMatches::const_iterator i(begin()); i != end(); ++i)
}
void AutocompleteResult::MergeMatchesByProvider(
- AutocompleteInput::PageClassification page_classification,
+ OmniboxEventProto::PageClassification page_classification,
const ACMatches& old_matches,
const ACMatches& new_matches) {
if (new_matches.size() >= old_matches.size())
AutocompleteMatch match = *i;
match.relevance = std::min(max_relevance, match.relevance);
match.from_previous = true;
- AddMatch(page_classification, match);
+ matches_.push_back(match);
delta--;
}
}