| OLD | NEW |
| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/string16.h" | 11 #include "base/string16.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 13 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 14 #include "chrome/browser/autofill/autofill_dialog.h" | 14 #include "chrome/browser/autofill/autofill_dialog.h" |
| 15 #include "chrome/browser/autofill/form_structure.h" | 15 #include "chrome/browser/autofill/form_structure.h" |
| 16 #include "chrome/browser/autofill/select_control_handler.h" | 16 #include "chrome/browser/autofill/select_control_handler.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| 19 #include "chrome/browser/renderer_host/render_view_host.h" | 19 #include "chrome/browser/renderer_host/render_view_host.h" |
| 20 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/notification_source.h" |
| 23 #include "chrome/common/notification_service.h" |
| 22 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 24 #include "webkit/glue/form_data.h" | 26 #include "webkit/glue/form_data.h" |
| 25 #include "webkit/glue/form_field.h" | 27 #include "webkit/glue/form_field.h" |
| 26 | 28 |
| 27 using webkit_glue::FormData; | 29 using webkit_glue::FormData; |
| 28 using webkit_glue::FormField; | 30 using webkit_glue::FormField; |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } // namespace | 95 } // namespace |
| 94 | 96 |
| 95 // TODO(jhawkins): Maybe this should be in a grd file? | 97 // TODO(jhawkins): Maybe this should be in a grd file? |
| 96 const char* kAutoFillLearnMoreUrl = | 98 const char* kAutoFillLearnMoreUrl = |
| 97 "http://www.google.com/support/chrome/bin/answer.py?answer=142893"; | 99 "http://www.google.com/support/chrome/bin/answer.py?answer=142893"; |
| 98 | 100 |
| 99 AutoFillManager::AutoFillManager(TabContents* tab_contents) | 101 AutoFillManager::AutoFillManager(TabContents* tab_contents) |
| 100 : tab_contents_(tab_contents), | 102 : tab_contents_(tab_contents), |
| 101 personal_data_(NULL), | 103 personal_data_(NULL), |
| 102 download_manager_(tab_contents_->profile()), | 104 download_manager_(tab_contents_->profile()), |
| 105 locale_model_(tab_contents_), |
| 103 disable_download_manager_requests_(false) { | 106 disable_download_manager_requests_(false) { |
| 104 DCHECK(tab_contents); | 107 DCHECK(tab_contents); |
| 105 | 108 |
| 106 // |personal_data_| is NULL when using TestTabContents. | 109 // |personal_data_| is NULL when using TestTabContents. |
| 107 personal_data_ = | 110 personal_data_ = |
| 108 tab_contents_->profile()->GetOriginalProfile()->GetPersonalDataManager(); | 111 tab_contents_->profile()->GetOriginalProfile()->GetPersonalDataManager(); |
| 109 download_manager_.SetObserver(this); | 112 download_manager_.SetObserver(this); |
| 113 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, |
| 114 Source<TabContents>(tab_contents_)); |
| 110 } | 115 } |
| 111 | 116 |
| 112 AutoFillManager::~AutoFillManager() { | 117 AutoFillManager::~AutoFillManager() { |
| 113 download_manager_.SetObserver(NULL); | 118 download_manager_.SetObserver(NULL); |
| 119 notification_registrar_.Remove(this, |
| 120 NotificationType::TAB_LANGUAGE_DETERMINED, |
| 121 Source<TabContents>(tab_contents_)); |
| 114 } | 122 } |
| 115 | 123 |
| 116 // static | 124 // static |
| 117 void AutoFillManager::RegisterBrowserPrefs(PrefService* prefs) { | 125 void AutoFillManager::RegisterBrowserPrefs(PrefService* prefs) { |
| 118 prefs->RegisterDictionaryPref(prefs::kAutoFillDialogPlacement); | 126 prefs->RegisterDictionaryPref(prefs::kAutoFillDialogPlacement); |
| 119 } | 127 } |
| 120 | 128 |
| 121 // static | 129 // static |
| 122 void AutoFillManager::RegisterUserPrefs(PrefService* prefs) { | 130 void AutoFillManager::RegisterUserPrefs(PrefService* prefs) { |
| 123 prefs->RegisterBooleanPref(prefs::kAutoFillEnabled, true); | 131 prefs->RegisterBooleanPref(prefs::kAutoFillEnabled, true); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 138 | 146 |
| 139 if (tab_contents_->profile()->IsOffTheRecord()) | 147 if (tab_contents_->profile()->IsOffTheRecord()) |
| 140 return; | 148 return; |
| 141 | 149 |
| 142 // Don't save data that was submitted through JavaScript. | 150 // Don't save data that was submitted through JavaScript. |
| 143 if (!form.user_submitted) | 151 if (!form.user_submitted) |
| 144 return; | 152 return; |
| 145 | 153 |
| 146 // Grab a copy of the form data. | 154 // Grab a copy of the form data. |
| 147 upload_form_structure_.reset(new FormStructure(form)); | 155 upload_form_structure_.reset(new FormStructure(form)); |
| 156 if (locale_model_.tab_language_determined()) |
| 157 locale_model_.UpdateLocale(upload_form_structure_.get()); |
| 148 | 158 |
| 149 if (!upload_form_structure_->IsAutoFillable()) | 159 if (!upload_form_structure_->IsAutoFillable()) |
| 150 return; | 160 return; |
| 151 | 161 |
| 152 // Determine the possible field types and upload the form structure to the | 162 // Determine the possible field types and upload the form structure to the |
| 153 // PersonalDataManager. | 163 // PersonalDataManager. |
| 154 DeterminePossibleFieldTypes(upload_form_structure_.get()); | 164 DeterminePossibleFieldTypes(upload_form_structure_.get()); |
| 155 HandleSubmit(); | 165 HandleSubmit(); |
| 156 } | 166 } |
| 157 | 167 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 | 378 |
| 369 void AutoFillManager::ShowAutoFillDialog() { | 379 void AutoFillManager::ShowAutoFillDialog() { |
| 370 ::ShowAutoFillDialog(tab_contents_->GetContentNativeView(), | 380 ::ShowAutoFillDialog(tab_contents_->GetContentNativeView(), |
| 371 personal_data_, | 381 personal_data_, |
| 372 tab_contents_->profile()->GetOriginalProfile()); | 382 tab_contents_->profile()->GetOriginalProfile()); |
| 373 } | 383 } |
| 374 | 384 |
| 375 void AutoFillManager::Reset() { | 385 void AutoFillManager::Reset() { |
| 376 upload_form_structure_.reset(); | 386 upload_form_structure_.reset(); |
| 377 form_structures_.reset(); | 387 form_structures_.reset(); |
| 388 locale_model_.set_tab_language_determined(false); |
| 378 } | 389 } |
| 379 | 390 |
| 380 void AutoFillManager::OnLoadedAutoFillHeuristics( | 391 void AutoFillManager::OnLoadedAutoFillHeuristics( |
| 381 const std::string& heuristic_xml) { | 392 const std::string& heuristic_xml) { |
| 382 // TODO(jhawkins): Store |upload_required| in the AutoFillManager. | 393 // TODO(jhawkins): Store |upload_required| in the AutoFillManager. |
| 383 UploadRequired upload_required; | 394 UploadRequired upload_required; |
| 384 FormStructure::ParseQueryResponse(heuristic_xml, | 395 FormStructure::ParseQueryResponse(heuristic_xml, |
| 385 form_structures_.get(), | 396 form_structures_.get(), |
| 386 &upload_required); | 397 &upload_required); |
| 387 } | 398 } |
| 388 | 399 |
| 389 void AutoFillManager::OnUploadedAutoFillHeuristics( | 400 void AutoFillManager::OnUploadedAutoFillHeuristics( |
| 390 const std::string& form_signature) { | 401 const std::string& form_signature) { |
| 391 } | 402 } |
| 392 | 403 |
| 393 void AutoFillManager::OnHeuristicsRequestError( | 404 void AutoFillManager::OnHeuristicsRequestError( |
| 394 const std::string& form_signature, | 405 const std::string& form_signature, |
| 395 AutoFillDownloadManager::AutoFillRequestType request_type, | 406 AutoFillDownloadManager::AutoFillRequestType request_type, |
| 396 int http_error) { | 407 int http_error) { |
| 397 } | 408 } |
| 398 | 409 |
| 410 void AutoFillManager::Observe(NotificationType type, |
| 411 const NotificationSource& source, |
| 412 const NotificationDetails& details) { |
| 413 if (type != NotificationType::TAB_LANGUAGE_DETERMINED) { |
| 414 NOTREACHED(); |
| 415 return; |
| 416 } |
| 417 |
| 418 DCHECK(source == Source<TabContents>(tab_contents_)); |
| 419 |
| 420 // Now that the tab language is known, update the locales of the forms we've |
| 421 // seen. |
| 422 locale_model_.set_tab_language_determined(true); |
| 423 for (std::vector<FormStructure*>::iterator it = form_structures_.begin(); |
| 424 it != form_structures_.end(); ++it) { |
| 425 locale_model_.UpdateLocale(*it); |
| 426 } |
| 427 } |
| 428 |
| 399 bool AutoFillManager::IsAutoFillEnabled() const { | 429 bool AutoFillManager::IsAutoFillEnabled() const { |
| 400 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | 430 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| 401 | 431 |
| 402 // Migrate obsolete AutoFill pref. | 432 // Migrate obsolete AutoFill pref. |
| 403 if (prefs->FindPreference(prefs::kFormAutofillEnabled)) { | 433 if (prefs->FindPreference(prefs::kFormAutofillEnabled)) { |
| 404 bool enabled = prefs->GetBoolean(prefs::kFormAutofillEnabled); | 434 bool enabled = prefs->GetBoolean(prefs::kFormAutofillEnabled); |
| 405 prefs->ClearPref(prefs::kFormAutofillEnabled); | 435 prefs->ClearPref(prefs::kFormAutofillEnabled); |
| 406 prefs->SetBoolean(prefs::kAutoFillEnabled, enabled); | 436 prefs->SetBoolean(prefs::kAutoFillEnabled, enabled); |
| 407 return enabled; | 437 return enabled; |
| 408 } | 438 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 void AutoFillManager::OnInfoBarClosed(bool should_save) { | 498 void AutoFillManager::OnInfoBarClosed(bool should_save) { |
| 469 if (should_save) | 499 if (should_save) |
| 470 personal_data_->SaveImportedCreditCard(); | 500 personal_data_->SaveImportedCreditCard(); |
| 471 UploadFormData(); | 501 UploadFormData(); |
| 472 } | 502 } |
| 473 | 503 |
| 474 AutoFillManager::AutoFillManager() | 504 AutoFillManager::AutoFillManager() |
| 475 : tab_contents_(NULL), | 505 : tab_contents_(NULL), |
| 476 personal_data_(NULL), | 506 personal_data_(NULL), |
| 477 download_manager_(NULL), | 507 download_manager_(NULL), |
| 508 locale_model_(NULL), |
| 478 disable_download_manager_requests_(false) { | 509 disable_download_manager_requests_(false) { |
| 510 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, |
| 511 Source<TabContents>(tab_contents_)); |
| 479 } | 512 } |
| 480 | 513 |
| 481 AutoFillManager::AutoFillManager(TabContents* tab_contents, | 514 AutoFillManager::AutoFillManager(TabContents* tab_contents, |
| 482 PersonalDataManager* personal_data) | 515 PersonalDataManager* personal_data) |
| 483 : tab_contents_(tab_contents), | 516 : tab_contents_(tab_contents), |
| 484 personal_data_(personal_data), | 517 personal_data_(personal_data), |
| 485 download_manager_(NULL), | 518 download_manager_(NULL), |
| 519 locale_model_(tab_contents), |
| 486 disable_download_manager_requests_(false) { | 520 disable_download_manager_requests_(false) { |
| 487 DCHECK(tab_contents); | 521 DCHECK(tab_contents); |
| 522 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, |
| 523 Source<TabContents>(tab_contents_)); |
| 488 } | 524 } |
| 489 | 525 |
| 490 void AutoFillManager::GetProfileSuggestions(FormStructure* form, | 526 void AutoFillManager::GetProfileSuggestions(FormStructure* form, |
| 491 const FormField& field, | 527 const FormField& field, |
| 492 AutoFillType type, | 528 AutoFillType type, |
| 493 bool include_cc_labels, | 529 bool include_cc_labels, |
| 494 std::vector<string16>* values, | 530 std::vector<string16>* values, |
| 495 std::vector<string16>* labels, | 531 std::vector<string16>* labels, |
| 496 std::vector<string16>* icons, | 532 std::vector<string16>* icons, |
| 497 std::vector<int>* unique_ids) { | 533 std::vector<int>* unique_ids) { |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 number = number.substr(kAutoFillPhoneNumberSuffixOffset, | 801 number = number.substr(kAutoFillPhoneNumberSuffixOffset, |
| 766 kAutoFillPhoneNumberSuffixCount); | 802 kAutoFillPhoneNumberSuffixCount); |
| 767 field->set_value(number); | 803 field->set_value(number); |
| 768 } else { | 804 } else { |
| 769 field->set_value(number); | 805 field->set_value(number); |
| 770 } | 806 } |
| 771 } | 807 } |
| 772 | 808 |
| 773 void AutoFillManager::ParseForms( | 809 void AutoFillManager::ParseForms( |
| 774 const std::vector<webkit_glue::FormData>& forms) { | 810 const std::vector<webkit_glue::FormData>& forms) { |
| 775 for (std::vector<FormData>::const_iterator iter = | 811 for (std::vector<FormData>::const_iterator iter = forms.begin(); |
| 776 forms.begin(); | |
| 777 iter != forms.end(); ++iter) { | 812 iter != forms.end(); ++iter) { |
| 778 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); | 813 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); |
| 779 if (!form_structure->ShouldBeParsed()) | 814 if (!form_structure->ShouldBeParsed()) |
| 780 continue; | 815 continue; |
| 781 | 816 |
| 782 DeterminePossibleFieldTypes(form_structure.get()); | 817 DeterminePossibleFieldTypes(form_structure.get()); |
| 818 if (locale_model_.tab_language_determined()) |
| 819 locale_model_.UpdateLocale(form_structure.get()); |
| 820 |
| 783 form_structures_.push_back(form_structure.release()); | 821 form_structures_.push_back(form_structure.release()); |
| 784 } | 822 } |
| 785 | 823 |
| 786 // If none of the forms were parsed, no use querying the server. | 824 // If none of the forms were parsed, no use querying the server. |
| 787 if (!form_structures_.empty() && !disable_download_manager_requests_) | 825 if (!form_structures_.empty() && !disable_download_manager_requests_) |
| 788 download_manager_.StartQueryRequest(form_structures_); | 826 download_manager_.StartQueryRequest(form_structures_); |
| 789 } | 827 } |
| 790 | 828 |
| 791 // When sending IDs (across processes) to the renderer we pack credit card and | 829 // When sending IDs (across processes) to the renderer we pack credit card and |
| 792 // profile IDs into a single integer. Credit card IDs are sent in the high | 830 // profile IDs into a single integer. Credit card IDs are sent in the high |
| 793 // word and profile IDs are sent in the low word. | 831 // word and profile IDs are sent in the low word. |
| 794 // static | 832 // static |
| 795 int AutoFillManager::PackIDs(int cc_id, int profile_id) { | 833 int AutoFillManager::PackIDs(int cc_id, int profile_id) { |
| 796 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); | 834 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); |
| 797 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); | 835 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); |
| 798 | 836 |
| 799 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; | 837 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; |
| 800 } | 838 } |
| 801 | 839 |
| 802 // When receiving IDs (across processes) from the renderer we unpack credit card | 840 // When receiving IDs (across processes) from the renderer we unpack credit card |
| 803 // and profile IDs from a single integer. Credit card IDs are stored in the | 841 // and profile IDs from a single integer. Credit card IDs are stored in the |
| 804 // high word and profile IDs are stored in the low word. | 842 // high word and profile IDs are stored in the low word. |
| 805 // static | 843 // static |
| 806 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { | 844 void AutoFillManager::UnpackIDs(int id, int* cc_id, int* profile_id) { |
| 807 *cc_id = id >> std::numeric_limits<unsigned short>::digits & | 845 *cc_id = id >> std::numeric_limits<unsigned short>::digits & |
| 808 std::numeric_limits<unsigned short>::max(); | 846 std::numeric_limits<unsigned short>::max(); |
| 809 *profile_id = id & std::numeric_limits<unsigned short>::max(); | 847 *profile_id = id & std::numeric_limits<unsigned short>::max(); |
| 810 } | 848 } |
| OLD | NEW |