diff options
Diffstat (limited to 'chrome/browser/autocomplete/autocomplete_edit.cc')
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit.cc | 134 |
1 files changed, 76 insertions, 58 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc index 9b0aefb..f4c2583 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.cc +++ b/chrome/browser/autocomplete/autocomplete_edit.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2008 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. @@ -9,7 +9,6 @@ #include "base/basictypes.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" -#include "chrome/browser/autocomplete/autocomplete_classifier.h" #include "chrome/browser/autocomplete/autocomplete_edit_view.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/autocomplete/keyword_provider.h" @@ -20,6 +19,7 @@ #include "chrome/browser/profile.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_model.h" +#include "chrome/browser/search_versus_navigate_classifier.h" #include "chrome/common/notification_service.h" #include "googleurl/src/gurl.h" #include "googleurl/src/url_util.h" @@ -44,6 +44,7 @@ AutocompleteEditModel::AutocompleteEditModel( control_key_state_(UP), is_keyword_hint_(false), keyword_ui_state_(NORMAL), + show_search_hint_(true), paste_and_go_transition_(PageTransition::TYPED), profile_(profile) { } @@ -80,7 +81,7 @@ const AutocompleteEditModel::State } return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_, - keyword_ui_state_); + keyword_ui_state_, show_search_hint_); } void AutocompleteEditModel::RestoreState(const State& state) { @@ -91,6 +92,7 @@ void AutocompleteEditModel::RestoreState(const State& state) { keyword_ = state.keyword; is_keyword_hint_ = state.is_keyword_hint; keyword_ui_state_ = state.keyword_ui_state; + show_search_hint_ = state.show_search_hint; view_->SetUserText(state.user_text, DisplayTextFromUserText(state.user_text), false); } @@ -120,9 +122,7 @@ void AutocompleteEditModel::SetUserText(const std::wstring& text) { void AutocompleteEditModel::GetDataForURLExport(GURL* url, std::wstring* title, SkBitmap* favicon) { - AutocompleteMatch match; - GetInfoForCurrentText(&match, NULL); - *url = match.destination_url; + *url = GetURLForCurrentText(NULL, NULL, NULL); if (UTF8ToWide(url->possibly_invalid_spec()) == permanent_text_) { *title = controller_->GetTitle(); *favicon = controller_->GetFavIcon(); @@ -134,7 +134,7 @@ std::wstring AutocompleteEditModel::GetDesiredTLD() const { std::wstring(L"com") : std::wstring(); } -bool AutocompleteEditModel::CurrentTextIsURL() const { +bool AutocompleteEditModel::CurrentTextIsURL() { // If !user_input_in_progress_, the permanent text is showing, which should // always be a URL, so no further checking is needed. By avoiding checking in // this case, we avoid calling into the autocomplete providers, and thus @@ -142,15 +142,9 @@ bool AutocompleteEditModel::CurrentTextIsURL() const { if (!user_input_in_progress_) return true; - AutocompleteMatch match; - GetInfoForCurrentText(&match, NULL); - return match.transition == PageTransition::TYPED; -} - -AutocompleteMatch::Type AutocompleteEditModel::CurrentTextType() const { - AutocompleteMatch match; - GetInfoForCurrentText(&match, NULL); - return match.type; + PageTransition::Type transition = PageTransition::LINK; + GetURLForCurrentText(&transition, NULL, NULL); + return transition == PageTransition::TYPED; } bool AutocompleteEditModel::GetURLForText(const std::wstring& text, @@ -180,6 +174,7 @@ void AutocompleteEditModel::Revert() { keyword_.clear(); is_keyword_hint_ = false; keyword_ui_state_ = NORMAL; + show_search_hint_ = permanent_text_.empty(); has_temporary_text_ = false; view_->SetWindowTextAndCaretPos(permanent_text_, has_focus_ ? permanent_text_.length() : 0); @@ -196,11 +191,14 @@ bool AutocompleteEditModel::CanPasteAndGo(const std::wstring& text) const { if (!view_->GetCommandUpdater()->IsCommandEnabled(IDC_OPEN_CURRENT_URL)) return false; - AutocompleteMatch match; - profile_->GetAutocompleteClassifier()->Classify(text, std::wstring(), - &match, &paste_and_go_alternate_nav_url_); - paste_and_go_url_ = match.destination_url; - paste_and_go_transition_ = match.transition; + paste_and_go_url_ = GURL(); + paste_and_go_transition_ = PageTransition::TYPED; + paste_and_go_alternate_nav_url_ = GURL(); + + profile_->GetSearchVersusNavigateClassifier()->Classify(text, std::wstring(), + NULL, &paste_and_go_url_, &paste_and_go_transition_, NULL, + &paste_and_go_alternate_nav_url_); + return paste_and_go_url_.is_valid(); } @@ -217,30 +215,33 @@ void AutocompleteEditModel::PasteAndGo() { void AutocompleteEditModel::AcceptInput(WindowOpenDisposition disposition, bool for_drop) { // Get the URL and transition type for the selected entry. - AutocompleteMatch match; + PageTransition::Type transition; + bool is_history_what_you_typed_match; GURL alternate_nav_url; - GetInfoForCurrentText(&match, &alternate_nav_url); - if (!match.destination_url.is_valid()) + const GURL url(GetURLForCurrentText(&transition, + &is_history_what_you_typed_match, + &alternate_nav_url)); + if (!url.is_valid()) return; - if (UTF8ToWide(match.destination_url.spec()) == permanent_text_) { + if (UTF8ToWide(url.spec()) == permanent_text_) { // When the user hit enter on the existing permanent URL, treat it like a // reload for scoring purposes. We could detect this by just checking // user_input_in_progress_, but it seems better to treat "edits" that end // up leaving the URL unchanged (e.g. deleting the last character and then // retyping it) as reloads too. - match.transition = PageTransition::RELOAD; + transition = PageTransition::RELOAD; } else if (for_drop || ((paste_state_ != NONE) && - match.is_history_what_you_typed_match)) { + is_history_what_you_typed_match)) { // When the user pasted in a URL and hit enter, score it like a link click // rather than a normal typed URL, so it doesn't get inline autocompleted // as aggressively later. - match.transition = PageTransition::LINK; + transition = PageTransition::LINK; } - view_->OpenURL(match.destination_url, disposition, match.transition, - alternate_nav_url, AutocompletePopupModel::kNoMatch, - is_keyword_hint_ ? std::wstring() : keyword_); + view_->OpenURL(url, disposition, transition, alternate_nav_url, + AutocompletePopupModel::kNoMatch, + is_keyword_hint_ ? std::wstring() : keyword_); } void AutocompleteEditModel::SendOpenNotification(size_t selected_line, @@ -324,20 +325,17 @@ void AutocompleteEditModel::OnKillFocus() { } bool AutocompleteEditModel::OnEscapeKeyPressed() { - if (has_temporary_text_) { - AutocompleteMatch match; - popup_->InfoForCurrentSelection(&match, NULL); - if (match.destination_url != original_url_) { - // The user typed something, then selected a different item. Restore the - // text they typed and change back to the default item. - // NOTE: This purposefully does not reset paste_state_. - just_deleted_text_ = false; - has_temporary_text_ = false; - keyword_ui_state_ = original_keyword_ui_state_; - popup_->ResetToDefaultMatch(); - view_->OnRevertTemporaryText(); - return true; - } + if (has_temporary_text_ && + (popup_->URLsForCurrentSelection(NULL, NULL, NULL) != original_url_)) { + // The user typed something, then selected a different item. Restore the + // text they typed and change back to the default item. + // NOTE: This purposefully does not reset paste_state_. + just_deleted_text_ = false; + has_temporary_text_ = false; + keyword_ui_state_ = original_keyword_ui_state_; + popup_->ResetToDefaultMatch(); + view_->OnRevertTemporaryText(); + return true; } // If the user wasn't editing, but merely had focus in the edit, allow <esc> @@ -407,24 +405,35 @@ void AutocompleteEditModel::OnUpOrDownKeyPressed(int count) { void AutocompleteEditModel::OnPopupDataChanged( const std::wstring& text, - GURL* destination_for_temporary_text_change, + bool is_temporary_text, const std::wstring& keyword, - bool is_keyword_hint) { + bool is_keyword_hint, + AutocompleteMatch::Type type) { + // We don't want to show the search hint if we're showing a keyword hint or + // selected keyword, or (subtle!) if we would be showing a selected keyword + // but for keyword_ui_state_ == NO_KEYWORD. + const bool show_search_hint = keyword.empty() && + ((type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED) || + (type == AutocompleteMatch::SEARCH_HISTORY) || + (type == AutocompleteMatch::SEARCH_SUGGEST)); + // Update keyword/hint-related local state. bool keyword_state_changed = (keyword_ != keyword) || - ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()); + ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()) || + (show_search_hint_ != show_search_hint); if (keyword_state_changed) { keyword_ = keyword; is_keyword_hint_ = is_keyword_hint; + show_search_hint_ = show_search_hint; } // Handle changes to temporary text. - if (destination_for_temporary_text_change != NULL) { + if (is_temporary_text) { const bool save_original_selection = !has_temporary_text_; if (save_original_selection) { // Save the original selection and URL so it can be reverted later. has_temporary_text_ = true; - original_url_ = *destination_for_temporary_text_change; + original_url_ = popup_->URLsForCurrentSelection(NULL, NULL, NULL); original_keyword_ui_state_ = keyword_ui_state_; } if (control_key_state_ == DOWN_WITHOUT_CHANGE) { @@ -533,6 +542,7 @@ void AutocompleteEditModel::Observe(NotificationType type, std::wstring inline_autocomplete_text; std::wstring keyword; bool is_keyword_hint = false; + AutocompleteMatch::Type match_type = AutocompleteMatch::SEARCH_WHAT_YOU_TYPED; const AutocompleteResult* result = Details<const AutocompleteResult>(details).ptr(); const AutocompleteResult::const_iterator match(result->default_match()); @@ -549,9 +559,11 @@ void AutocompleteEditModel::Observe(NotificationType type, // the OS DNS cache could suffer eviction problems for minimal gain. is_keyword_hint = popup_->GetKeywordForMatch(*match, &keyword); + match_type = match->type; } - OnPopupDataChanged(inline_autocomplete_text, NULL, keyword, is_keyword_hint); + OnPopupDataChanged(inline_autocomplete_text, false, keyword, is_keyword_hint, + match_type); } void AutocompleteEditModel::InternalSetUserText(const std::wstring& text) { @@ -574,14 +586,20 @@ std::wstring AutocompleteEditModel::UserTextFromDisplayText( text : (keyword_ + L" " + text); } -void AutocompleteEditModel::GetInfoForCurrentText( - AutocompleteMatch* match, +GURL AutocompleteEditModel::GetURLForCurrentText( + PageTransition::Type* transition, + bool* is_history_what_you_typed_match, GURL* alternate_nav_url) const { if (popup_->IsOpen() || query_in_progress()) { - popup_->InfoForCurrentSelection(match, alternate_nav_url); - } else { - profile_->GetAutocompleteClassifier()->Classify( - UserTextFromDisplayText(view_->GetText()), GetDesiredTLD(), match, - alternate_nav_url); + return popup_->URLsForCurrentSelection(transition, + is_history_what_you_typed_match, + alternate_nav_url); } + + GURL destination_url; + profile_->GetSearchVersusNavigateClassifier()->Classify( + UserTextFromDisplayText(view_->GetText()), GetDesiredTLD(), NULL, + &destination_url, transition, is_history_what_you_typed_match, + alternate_nav_url); + return destination_url; } |