diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-16 16:31:27 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-16 16:31:27 +0000 |
commit | 28b6e256ff306cac4d42fce6684cb7329aaef0cc (patch) | |
tree | b163675cd3aec3130d97969de2eaccf1513ba7ef /chrome/browser/autocomplete | |
parent | cb15d15effa70c3052b474437a895b387367520c (diff) | |
download | chromium_src-28b6e256ff306cac4d42fce6684cb7329aaef0cc.zip chromium_src-28b6e256ff306cac4d42fce6684cb7329aaef0cc.tar.gz chromium_src-28b6e256ff306cac4d42fce6684cb7329aaef0cc.tar.bz2 |
Autocomplete cleanup patch. Adds AutocompleteControllerDelegate in
addition to the notification. The notification is now only sent out
when done. Also makes the editmodel own the controller.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6519014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75126 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete')
19 files changed, 195 insertions, 237 deletions
diff --git a/chrome/browser/autocomplete/autocomplete.cc b/chrome/browser/autocomplete/autocomplete.cc index 6b78e71..f990da2 100644 --- a/chrome/browser/autocomplete/autocomplete.cc +++ b/chrome/browser/autocomplete/autocomplete.cc @@ -12,6 +12,7 @@ #include "base/string_number_conversions.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" #include "chrome/browser/autocomplete/autocomplete_match.h" #include "chrome/browser/autocomplete/history_quick_provider.h" #include "chrome/browser/autocomplete/history_url_provider.h" @@ -779,8 +780,11 @@ const int AutocompleteController::kNoItemSelected = -1; // they initiate a query. static const int kExpireTimeMS = 500; -AutocompleteController::AutocompleteController(Profile* profile) - : done_(true), +AutocompleteController::AutocompleteController( + Profile* profile, + AutocompleteControllerDelegate* delegate) + : delegate_(delegate), + done_(true), in_start_(false) { search_provider_ = new SearchProvider(this, profile); providers_.push_back(search_provider_); @@ -948,10 +952,14 @@ void AutocompleteController::UpdateResult(bool is_synchronous_pass) { } void AutocompleteController::NotifyChanged(bool notify_default_match) { - NotificationService::current()->Notify( - NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED, - Source<AutocompleteController>(this), - Details<bool>(¬ify_default_match)); + if (delegate_) + delegate_->OnResultChanged(notify_default_match); + if (done_) { + NotificationService::current()->Notify( + NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_READY, + Source<AutocompleteController>(this), + NotificationService::NoDetails()); + } } void AutocompleteController::CheckIfDone() { diff --git a/chrome/browser/autocomplete/autocomplete.h b/chrome/browser/autocomplete/autocomplete.h index 72e24e2..9a182b1 100644 --- a/chrome/browser/autocomplete/autocomplete.h +++ b/chrome/browser/autocomplete/autocomplete.h @@ -149,11 +149,12 @@ // --: relevance score falls off over two days (discounted 99 points after two // days). +class AutocompleteController; +class AutocompleteControllerDelegate; class AutocompleteInput; struct AutocompleteMatch; class AutocompleteProvider; class AutocompleteResult; -class AutocompleteController; class HistoryContentsProvider; class Profile; class SearchProvider; @@ -570,10 +571,12 @@ class AutocompleteController : public ACProviderListener { // second to set the providers to some known testing providers. The default // providers will be overridden and the controller will take ownership of the // providers, Release()ing them on destruction. - explicit AutocompleteController(Profile* profile); + AutocompleteController(Profile* profile, + AutocompleteControllerDelegate* delegate); #ifdef UNIT_TEST explicit AutocompleteController(const ACProviders& providers) - : providers_(providers), + : delegate_(NULL), + providers_(providers), search_provider_(NULL), done_(true), in_start_(false) { @@ -610,10 +613,11 @@ class AutocompleteController : public ACProviderListener { // return matches which are synchronously available, which should mean that // all providers will be done immediately. // - // The controller will fire AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED from + // The controller calls AutocompleteControllerDelegate::OnResultChanged() from // inside this call at least once. If matches are available later on that - // result in changing the result set AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED - // is sent again. + // result in changing the result set the delegate is notified again. When the + // controller is done the notification AUTOCOMPLETE_CONTROLLER_RESULT_READY is + // sent. void Start(const string16& text, const string16& desired_tld, bool prevent_inline_autocomplete, @@ -653,12 +657,8 @@ class AutocompleteController : public ACProviderListener { // Start() is calling this to get the synchronous result. void UpdateResult(bool is_synchronous_pass); - // Sends notifications that the results were updated. If - // |notify_default_match| is true, - // AUTOCOMPLETE_CONTROLLER_DEFAULT_MATCH_UPDATED is sent in addition to - // AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED. - // TODO(pkasting): Don't hardcode assumptions about who listens to these - // notificiations. + // Calls AutocompleteControllerDelegate::OnResultChanged() and if done sends + // AUTOCOMPLETE_CONTROLLER_RESULT_READY. void NotifyChanged(bool notify_default_match); // Updates |done_| to be accurate with respect to current providers' statuses. @@ -667,6 +667,8 @@ class AutocompleteController : public ACProviderListener { // Starts the expire timer. void StartExpireTimer(); + AutocompleteControllerDelegate* delegate_; + // A list of all providers. ACProviders providers_; diff --git a/chrome/browser/autocomplete/autocomplete_classifier.cc b/chrome/browser/autocomplete/autocomplete_classifier.cc index 45deb87..ad9e2d7 100644 --- a/chrome/browser/autocomplete/autocomplete_classifier.cc +++ b/chrome/browser/autocomplete/autocomplete_classifier.cc @@ -9,7 +9,7 @@ #include "googleurl/src/gurl.h" AutocompleteClassifier::AutocompleteClassifier(Profile* profile) - : controller_(new AutocompleteController(profile)) { + : controller_(new AutocompleteController(profile, NULL)) { } AutocompleteClassifier::~AutocompleteClassifier() { diff --git a/chrome/browser/autocomplete/autocomplete_controller_delegate.h b/chrome/browser/autocomplete/autocomplete_controller_delegate.h new file mode 100644 index 0000000..7446456 --- /dev/null +++ b/chrome/browser/autocomplete/autocomplete_controller_delegate.h @@ -0,0 +1,20 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_ +#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_ +#pragma once + +class AutocompleteControllerDelegate { + public: + // Invoked when the result set of the AutocompleteController changes. If + // |default_match_changed| is true, the default match of the result set has + // changed. + virtual void OnResultChanged(bool default_match_changed) = 0; + + protected: + virtual ~AutocompleteControllerDelegate() {} +}; + +#endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_ diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc index 5f3aeef..6dd411f 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.cc +++ b/chrome/browser/autocomplete/autocomplete_edit.cc @@ -17,6 +17,7 @@ #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/autocomplete/autocomplete_popup_view.h" #include "chrome/browser/autocomplete/keyword_provider.h" +#include "chrome/browser/autocomplete/search_provider.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/command_updater.h" #include "chrome/browser/extensions/extension_omnibox_api.h" @@ -63,7 +64,9 @@ AutocompleteEditModel::AutocompleteEditModel( AutocompleteEditView* view, AutocompleteEditController* controller, Profile* profile) - : view_(view), + : ALLOW_THIS_IN_INITIALIZER_LIST( + autocomplete_controller_(new AutocompleteController(profile, this))), + view_(view), popup_(NULL), controller_(controller), has_focus_(false), @@ -80,17 +83,11 @@ AutocompleteEditModel::AutocompleteEditModel( AutocompleteEditModel::~AutocompleteEditModel() { } -void AutocompleteEditModel::SetPopupModel(AutocompletePopupModel* popup_model) { - popup_ = popup_model; - registrar_.Add(this, - NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED, - Source<AutocompleteController>(popup_->autocomplete_controller())); -} - void AutocompleteEditModel::SetProfile(Profile* profile) { DCHECK(profile); profile_ = profile; - popup_->SetProfile(profile); + autocomplete_controller_->SetProfile(profile); + popup_->set_profile(profile); } const AutocompleteEditModel::State @@ -162,8 +159,10 @@ void AutocompleteEditModel::FinalizeInstantQuery( view_->OnBeforePossibleChange(); view_->SetWindowTextAndCaretPos(final_text, final_text.length()); view_->OnAfterPossibleChange(); - } else { - popup_->FinalizeInstantQuery(input_text, suggest_text); + } else if (popup_->IsOpen()) { + SearchProvider* search_provider = + autocomplete_controller_->search_provider(); + search_provider->FinalizeInstantQuery(input_text, suggest_text); } } @@ -184,7 +183,7 @@ bool AutocompleteEditModel::UseVerbatimInstant() { #if defined(OS_MACOSX) // TODO(suzhe): Fix Mac port to display Instant suggest in a separated NSView, // so that we can display instant suggest along with composition text. - const AutocompleteInput& input = popup_->autocomplete_controller()->input(); + const AutocompleteInput& input = autocomplete_controller_->input(); if (input.initial_prevent_inline_autocomplete()) return true; #endif @@ -308,10 +307,19 @@ void AutocompleteEditModel::StartAutocomplete( bool has_selected_text, bool prevent_inline_autocomplete) const { bool keyword_is_selected = KeywordIsSelected(); - popup_->StartAutocomplete(user_text_, GetDesiredTLD(), + popup_->SetHoveredLine(AutocompletePopupModel::kNoMatch); + // We don't explicitly clear AutocompletePopupModel::manually_selected_match, + // as Start ends up invoking AutocompletePopupModel::OnResultChanged which + // clears it. + autocomplete_controller_->Start( + user_text_, GetDesiredTLD(), prevent_inline_autocomplete || just_deleted_text_ || (has_selected_text && inline_autocomplete_text_.empty()) || - (paste_state_ != NONE), keyword_is_selected, keyword_is_selected); + (paste_state_ != NONE), keyword_is_selected, keyword_is_selected, false); +} + +void AutocompleteEditModel::StopAutocomplete() { + autocomplete_controller_->Stop(true); } bool AutocompleteEditModel::CanPasteAndGo(const string16& text) const { @@ -395,14 +403,16 @@ void AutocompleteEditModel::OpenURL(const GURL& url, // We only care about cases where there is a selection (i.e. the popup is // open). if (popup_->IsOpen()) { - scoped_ptr<AutocompleteLog> log(popup_->GetAutocompleteLog()); + AutocompleteLog log(autocomplete_controller_->input().text(), + autocomplete_controller_->input().type(), + popup_->selected_line(), 0, result()); if (index != AutocompletePopupModel::kNoMatch) - log->selected_index = index; + log.selected_index = index; else if (!has_temporary_text_) - log->inline_autocompleted_length = inline_autocomplete_text_.length(); + log.inline_autocompleted_length = inline_autocomplete_text_.length(); NotificationService::current()->Notify( NotificationType::OMNIBOX_OPENED_URL, Source<Profile>(profile_), - Details<AutocompleteLog>(log.get())); + Details<AutocompleteLog>(&log)); } TemplateURLModel* template_url_model = profile_->GetTemplateURLModel(); @@ -473,12 +483,8 @@ void AutocompleteEditModel::ClearKeyword(const string16& visible_text) { // longer. } -bool AutocompleteEditModel::query_in_progress() const { - return !popup_->autocomplete_controller()->done(); -} - const AutocompleteResult& AutocompleteEditModel::result() const { - return popup_->autocomplete_controller()->result(); + return autocomplete_controller_->result(); } void AutocompleteEditModel::OnSetFocus(bool control_down) { @@ -499,7 +505,7 @@ void AutocompleteEditModel::OnKillFocus() { bool AutocompleteEditModel::OnEscapeKeyPressed() { if (has_temporary_text_) { AutocompleteMatch match; - popup_->InfoForCurrentSelection(&match, NULL); + InfoForCurrentSelection(&match, NULL); if (match.destination_url != original_url_) { RevertTemporaryText(true); return true; @@ -706,19 +712,13 @@ static bool IsPreconnectable(AutocompleteMatch::Type type) { } } -void AutocompleteEditModel::Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - DCHECK_EQ(NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED, - type.value); - +void AutocompleteEditModel::OnResultChanged(bool default_match_changed) { const bool was_open = popup_->view()->IsOpen(); - if (*(Details<bool>(details).ptr())) { + if (default_match_changed) { string16 inline_autocomplete_text; string16 keyword; bool is_keyword_hint = false; - const AutocompleteResult& result = - popup_->autocomplete_controller()->result(); + const AutocompleteResult& result = this->result(); const AutocompleteResult::const_iterator match(result.default_match()); if (match != result.end()) { if ((match->inline_autocomplete_offset != string16::npos) && @@ -758,6 +758,10 @@ void AutocompleteEditModel::Observe(NotificationType type, } } +bool AutocompleteEditModel::query_in_progress() const { + return !autocomplete_controller_->done(); +} + void AutocompleteEditModel::InternalSetUserText(const string16& text) { user_text_ = text; just_deleted_text_ = false; @@ -779,11 +783,38 @@ string16 AutocompleteEditModel::UserTextFromDisplayText( return KeywordIsSelected() ? (keyword_ + char16(' ') + text) : text; } +void AutocompleteEditModel::InfoForCurrentSelection( + AutocompleteMatch* match, + GURL* alternate_nav_url) const { + DCHECK(match != NULL); + const AutocompleteResult& result = this->result(); + if (!autocomplete_controller_->done()) { + // It's technically possible for |result| to be empty if no provider returns + // a synchronous result but the query has not completed synchronously; + // pratically, however, that should never actually happen. + if (result.empty()) + return; + // The user cannot have manually selected a match, or the query would have + // stopped. So the default match must be the desired selection. + *match = *result.default_match(); + } else { + CHECK(popup_->IsOpen()); + // If there are no results, the popup should be closed (so we should have + // failed the CHECK above), and URLsForDefaultMatch() should have been + // called instead. + CHECK(!result.empty()); + CHECK(popup_->selected_line() < result.size()); + *match = result.match_at(popup_->selected_line()); + } + if (alternate_nav_url && popup_->manually_selected_match().empty()) + *alternate_nav_url = result.alternate_nav_url(); +} + void AutocompleteEditModel::GetInfoForCurrentText( AutocompleteMatch* match, GURL* alternate_nav_url) const { if (popup_->IsOpen() || query_in_progress()) { - popup_->InfoForCurrentSelection(match, alternate_nav_url); + InfoForCurrentSelection(match, alternate_nav_url); } else { profile_->GetAutocompleteClassifier()->Classify( UserTextFromDisplayText(view_->GetText()), GetDesiredTLD(), true, diff --git a/chrome/browser/autocomplete/autocomplete_edit.h b/chrome/browser/autocomplete/autocomplete_edit.h index 8a94be4..7ca90f2 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.h +++ b/chrome/browser/autocomplete/autocomplete_edit.h @@ -6,23 +6,23 @@ #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ #pragma once +#include "base/scoped_ptr.h" #include "base/string16.h" +#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" #include "chrome/browser/autocomplete/autocomplete_match.h" -#include "chrome/common/notification_observer.h" -#include "chrome/common/notification_registrar.h" #include "chrome/common/page_transition_types.h" #include "googleurl/src/gurl.h" #include "ui/gfx/native_widget_types.h" #include "webkit/glue/window_open_disposition.h" -class AutocompletePopupModel; -class Profile; -class SkBitmap; - +class AutocompleteController; class AutocompleteEditController; class AutocompleteEditModel; class AutocompleteEditView; +class AutocompletePopupModel; class AutocompleteResult; +class Profile; +class SkBitmap; namespace gfx { class Rect; @@ -102,7 +102,7 @@ class AutocompleteEditController { virtual ~AutocompleteEditController(); }; -class AutocompleteEditModel : public NotificationObserver { +class AutocompleteEditModel : public AutocompleteControllerDelegate { public: struct State { State(bool user_input_in_progress, @@ -122,7 +122,13 @@ class AutocompleteEditModel : public NotificationObserver { Profile* profile); ~AutocompleteEditModel(); - void SetPopupModel(AutocompletePopupModel* popup_model); + AutocompleteController* autocomplete_controller() const { + return autocomplete_controller_.get(); + } + + void set_popup_model(AutocompletePopupModel* popup_model) { + popup_ = popup_model; + } // TODO: The edit and popup should be siblings owned by the LocationBarView, // making this accessor unnecessary. @@ -207,6 +213,9 @@ class AutocompleteEditModel : public NotificationObserver { void StartAutocomplete(bool has_selected_text, bool prevent_inline_autocomplete) const; + // Closes the popup and cancels any pending asynchronous queries. + void StopAutocomplete(); + // Determines whether the user can "paste and go", given the specified text. // This also updates the internal paste-and-go-related state variables as // appropriate so that the controller doesn't need to be repeatedly queried @@ -256,12 +265,6 @@ class AutocompleteEditModel : public NotificationObserver { // currently visible in the edit. void ClearKeyword(const string16& visible_text); - // Returns true if a query to an autocomplete provider is currently - // in progress. This logic should in the future live in - // AutocompleteController but resides here for now. This method is used by - // AutomationProvider::AutocompleteEditIsQueryInProgress. - bool query_in_progress() const; - // Returns the current autocomplete result. This logic should in the future // live in AutocompleteController but resides here for now. This method is // used by AutomationProvider::AutocompleteEditGetMatches. @@ -350,10 +353,14 @@ class AutocompleteEditModel : public NotificationObserver { // he intended to hit "ctrl-enter". }; - // NotificationObserver - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); + // AutocompleteControllerDelegate: + virtual void OnResultChanged(bool default_match_changed); + + // Returns true if a query to an autocomplete provider is currently + // in progress. This logic should in the future live in + // AutocompleteController but resides here for now. This method is used by + // AutomationProvider::AutocompleteEditIsQueryInProgress. + bool query_in_progress() const; // Called whenever user_text_ should change. void InternalSetUserText(const string16& text); @@ -367,6 +374,19 @@ class AutocompleteEditModel : public NotificationObserver { string16 DisplayTextFromUserText(const string16& text) const; string16 UserTextFromDisplayText(const string16& text) const; + // Copies the selected match into |match|. If an update is in progress, + // "selected" means "default in the latest matches". If there are no matches, + // does not update |match|. + // + // If |alternate_nav_url| is non-NULL, it will be set to the alternate + // navigation URL for |url| if one exists, or left unchanged otherwise. See + // comments on AutocompleteResult::GetAlternateNavURL(). + // + // TODO(pkasting): When manually_selected_match_ moves to the controller, this + // can move too. + void InfoForCurrentSelection(AutocompleteMatch* match, + GURL* alternate_nav_url) const; + // Returns the default match for the current text, as well as the alternate // nav URL, if |alternate_nav_url| is non-NULL and there is such a URL. void GetInfoForCurrentText(AutocompleteMatch* match, @@ -394,14 +414,14 @@ class AutocompleteEditModel : public NotificationObserver { // keyword. static bool IsSpaceCharForAcceptingKeyword(wchar_t c); + scoped_ptr<AutocompleteController> autocomplete_controller_; + AutocompleteEditView* view_; AutocompletePopupModel* popup_; AutocompleteEditController* controller_; - NotificationRegistrar registrar_; - // Whether the edit has focus. bool has_focus_; diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc index 0cb8fdf..ce91cf3 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc @@ -222,7 +222,7 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, NotificationRegistrar registrar; registrar.Add(this, - NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED, + NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_READY, Source<AutocompleteController>(controller)); while (!HasFailure() && !controller->done()) @@ -325,7 +325,7 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, case NotificationType::TAB_PARENTED: case NotificationType::TAB_CLOSED: case NotificationType::TEMPLATE_URL_MODEL_LOADED: - case NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED: + case NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_READY: case NotificationType::HISTORY_LOADED: case NotificationType::BOOKMARK_MODEL_LOADED: break; diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc index 33eb4ff..fd04090 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc @@ -196,8 +196,6 @@ AutocompleteEditViewGtk::AutocompleteEditViewGtk( new AutocompletePopupViewGtk #endif (GetFont(), this, model_.get(), profile, location_bar)); - - model_->SetPopupModel(popup_view_->GetModel()); } AutocompleteEditViewGtk::~AutocompleteEditViewGtk() { @@ -610,10 +608,10 @@ void AutocompleteEditViewGtk::UpdatePopup() { } void AutocompleteEditViewGtk::ClosePopup() { - if (popup_view_->GetModel()->IsOpen()) + if (model_->popup_model()->IsOpen()) controller_->OnAutocompleteWillClosePopup(); - popup_view_->GetModel()->StopAutocomplete(); + model_->StopAutocomplete(); } void AutocompleteEditViewGtk::OnTemporaryTextMaybeChanged( @@ -1183,9 +1181,8 @@ gboolean AutocompleteEditViewGtk::HandleKeyPress(GtkWidget* widget, // If shift+del didn't change the text, we let this delete an entry from // the popup. We can't check to see if the IME handled it because even if // nothing is selected, the IME or the TextView still report handling it. - AutocompletePopupModel* popup_model = popup_view_->GetModel(); - if (popup_model->IsOpen()) - popup_model->TryDeletingCurrentItem(); + if (model_->popup_model()->IsOpen()) + model_->popup_model()->TryDeletingCurrentItem(); } // Set |enter_was_pressed_| to false, to make sure OnAfterPossibleChange() can diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 759de98..5728bfd 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -426,10 +426,10 @@ void AutocompleteEditViewMac::UpdatePopup() { } void AutocompleteEditViewMac::ClosePopup() { - if (popup_view_->GetModel()->IsOpen()) + if (model_->popup_model()->IsOpen()) controller_->OnAutocompleteWillClosePopup(); - popup_view_->GetModel()->StopAutocomplete(); + model_->StopAutocomplete(); } void AutocompleteEditViewMac::SetFocus() { @@ -863,8 +863,8 @@ bool AutocompleteEditViewMac::OnDoCommandBySelector(SEL cmd) { if (cmd == @selector(deleteForward:)) { const NSUInteger modifiers = [[NSApp currentEvent] modifierFlags]; if ((modifiers & NSShiftKeyMask) != 0) { - if (popup_view_->IsOpen()) { - popup_view_->GetModel()->TryDeletingCurrentItem(); + if (model_->popup_model()->IsOpen()) { + model_->popup_model()->TryDeletingCurrentItem(); return true; } } diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_views.cc b/chrome/browser/autocomplete/autocomplete_edit_view_views.cc index 80baa0d..68e84ec2 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_views.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_views.cc @@ -119,7 +119,6 @@ AutocompleteEditViewViews::AutocompleteEditViewViews( security_level_(ToolbarModel::NONE), delete_was_pressed_(false), delete_at_end_pressed_(false) { - model_->SetPopupModel(popup_view_->GetModel()); set_border(views::Border::CreateEmptyBorder(kAutocompleteVerticalMargin, 0, kAutocompleteVerticalMargin, 0)); } @@ -184,9 +183,8 @@ bool AutocompleteEditViewViews::HandleAfterKeyEvent( // If shift+del didn't change the text, we let this delete an entry from // the popup. We can't check to see if the IME handled it because even if // nothing is selected, the IME or the TextView still report handling it. - AutocompletePopupModel* popup_model = popup_view_->GetModel(); - if (popup_model->IsOpen()) - popup_model->TryDeletingCurrentItem(); + if (model_->popup_model()->IsOpen()) + model_->popup_model()->TryDeletingCurrentItem(); } else if (!handled && event.key_code() == ui::VKEY_UP) { model_->OnUpOrDownKeyPressed(-1); handled = true; @@ -432,10 +430,10 @@ void AutocompleteEditViewViews::UpdatePopup() { } void AutocompleteEditViewViews::ClosePopup() { - if (popup_view_->GetModel()->IsOpen()) + if (model_->popup_model()->IsOpen()) controller_->OnAutocompleteWillClosePopup(); - popup_view_->GetModel()->StopAutocomplete(); + model_->StopAutocomplete(); } void AutocompleteEditViewViews::SetFocus() { diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc index fb04276..6c8b923 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc @@ -435,8 +435,6 @@ AutocompleteEditViewWin::AutocompleteEditViewWin( // import dependency on the dll. CreateTextServices(NULL, NULL, NULL); - model_->SetPopupModel(popup_view_->GetModel()); - saved_selection_for_focus_change_.cpMin = -1; g_paint_patcher.Pointer()->RefPatch(); @@ -730,10 +728,10 @@ void AutocompleteEditViewWin::UpdatePopup() { } void AutocompleteEditViewWin::ClosePopup() { - if (popup_view_->GetModel()->IsOpen()) + if (model_->popup_model()->IsOpen()) controller_->OnAutocompleteWillClosePopup(); - popup_view_->GetModel()->StopAutocomplete(); + model_->StopAutocomplete(); } void AutocompleteEditViewWin::SetFocus() { @@ -1922,13 +1920,12 @@ bool AutocompleteEditViewWin::OnKeyDownOnlyWritable(TCHAR key, Cut(); OnAfterPossibleChange(); } else { - AutocompletePopupModel* popup_model = popup_view_->GetModel(); - if (popup_model->IsOpen()) { + if (model_->popup_model()->IsOpen()) { // This is a bit overloaded, but we hijack Shift-Delete in this // case to delete the current item from the pop-up. We prefer // cutting to this when possible since that's the behavior more // people expect from Shift-Delete, and it's more commonly useful. - popup_model->TryDeletingCurrentItem(); + model_->popup_model()->TryDeletingCurrentItem(); } } } diff --git a/chrome/browser/autocomplete/autocomplete_popup_model.cc b/chrome/browser/autocomplete/autocomplete_popup_model.cc index 6809d0c..073c2da 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_model.cc +++ b/chrome/browser/autocomplete/autocomplete_popup_model.cc @@ -13,7 +13,6 @@ #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_match.h" #include "chrome/browser/autocomplete/autocomplete_popup_view.h" -#include "chrome/browser/autocomplete/search_provider.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url.h" @@ -29,47 +28,22 @@ AutocompletePopupModel::AutocompletePopupModel( Profile* profile) : view_(popup_view), edit_model_(edit_model), - controller_(new AutocompleteController(profile)), profile_(profile), hovered_line_(kNoMatch), selected_line_(kNoMatch) { + edit_model->set_popup_model(this); } AutocompletePopupModel::~AutocompletePopupModel() { } -void AutocompletePopupModel::SetProfile(Profile* profile) { - DCHECK(profile); - profile_ = profile; - controller_->SetProfile(profile); -} - -void AutocompletePopupModel::StartAutocomplete( - const string16& text, - const string16& desired_tld, - bool prevent_inline_autocomplete, - bool prefer_keyword, - bool allow_exact_keyword_match) { - // The user is interacting with the edit, so stop tracking hover. - SetHoveredLine(kNoMatch); - - manually_selected_match_.Clear(); - - controller_->Start(text, desired_tld, prevent_inline_autocomplete, - prefer_keyword, allow_exact_keyword_match, false); -} - -void AutocompletePopupModel::StopAutocomplete() { - controller_->Stop(true); -} - bool AutocompletePopupModel::IsOpen() const { return view_->IsOpen(); } void AutocompletePopupModel::SetHoveredLine(size_t line) { const bool is_disabling = (line == kNoMatch); - DCHECK(is_disabling || (line < controller_->result().size())); + DCHECK(is_disabling || (line < result().size())); if (line == hovered_line_) return; // Nothing to do @@ -88,12 +62,12 @@ void AutocompletePopupModel::SetHoveredLine(size_t line) { void AutocompletePopupModel::SetSelectedLine(size_t line, bool reset_to_default, bool force) { - const AutocompleteResult& result = controller_->result(); + const AutocompleteResult& result = this->result(); if (result.empty()) return; // Cancel the query so the matches don't change on the user. - controller_->Stop(false); + autocomplete_controller()->Stop(false); line = std::min(line, result.size() - 1); const AutocompleteMatch& match = result.match_at(line); @@ -148,39 +122,12 @@ void AutocompletePopupModel::SetSelectedLine(size_t line, } void AutocompletePopupModel::ResetToDefaultMatch() { - const AutocompleteResult& result = controller_->result(); + const AutocompleteResult& result = this->result(); CHECK(!result.empty()); SetSelectedLine(result.default_match() - result.begin(), true, false); view_->OnDragCanceled(); } -void AutocompletePopupModel::InfoForCurrentSelection( - AutocompleteMatch* match, - GURL* alternate_nav_url) const { - DCHECK(match != NULL); - const AutocompleteResult& result = controller_->result(); - if (!controller_->done()) { - // It's technically possible for |result| to be empty if no provider returns - // a synchronous result but the query has not completed synchronously; - // pratically, however, that should never actually happen. - if (result.empty()) - return; - // The user cannot have manually selected a match, or the query would have - // stopped. So the default match must be the desired selection. - *match = *result.default_match(); - } else { - CHECK(IsOpen()); - // If there are no results, the popup should be closed (so we should have - // failed the CHECK above), and URLsForDefaultMatch() should have been - // called instead. - CHECK(!result.empty()); - CHECK(selected_line_ < result.size()); - *match = result.match_at(selected_line_); - } - if (alternate_nav_url && manually_selected_match_.empty()) - *alternate_nav_url = result.alternate_nav_url(); -} - bool AutocompletePopupModel::GetKeywordForMatch(const AutocompleteMatch& match, string16* keyword) const { // Assume we have no keyword until we find otherwise. @@ -221,22 +168,8 @@ bool AutocompletePopupModel::GetKeywordForMatch(const AutocompleteMatch& match, return true; } -void AutocompletePopupModel::FinalizeInstantQuery( - const string16& input_text, - const string16& suggest_text) { - if (IsOpen()) { - SearchProvider* search_provider = controller_->search_provider(); - search_provider->FinalizeInstantQuery(input_text, suggest_text); - } -} - -AutocompleteLog* AutocompletePopupModel::GetAutocompleteLog() { - return new AutocompleteLog(controller_->input().text(), - controller_->input().type(), selected_line_, 0, controller_->result()); -} - void AutocompletePopupModel::Move(int count) { - const AutocompleteResult& result = controller_->result(); + const AutocompleteResult& result = this->result(); if (result.empty()) return; @@ -258,18 +191,17 @@ void AutocompletePopupModel::TryDeletingCurrentItem() { return; // Cancel the query so the matches don't change on the user. - controller_->Stop(false); + autocomplete_controller()->Stop(false); - const AutocompleteMatch& match = - controller_->result().match_at(selected_line_); + const AutocompleteMatch& match = result().match_at(selected_line_); if (match.deletable) { const size_t selected_line = selected_line_; const bool was_temporary_text = !manually_selected_match_.empty(); // This will synchronously notify both the edit and us that the results // have changed, causing both to revert to the default match. - controller_->DeleteMatch(match); - const AutocompleteResult& result = controller_->result(); + autocomplete_controller()->DeleteMatch(match); + const AutocompleteResult& result = this->result(); if (!result.empty() && (was_temporary_text || selected_line != selected_line_)) { // Move the selection to the next choice after the deleted one. @@ -293,7 +225,7 @@ const SkBitmap* AutocompletePopupModel::GetSpecialIconForMatch( } void AutocompletePopupModel::OnResultChanged() { - const AutocompleteResult& result = controller_->result(); + const AutocompleteResult& result = this->result(); selected_line_ = result.default_match() == result.end() ? kNoMatch : static_cast<size_t>(result.default_match() - result.begin()); // There had better not be a nonempty result set with no default match. diff --git a/chrome/browser/autocomplete/autocomplete_popup_model.h b/chrome/browser/autocomplete/autocomplete_popup_model.h index 6402ff2..ec30b1f 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_model.h +++ b/chrome/browser/autocomplete/autocomplete_popup_model.h @@ -8,14 +8,13 @@ #include "base/scoped_ptr.h" #include "chrome/browser/autocomplete/autocomplete.h" +#include "chrome/browser/autocomplete/autocomplete_edit.h" -class AutocompleteEditModel; class AutocompleteEditView; +class AutocompletePopupView; class Profile; class SkBitmap; -class AutocompletePopupView; - class AutocompletePopupModel { public: AutocompletePopupModel(AutocompletePopupView* popup_view, @@ -24,18 +23,10 @@ class AutocompletePopupModel { ~AutocompletePopupModel(); // Invoked when the profile has changed. - void SetProfile(Profile* profile); + void set_profile(Profile* profile) { profile_ = profile; } - // Starts a new query running. These parameters are passed through to the - // autocomplete controller; see comments there. - void StartAutocomplete(const string16& text, - const string16& desired_tld, - bool prevent_inline_autocomplete, - bool prefer_keyword, - bool allow_exact_keyword_match); - - // Closes the window and cancels any pending asynchronous queries. - void StopAutocomplete(); + // TODO(sky): see about removing this. + Profile* profile() const { return profile_; } // Returns true if the popup is currently open. bool IsOpen() const; @@ -44,11 +35,11 @@ class AutocompletePopupModel { // Returns the AutocompleteController used by this popup. AutocompleteController* autocomplete_controller() const { - return controller_.get(); + return edit_model_->autocomplete_controller(); } const AutocompleteResult& result() const { - return controller_->result(); + return autocomplete_controller()->result(); } size_t hovered_line() const { @@ -79,19 +70,6 @@ class AutocompletePopupModel { // will change the selected line back to the default match and redraw. void ResetToDefaultMatch(); - // Copies the selected match into |match|. If an update is in progress, - // "selected" means "default in the latest matches". If there are no matches, - // does not update |match|. - // - // If |alternate_nav_url| is non-NULL, it will be set to the alternate - // navigation URL for |url| if one exists, or left unchanged otherwise. See - // comments on AutocompleteResult::GetAlternateNavURL(). - // - // TODO(pkasting): When manually_selected_match_ moves to the controller, this - // can move too. - void InfoForCurrentSelection(AutocompleteMatch* match, - GURL* alternate_nav_url) const; - // Gets the selected keyword or keyword hint for the given match. Returns // true if |keyword| represents a keyword hint, or false if |keyword| // represents a selected keyword. (|keyword| will always be set [though @@ -100,15 +78,6 @@ class AutocompletePopupModel { bool GetKeywordForMatch(const AutocompleteMatch& match, string16* keyword) const; - // Calls through to SearchProvider::FinalizeInstantQuery. - void FinalizeInstantQuery(const string16& input_text, - const string16& suggest_text); - - // Returns a pointer to a heap-allocated AutocompleteLog containing the - // current input text, selected match, and result set. The caller is - // responsible for deleting the object. - AutocompleteLog* GetAutocompleteLog(); - // Immediately updates and opens the popup if necessary, then moves the // current selection down (|count| > 0) or up (|count| < 0), clamping to the // first or last result if necessary. If |count| == 0, the selection will be @@ -124,7 +93,10 @@ class AutocompletePopupModel { // use a standard style icon. const SkBitmap* GetSpecialIconForMatch(const AutocompleteMatch& match) const; - Profile* profile() const { return profile_; } + // The match the user has manually chosen, if any. + const AutocompleteResult::Selection& manually_selected_match() const { + return manually_selected_match_; + } // Invoked from the edit model any time the result set of the controller // changes. @@ -138,7 +110,6 @@ class AutocompletePopupModel { AutocompletePopupView* view_; AutocompleteEditModel* edit_model_; - scoped_ptr<AutocompleteController> controller_; // Profile for current tab. Profile* profile_; diff --git a/chrome/browser/autocomplete/autocomplete_popup_view.h b/chrome/browser/autocomplete/autocomplete_popup_view.h index 64faa2f..19b36ec 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view.h +++ b/chrome/browser/autocomplete/autocomplete_popup_view.h @@ -18,8 +18,6 @@ namespace gfx { class Rect; } -class AutocompletePopupModel; - class AutocompletePopupView { public: virtual ~AutocompletePopupView() {} @@ -47,9 +45,6 @@ class AutocompletePopupView { // action (e.g. releasing mouse capture). Note that this can be called when // no drag is in progress. virtual void OnDragCanceled() = 0; - - // Returns the popup's model. - virtual AutocompletePopupModel* GetModel() = 0; }; #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_VIEW_H_ diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc index 6834156..baa3375 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc +++ b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc @@ -382,10 +382,6 @@ void AutocompletePopupViewGtk::OnDragCanceled() { ignore_mouse_drag_ = true; } -AutocompletePopupModel* AutocompletePopupViewGtk::GetModel() { - return model_.get(); -} - void AutocompletePopupViewGtk::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h index 4350ae9..9c0e273 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h +++ b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h @@ -43,7 +43,6 @@ class AutocompletePopupViewGtk : public AutocompletePopupView, virtual gfx::Rect GetTargetBounds(); virtual void PaintUpdatesNow(); virtual void OnDragCanceled(); - virtual AutocompletePopupModel* GetModel(); // Overridden from NotificationObserver: virtual void Observe(NotificationType type, diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h index f05dc7a..b282f49 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h @@ -82,9 +82,6 @@ class AutocompletePopupViewMac : public AutocompletePopupView, virtual void OnDragCanceled() {} - // Returns the popup's model. - virtual AutocompletePopupModel* GetModel(); - // Opens the URL corresponding to the given |row|. If |force_background| is // true, forces the URL to open in a background tab. Otherwise, determines // the proper window open disposition from the modifier flags on |[NSApp diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index 00c31c3..3d3bd55 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -293,7 +293,6 @@ AutocompletePopupViewMac::AutocompletePopupViewMac( DCHECK(edit_view); DCHECK(edit_model); DCHECK(profile); - edit_model->SetPopupModel(model_.get()); } AutocompletePopupViewMac::~AutocompletePopupViewMac() { @@ -528,10 +527,6 @@ void AutocompletePopupViewMac::PaintUpdatesNow() { [matrix selectCellAtRow:model_->selected_line() column:0]; } -AutocompletePopupModel* AutocompletePopupViewMac::GetModel() { - return model_.get(); -} - void AutocompletePopupViewMac::OpenURLForRow(int row, bool force_background) { DCHECK_GE(row, 0); diff --git a/chrome/browser/autocomplete/autocomplete_unittest.cc b/chrome/browser/autocomplete/autocomplete_unittest.cc index cb8e719..0cc13e0 100644 --- a/chrome/browser/autocomplete/autocomplete_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_unittest.cc @@ -158,7 +158,7 @@ void AutocompleteProviderTest::ResetControllerWithTestProviders( // The providers don't complete synchronously, so listen for "result updated" // notifications. - registrar_.Add(this, NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_UPDATED, + registrar_.Add(this, NotificationType::AUTOCOMPLETE_CONTROLLER_RESULT_READY, NotificationService::AllSources()); } |