| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autofill_external_delegate.h" | 5 #include "components/autofill/core/browser/autofill_external_delegate.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "components/autofill/core/browser/autocomplete_history_manager.h" | 8 #include "components/autofill/core/browser/autocomplete_history_manager.h" |
| 9 #include "components/autofill/core/browser/autofill_driver.h" | 9 #include "components/autofill/core/browser/autofill_driver.h" |
| 10 #include "components/autofill/core/browser/autofill_manager.h" | 10 #include "components/autofill/core/browser/autofill_manager.h" |
| 11 #include "components/autofill/core/browser/popup_item_ids.h" |
| 11 #include "grit/component_strings.h" | 12 #include "grit/component_strings.h" |
| 12 #include "third_party/WebKit/public/web/WebAutofillClient.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 14 | 14 |
| 15 using blink::WebAutofillClient; | |
| 16 | |
| 17 namespace autofill { | 15 namespace autofill { |
| 18 | 16 |
| 19 AutofillExternalDelegate::AutofillExternalDelegate( | 17 AutofillExternalDelegate::AutofillExternalDelegate( |
| 20 AutofillManager* autofill_manager, | 18 AutofillManager* autofill_manager, |
| 21 AutofillDriver* autofill_driver) | 19 AutofillDriver* autofill_driver) |
| 22 : autofill_manager_(autofill_manager), | 20 : autofill_manager_(autofill_manager), |
| 23 autofill_driver_(autofill_driver), | 21 autofill_driver_(autofill_driver), |
| 24 password_autofill_manager_(autofill_driver), | 22 password_autofill_manager_(autofill_driver), |
| 25 autofill_query_id_(0), | 23 autofill_query_id_(0), |
| 26 display_warning_if_disabled_(false), | 24 display_warning_if_disabled_(false), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 std::vector<base::string16> icons(autofill_icons); | 56 std::vector<base::string16> icons(autofill_icons); |
| 59 std::vector<int> ids(autofill_unique_ids); | 57 std::vector<int> ids(autofill_unique_ids); |
| 60 | 58 |
| 61 // Add or hide warnings as appropriate. | 59 // Add or hide warnings as appropriate. |
| 62 ApplyAutofillWarnings(&values, &labels, &icons, &ids); | 60 ApplyAutofillWarnings(&values, &labels, &icons, &ids); |
| 63 | 61 |
| 64 // Add a separator to go between the values and menu items. | 62 // Add a separator to go between the values and menu items. |
| 65 values.push_back(base::string16()); | 63 values.push_back(base::string16()); |
| 66 labels.push_back(base::string16()); | 64 labels.push_back(base::string16()); |
| 67 icons.push_back(base::string16()); | 65 icons.push_back(base::string16()); |
| 68 ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 66 ids.push_back(POPUP_ITEM_ID_SEPARATOR); |
| 69 | 67 |
| 70 // Only include "Autofill Options" special menu item if we have Autofill | 68 // Only include "Autofill Options" special menu item if we have Autofill |
| 71 // suggestions. | 69 // suggestions. |
| 72 has_autofill_suggestion_ = false; | 70 has_autofill_suggestion_ = false; |
| 73 for (size_t i = 0; i < ids.size(); ++i) { | 71 for (size_t i = 0; i < ids.size(); ++i) { |
| 74 if (ids[i] > 0) { | 72 if (ids[i] > 0) { |
| 75 has_autofill_suggestion_ = true; | 73 has_autofill_suggestion_ = true; |
| 76 break; | 74 break; |
| 77 } | 75 } |
| 78 } | 76 } |
| 79 | 77 |
| 80 if (has_autofill_suggestion_) | 78 if (has_autofill_suggestion_) |
| 81 ApplyAutofillOptions(&values, &labels, &icons, &ids); | 79 ApplyAutofillOptions(&values, &labels, &icons, &ids); |
| 82 | 80 |
| 83 // Remove the separator if it is the last element. | 81 // Remove the separator if it is the last element. |
| 84 DCHECK_GT(ids.size(), 0U); | 82 DCHECK_GT(ids.size(), 0U); |
| 85 if (ids.back() == WebAutofillClient::MenuItemIDSeparator) { | 83 if (ids.back() == POPUP_ITEM_ID_SEPARATOR) { |
| 86 values.pop_back(); | 84 values.pop_back(); |
| 87 labels.pop_back(); | 85 labels.pop_back(); |
| 88 icons.pop_back(); | 86 icons.pop_back(); |
| 89 ids.pop_back(); | 87 ids.pop_back(); |
| 90 } | 88 } |
| 91 | 89 |
| 92 // If anything else is added to modify the values after inserting the data | 90 // If anything else is added to modify the values after inserting the data |
| 93 // list, AutofillPopupControllerImpl::UpdateDataListValues will need to be | 91 // list, AutofillPopupControllerImpl::UpdateDataListValues will need to be |
| 94 // updated to match. | 92 // updated to match. |
| 95 InsertDataListValues(&values, &labels, &icons, &ids); | 93 InsertDataListValues(&values, &labels, &icons, &ids); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 121 autofill_query_field_ = field; | 119 autofill_query_field_ = field; |
| 122 element_bounds_ = element_bounds; | 120 element_bounds_ = element_bounds; |
| 123 | 121 |
| 124 if (suggestions.empty()) { | 122 if (suggestions.empty()) { |
| 125 autofill_manager_->delegate()->HideAutofillPopup(); | 123 autofill_manager_->delegate()->HideAutofillPopup(); |
| 126 return; | 124 return; |
| 127 } | 125 } |
| 128 | 126 |
| 129 std::vector<base::string16> empty(suggestions.size()); | 127 std::vector<base::string16> empty(suggestions.size()); |
| 130 std::vector<int> password_ids(suggestions.size(), | 128 std::vector<int> password_ids(suggestions.size(), |
| 131 WebAutofillClient::MenuItemIDPasswordEntry); | 129 POPUP_ITEM_ID_PASSWORD_ENTRY); |
| 132 autofill_manager_->delegate()->ShowAutofillPopup( | 130 autofill_manager_->delegate()->ShowAutofillPopup( |
| 133 element_bounds_, | 131 element_bounds_, |
| 134 autofill_query_field_.text_direction, | 132 autofill_query_field_.text_direction, |
| 135 suggestions, | 133 suggestions, |
| 136 realms, | 134 realms, |
| 137 empty, | 135 empty, |
| 138 password_ids, | 136 password_ids, |
| 139 GetWeakPtr()); | 137 GetWeakPtr()); |
| 140 } | 138 } |
| 141 | 139 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 167 void AutofillExternalDelegate::DidSelectSuggestion(int identifier) { | 165 void AutofillExternalDelegate::DidSelectSuggestion(int identifier) { |
| 168 ClearPreviewedForm(); | 166 ClearPreviewedForm(); |
| 169 | 167 |
| 170 // Only preview the data if it is a profile. | 168 // Only preview the data if it is a profile. |
| 171 if (identifier > 0) | 169 if (identifier > 0) |
| 172 FillAutofillFormData(identifier, true); | 170 FillAutofillFormData(identifier, true); |
| 173 } | 171 } |
| 174 | 172 |
| 175 void AutofillExternalDelegate::DidAcceptSuggestion(const base::string16& value, | 173 void AutofillExternalDelegate::DidAcceptSuggestion(const base::string16& value, |
| 176 int identifier) { | 174 int identifier) { |
| 177 if (identifier == WebAutofillClient::MenuItemIDAutofillOptions) { | 175 if (identifier == POPUP_ITEM_ID_AUTOFILL_OPTIONS) { |
| 178 // User selected 'Autofill Options'. | 176 // User selected 'Autofill Options'. |
| 179 autofill_manager_->ShowAutofillSettings(); | 177 autofill_manager_->ShowAutofillSettings(); |
| 180 } else if (identifier == WebAutofillClient::MenuItemIDClearForm) { | 178 } else if (identifier == POPUP_ITEM_ID_CLEAR_FORM) { |
| 181 // User selected 'Clear form'. | 179 // User selected 'Clear form'. |
| 182 autofill_driver_->RendererShouldClearFilledForm(); | 180 autofill_driver_->RendererShouldClearFilledForm(); |
| 183 } else if (identifier == WebAutofillClient::MenuItemIDPasswordEntry) { | 181 } else if (identifier == POPUP_ITEM_ID_PASSWORD_ENTRY) { |
| 184 bool success = password_autofill_manager_.DidAcceptAutofillSuggestion( | 182 bool success = password_autofill_manager_.DidAcceptAutofillSuggestion( |
| 185 autofill_query_field_, value); | 183 autofill_query_field_, value); |
| 186 DCHECK(success); | 184 DCHECK(success); |
| 187 } else if (identifier == WebAutofillClient::MenuItemIDDataListEntry) { | 185 } else if (identifier == POPUP_ITEM_ID_DATALIST_ENTRY) { |
| 188 autofill_driver_->RendererShouldAcceptDataListSuggestion(value); | 186 autofill_driver_->RendererShouldAcceptDataListSuggestion(value); |
| 189 } else if (identifier == WebAutofillClient::MenuItemIDAutocompleteEntry) { | 187 } else if (identifier == POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY) { |
| 190 // User selected an Autocomplete, so we fill directly. | 188 // User selected an Autocomplete, so we fill directly. |
| 191 autofill_driver_->RendererShouldSetNodeText(value); | 189 autofill_driver_->RendererShouldSetNodeText(value); |
| 192 } else { | 190 } else { |
| 193 FillAutofillFormData(identifier, false); | 191 FillAutofillFormData(identifier, false); |
| 194 } | 192 } |
| 195 | 193 |
| 196 autofill_manager_->delegate()->HideAutofillPopup(); | 194 autofill_manager_->delegate()->HideAutofillPopup(); |
| 197 } | 195 } |
| 198 | 196 |
| 199 void AutofillExternalDelegate::RemoveSuggestion(const base::string16& value, | 197 void AutofillExternalDelegate::RemoveSuggestion(const base::string16& value, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 228 password_autofill_manager_.AddPasswordFormMapping(username_field, fill_data); | 226 password_autofill_manager_.AddPasswordFormMapping(username_field, fill_data); |
| 229 } | 227 } |
| 230 | 228 |
| 231 base::WeakPtr<AutofillExternalDelegate> AutofillExternalDelegate::GetWeakPtr() { | 229 base::WeakPtr<AutofillExternalDelegate> AutofillExternalDelegate::GetWeakPtr() { |
| 232 return weak_ptr_factory_.GetWeakPtr(); | 230 return weak_ptr_factory_.GetWeakPtr(); |
| 233 } | 231 } |
| 234 | 232 |
| 235 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, | 233 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, |
| 236 bool is_preview) { | 234 bool is_preview) { |
| 237 // If the selected element is a warning we don't want to do anything. | 235 // If the selected element is a warning we don't want to do anything. |
| 238 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 236 if (unique_id == POPUP_ITEM_ID_WARNING_MESSAGE) |
| 239 return; | 237 return; |
| 240 | 238 |
| 241 AutofillDriver::RendererFormDataAction renderer_action = is_preview ? | 239 AutofillDriver::RendererFormDataAction renderer_action = is_preview ? |
| 242 AutofillDriver::FORM_DATA_ACTION_PREVIEW : | 240 AutofillDriver::FORM_DATA_ACTION_PREVIEW : |
| 243 AutofillDriver::FORM_DATA_ACTION_FILL; | 241 AutofillDriver::FORM_DATA_ACTION_FILL; |
| 244 | 242 |
| 245 DCHECK(autofill_driver_->RendererIsAvailable()); | 243 DCHECK(autofill_driver_->RendererIsAvailable()); |
| 246 autofill_driver_->SetRendererActionOnFormDataReception(renderer_action); | 244 autofill_driver_->SetRendererActionOnFormDataReception(renderer_action); |
| 247 // Fill the values for the whole form. | 245 // Fill the values for the whole form. |
| 248 autofill_manager_->OnFillAutofillFormData(autofill_query_id_, | 246 autofill_manager_->OnFillAutofillFormData(autofill_query_id_, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 259 if (!autofill_query_field_.should_autocomplete) { | 257 if (!autofill_query_field_.should_autocomplete) { |
| 260 // Autofill is disabled. If there were some profile or credit card | 258 // Autofill is disabled. If there were some profile or credit card |
| 261 // suggestions to show, show a warning instead. Otherwise, clear out the | 259 // suggestions to show, show a warning instead. Otherwise, clear out the |
| 262 // list of suggestions. | 260 // list of suggestions. |
| 263 if (!autofill_unique_ids->empty() && (*autofill_unique_ids)[0] > 0) { | 261 if (!autofill_unique_ids->empty() && (*autofill_unique_ids)[0] > 0) { |
| 264 // If autofill is disabled and we had suggestions, show a warning instead. | 262 // If autofill is disabled and we had suggestions, show a warning instead. |
| 265 autofill_values->assign( | 263 autofill_values->assign( |
| 266 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 264 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
| 267 autofill_labels->assign(1, base::string16()); | 265 autofill_labels->assign(1, base::string16()); |
| 268 autofill_icons->assign(1, base::string16()); | 266 autofill_icons->assign(1, base::string16()); |
| 269 autofill_unique_ids->assign(1, | 267 autofill_unique_ids->assign(1, POPUP_ITEM_ID_WARNING_MESSAGE); |
| 270 WebAutofillClient::MenuItemIDWarningMessage); | |
| 271 } else { | 268 } else { |
| 272 autofill_values->clear(); | 269 autofill_values->clear(); |
| 273 autofill_labels->clear(); | 270 autofill_labels->clear(); |
| 274 autofill_icons->clear(); | 271 autofill_icons->clear(); |
| 275 autofill_unique_ids->clear(); | 272 autofill_unique_ids->clear(); |
| 276 } | 273 } |
| 277 } else if (autofill_unique_ids->size() > 1 && | 274 } else if (autofill_unique_ids->size() > 1 && |
| 278 (*autofill_unique_ids)[0] == | 275 (*autofill_unique_ids)[0] == POPUP_ITEM_ID_WARNING_MESSAGE) { |
| 279 WebAutofillClient::MenuItemIDWarningMessage) { | |
| 280 // If we received a warning instead of suggestions from autofill but regular | 276 // If we received a warning instead of suggestions from autofill but regular |
| 281 // suggestions from autocomplete, don't show the autofill warning. | 277 // suggestions from autocomplete, don't show the autofill warning. |
| 282 autofill_values->erase(autofill_values->begin()); | 278 autofill_values->erase(autofill_values->begin()); |
| 283 autofill_labels->erase(autofill_labels->begin()); | 279 autofill_labels->erase(autofill_labels->begin()); |
| 284 autofill_icons->erase(autofill_icons->begin()); | 280 autofill_icons->erase(autofill_icons->begin()); |
| 285 autofill_unique_ids->erase(autofill_unique_ids->begin()); | 281 autofill_unique_ids->erase(autofill_unique_ids->begin()); |
| 286 } | 282 } |
| 287 | 283 |
| 288 // If we were about to show a warning and we shouldn't, don't. | 284 // If we were about to show a warning and we shouldn't, don't. |
| 289 if (!autofill_unique_ids->empty() && | 285 if (!autofill_unique_ids->empty() && |
| 290 (*autofill_unique_ids)[0] == | 286 (*autofill_unique_ids)[0] == POPUP_ITEM_ID_WARNING_MESSAGE && |
| 291 WebAutofillClient::MenuItemIDWarningMessage && | |
| 292 !display_warning_if_disabled_) { | 287 !display_warning_if_disabled_) { |
| 293 autofill_values->clear(); | 288 autofill_values->clear(); |
| 294 autofill_labels->clear(); | 289 autofill_labels->clear(); |
| 295 autofill_icons->clear(); | 290 autofill_icons->clear(); |
| 296 autofill_unique_ids->clear(); | 291 autofill_unique_ids->clear(); |
| 297 } | 292 } |
| 298 } | 293 } |
| 299 | 294 |
| 300 void AutofillExternalDelegate::ApplyAutofillOptions( | 295 void AutofillExternalDelegate::ApplyAutofillOptions( |
| 301 std::vector<base::string16>* autofill_values, | 296 std::vector<base::string16>* autofill_values, |
| 302 std::vector<base::string16>* autofill_labels, | 297 std::vector<base::string16>* autofill_labels, |
| 303 std::vector<base::string16>* autofill_icons, | 298 std::vector<base::string16>* autofill_icons, |
| 304 std::vector<int>* autofill_unique_ids) { | 299 std::vector<int>* autofill_unique_ids) { |
| 305 // The form has been auto-filled, so give the user the chance to clear the | 300 // The form has been auto-filled, so give the user the chance to clear the |
| 306 // form. Append the 'Clear form' menu item. | 301 // form. Append the 'Clear form' menu item. |
| 307 if (autofill_query_field_.is_autofilled) { | 302 if (autofill_query_field_.is_autofilled) { |
| 308 autofill_values->push_back( | 303 autofill_values->push_back( |
| 309 l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); | 304 l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| 310 autofill_labels->push_back(base::string16()); | 305 autofill_labels->push_back(base::string16()); |
| 311 autofill_icons->push_back(base::string16()); | 306 autofill_icons->push_back(base::string16()); |
| 312 autofill_unique_ids->push_back(WebAutofillClient::MenuItemIDClearForm); | 307 autofill_unique_ids->push_back(POPUP_ITEM_ID_CLEAR_FORM); |
| 313 } | 308 } |
| 314 | 309 |
| 315 // Append the 'Chrome Autofill options' menu item; | 310 // Append the 'Chrome Autofill options' menu item; |
| 316 autofill_values->push_back( | 311 autofill_values->push_back( |
| 317 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); | 312 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); |
| 318 autofill_labels->push_back(base::string16()); | 313 autofill_labels->push_back(base::string16()); |
| 319 autofill_icons->push_back(base::string16()); | 314 autofill_icons->push_back(base::string16()); |
| 320 autofill_unique_ids->push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 315 autofill_unique_ids->push_back(POPUP_ITEM_ID_AUTOFILL_OPTIONS); |
| 321 } | 316 } |
| 322 | 317 |
| 323 void AutofillExternalDelegate::InsertDataListValues( | 318 void AutofillExternalDelegate::InsertDataListValues( |
| 324 std::vector<base::string16>* autofill_values, | 319 std::vector<base::string16>* autofill_values, |
| 325 std::vector<base::string16>* autofill_labels, | 320 std::vector<base::string16>* autofill_labels, |
| 326 std::vector<base::string16>* autofill_icons, | 321 std::vector<base::string16>* autofill_icons, |
| 327 std::vector<int>* autofill_unique_ids) { | 322 std::vector<int>* autofill_unique_ids) { |
| 328 if (data_list_values_.empty()) | 323 if (data_list_values_.empty()) |
| 329 return; | 324 return; |
| 330 | 325 |
| 331 // Insert the separator between the datalist and Autofill values (if there | 326 // Insert the separator between the datalist and Autofill values (if there |
| 332 // are any). | 327 // are any). |
| 333 if (!autofill_values->empty()) { | 328 if (!autofill_values->empty()) { |
| 334 autofill_values->insert(autofill_values->begin(), base::string16()); | 329 autofill_values->insert(autofill_values->begin(), base::string16()); |
| 335 autofill_labels->insert(autofill_labels->begin(), base::string16()); | 330 autofill_labels->insert(autofill_labels->begin(), base::string16()); |
| 336 autofill_icons->insert(autofill_icons->begin(), base::string16()); | 331 autofill_icons->insert(autofill_icons->begin(), base::string16()); |
| 337 autofill_unique_ids->insert(autofill_unique_ids->begin(), | 332 autofill_unique_ids->insert(autofill_unique_ids->begin(), |
| 338 WebAutofillClient::MenuItemIDSeparator); | 333 POPUP_ITEM_ID_SEPARATOR); |
| 339 } | 334 } |
| 340 | 335 |
| 341 // Insert the datalist elements. | 336 // Insert the datalist elements. |
| 342 autofill_values->insert(autofill_values->begin(), | 337 autofill_values->insert(autofill_values->begin(), |
| 343 data_list_values_.begin(), | 338 data_list_values_.begin(), |
| 344 data_list_values_.end()); | 339 data_list_values_.end()); |
| 345 autofill_labels->insert(autofill_labels->begin(), | 340 autofill_labels->insert(autofill_labels->begin(), |
| 346 data_list_labels_.begin(), | 341 data_list_labels_.begin(), |
| 347 data_list_labels_.end()); | 342 data_list_labels_.end()); |
| 348 | 343 |
| 349 // Set the values that all datalist elements share. | 344 // Set the values that all datalist elements share. |
| 350 autofill_icons->insert(autofill_icons->begin(), | 345 autofill_icons->insert(autofill_icons->begin(), |
| 351 data_list_values_.size(), | 346 data_list_values_.size(), |
| 352 base::string16()); | 347 base::string16()); |
| 353 autofill_unique_ids->insert(autofill_unique_ids->begin(), | 348 autofill_unique_ids->insert(autofill_unique_ids->begin(), |
| 354 data_list_values_.size(), | 349 data_list_values_.size(), |
| 355 WebAutofillClient::MenuItemIDDataListEntry); | 350 POPUP_ITEM_ID_DATALIST_ENTRY); |
| 356 } | 351 } |
| 357 | 352 |
| 358 } // namespace autofill | 353 } // namespace autofill |
| OLD | NEW |