From 95056b58863ba1fc6716ef796bb847cca8919188 Mon Sep 17 00:00:00 2001 From: "jhawkins@chromium.org" Date: Thu, 18 Feb 2010 01:29:24 +0000 Subject: Send suggestions from the AutoFillManager to the AutoFillSuggestionsPopup in WebKit. BUG=18201 TEST=none Review URL: http://codereview.chromium.org/627005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39310 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/autofill/autofill_manager.cc | 74 ++++++++++++++++++++++ chrome/browser/autofill/autofill_manager.h | 3 + chrome/browser/renderer_host/render_view_host.cc | 39 +++++++++--- chrome/browser/renderer_host/render_view_host.h | 11 +++- .../renderer_host/render_view_host_delegate.h | 10 ++- 5 files changed, 124 insertions(+), 13 deletions(-) (limited to 'chrome/browser') diff --git a/chrome/browser/autofill/autofill_manager.cc b/chrome/browser/autofill/autofill_manager.cc index bf0857d..33f7dcb0 100644 --- a/chrome/browser/autofill/autofill_manager.cc +++ b/chrome/browser/autofill/autofill_manager.cc @@ -11,10 +11,12 @@ #include "chrome/browser/autofill/autofill_infobar_delegate.h" #include "chrome/browser/autofill/form_structure.h" #include "chrome/browser/profile.h" +#include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" +#include "webkit/glue/form_field.h" #include "webkit/glue/form_field_values.h" AutoFillManager::AutoFillManager(TabContents* tab_contents) @@ -73,6 +75,78 @@ void AutoFillManager::FormsSeen( } } +bool AutoFillManager::GetAutoFillSuggestions( + int query_id, const webkit_glue::FormField& field) { + // TODO(jhawkins): Use the autofill preference. + if (!CommandLine::ForCurrentProcess()->HasSwitch( + switches::kEnableNewAutoFill)) + return false; + + RenderViewHost* host = tab_contents_->render_view_host(); + if (!host) + return false; + + const std::vector& profiles = personal_data_->profiles(); + if (profiles.empty()) + return false; + + AutoFillFieldType type = UNKNOWN_TYPE; + for (std::vector::iterator form = form_structures_.begin(); + form != form_structures_.end(); ++form) { + for (std::vector::const_iterator iter = (*form)->begin(); + iter != (*form)->end(); ++iter) { + // The field list is terminated with a NULL AutoFillField, so don't try to + // dereference it. + if (!*iter) + break; + + AutoFillField* form_field = *iter; + if (*form_field != field) + continue; + + if (form_field->possible_types().find(NAME_FIRST) != + form_field->possible_types().end() || + form_field->heuristic_type() == NAME_FIRST) { + type = NAME_FIRST; + break; + } + + if (form_field->possible_types().find(NAME_FULL) != + form_field->possible_types().end() || + form_field->heuristic_type() == NAME_FULL) { + type = NAME_FULL; + break; + } + } + } + + if (type == UNKNOWN_TYPE) + return false; + + std::vector names; + std::vector labels; + for (std::vector::const_iterator iter = profiles.begin(); + iter != profiles.end(); ++iter) { + string16 name = (*iter)->GetFieldText(AutoFillType(type)); + string16 label = (*iter)->Label(); + + // TODO(jhawkins): What if name.length() == 0? + if (StartsWith(name, field.value(), false)) { + names.push_back(name); + labels.push_back(label); + } + } + + // No suggestions. + if (names.empty()) + return false; + + // TODO(jhawkins): If the default profile is in this list, set it as the + // default suggestion index. + host->AutoFillSuggestionsReturned(query_id, names, labels, -1); + return true; +} + void AutoFillManager::OnAutoFillDialogApply( std::vector* profiles, std::vector* credit_cards) { diff --git a/chrome/browser/autofill/autofill_manager.h b/chrome/browser/autofill/autofill_manager.h index ea7ae691..818e85a 100644 --- a/chrome/browser/autofill/autofill_manager.h +++ b/chrome/browser/autofill/autofill_manager.h @@ -14,6 +14,7 @@ #include "chrome/browser/renderer_host/render_view_host_delegate.h" namespace webkit_glue { +class FormField; class FormFieldValues; } @@ -41,6 +42,8 @@ class AutoFillManager : public RenderViewHostDelegate::AutoFill, const webkit_glue::FormFieldValues& form); virtual void FormsSeen( const std::vector& forms); + virtual bool GetAutoFillSuggestions(int query_id, + const webkit_glue::FormField& field); // AutoFillDialogObserver implementation: virtual void OnAutoFillDialogApply( diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index 2c1bdf2..055a15d 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -42,6 +42,7 @@ #include "net/base/net_util.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/WebKit/WebKit/chromium/public/WebFindOptions.h" +#include "webkit/glue/form_field.h" #include "webkit/glue/form_field_values.h" #if defined(OS_WIN) @@ -1558,18 +1559,27 @@ void RenderViewHost::OnMsgShouldCloseACK(bool proceed) { } } -void RenderViewHost::OnQueryFormFieldAutofill(int query_id, - const string16& field_name, - const string16& user_text) { +void RenderViewHost::OnQueryFormFieldAutofill( + int query_id, const webkit_glue::FormField& field) { + RenderViewHostDelegate::AutoFill* autofill_delegate = + delegate_->GetAutoFillDelegate(); + // If the AutoFill delegate has results to return, we don't need any results + // from the FormFieldHistory delegate. + if (autofill_delegate && + autofill_delegate->GetAutoFillSuggestions(query_id, field)) { + return; + } + RenderViewHostDelegate::FormFieldHistory* formfield_history_delegate = delegate_->GetFormFieldHistoryDelegate(); - bool ok = false; - if (formfield_history_delegate) { - ok = formfield_history_delegate->GetFormFieldHistorySuggestions( - query_id, field_name, user_text); + if (formfield_history_delegate && + formfield_history_delegate->GetFormFieldHistorySuggestions( + query_id, field.name(), field.value())) { + return; } - if (!ok) - AutocompleteSuggestionsReturned(query_id, std::vector(), -1); + + // No suggestions provided, so send an empty vector as the results. + AutocompleteSuggestionsReturned(query_id, std::vector(), -1); } void RenderViewHost::OnRemoveAutofillEntry(const string16& field_name, @@ -1580,10 +1590,19 @@ void RenderViewHost::OnRemoveAutofillEntry(const string16& field_name, formfield_history_delegate->RemoveFormFieldHistoryEntry(field_name, value); } +void RenderViewHost::AutoFillSuggestionsReturned( + int query_id, + const std::vector& names, + const std::vector& labels, + int default_suggestion_index) { + Send(new ViewMsg_AutoFillSuggestionsReturned( + routing_id(), query_id, names, labels, default_suggestion_index)); +} + void RenderViewHost::AutocompleteSuggestionsReturned( int query_id, const std::vector& suggestions, int default_suggestion_index) { - Send(new ViewMsg_QueryFormFieldAutofill_ACK( + Send(new ViewMsg_AutocompleteSuggestionsReturned( routing_id(), query_id, suggestions, -1)); // Default index -1 means no default suggestion. } diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index 5e952d9..52453a2 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -41,6 +41,7 @@ class Point; } namespace webkit_glue { +class FormField; class FormFieldValues; struct WebApplicationInfo; } @@ -385,6 +386,13 @@ class RenderViewHost : public RenderWidgetHost { // notification. void PopupNotificationVisibilityChanged(bool visible); + // Called by the AutoFillManager when the list of suggestions is ready. + void AutoFillSuggestionsReturned( + int query_id, + const std::vector& names, + const std::vector& labels, + int default_suggestion_index); + // Called by the FormFieldHistoryManager when the list of suggestions is // ready. void AutocompleteSuggestionsReturned( @@ -578,8 +586,7 @@ class RenderViewHost : public RenderWidgetHost { const webkit_glue::WebApplicationInfo& info); void OnMsgShouldCloseACK(bool proceed); void OnQueryFormFieldAutofill(int request_id, - const string16& field_name, - const string16& user_text); + const webkit_glue::FormField& field); void OnRemoveAutofillEntry(const string16& field_name, const string16& value); diff --git a/chrome/browser/renderer_host/render_view_host_delegate.h b/chrome/browser/renderer_host/render_view_host_delegate.h index 3dd7b9a..b05c04c 100644 --- a/chrome/browser/renderer_host/render_view_host_delegate.h +++ b/chrome/browser/renderer_host/render_view_host_delegate.h @@ -51,6 +51,7 @@ class Message; } namespace webkit_glue { +class FormField; class FormFieldValues; struct PasswordForm; struct WebApplicationInfo; @@ -366,7 +367,7 @@ class RenderViewHostDelegate { // Called to retrieve a list of suggestions from the web database given // the name of the field |field_name| and what the user has already typed - // in the field |user_text|. Appeals to the database thead to perform the + // in the field |user_text|. Appeals to the database thread to perform the // query. When the database thread is finished, the FormFieldHistory manager // retrieves the calling RenderViewHost and then passes the vector of // suggestions to RenderViewHost::AutocompleteSuggestionsReturned. @@ -395,6 +396,13 @@ class RenderViewHostDelegate { // frame. virtual void FormsSeen( const std::vector& forms) = 0; + + // Called to retrieve a list of AutoFill suggestions from the web database + // given the name of the field and what the user has already typed in the + // field. Returns true to indicate that + // RenderViewHost::AutoFillSuggestionsReturned has been called. + virtual bool GetAutoFillSuggestions( + int query_id, const webkit_glue::FormField& field) = 0; }; // --------------------------------------------------------------------------- -- cgit v1.1