Chromium Code Reviews| 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/common/autofill_enums.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(MenuItemIDSeparator); |
| 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() == MenuItemIDSeparator) { |
| 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 24 matching lines...) Expand all Loading... | |
| 120 const gfx::RectF& element_bounds) { | 118 const gfx::RectF& element_bounds) { |
| 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(), MenuItemIDPasswordEntry); |
| 131 WebAutofillClient::MenuItemIDPasswordEntry); | |
| 132 autofill_manager_->delegate()->ShowAutofillPopup( | 129 autofill_manager_->delegate()->ShowAutofillPopup( |
| 133 element_bounds_, | 130 element_bounds_, |
| 134 autofill_query_field_.text_direction, | 131 autofill_query_field_.text_direction, |
| 135 suggestions, | 132 suggestions, |
| 136 realms, | 133 realms, |
| 137 empty, | 134 empty, |
| 138 password_ids, | 135 password_ids, |
| 139 GetWeakPtr()); | 136 GetWeakPtr()); |
| 140 } | 137 } |
| 141 | 138 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 167 void AutofillExternalDelegate::DidSelectSuggestion(int identifier) { | 164 void AutofillExternalDelegate::DidSelectSuggestion(int identifier) { |
| 168 ClearPreviewedForm(); | 165 ClearPreviewedForm(); |
| 169 | 166 |
| 170 // Only preview the data if it is a profile. | 167 // Only preview the data if it is a profile. |
| 171 if (identifier > 0) | 168 if (identifier > 0) |
| 172 FillAutofillFormData(identifier, true); | 169 FillAutofillFormData(identifier, true); |
| 173 } | 170 } |
| 174 | 171 |
| 175 void AutofillExternalDelegate::DidAcceptSuggestion(const base::string16& value, | 172 void AutofillExternalDelegate::DidAcceptSuggestion(const base::string16& value, |
| 176 int identifier) { | 173 int identifier) { |
| 177 if (identifier == WebAutofillClient::MenuItemIDAutofillOptions) { | 174 if (identifier == MenuItemIDAutofillOptions) { |
| 178 // User selected 'Autofill Options'. | 175 // User selected 'Autofill Options'. |
| 179 autofill_manager_->ShowAutofillSettings(); | 176 autofill_manager_->ShowAutofillSettings(); |
| 180 } else if (identifier == WebAutofillClient::MenuItemIDClearForm) { | 177 } else if (identifier == MenuItemIDClearForm) { |
| 181 // User selected 'Clear form'. | 178 // User selected 'Clear form'. |
| 182 autofill_driver_->RendererShouldClearFilledForm(); | 179 autofill_driver_->RendererShouldClearFilledForm(); |
| 183 } else if (identifier == WebAutofillClient::MenuItemIDPasswordEntry) { | 180 } else if (identifier == MenuItemIDPasswordEntry) { |
| 184 bool success = password_autofill_manager_.DidAcceptAutofillSuggestion( | 181 bool success = password_autofill_manager_.DidAcceptAutofillSuggestion( |
| 185 autofill_query_field_, value); | 182 autofill_query_field_, value); |
| 186 DCHECK(success); | 183 DCHECK(success); |
| 187 } else if (identifier == WebAutofillClient::MenuItemIDDataListEntry) { | 184 } else if (identifier == MenuItemIDDataListEntry) { |
| 188 autofill_driver_->RendererShouldAcceptDataListSuggestion(value); | 185 autofill_driver_->RendererShouldAcceptDataListSuggestion(value); |
| 189 } else if (identifier == WebAutofillClient::MenuItemIDAutocompleteEntry) { | 186 } else if (identifier == MenuItemIDAutocompleteEntry) { |
| 190 // User selected an Autocomplete, so we fill directly. | 187 // User selected an Autocomplete, so we fill directly. |
| 191 autofill_driver_->RendererShouldSetNodeText(value); | 188 autofill_driver_->RendererShouldSetNodeText(value); |
| 192 } else { | 189 } else { |
| 193 FillAutofillFormData(identifier, false); | 190 FillAutofillFormData(identifier, false); |
| 194 } | 191 } |
| 195 | 192 |
| 196 autofill_manager_->delegate()->HideAutofillPopup(); | 193 autofill_manager_->delegate()->HideAutofillPopup(); |
| 197 } | 194 } |
| 198 | 195 |
| 199 void AutofillExternalDelegate::RemoveSuggestion(const base::string16& value, | 196 void AutofillExternalDelegate::RemoveSuggestion(const base::string16& value, |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 228 password_autofill_manager_.AddPasswordFormMapping(username_field, fill_data); | 225 password_autofill_manager_.AddPasswordFormMapping(username_field, fill_data); |
| 229 } | 226 } |
| 230 | 227 |
| 231 base::WeakPtr<AutofillExternalDelegate> AutofillExternalDelegate::GetWeakPtr() { | 228 base::WeakPtr<AutofillExternalDelegate> AutofillExternalDelegate::GetWeakPtr() { |
| 232 return weak_ptr_factory_.GetWeakPtr(); | 229 return weak_ptr_factory_.GetWeakPtr(); |
| 233 } | 230 } |
| 234 | 231 |
| 235 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, | 232 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, |
| 236 bool is_preview) { | 233 bool is_preview) { |
| 237 // If the selected element is a warning we don't want to do anything. | 234 // If the selected element is a warning we don't want to do anything. |
| 238 if (unique_id == WebAutofillClient::MenuItemIDWarningMessage) | 235 if (unique_id == MenuItemIDWarningMessage) |
| 239 return; | 236 return; |
| 240 | 237 |
| 241 AutofillDriver::RendererFormDataAction renderer_action = is_preview ? | 238 AutofillDriver::RendererFormDataAction renderer_action = is_preview ? |
| 242 AutofillDriver::FORM_DATA_ACTION_PREVIEW : | 239 AutofillDriver::FORM_DATA_ACTION_PREVIEW : |
| 243 AutofillDriver::FORM_DATA_ACTION_FILL; | 240 AutofillDriver::FORM_DATA_ACTION_FILL; |
| 244 | 241 |
| 245 DCHECK(autofill_driver_->RendererIsAvailable()); | 242 DCHECK(autofill_driver_->RendererIsAvailable()); |
| 246 autofill_driver_->SetRendererActionOnFormDataReception(renderer_action); | 243 autofill_driver_->SetRendererActionOnFormDataReception(renderer_action); |
| 247 // Fill the values for the whole form. | 244 // Fill the values for the whole form. |
| 248 autofill_manager_->OnFillAutofillFormData(autofill_query_id_, | 245 autofill_manager_->OnFillAutofillFormData(autofill_query_id_, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 259 if (!autofill_query_field_.should_autocomplete) { | 256 if (!autofill_query_field_.should_autocomplete) { |
| 260 // Autofill is disabled. If there were some profile or credit card | 257 // Autofill is disabled. If there were some profile or credit card |
| 261 // suggestions to show, show a warning instead. Otherwise, clear out the | 258 // suggestions to show, show a warning instead. Otherwise, clear out the |
| 262 // list of suggestions. | 259 // list of suggestions. |
| 263 if (!autofill_unique_ids->empty() && (*autofill_unique_ids)[0] > 0) { | 260 if (!autofill_unique_ids->empty() && (*autofill_unique_ids)[0] > 0) { |
| 264 // If autofill is disabled and we had suggestions, show a warning instead. | 261 // If autofill is disabled and we had suggestions, show a warning instead. |
| 265 autofill_values->assign( | 262 autofill_values->assign( |
| 266 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 263 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
| 267 autofill_labels->assign(1, base::string16()); | 264 autofill_labels->assign(1, base::string16()); |
| 268 autofill_icons->assign(1, base::string16()); | 265 autofill_icons->assign(1, base::string16()); |
| 269 autofill_unique_ids->assign(1, | 266 autofill_unique_ids->assign(1, MenuItemIDWarningMessage); |
| 270 WebAutofillClient::MenuItemIDWarningMessage); | |
| 271 } else { | 267 } else { |
| 272 autofill_values->clear(); | 268 autofill_values->clear(); |
| 273 autofill_labels->clear(); | 269 autofill_labels->clear(); |
| 274 autofill_icons->clear(); | 270 autofill_icons->clear(); |
| 275 autofill_unique_ids->clear(); | 271 autofill_unique_ids->clear(); |
| 276 } | 272 } |
| 277 } else if (autofill_unique_ids->size() > 1 && | 273 } else if (autofill_unique_ids->size() > 1 && |
| 278 (*autofill_unique_ids)[0] == | 274 (*autofill_unique_ids)[0] == MenuItemIDWarningMessage) { |
| 279 WebAutofillClient::MenuItemIDWarningMessage) { | |
| 280 // If we received a warning instead of suggestions from autofill but regular | 275 // If we received a warning instead of suggestions from autofill but regular |
| 281 // suggestions from autocomplete, don't show the autofill warning. | 276 // suggestions from autocomplete, don't show the autofill warning. |
| 282 autofill_values->erase(autofill_values->begin()); | 277 autofill_values->erase(autofill_values->begin()); |
| 283 autofill_labels->erase(autofill_labels->begin()); | 278 autofill_labels->erase(autofill_labels->begin()); |
| 284 autofill_icons->erase(autofill_icons->begin()); | 279 autofill_icons->erase(autofill_icons->begin()); |
| 285 autofill_unique_ids->erase(autofill_unique_ids->begin()); | 280 autofill_unique_ids->erase(autofill_unique_ids->begin()); |
| 286 } | 281 } |
| 287 | 282 |
| 288 // If we were about to show a warning and we shouldn't, don't. | 283 // If we were about to show a warning and we shouldn't, don't. |
| 289 if (!autofill_unique_ids->empty() && | 284 if (!autofill_unique_ids->empty() && |
| 290 (*autofill_unique_ids)[0] == | 285 (*autofill_unique_ids)[0] == |
| 291 WebAutofillClient::MenuItemIDWarningMessage && | 286 MenuItemIDWarningMessage && |
|
Ilya Sherman
2014/02/11 22:21:33
nit: Looks like this line no longer needs to wrap.
gnana
2014/02/12 13:34:09
Done.
| |
| 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(MenuItemIDClearForm); |
| 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(MenuItemIDAutofillOptions); |
| 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 MenuItemIDSeparator); |
| 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 MenuItemIDDataListEntry); |
| 356 } | 351 } |
| 357 | 352 |
| 358 } // namespace autofill | 353 } // namespace autofill |
| OLD | NEW |