This source file includes following definitions.
- GetMatchToPrefetch
- autocomplete_controller_
- StartAutocomplete
- OnResultChanged
- InvalidateCurrentMatch
- ClearPopupKeywordMode
- DoPreconnect
#include "chrome/browser/ui/omnibox/omnibox_controller.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/autocomplete/autocomplete_classifier.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/search_provider.h"
#include "chrome/browser/net/predictor.h"
#include "chrome/browser/predictors/autocomplete_action_predictor.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
#include "chrome/common/instant_types.h"
#include "extensions/common/constants.h"
#include "ui/gfx/rect.h"
namespace {
const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
const AutocompleteResult::const_iterator default_match(
result.default_match());
if ((default_match != result.end()) &&
SearchProvider::ShouldPrefetch(*default_match))
return &(*default_match);
if ((result.ShouldHideTopMatch() ||
result.TopMatchIsStandaloneVerbatimMatch()) &&
(result.size() > 1) &&
SearchProvider::ShouldPrefetch(result.match_at(1)))
return &result.match_at(1);
return NULL;
}
}
OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
Profile* profile)
: omnibox_edit_model_(omnibox_edit_model),
profile_(profile),
popup_(NULL),
autocomplete_controller_(new AutocompleteController(profile, this,
AutocompleteClassifier::kDefaultOmniboxProviders)) {
}
OmniboxController::~OmniboxController() {
}
void OmniboxController::StartAutocomplete(
base::string16 user_text,
size_t cursor_position,
const GURL& current_url,
AutocompleteInput::PageClassification current_page_classification,
bool prevent_inline_autocomplete,
bool prefer_keyword,
bool allow_exact_keyword_match) const {
ClearPopupKeywordMode();
popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
autocomplete_controller_->Start(AutocompleteInput(
user_text, cursor_position, base::string16(), current_url,
current_page_classification, prevent_inline_autocomplete,
prefer_keyword, allow_exact_keyword_match,
AutocompleteInput::ALL_MATCHES));
}
void OmniboxController::OnResultChanged(bool default_match_changed) {
const bool was_open = popup_->IsOpen();
if (default_match_changed) {
const AutocompleteResult& result = this->result();
const AutocompleteResult::const_iterator match(result.default_match());
if (match != result.end()) {
current_match_ = *match;
if (!prerender::IsOmniboxEnabled(profile_))
DoPreconnect(*match);
omnibox_edit_model_->OnCurrentMatchChanged();
if (chrome::IsInstantExtendedAPIEnabled()) {
InstantSuggestion prefetch_suggestion;
const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result);
if (match_to_prefetch) {
prefetch_suggestion.text = match_to_prefetch->contents;
prefetch_suggestion.metadata =
SearchProvider::GetSuggestMetadata(*match_to_prefetch);
}
omnibox_edit_model_->SetSuggestionToPrefetch(prefetch_suggestion);
}
} else {
InvalidateCurrentMatch();
popup_->OnResultChanged();
omnibox_edit_model_->OnPopupDataChanged(base::string16(), NULL,
base::string16(), false);
}
} else {
popup_->OnResultChanged();
}
if (!popup_->IsOpen() && was_open) {
omnibox_edit_model_->AcceptTemporaryTextAsUserText();
}
}
void OmniboxController::InvalidateCurrentMatch() {
current_match_ = AutocompleteMatch();
}
void OmniboxController::ClearPopupKeywordMode() const {
if (popup_->IsOpen() &&
popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
}
void OmniboxController::DoPreconnect(const AutocompleteMatch& match) {
if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) {
UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type,
AutocompleteMatchType::NUM_TYPES);
if (profile_->GetNetworkPredictor()) {
profile_->GetNetworkPredictor()->AnticipateOmniboxUrl(
match.destination_url,
predictors::AutocompleteActionPredictor::IsPreconnectable(match));
}
}
}