Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: components/autofill/browser/autofill_manager.cc

Issue 15097004: Enable Autocomplete feature for chromium webview (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@setSaveFormData2
Patch Set: added a unit test Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/autofill/browser/autofill_manager.h" 5 #include "components/autofill/browser/autofill_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <map> 10 #include <map>
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 IPC_MESSAGE_HANDLER(AutofillHostMsg_MaybeShowAutocheckoutBubble, 322 IPC_MESSAGE_HANDLER(AutofillHostMsg_MaybeShowAutocheckoutBubble,
323 OnMaybeShowAutocheckoutBubble) 323 OnMaybeShowAutocheckoutBubble)
324 IPC_MESSAGE_UNHANDLED(handled = false) 324 IPC_MESSAGE_UNHANDLED(handled = false)
325 IPC_END_MESSAGE_MAP() 325 IPC_END_MESSAGE_MAP()
326 326
327 return handled; 327 return handled;
328 } 328 }
329 329
330 bool AutofillManager::OnFormSubmitted(const FormData& form, 330 bool AutofillManager::OnFormSubmitted(const FormData& form,
331 const TimeTicks& timestamp) { 331 const TimeTicks& timestamp) {
332 // Let AutoComplete know as well.
333 autocomplete_history_manager_.OnFormSubmitted(form);
334
335 if (!IsAutofillEnabled()) 332 if (!IsAutofillEnabled())
336 return false; 333 return false;
337 334
338 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) 335 // Let AutoComplete know as well.
339 return false; 336 autocomplete_history_manager_.OnFormSubmitted(form);
Ilya Sherman 2013/06/15 00:28:09 I think this check is still needed.
sgurun-gerrit only 2013/06/17 21:20:32 Done.
Ilya Sherman 2013/06/15 00:28:09 Does this re-ordering still make sense given the u
sgurun-gerrit only 2013/06/17 21:20:32 There seems multiple issues with enabling/disablin
340 337
341 // Don't save data that was submitted through JavaScript. 338 // Don't save data that was submitted through JavaScript.
342 if (!form.user_submitted) 339 if (!form.user_submitted)
343 return false; 340 return false;
344 341
342 // If there is no personal_data_, nothing to do.
343 if (!personal_data_)
344 return false;
345
345 // Grab a copy of the form data. 346 // Grab a copy of the form data.
346 scoped_ptr<FormStructure> submitted_form( 347 scoped_ptr<FormStructure> submitted_form(
347 new FormStructure(form, GetAutocheckoutURLPrefix())); 348 new FormStructure(form, GetAutocheckoutURLPrefix()));
348 349
349 // Disregard forms that we wouldn't ever autofill in the first place. 350 // Disregard forms that we wouldn't ever autofill in the first place.
350 if (!submitted_form->ShouldBeParsed(true)) 351 if (!submitted_form->ShouldBeParsed(true))
351 return false; 352 return false;
352 353
353 // Ignore forms not present in our cache. These are typically forms with 354 // Ignore forms not present in our cache. These are typically forms with
354 // wonky JavaScript that also makes them not auto-fillable. 355 // wonky JavaScript that also makes them not auto-fillable.
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 std::vector<int> unique_ids; 488 std::vector<int> unique_ids;
488 489
489 if (external_delegate_) { 490 if (external_delegate_) {
490 external_delegate_->OnQuery(query_id, 491 external_delegate_->OnQuery(query_id,
491 form, 492 form,
492 field, 493 field,
493 bounding_box, 494 bounding_box,
494 display_warning); 495 display_warning);
495 } 496 }
496 497
498 GetAutofillSuggestions(form,
499 field,
500 &values,
501 &labels,
502 &icons,
503 &unique_ids);
Ilya Sherman 2013/06/15 00:28:09 nit: This all fits on a single line.
Ilya Sherman 2013/06/15 00:28:09 What's the purpose of decomposing out this method?
sgurun-gerrit only 2013/06/17 21:20:32 Done.
sgurun-gerrit only 2013/06/17 21:20:32 Done.
504
505 // Add the results from AutoComplete. They come back asynchronously, so we
506 // hand off what we generated and they will send the results back to the
507 // renderer.
508 autocomplete_history_manager_.OnGetAutocompleteSuggestions(
509 query_id, field.name, field.value, values, labels, icons, unique_ids);
510 }
511
512 void AutofillManager::GetAutofillSuggestions(
513 const FormData& form,
514 const FormFieldData& field,
515 std::vector<base::string16>* values,
516 std::vector<base::string16>* labels,
517 std::vector<string16>* icons,
518 std::vector<int>* unique_ids) {
497 RenderViewHost* host = NULL; 519 RenderViewHost* host = NULL;
498 FormStructure* form_structure = NULL; 520 FormStructure* form_structure = NULL;
499 AutofillField* autofill_field = NULL; 521 AutofillField* autofill_field = NULL;
500 if (GetHost(&host) && 522 if (GetHost(&host) &&
501 GetCachedFormAndField(form, field, &form_structure, &autofill_field) && 523 GetCachedFormAndField(form, field, &form_structure, &autofill_field) &&
502 // Don't send suggestions for forms that aren't auto-fillable. 524 // Don't send suggestions for forms that aren't auto-fillable.
503 form_structure->IsAutofillable(false)) { 525 form_structure->IsAutofillable(false)) {
504 AutofillFieldType type = autofill_field->type(); 526 AutofillFieldType type = autofill_field->type();
505 bool is_filling_credit_card = 527 bool is_filling_credit_card =
506 (AutofillType(type).group() == AutofillType::CREDIT_CARD); 528 (AutofillType(type).group() == AutofillType::CREDIT_CARD);
507 if (is_filling_credit_card) { 529 if (is_filling_credit_card) {
508 GetCreditCardSuggestions( 530 GetCreditCardSuggestions(
509 field, type, &values, &labels, &icons, &unique_ids); 531 field, type, values, labels, icons, unique_ids);
510 } else { 532 } else {
511 GetProfileSuggestions( 533 GetProfileSuggestions(
512 form_structure, field, type, &values, &labels, &icons, &unique_ids); 534 form_structure, field, type, values, labels, icons, unique_ids);
513 } 535 }
514 536
515 DCHECK_EQ(values.size(), labels.size()); 537 DCHECK_EQ(values->size(), labels->size());
516 DCHECK_EQ(values.size(), icons.size()); 538 DCHECK_EQ(values->size(), icons->size());
517 DCHECK_EQ(values.size(), unique_ids.size()); 539 DCHECK_EQ(values->size(), unique_ids->size());
518 540
519 if (!values.empty()) { 541 if (!values->empty()) {
520 // Don't provide Autofill suggestions when Autofill is disabled, and don't 542 // Don't provide Autofill suggestions when Autofill is disabled, and don't
521 // provide credit card suggestions for non-HTTPS pages. However, provide a 543 // provide credit card suggestions for non-HTTPS pages. However, provide a
522 // warning to the user in these cases. 544 // warning to the user in these cases.
523 int warning = 0; 545 int warning = 0;
524 if (!form_structure->IsAutofillable(true)) 546 if (!form_structure->IsAutofillable(true))
525 warning = IDS_AUTOFILL_WARNING_FORM_DISABLED; 547 warning = IDS_AUTOFILL_WARNING_FORM_DISABLED;
526 else if (is_filling_credit_card && !FormIsHTTPS(*form_structure)) 548 else if (is_filling_credit_card && !FormIsHTTPS(*form_structure))
527 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION; 549 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION;
528 if (warning) { 550 if (warning) {
529 values.assign(1, l10n_util::GetStringUTF16(warning)); 551 values->assign(1, l10n_util::GetStringUTF16(warning));
530 labels.assign(1, base::string16()); 552 labels->assign(1, base::string16());
531 icons.assign(1, base::string16()); 553 icons->assign(1, base::string16());
532 unique_ids.assign(1, 554 unique_ids->assign(1,
533 WebKit::WebAutofillClient::MenuItemIDWarningMessage); 555 WebKit::WebAutofillClient::MenuItemIDWarningMessage);
534 } else { 556 } else {
535 bool section_is_autofilled = 557 bool section_is_autofilled =
536 SectionIsAutofilled(*form_structure, form, 558 SectionIsAutofilled(*form_structure, form,
537 autofill_field->section()); 559 autofill_field->section());
538 if (section_is_autofilled) { 560 if (section_is_autofilled) {
539 // If the relevant section is auto-filled and the renderer is querying 561 // If the relevant section is auto-filled and the renderer is querying
540 // for suggestions, then the user is editing the value of a field. 562 // for suggestions, then the user is editing the value of a field.
541 // In this case, mimic autocomplete: don't display labels or icons, 563 // In this case, mimic autocomplete: don't display labels or icons,
542 // as that information is redundant. 564 // as that information is redundant.
543 labels.assign(labels.size(), base::string16()); 565 labels->assign(labels->size(), base::string16());
544 icons.assign(icons.size(), base::string16()); 566 icons->assign(icons->size(), base::string16());
545 } 567 }
546 568
547 // When filling credit card suggestions, the values and labels are 569 // When filling credit card suggestions, the values and labels are
548 // typically obfuscated, which makes detecting duplicates hard. Since 570 // typically obfuscated, which makes detecting duplicates hard. Since
549 // duplicates only tend to be a problem when filling address forms 571 // duplicates only tend to be a problem when filling address forms
550 // anyway, only don't de-dup credit card suggestions. 572 // anyway, only don't de-dup credit card suggestions.
551 if (!is_filling_credit_card) 573 if (!is_filling_credit_card)
552 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); 574 RemoveDuplicateSuggestions(values, labels, icons, unique_ids);
553 575
554 // The first time we show suggestions on this page, log the number of 576 // The first time we show suggestions on this page, log the number of
555 // suggestions shown. 577 // suggestions shown.
556 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) { 578 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
557 metric_logger_->LogAddressSuggestionsCount(values.size()); 579 metric_logger_->LogAddressSuggestionsCount(values->size());
558 has_logged_address_suggestions_count_ = true; 580 has_logged_address_suggestions_count_ = true;
559 } 581 }
560 } 582 }
561 } 583 }
562 } 584 }
563
564 // Add the results from AutoComplete. They come back asynchronously, so we
565 // hand off what we generated and they will send the results back to the
566 // renderer.
567 autocomplete_history_manager_.OnGetAutocompleteSuggestions(
568 query_id, field.name, field.value, values, labels, icons, unique_ids);
569 } 585 }
570 586
571 void AutofillManager::OnFillAutofillFormData(int query_id, 587 void AutofillManager::OnFillAutofillFormData(int query_id,
572 const FormData& form, 588 const FormData& form,
573 const FormFieldData& field, 589 const FormFieldData& field,
574 int unique_id) { 590 int unique_id) {
575 RenderViewHost* host = NULL; 591 RenderViewHost* host = NULL;
576 const AutofillDataModel* data_model = NULL; 592 const AutofillDataModel* data_model = NULL;
577 size_t variant = 0; 593 size_t variant = 0;
578 FormStructure* form_structure = NULL; 594 FormStructure* form_structure = NULL;
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 autocheckout_manager_.OnClickFailed(status); 873 autocheckout_manager_.OnClickFailed(status);
858 } 874 }
859 875
860 std::string AutofillManager::GetAutocheckoutURLPrefix() const { 876 std::string AutofillManager::GetAutocheckoutURLPrefix() const {
861 if (!web_contents()) 877 if (!web_contents())
862 return std::string(); 878 return std::string();
863 879
864 autofill::autocheckout::WhitelistManager* whitelist_manager = 880 autofill::autocheckout::WhitelistManager* whitelist_manager =
865 manager_delegate_->GetAutocheckoutWhitelistManager(); 881 manager_delegate_->GetAutocheckoutWhitelistManager();
866 882
867 return whitelist_manager->GetMatchedURLPrefix(web_contents()->GetURL()); 883 return whitelist_manager ?
884 whitelist_manager->GetMatchedURLPrefix(web_contents()->GetURL()) :
885 std::string();
868 } 886 }
869 887
870 bool AutofillManager::IsAutofillEnabled() const { 888 bool AutofillManager::IsAutofillEnabled() const {
871 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); 889 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled)
890 && !manager_delegate_->ShouldIgnoreFormData() ;
Ilya Sherman 2013/06/15 00:28:09 nit: The "&&" should go on the previous line.
sgurun-gerrit only 2013/06/17 21:20:32 Done.
872 } 891 }
873 892
874 void AutofillManager::SendAutofillTypePredictions( 893 void AutofillManager::SendAutofillTypePredictions(
875 const std::vector<FormStructure*>& forms) const { 894 const std::vector<FormStructure*>& forms) const {
876 if (!CommandLine::ForCurrentProcess()->HasSwitch( 895 if (!CommandLine::ForCurrentProcess()->HasSwitch(
877 switches::kShowAutofillTypePredictions)) 896 switches::kShowAutofillTypePredictions))
878 return; 897 return;
879 898
880 RenderViewHost* host = web_contents()->GetRenderViewHost(); 899 RenderViewHost* host = web_contents()->GetRenderViewHost();
881 if (!host) 900 if (!host)
882 return; 901 return;
883 902
884 std::vector<FormDataPredictions> type_predictions; 903 std::vector<FormDataPredictions> type_predictions;
885 FormStructure::GetFieldTypePredictions(forms, &type_predictions); 904 FormStructure::GetFieldTypePredictions(forms, &type_predictions);
886 host->Send( 905 host->Send(
887 new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(), 906 new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
888 type_predictions)); 907 type_predictions));
889 } 908 }
890 909
891 void AutofillManager::ImportFormData(const FormStructure& submitted_form) { 910 void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
892 const CreditCard* imported_credit_card; 911 const CreditCard* imported_credit_card;
912
Ilya Sherman 2013/06/15 00:28:09 nit: Spurious diff.
sgurun-gerrit only 2013/06/17 21:20:32 Done.
893 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card)) 913 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
894 return; 914 return;
895 915
896 // If credit card information was submitted, we need to confirm whether to 916 // If credit card information was submitted, we need to confirm whether to
897 // save it. 917 // save it.
898 if (imported_credit_card) { 918 if (imported_credit_card) {
899 manager_delegate_->ConfirmSaveCreditCard( 919 manager_delegate_->ConfirmSaveCreditCard(
900 *metric_logger_, 920 *metric_logger_,
901 *imported_credit_card, 921 *imported_credit_card,
902 base::Bind(&PersonalDataManager::SaveImportedCreditCard, 922 base::Bind(&PersonalDataManager::SaveImportedCreditCard,
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 weak_ptr_factory_(this) { 1023 weak_ptr_factory_(this) {
1004 DCHECK(web_contents); 1024 DCHECK(web_contents);
1005 DCHECK(manager_delegate_); 1025 DCHECK(manager_delegate_);
1006 } 1026 }
1007 1027
1008 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { 1028 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
1009 metric_logger_.reset(metric_logger); 1029 metric_logger_.reset(metric_logger);
1010 } 1030 }
1011 1031
1012 bool AutofillManager::GetHost(RenderViewHost** host) const { 1032 bool AutofillManager::GetHost(RenderViewHost** host) const {
1033
Ilya Sherman 2013/06/15 00:28:09 nit: Spurious diff.
sgurun-gerrit only 2013/06/17 21:20:32 Done.
1013 if (!IsAutofillEnabled()) 1034 if (!IsAutofillEnabled())
1014 return false; 1035 return false;
1015 1036
1037 if (!personal_data_)
1038 return false;
1039
1016 // No autofill data to return if the profiles are empty. 1040 // No autofill data to return if the profiles are empty.
1017 if (personal_data_->GetProfiles().empty() && 1041 if (personal_data_->GetProfiles().empty() &&
1018 personal_data_->GetCreditCards().empty()) { 1042 personal_data_->GetCreditCards().empty()) {
1019 return false; 1043 return false;
1020 } 1044 }
1021 1045
1022 *host = web_contents()->GetRenderViewHost(); 1046 *host = web_contents()->GetRenderViewHost();
1023 if (!*host) 1047 if (!*host)
1024 return false; 1048 return false;
1025 1049
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 1340
1317 void AutofillManager::UpdateInitialInteractionTimestamp( 1341 void AutofillManager::UpdateInitialInteractionTimestamp(
1318 const TimeTicks& interaction_timestamp) { 1342 const TimeTicks& interaction_timestamp) {
1319 if (initial_interaction_timestamp_.is_null() || 1343 if (initial_interaction_timestamp_.is_null() ||
1320 interaction_timestamp < initial_interaction_timestamp_) { 1344 interaction_timestamp < initial_interaction_timestamp_) {
1321 initial_interaction_timestamp_ = interaction_timestamp; 1345 initial_interaction_timestamp_ = interaction_timestamp;
1322 } 1346 }
1323 } 1347 }
1324 1348
1325 } // namespace autofill 1349 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698