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

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

Issue 6213002: Propagate correct data to the Toolbar servers (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/autofill/autofill_manager.h" 5 #include "chrome/browser/autofill/autofill_manager.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 if (!upload_form_structure_->ShouldBeParsed(true)) 212 if (!upload_form_structure_->ShouldBeParsed(true))
213 return; 213 return;
214 214
215 FormStructure* cached_upload_form_structure = NULL; 215 FormStructure* cached_upload_form_structure = NULL;
216 AutoFillField* ignored; 216 AutoFillField* ignored;
217 if (!FindCachedFormAndField(form, form.fields.front(), 217 if (!FindCachedFormAndField(form, form.fields.front(),
218 &cached_upload_form_structure, &ignored)) { 218 &cached_upload_form_structure, &ignored)) {
219 cached_upload_form_structure = NULL; 219 cached_upload_form_structure = NULL;
220 } 220 }
221 221
222 DeterminePossibleFieldTypesForUpload(cached_upload_form_structure); 222 std::string data_present = DeterminePossibleFieldTypesForUpload(
223 cached_upload_form_structure);
223 // TODO(isherman): Consider uploading to server here rather than in 224 // TODO(isherman): Consider uploading to server here rather than in
224 // HandleSubmit(). 225 // HandleSubmit().
225 226
226 if (!upload_form_structure_->IsAutoFillable(true)) 227 if (!upload_form_structure_->IsAutoFillable(true))
227 return; 228 return;
228 229
229 HandleSubmit(); 230 HandleSubmit(data_present);
230 } 231 }
231 232
232 void AutoFillManager::OnFormsSeen(const std::vector<FormData>& forms) { 233 void AutoFillManager::OnFormsSeen(const std::vector<FormData>& forms) {
233 if (!IsAutoFillEnabled()) 234 if (!IsAutoFillEnabled())
234 return; 235 return;
235 236
236 ParseForms(forms); 237 ParseForms(forms);
237 } 238 }
238 239
239 void AutoFillManager::OnQueryFormFieldAutoFill( 240 void AutoFillManager::OnQueryFormFieldAutoFill(
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 if (prefs->FindPreference(prefs::kFormAutofillEnabled)) { 478 if (prefs->FindPreference(prefs::kFormAutofillEnabled)) {
478 bool enabled = prefs->GetBoolean(prefs::kFormAutofillEnabled); 479 bool enabled = prefs->GetBoolean(prefs::kFormAutofillEnabled);
479 prefs->ClearPref(prefs::kFormAutofillEnabled); 480 prefs->ClearPref(prefs::kFormAutofillEnabled);
480 prefs->SetBoolean(prefs::kAutoFillEnabled, enabled); 481 prefs->SetBoolean(prefs::kAutoFillEnabled, enabled);
481 return enabled; 482 return enabled;
482 } 483 }
483 484
484 return prefs->GetBoolean(prefs::kAutoFillEnabled); 485 return prefs->GetBoolean(prefs::kAutoFillEnabled);
485 } 486 }
486 487
487 void AutoFillManager::DeterminePossibleFieldTypesForUpload( 488 std::string AutoFillManager::DeterminePossibleFieldTypesForUpload(
488 const FormStructure* cached_upload_form_structure) { 489 const FormStructure* cached_upload_form_structure) {
490 // Mark bits present for the upload for data_present field.
491 std::vector<uint8> presence_bitfield;
492 // Determine all of the field types that were autofilled. Pack bits into
493 // |presence_bitfield|. The necessary size for |presence_bitfield| is
494 // ceil((MAX_VALID_FIELD_TYPE + 7) / 8) bytes (uint8).
495 presence_bitfield.resize((MAX_VALID_FIELD_TYPE + 0x7) >> 3);
496 for (size_t i = 0; i < presence_bitfield.size(); ++i)
497 presence_bitfield[i] = 0;
498
489 for (size_t i = 0; i < upload_form_structure_->field_count(); i++) { 499 for (size_t i = 0; i < upload_form_structure_->field_count(); i++) {
490 const AutoFillField* field = upload_form_structure_->field(i); 500 const AutoFillField* field = upload_form_structure_->field(i);
491 FieldTypeSet field_types; 501 FieldTypeSet field_types;
492 personal_data_->GetPossibleFieldTypes(field->value(), &field_types); 502 personal_data_->GetPossibleFieldTypes(field->value(), &field_types);
503 if (field_types.size() > 1) {
504 // If there is more than one possibility, do not poison the crowd-sourced
505 // data by returning UNKNOWN_TYPE.
506 field_types.clear();
507 field_types.insert(UNKNOWN_TYPE);
508 }
493 DCHECK(!field_types.empty()); 509 DCHECK(!field_types.empty());
510 SetPresenceBit(*(field_types.begin()), &presence_bitfield);
511
494 upload_form_structure_->set_possible_types(i, field_types); 512 upload_form_structure_->set_possible_types(i, field_types);
495 513
496 if (field->form_control_type() == ASCIIToUTF16("select-one")) { 514 if (field->form_control_type() == ASCIIToUTF16("select-one")) {
497 // TODO(isherman): <select> fields don't support |is_autofilled()|. Since 515 // TODO(isherman): <select> fields don't support |is_autofilled()|. Since
498 // this is heavily relied upon by our metrics, we just don't log anything 516 // this is heavily relied upon by our metrics, we just don't log anything
499 // for all <select> fields. Better to have less data than misleading data. 517 // for all <select> fields. Better to have less data than misleading data.
500 continue; 518 continue;
501 } 519 }
502 520
503 // Log various quality metrics. 521 // Log various quality metrics.
(...skipping 28 matching lines...) Expand all
532 550
533 551
534 // TODO(isherman): Other things we might want to log here: 552 // TODO(isherman): Other things we might want to log here:
535 // * Per Vadim's email, a combination of (1) whether heuristics fired, 553 // * Per Vadim's email, a combination of (1) whether heuristics fired,
536 // (2) whether the server returned something interesting, (3) whether 554 // (2) whether the server returned something interesting, (3) whether
537 // the user filled the field 555 // the user filled the field
538 // * Whether the server type matches the heursitic type 556 // * Whether the server type matches the heursitic type
539 // - Perhaps only if at least one of the types is not unknown/no data. 557 // - Perhaps only if at least one of the types is not unknown/no data.
540 } 558 }
541 } 559 }
560
561 return ConvertPresenceBitsToString(presence_bitfield);
542 } 562 }
543 563
544 void AutoFillManager::HandleSubmit() { 564 void AutoFillManager::HandleSubmit(std::string const& data_present) {
545 // If there wasn't enough data to import then we don't want to send an upload 565 // If there wasn't enough data to import then we don't want to send an upload
546 // to the server. 566 // to the server.
547 // TODO(jhawkins): Import form data from |form_structures_|. That will 567 // TODO(jhawkins): Import form data from |form_structures_|. That will
548 // require querying the FormManager for updated field values. 568 // require querying the FormManager for updated field values.
549 std::vector<FormStructure*> import; 569 std::vector<FormStructure*> import;
550 import.push_back(upload_form_structure_.get()); 570 import.push_back(upload_form_structure_.get());
551 if (!personal_data_->ImportFormData(import)) 571 if (!personal_data_->ImportFormData(import))
552 return; 572 return;
553 573
554 // Did we get credit card info? 574 // Did we get credit card info?
555 AutoFillProfile* profile; 575 AutoFillProfile* profile;
556 CreditCard* credit_card; 576 CreditCard* credit_card;
557 personal_data_->GetImportedFormData(&profile, &credit_card); 577 personal_data_->GetImportedFormData(&profile, &credit_card);
558 578
559 if (!credit_card) { 579 if (!credit_card) {
560 UploadFormData(); 580 data_autofilled_.clear();
581 UploadFormData(data_present);
561 return; 582 return;
583 } else {
584 data_autofilled_ = data_present;
562 } 585 }
563 586
564 // Show an infobar to offer to save the credit card info. 587 // Show an infobar to offer to save the credit card info.
565 if (tab_contents_) { 588 if (tab_contents_) {
566 tab_contents_->AddInfoBar(new AutoFillCCInfoBarDelegate(tab_contents_, 589 tab_contents_->AddInfoBar(new AutoFillCCInfoBarDelegate(tab_contents_,
567 this)); 590 this));
568 } 591 }
569 } 592 }
570 593
571 void AutoFillManager::UploadFormData() { 594 void AutoFillManager::UploadFormData(std::string const& data_present) {
572 if (!disable_download_manager_requests_ && upload_form_structure_.get()) { 595 if (!disable_download_manager_requests_ && upload_form_structure_.get()) {
573 bool was_autofilled = false; 596 bool was_autofilled = false;
574 // Check if the form among last 3 forms that were auto-filled. 597 // Check if the form among last 3 forms that were auto-filled.
575 // Clear older signatures. 598 // Clear older signatures.
576 std::list<std::string>::iterator it; 599 std::list<std::string>::iterator it;
577 int total_form_checked = 0; 600 int total_form_checked = 0;
578 for (it = autofilled_forms_signatures_.begin(); 601 for (it = autofilled_forms_signatures_.begin();
579 it != autofilled_forms_signatures_.end() && total_form_checked < 3; 602 it != autofilled_forms_signatures_.end() && total_form_checked < 3;
580 ++it, ++total_form_checked) { 603 ++it, ++total_form_checked) {
581 if (*it == upload_form_structure_->FormSignature()) 604 if (*it == upload_form_structure_->FormSignature()) {
582 was_autofilled = true; 605 was_autofilled = true;
606 }
583 } 607 }
584 // Remove outdated form signatures. 608 // Remove outdated form signatures.
585 if (total_form_checked == 3 && it != autofilled_forms_signatures_.end()) { 609 if (total_form_checked == 3 && it != autofilled_forms_signatures_.end()) {
586 autofilled_forms_signatures_.erase(it, 610 autofilled_forms_signatures_.erase(it,
587 autofilled_forms_signatures_.end()); 611 autofilled_forms_signatures_.end());
588 } 612 }
589 download_manager_.StartUploadRequest(*(upload_form_structure_.get()), 613 download_manager_.StartUploadRequest(*(upload_form_structure_.get()),
590 was_autofilled); 614 was_autofilled,
615 data_present.c_str());
591 } 616 }
592 } 617 }
593 618
594 void AutoFillManager::Reset() { 619 void AutoFillManager::Reset() {
595 upload_form_structure_.reset(); 620 upload_form_structure_.reset();
596 form_structures_.reset(); 621 form_structures_.reset();
597 } 622 }
598 623
624 void AutoFillManager::SetPresenceBit(AutoFillFieldType field_type,
625 std::vector<uint8>* bitfield) {
626 DCHECK(bitfield);
627 DCHECK(bitfield->size() > (static_cast<size_t>(field_type) >> 3));
628 // Set bit in the bitfield: byte |field_type| / 8, bit in byte
629 // |field_type| % 8 from the left.
630 (*bitfield)[field_type >> 3] |= (0x80 >> (field_type & 7));
631 }
632
633 std::string AutoFillManager::ConvertPresenceBitsToString(
634 std::vector<uint8> const& bitfield) {
635 std::string present_data;
636 present_data.reserve(bitfield.size() * 2 + 1);
637 // Skip leading zeroes. If all mask is 0 - return empty string.
638 size_t data_end = bitfield.size();
639 for (; data_end > 0 && !bitfield[data_end - 1]; --data_end) {
640 }
641 // Print all non-zero bytes into the string.
642 for (size_t i = 0; i < data_end; ++i) {
643 base::StringAppendF(&present_data, "%02x", bitfield[i]);
644 }
645 return present_data;
646 }
647
599 void AutoFillManager::OnInfoBarClosed(bool should_save) { 648 void AutoFillManager::OnInfoBarClosed(bool should_save) {
600 if (should_save) 649 if (should_save)
601 personal_data_->SaveImportedCreditCard(); 650 personal_data_->SaveImportedCreditCard();
602 UploadFormData(); 651 UploadFormData(data_autofilled_);
603 } 652 }
604 653
605 AutoFillManager::AutoFillManager() 654 AutoFillManager::AutoFillManager()
606 : tab_contents_(NULL), 655 : tab_contents_(NULL),
607 personal_data_(NULL), 656 personal_data_(NULL),
608 download_manager_(NULL), 657 download_manager_(NULL),
609 disable_download_manager_requests_(true), 658 disable_download_manager_requests_(true),
610 metric_logger_(new AutoFillMetrics), 659 metric_logger_(new AutoFillMetrics),
611 cc_infobar_(NULL) { 660 cc_infobar_(NULL) {
612 } 661 }
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 return std::string(); 935 return std::string();
887 936
888 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); 937 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id);
889 if (iter == id_guid_map_.end()) { 938 if (iter == id_guid_map_.end()) {
890 NOTREACHED(); 939 NOTREACHED();
891 return std::string(); 940 return std::string();
892 } 941 }
893 942
894 return iter->second; 943 return iter->second;
895 } 944 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698