| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/browser/autocomplete_history_manager.h" |
| 6 #include "chrome/browser/autofill/autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 7 #include "chrome/browser/autofill/autofill_manager.h" | 8 #include "chrome/browser/autofill/autofill_manager.h" |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 9 #include "chrome/common/autofill_messages.h" | 10 #include "chrome/common/autofill_messages.h" |
| 10 #include "chrome/common/chrome_constants.h" | 11 #include "chrome/common/chrome_constants.h" |
| 11 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 13 #include "grit/chromium_strings.h" | 14 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 17 | 18 |
| 18 using content::RenderViewHost; | 19 using content::RenderViewHost; |
| 19 using WebKit::WebAutofillClient; | 20 using WebKit::WebAutofillClient; |
| 20 | 21 |
| 21 AutofillExternalDelegate::~AutofillExternalDelegate() { | 22 AutofillExternalDelegate::~AutofillExternalDelegate() { |
| 22 } | 23 } |
| 23 | 24 |
| 24 AutofillExternalDelegate::AutofillExternalDelegate( | 25 AutofillExternalDelegate::AutofillExternalDelegate( |
| 25 TabContentsWrapper* tab_contents_wrapper, | 26 TabContentsWrapper* tab_contents_wrapper, |
| 26 AutofillManager* autofill_manager) | 27 AutofillManager* autofill_manager) |
| 27 : tab_contents_wrapper_(tab_contents_wrapper), | 28 : tab_contents_wrapper_(tab_contents_wrapper), |
| 28 autofill_manager_(autofill_manager), | 29 autofill_manager_(autofill_manager), |
| 29 password_autofill_manager_( | 30 password_autofill_manager_( |
| 30 tab_contents_wrapper ? tab_contents_wrapper->web_contents() : NULL), | 31 tab_contents_wrapper ? tab_contents_wrapper->web_contents() : NULL), |
| 31 autofill_query_id_(0), | 32 autofill_query_id_(0), |
| 32 display_warning_if_disabled_(false), | 33 display_warning_if_disabled_(false), |
| 33 has_shown_autofill_popup_for_current_edit_(false), | 34 has_shown_autofill_popup_for_current_edit_(false) { |
| 34 suggestions_clear_index_(-1), | |
| 35 suggestions_options_index_(-1) { | |
| 36 } | 35 } |
| 37 | 36 |
| 38 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, | 37 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id) { |
| 39 int list_index) { | |
| 40 if (password_autofill_manager_.DidSelectAutofillSuggestion( | 38 if (password_autofill_manager_.DidSelectAutofillSuggestion( |
| 41 autofill_query_field_)) | 39 autofill_query_field_)) |
| 42 return; | 40 return; |
| 43 | 41 |
| 44 if (list_index == suggestions_options_index_ || | 42 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions || |
| 45 list_index == suggestions_clear_index_ || | 43 unique_id == WebAutofillClient::MenuItemIDClearForm || |
| 46 unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 44 unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
| 47 return; | 45 return; |
| 48 | 46 |
| 49 FillAutofillFormData(unique_id, true); | 47 FillAutofillFormData(unique_id, true); |
| 50 } | 48 } |
| 51 | 49 |
| 52 void AutofillExternalDelegate::OnQuery(int query_id, | 50 void AutofillExternalDelegate::OnQuery(int query_id, |
| 53 const webkit::forms::FormData& form, | 51 const webkit::forms::FormData& form, |
| 54 const webkit::forms::FormField& field, | 52 const webkit::forms::FormField& field, |
| 55 const gfx::Rect& bounds, | 53 const gfx::Rect& bounds, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 74 if (values.empty()) { | 72 if (values.empty()) { |
| 75 // No suggestions, any popup currently showing is obsolete. | 73 // No suggestions, any popup currently showing is obsolete. |
| 76 HideAutofillPopup(); | 74 HideAutofillPopup(); |
| 77 return; | 75 return; |
| 78 } | 76 } |
| 79 | 77 |
| 80 std::vector<string16> v(values); | 78 std::vector<string16> v(values); |
| 81 std::vector<string16> l(labels); | 79 std::vector<string16> l(labels); |
| 82 std::vector<string16> i(icons); | 80 std::vector<string16> i(icons); |
| 83 std::vector<int> ids(unique_ids); | 81 std::vector<int> ids(unique_ids); |
| 84 int separator_index = -1; | |
| 85 | 82 |
| 86 DCHECK_GT(ids.size(), 0U); | 83 DCHECK_GT(ids.size(), 0U); |
| 87 if (!autofill_query_field_.should_autocomplete) { | 84 if (!autofill_query_field_.should_autocomplete) { |
| 88 // If autofill is disabled and we had suggestions, show a warning instead. | 85 // If autofill is disabled and we had suggestions, show a warning instead. |
| 89 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 86 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
| 90 l.assign(1, string16()); | 87 l.assign(1, string16()); |
| 91 i.assign(1, string16()); | 88 i.assign(1, string16()); |
| 92 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); | 89 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); |
| 93 } else if (ids[0] < 0 && ids.size() > 1) { | 90 } else if (ids[0] < 0 && ids.size() > 1) { |
| 94 // If we received a warning instead of suggestions from autofill but regular | 91 // If we received a warning instead of suggestions from autofill but regular |
| (...skipping 13 matching lines...) Expand all Loading... |
| 108 bool has_autofill_item = false; | 105 bool has_autofill_item = false; |
| 109 for (size_t i = 0; i < ids.size(); ++i) { | 106 for (size_t i = 0; i < ids.size(); ++i) { |
| 110 if (ids[i] > 0) { | 107 if (ids[i] > 0) { |
| 111 has_autofill_item = true; | 108 has_autofill_item = true; |
| 112 break; | 109 break; |
| 113 } | 110 } |
| 114 } | 111 } |
| 115 | 112 |
| 116 // The form has been auto-filled, so give the user the chance to clear the | 113 // The form has been auto-filled, so give the user the chance to clear the |
| 117 // form. Append the 'Clear form' menu item. | 114 // form. Append the 'Clear form' menu item. |
| 118 if (has_autofill_item && | 115 if (has_autofill_item && autofill_query_field_.is_autofilled) { |
| 119 autofill_query_field_.is_autofilled) { | |
| 120 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); | 116 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| 121 l.push_back(string16()); | 117 l.push_back(string16()); |
| 122 i.push_back(string16()); | 118 i.push_back(string16()); |
| 123 ids.push_back(0); | 119 ids.push_back(WebAutofillClient::MenuItemIDClearForm); |
| 124 suggestions_clear_index_ = v.size() - 1; | |
| 125 separator_index = v.size() - 1; | |
| 126 } | 120 } |
| 127 | 121 |
| 128 if (has_autofill_item) { | 122 if (has_autofill_item) { |
| 129 // Append the 'Chrome Autofill options' menu item; | 123 // Append the 'Chrome Autofill options' menu item; |
| 130 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); | 124 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); |
| 131 l.push_back(string16()); | 125 l.push_back(string16()); |
| 132 i.push_back(string16()); | 126 i.push_back(string16()); |
| 133 ids.push_back(0); | 127 ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 134 suggestions_options_index_ = v.size() - 1; | |
| 135 separator_index = values.size(); | |
| 136 } | 128 } |
| 137 | 129 |
| 138 // Send to display. | 130 // Send to display. |
| 139 if (!v.empty() && autofill_query_field_.is_focusable) | 131 if (!v.empty() && autofill_query_field_.is_focusable) |
| 140 ApplyAutofillSuggestions(v, l, i, ids, separator_index); | 132 ApplyAutofillSuggestions(v, l, i, ids); |
| 141 | 133 |
| 142 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( | 134 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
| 143 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); | 135 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
| 144 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 136 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
| 145 } | 137 } |
| 146 | 138 |
| 147 void AutofillExternalDelegate::OnShowPasswordSuggestions( | 139 void AutofillExternalDelegate::OnShowPasswordSuggestions( |
| 148 const std::vector<string16>& suggestions, | 140 const std::vector<string16>& suggestions, |
| 149 const webkit::forms::FormField& field, | 141 const webkit::forms::FormField& field, |
| 150 const gfx::Rect& bounds) { | 142 const gfx::Rect& bounds) { |
| 151 autofill_query_field_ = field; | 143 autofill_query_field_ = field; |
| 152 | 144 |
| 153 if (suggestions.empty()) { | 145 if (suggestions.empty()) { |
| 154 HideAutofillPopup(); | 146 HideAutofillPopup(); |
| 155 return; | 147 return; |
| 156 } | 148 } |
| 157 | 149 |
| 158 SetBounds(bounds); | 150 SetBounds(bounds); |
| 159 | 151 |
| 160 std::vector<string16> empty(suggestions.size()); | 152 std::vector<string16> empty(suggestions.size()); |
| 161 std::vector<int> password_ids(suggestions.size(), | 153 std::vector<int> password_ids(suggestions.size(), |
| 162 WebAutofillClient::MenuItemIDPasswordEntry); | 154 WebAutofillClient::MenuItemIDPasswordEntry); |
| 163 ApplyAutofillSuggestions(suggestions, empty, empty, password_ids, -1); | 155 ApplyAutofillSuggestions(suggestions, empty, empty, password_ids); |
| 164 } | 156 } |
| 165 | 157 |
| 158 void AutofillExternalDelegate::RemoveAutocompleteEntry(const string16& value) { |
| 159 if (tab_contents_wrapper_) { |
| 160 tab_contents_wrapper_->autocomplete_history_manager()-> |
| 161 OnRemoveAutocompleteEntry(autofill_query_field_.name, value); |
| 162 } |
| 163 } |
| 164 |
| 165 void AutofillExternalDelegate::RemoveAutofillProfileOrCreditCard( |
| 166 int unique_id) { |
| 167 autofill_manager_->RemoveAutofillProfileOrCreditCard(unique_id); |
| 168 } |
| 169 |
| 170 |
| 166 void AutofillExternalDelegate::DidEndTextFieldEditing() { | 171 void AutofillExternalDelegate::DidEndTextFieldEditing() { |
| 167 has_shown_autofill_popup_for_current_edit_ = false; | 172 has_shown_autofill_popup_for_current_edit_ = false; |
| 168 } | 173 } |
| 169 | 174 |
| 170 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( | 175 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( |
| 171 const string16& value, | 176 const string16& value, |
| 172 int unique_id, | 177 int unique_id, |
| 173 unsigned index) { | 178 unsigned index) { |
| 174 // If the selected element is a warning we don't want to do anything. | 179 // If the selected element is a warning we don't want to do anything. |
| 175 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 180 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) |
| 176 return false; | 181 return false; |
| 177 | 182 |
| 178 if (suggestions_options_index_ != -1 && | 183 if (unique_id == WebAutofillClient::MenuItemIDAutofillOptions) { |
| 179 index == static_cast<unsigned>(suggestions_options_index_)) { | |
| 180 // User selected 'Autofill Options'. | 184 // User selected 'Autofill Options'. |
| 181 autofill_manager_->OnShowAutofillDialog(); | 185 autofill_manager_->OnShowAutofillDialog(); |
| 182 } else if (suggestions_clear_index_ != -1 && | 186 } else if (unique_id == WebAutofillClient::MenuItemIDClearForm) { |
| 183 index == static_cast<unsigned>(suggestions_clear_index_)) { | |
| 184 // User selected 'Clear form'. | 187 // User selected 'Clear form'. |
| 185 RenderViewHost* host = | 188 RenderViewHost* host = |
| 186 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 189 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| 187 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); | 190 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); |
| 188 } else if (password_autofill_manager_.DidAcceptAutofillSuggestion( | 191 } else if (password_autofill_manager_.DidAcceptAutofillSuggestion( |
| 189 autofill_query_field_, value)) { | 192 autofill_query_field_, value)) { |
| 190 // DidAcceptAutofillSuggestion has already handled the work to fill in | 193 // DidAcceptAutofillSuggestion has already handled the work to fill in |
| 191 // the page as required. | 194 // the page as required. |
| 192 } else if (!unique_id) { | 195 } else if (unique_id == WebAutofillClient::MenuItemIDAutocompleteEntry) { |
| 193 // User selected an Autocomplete, so we fill directly. | 196 // User selected an Autocomplete, so we fill directly. |
| 194 RenderViewHost* host = | 197 RenderViewHost* host = |
| 195 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 198 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| 196 host->Send(new AutofillMsg_SetNodeText( | 199 host->Send(new AutofillMsg_SetNodeText( |
| 197 host->GetRoutingID(), | 200 host->GetRoutingID(), |
| 198 value)); | 201 value)); |
| 199 } else { | 202 } else { |
| 200 FillAutofillFormData(unique_id, false); | 203 FillAutofillFormData(unique_id, false); |
| 201 } | 204 } |
| 202 | 205 |
| 203 HideAutofillPopup(); | 206 HideAutofillPopup(); |
| 204 | 207 |
| 205 return true; | 208 return true; |
| 206 } | 209 } |
| 207 | 210 |
| 208 void AutofillExternalDelegate::ClearPreviewedForm() { | 211 void AutofillExternalDelegate::ClearPreviewedForm() { |
| 209 if (password_autofill_manager_.DidClearAutofillSelection( | 212 if (password_autofill_manager_.DidClearAutofillSelection( |
| 210 autofill_query_field_)) | 213 autofill_query_field_)) |
| 211 return; | 214 return; |
| 212 | 215 |
| 213 RenderViewHost* host = | 216 RenderViewHost* host = |
| 214 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 217 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
| 215 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); | 218 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); |
| 216 } | 219 } |
| 217 | 220 |
| 218 void AutofillExternalDelegate::HideAutofillPopup() { | 221 void AutofillExternalDelegate::HideAutofillPopup() { |
| 219 suggestions_clear_index_ = -1; | |
| 220 suggestions_options_index_ = -1; | |
| 221 | |
| 222 HideAutofillPopupInternal(); | 222 HideAutofillPopupInternal(); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void AutofillExternalDelegate::Reset() { | 225 void AutofillExternalDelegate::Reset() { |
| 226 HideAutofillPopup(); | 226 HideAutofillPopup(); |
| 227 | 227 |
| 228 password_autofill_manager_.Reset(); | 228 password_autofill_manager_.Reset(); |
| 229 } | 229 } |
| 230 | 230 |
| 231 void AutofillExternalDelegate::AddPasswordFormMapping( | 231 void AutofillExternalDelegate::AddPasswordFormMapping( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 259 // none, so all platforms use the default. | 259 // none, so all platforms use the default. |
| 260 | 260 |
| 261 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) | 261 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) |
| 262 | 262 |
| 263 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 263 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
| 264 TabContentsWrapper*, AutofillManager*) { | 264 TabContentsWrapper*, AutofillManager*) { |
| 265 return NULL; | 265 return NULL; |
| 266 } | 266 } |
| 267 | 267 |
| 268 #endif | 268 #endif |
| OLD | NEW |