Chromium Code Reviews| Index: chrome/browser/autofill/autofill_external_delegate.cc |
| diff --git a/chrome/browser/autofill/autofill_external_delegate.cc b/chrome/browser/autofill/autofill_external_delegate.cc |
| index 1a7d2e56c42dcd5a72c4140fe6b34194719a982c..a73a424adbc26a4e78b0dc7cd8659bfa36ece249 100644 |
| --- a/chrome/browser/autofill/autofill_external_delegate.cc |
| +++ b/chrome/browser/autofill/autofill_external_delegate.cc |
| @@ -30,22 +30,16 @@ AutofillExternalDelegate::AutofillExternalDelegate( |
| tab_contents_wrapper ? tab_contents_wrapper->web_contents() : NULL), |
| autofill_query_id_(0), |
| display_warning_if_disabled_(false), |
| - has_shown_autofill_popup_for_current_edit_(false) { |
| + has_shown_autofill_popup_for_current_edit_(false), |
| + popup_visible_(false) { |
| } |
| void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id) { |
| - if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions || |
| - unique_id == WebAutofillClient::MenuItemIDClearForm || |
| - unique_id == WebAutofillClient::MenuItemIDSeparator || |
| - unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
| - return; |
| - |
| ClearPreviewedForm(); |
| - if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry) |
| - return; |
| - |
| - FillAutofillFormData(unique_id, true); |
| + // Only preview the data if it is a profile. |
| + if (unique_id > 0) |
| + FillAutofillFormData(unique_id, true); |
| } |
| void AutofillExternalDelegate::OnQuery(int query_id, |
| @@ -70,12 +64,6 @@ void AutofillExternalDelegate::OnSuggestionsReturned( |
| if (query_id != autofill_query_id_) |
| return; |
| - if (values.empty()) { |
| - // No suggestions, any popup currently showing is obsolete. |
| - HideAutofillPopup(); |
| - return; |
| - } |
| - |
| std::vector<string16> v(values); |
| std::vector<string16> l(labels); |
| std::vector<string16> i(icons); |
| @@ -87,26 +75,6 @@ void AutofillExternalDelegate::OnSuggestionsReturned( |
| i.push_back(string16()); |
| ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
| - DCHECK_GT(ids.size(), 0U); |
| - if (!autofill_query_field_.should_autocomplete) { |
| - // If autofill is disabled and we had suggestions, show a warning instead. |
| - v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
| - l.assign(1, string16()); |
| - i.assign(1, string16()); |
| - ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); |
| - } else if (ids[0] < 0 && ids.size() > 1) { |
| - // If we received a warning instead of suggestions from autofill but regular |
| - // suggestions from autocomplete, don't show the autofill warning. |
| - v.erase(v.begin()); |
| - l.erase(l.begin()); |
| - i.erase(i.begin()); |
| - ids.erase(ids.begin()); |
| - } |
| - |
| - // If we were about to show a warning and we shouldn't, don't. |
| - if (ids[0] < 0 && !display_warning_if_disabled_) |
| - return; |
|
Ilya Sherman
2012/06/04 22:05:01
What happened to this early return?
csharp
2012/06/06 14:06:52
Because we now have datalist elements that might a
|
| - |
| // Only include "Autofill Options" special menu item if we have Autofill |
| // items, identified by |unique_ids| having at least one valid value. |
| bool has_autofill_item = false; |
| @@ -117,22 +85,12 @@ void AutofillExternalDelegate::OnSuggestionsReturned( |
| } |
| } |
| - // The form has been auto-filled, so give the user the chance to clear the |
| - // form. Append the 'Clear form' menu item. |
| - if (has_autofill_item && autofill_query_field_.is_autofilled) { |
| - v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| - l.push_back(string16()); |
| - i.push_back(string16()); |
| - ids.push_back(WebAutofillClient::MenuItemIDClearForm); |
| - } |
| + ApplyAutofillWarnings(&v, &l, &i, &ids); |
|
Ilya Sherman
2012/06/04 22:05:01
This should happen before determining the value of
csharp
2012/06/06 14:06:52
Done.
|
| - if (has_autofill_item) { |
| - // Append the 'Chrome Autofill options' menu item; |
| - v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); |
| - l.push_back(string16()); |
| - i.push_back(string16()); |
| - ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| - } |
| + if (has_autofill_item) |
| + ApplyAutofillOptions(&v, &l, &i, &ids); |
| + |
| + InsertDataListValues(&v, &l, &i, &ids); |
| // Remove the separator if it is the last element. |
| if (*(ids.rbegin()) == WebAutofillClient::MenuItemIDSeparator) { |
| @@ -142,9 +100,17 @@ void AutofillExternalDelegate::OnSuggestionsReturned( |
| ids.pop_back(); |
| } |
| + if (v.empty()) { |
| + // No suggestions, any popup currently showing is obsolete. |
| + HideAutofillPopup(); |
| + return; |
| + } |
| + |
| // Send to display. |
| - if (!v.empty() && autofill_query_field_.is_focusable) |
| + if (!v.empty() && autofill_query_field_.is_focusable) { |
| + popup_visible_ = true; |
| ApplyAutofillSuggestions(v, l, i, ids); |
| + } |
| tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
| has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
| @@ -170,6 +136,22 @@ void AutofillExternalDelegate::OnShowPasswordSuggestions( |
| ApplyAutofillSuggestions(suggestions, empty, empty, password_ids); |
| } |
| +void AutofillExternalDelegate::SetCurrentDataListValues( |
| + const std::vector<string16>& data_list_values, |
| + const std::vector<string16>& data_list_labels, |
| + const std::vector<string16>& data_list_icons, |
| + const std::vector<int>& data_list_unique_ids) { |
| + // TODO(csharp): Modify the code to allow the data list values to change |
| + // even if the popup is visible. |
| + // crbug.com/131003 |
|
Ilya Sherman
2012/06/04 22:05:01
nit: Please prepend http:// to the link URL
csharp
2012/06/06 14:06:52
Done.
|
| + if (!popup_visible_) { |
| + data_list_values_ = data_list_values; |
| + data_list_labels_ = data_list_labels; |
| + data_list_icons_ = data_list_icons; |
| + data_list_unique_ids_ = data_list_unique_ids; |
| + } |
| +} |
| + |
| void AutofillExternalDelegate::RemoveAutocompleteEntry(const string16& value) { |
| if (tab_contents_wrapper_) { |
| tab_contents_wrapper_->autocomplete_history_manager()-> |
| @@ -197,23 +179,25 @@ bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( |
| if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
| return false; |
| + RenderViewHost* host = |
| + tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| + |
| if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions) { |
| // User selected 'Autofill Options'. |
| autofill_manager_->OnShowAutofillDialog(); |
| } else if (unique_id == WebAutofillClient::MenuItemIDClearForm) { |
| // User selected 'Clear form'. |
| - RenderViewHost* host = |
| - tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); |
| } else if (unique_id == WebAutofillClient::MenuItemIDPasswordEntry && |
| password_autofill_manager_.DidAcceptAutofillSuggestion( |
| autofill_query_field_, value)) { |
| // DidAcceptAutofillSuggestion has already handled the work to fill in |
| // the page as required. |
| + } else if (unique_id == WebAutofillClient::MenuItemIDDataListEntry) { |
| + host->Send(new AutofillMsg_AcceptDataListSuggestion(host->GetRoutingID(), |
| + value)); |
| } else if (unique_id == WebAutofillClient::MenuItemIDAutocompleteEntry) { |
| // User selected an Autocomplete, so we fill directly. |
| - RenderViewHost* host = |
| - tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| host->Send(new AutofillMsg_SetNodeText( |
| host->GetRoutingID(), |
| value)); |
| @@ -233,6 +217,8 @@ void AutofillExternalDelegate::ClearPreviewedForm() { |
| } |
| void AutofillExternalDelegate::HideAutofillPopup() { |
| + popup_visible_ = false; |
| + |
| HideAutofillPopupInternal(); |
| } |
| @@ -268,6 +254,96 @@ void AutofillExternalDelegate::FillAutofillFormData(int unique_id, |
| unique_id); |
| } |
| +void AutofillExternalDelegate::ApplyAutofillWarnings( |
| + std::vector<string16>* autofill_values, |
| + std::vector<string16>* autofill_labels, |
| + std::vector<string16>* autofill_icons, |
| + std::vector<int>* autofill_unique_ids) { |
| + if (!autofill_query_field_.should_autocomplete) { |
| + // If autofill is disabled and we had suggestions, show a warning instead. |
| + autofill_values->assign( |
| + 1, |
| + l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
|
Ilya Sherman
2012/06/04 22:05:01
nit: I would probably move this to previous line,
csharp
2012/06/06 14:06:52
Done.
|
| + autofill_labels->assign(1, string16()); |
| + autofill_icons->assign(1, string16()); |
| + autofill_unique_ids->assign(1, WebAutofillClient::MenuItemIDWarningMessage); |
| + } else if ((*autofill_unique_ids)[0] == |
|
Ilya Sherman
2012/06/04 22:05:01
What if the vector is empty?
csharp
2012/06/06 14:06:52
Done.
|
| + WebAutofillClient::MenuItemIDWarningMessage && |
|
Ilya Sherman
2012/06/04 22:05:01
nit: This should probably be indented two more spa
csharp
2012/06/06 14:06:52
Done.
|
| + autofill_unique_ids->size() > 1) { |
| + // If we received a warning instead of suggestions from autofill but regular |
| + // suggestions from autocomplete, don't show the autofill warning. |
| + autofill_values->erase(autofill_values->begin()); |
| + autofill_labels->erase(autofill_labels->begin()); |
| + autofill_icons->erase(autofill_icons->begin()); |
| + autofill_unique_ids->erase(autofill_unique_ids->begin()); |
| + } |
| + |
| + // If we were about to show a warning and we shouldn't, don't. |
| + if ((*autofill_unique_ids)[0] == |
|
Ilya Sherman
2012/06/04 22:05:01
What if the vector is empty?
csharp
2012/06/06 14:06:52
Done.
|
| + WebAutofillClient::MenuItemIDWarningMessage && |
|
Ilya Sherman
2012/06/04 22:05:01
nit: This should probably be indented 4 more space
csharp
2012/06/06 14:06:52
Done.
|
| + !display_warning_if_disabled_) { |
| + autofill_values->clear(); |
| + autofill_labels->clear(); |
| + autofill_icons->clear(); |
| + autofill_unique_ids->clear(); |
| + } |
| +} |
| + |
| +void AutofillExternalDelegate::ApplyAutofillOptions( |
| + std::vector<string16>* autofill_values, |
| + std::vector<string16>* autofill_labels, |
| + std::vector<string16>* autofill_icons, |
| + std::vector<int>* autofill_unique_ids) { |
| + // The form has been auto-filled, so give the user the chance to clear the |
| + // form. Append the 'Clear form' menu item. |
| + if (autofill_query_field_.is_autofilled) { |
| + autofill_values->push_back( |
| + l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| + autofill_labels->push_back(string16()); |
| + autofill_icons->push_back(string16()); |
| + autofill_unique_ids->push_back(WebAutofillClient::MenuItemIDClearForm); |
| + } |
| + |
| + // Append the 'Chrome Autofill options' menu item; |
| + autofill_values->push_back( |
| + l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); |
| + autofill_labels->push_back(string16()); |
| + autofill_icons->push_back(string16()); |
| + autofill_unique_ids->push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| +} |
| + |
| +void AutofillExternalDelegate::InsertDataListValues( |
| + std::vector<string16>* autofill_values, |
| + std::vector<string16>* autofill_labels, |
| + std::vector<string16>* autofill_icons, |
| + std::vector<int>* autofill_unique_ids) { |
| + if (!data_list_values_.empty()) { |
|
Ilya Sherman
2012/06/04 22:05:01
nit: Please write this as an early-return. This a
csharp
2012/06/06 14:06:52
Done.
|
| + // Insert the separator between the datalist and Autofill values (if there |
| + // are any). |
| + if (autofill_values->size() != 0) { |
|
Ilya Sherman
2012/06/04 22:05:01
nit: if (!autofill_values.empty())
csharp
2012/06/06 14:06:52
Done.
|
| + autofill_values->insert(autofill_values->begin(), string16()); |
| + autofill_labels->insert(autofill_labels->begin(), string16()); |
| + autofill_icons->insert(autofill_icons->begin(), string16()); |
| + autofill_unique_ids->insert(autofill_unique_ids->begin(), |
| + WebAutofillClient::MenuItemIDSeparator); |
| + } |
| + |
| + // Insert the datalist elements. |
| + autofill_values->insert(autofill_values->begin(), |
| + data_list_values_.begin(), |
| + data_list_values_.end()); |
| + autofill_labels->insert(autofill_labels->begin(), |
| + data_list_labels_.begin(), |
| + data_list_labels_.end()); |
| + autofill_icons->insert(autofill_icons->begin(), |
| + data_list_icons_.begin(), |
| + data_list_icons_.end()); |
| + autofill_unique_ids->insert(autofill_unique_ids->begin(), |
| + data_list_unique_ids_.begin(), |
| + data_list_unique_ids_.end()); |
| + } |
| +} |
| + |
| // Add a "!defined(OS_YOUROS) for each platform that implements this |
| // in an autofill_external_delegate_YOUROS.cc. Currently there are |
| // none, so all platforms use the default. |