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 "chrome/browser/autofill/android/personal_data_manager_android.h" | 5 #include "chrome/browser/autofill/android/personal_data_manager_android.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "chrome/browser/android/resource_mapper.h" | 20 #include "chrome/browser/android/resource_mapper.h" |
21 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 21 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/profiles/profile_manager.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "components/autofill/content/browser/content_autofill_driver.h" | 26 #include "components/autofill/content/browser/content_autofill_driver.h" |
27 #include "components/autofill/content/browser/content_autofill_driver_factory.h" | 27 #include "components/autofill/content/browser/content_autofill_driver_factory.h" |
28 #include "components/autofill/core/browser/address_i18n.h" | |
28 #include "components/autofill/core/browser/autofill_country.h" | 29 #include "components/autofill/core/browser/autofill_country.h" |
29 #include "components/autofill/core/browser/autofill_data_util.h" | 30 #include "components/autofill/core/browser/autofill_data_util.h" |
30 #include "components/autofill/core/browser/autofill_type.h" | 31 #include "components/autofill/core/browser/autofill_type.h" |
31 #include "components/autofill/core/browser/country_names.h" | 32 #include "components/autofill/core/browser/country_names.h" |
32 #include "components/autofill/core/browser/field_types.h" | 33 #include "components/autofill/core/browser/field_types.h" |
33 #include "components/autofill/core/browser/payments/full_card_request.h" | 34 #include "components/autofill/core/browser/payments/full_card_request.h" |
34 #include "components/autofill/core/browser/personal_data_manager.h" | 35 #include "components/autofill/core/browser/personal_data_manager.h" |
35 #include "components/autofill/core/browser/validation.h" | 36 #include "components/autofill/core/browser/validation.h" |
36 #include "components/autofill/core/common/autofill_constants.h" | 37 #include "components/autofill/core/common/autofill_constants.h" |
37 #include "components/autofill/core/common/autofill_pref_names.h" | 38 #include "components/autofill/core/common/autofill_pref_names.h" |
38 #include "components/autofill/core/common/autofill_switches.h" | 39 #include "components/autofill/core/common/autofill_switches.h" |
39 #include "components/grit/components_scaled_resources.h" | 40 #include "components/grit/components_scaled_resources.h" |
40 #include "components/prefs/pref_service.h" | 41 #include "components/prefs/pref_service.h" |
41 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
42 #include "jni/PersonalDataManager_jni.h" | 43 #include "jni/PersonalDataManager_jni.h" |
44 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" | |
43 | 45 |
44 using base::android::ConvertJavaStringToUTF8; | 46 using base::android::ConvertJavaStringToUTF8; |
45 using base::android::ConvertUTF16ToJavaString; | 47 using base::android::ConvertUTF16ToJavaString; |
46 using base::android::ConvertUTF8ToJavaString; | 48 using base::android::ConvertUTF8ToJavaString; |
47 using base::android::JavaParamRef; | 49 using base::android::JavaParamRef; |
48 using base::android::ScopedJavaGlobalRef; | 50 using base::android::ScopedJavaGlobalRef; |
49 using base::android::ScopedJavaLocalRef; | 51 using base::android::ScopedJavaLocalRef; |
50 | 52 |
51 namespace autofill { | 53 namespace autofill { |
52 namespace { | 54 namespace { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 card->set_record_type(CreditCard::MASKED_SERVER_CARD); | 189 card->set_record_type(CreditCard::MASKED_SERVER_CARD); |
188 card->SetTypeForMaskedCard( | 190 card->SetTypeForMaskedCard( |
189 data_util::GetCardTypeForBasicCardPaymentType(ConvertJavaStringToUTF8( | 191 data_util::GetCardTypeForBasicCardPaymentType(ConvertJavaStringToUTF8( |
190 env, Java_CreditCard_getBasicCardPaymentType(env, jcard)))); | 192 env, Java_CreditCard_getBasicCardPaymentType(env, jcard)))); |
191 } | 193 } |
192 } | 194 } |
193 } | 195 } |
194 | 196 |
195 // Self-deleting requester of full card details, including full PAN and the CVC | 197 // Self-deleting requester of full card details, including full PAN and the CVC |
196 // number. | 198 // number. |
197 class FullCardRequester : public payments::FullCardRequest::Delegate, | 199 class FullCardRequester : public payments::FullCardRequest::Delegate, |
please use gerrit instead
2016/09/23 09:04:40
Is it possible to use this pattern of self-deletin
sebsg
2016/09/27 18:48:38
Done.
| |
198 public base::SupportsWeakPtr<FullCardRequester> { | 200 public base::SupportsWeakPtr<FullCardRequester> { |
199 public: | 201 public: |
200 FullCardRequester() {} | 202 FullCardRequester() {} |
201 | 203 |
202 // Takes ownership of |card|. | 204 // Takes ownership of |card|. |
203 void GetFullCard(JNIEnv* env, | 205 void GetFullCard(JNIEnv* env, |
204 const base::android::JavaParamRef<jobject>& jweb_contents, | 206 const base::android::JavaParamRef<jobject>& jweb_contents, |
205 const base::android::JavaParamRef<jobject>& jdelegate, | 207 const base::android::JavaParamRef<jobject>& jdelegate, |
206 std::unique_ptr<CreditCard> card) { | 208 std::unique_ptr<CreditCard> card) { |
207 card_ = std::move(card); | 209 card_ = std::move(card); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 }; | 266 }; |
265 | 267 |
266 } // namespace | 268 } // namespace |
267 | 269 |
268 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, | 270 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, |
269 jobject obj) | 271 jobject obj) |
270 : weak_java_obj_(env, obj), | 272 : weak_java_obj_(env, obj), |
271 personal_data_manager_(PersonalDataManagerFactory::GetForProfile( | 273 personal_data_manager_(PersonalDataManagerFactory::GetForProfile( |
272 ProfileManager::GetActiveUserProfile())) { | 274 ProfileManager::GetActiveUserProfile())) { |
273 personal_data_manager_->AddObserver(this); | 275 personal_data_manager_->AddObserver(this); |
276 | |
277 address_validator_.reset(new AddressValidator( | |
278 std::unique_ptr<::i18n::addressinput::Source>( | |
279 new autofill::ChromeMetadataSource( | |
280 I18N_ADDRESS_VALIDATION_DATA_URL, | |
281 personal_data_manager_->GetURLRequestContextGetter())), | |
282 ValidationRulesStorageFactory::CreateStorage(), this)); | |
274 } | 283 } |
275 | 284 |
276 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { | 285 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { |
277 personal_data_manager_->RemoveObserver(this); | 286 personal_data_manager_->RemoveObserver(this); |
278 } | 287 } |
279 | 288 |
280 jboolean PersonalDataManagerAndroid::IsDataLoaded( | 289 jboolean PersonalDataManagerAndroid::IsDataLoaded( |
281 JNIEnv* env, | 290 JNIEnv* env, |
282 const base::android::JavaParamRef<jobject>& unused_obj) const { | 291 const base::android::JavaParamRef<jobject>& unused_obj) const { |
283 return personal_data_manager_->IsDataLoaded(); | 292 return personal_data_manager_->IsDataLoaded(); |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
594 return card->use_date().ToTimeT(); | 603 return card->use_date().ToTimeT(); |
595 } | 604 } |
596 | 605 |
597 // TODO(crbug.com/629507): Use a mock clock for testing. | 606 // TODO(crbug.com/629507): Use a mock clock for testing. |
598 jlong PersonalDataManagerAndroid::GetCurrentDateForTesting( | 607 jlong PersonalDataManagerAndroid::GetCurrentDateForTesting( |
599 JNIEnv* env, | 608 JNIEnv* env, |
600 const base::android::JavaParamRef<jobject>& unused_obj) { | 609 const base::android::JavaParamRef<jobject>& unused_obj) { |
601 return base::Time::Now().ToTimeT(); | 610 return base::Time::Now().ToTimeT(); |
602 } | 611 } |
603 | 612 |
613 void PersonalDataManagerAndroid::LoadRulesForRegion( | |
614 JNIEnv* env, | |
615 const base::android::JavaParamRef<jobject>& unused_obj, | |
616 const base::android::JavaParamRef<jstring>& jregion_code) { | |
617 address_validator_->LoadRules(ConvertJavaStringToUTF8(env, jregion_code)); | |
618 } | |
619 | |
620 void PersonalDataManagerAndroid::StartAddressNormalizationTask( | |
621 JNIEnv* env, | |
622 const JavaParamRef<jobject>& unused_obj, | |
623 const JavaParamRef<jstring>& jguid, | |
624 const JavaParamRef<jstring>& jregion_code) { | |
625 std::string region_code = ConvertJavaStringToUTF8(env, jregion_code); | |
626 | |
627 // Check if the rules are already done loading. | |
628 if (AreRulesDoneLoadingForRegion(region_code)) { | |
629 NormalizeAddress(ConvertJavaStringToUTF8(env, jguid), region_code); | |
630 } else { | |
631 // Setup the variables so the profile gets normalized when the rules have | |
632 // finished loading. | |
633 pending_normalization_profile_guid_ = ConvertJavaStringToUTF8(env, jguid); | |
634 pending_normalization_region_code_ = region_code; | |
635 } | |
636 } | |
637 | |
638 void PersonalDataManagerAndroid::CancelAddressNormalizationTask( | |
639 JNIEnv* env, | |
640 const base::android::JavaParamRef<jobject>& unused_obj) { | |
641 pending_normalization_profile_guid_ = ""; | |
please use gerrit instead
2016/09/23 09:04:40
pending_normalization_profile_guid_.clear();
sebsg
2016/09/27 18:48:38
Done.
| |
642 pending_normalization_region_code_ = ""; | |
please use gerrit instead
2016/09/23 09:04:40
pending_normalization_region_code_.clear();
sebsg
2016/09/27 18:48:38
Done.
| |
643 } | |
644 | |
645 void PersonalDataManagerAndroid::NormalizeAddress(std::string guid, | |
646 std::string region_code) { | |
647 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(guid); | |
648 | |
649 if (!profile || !AreRulesDoneLoadingForRegion(region_code) || | |
650 !AreRulesSuccessfullyLoadedForRegion(region_code)) | |
651 return; | |
652 | |
653 // Create the AddressData from the profile. | |
654 ::i18n::addressinput::AddressData address_data = | |
655 *i18n::CreateAddressDataFromAutofillProfile( | |
656 *profile, personal_data_manager_->app_locale()); | |
657 | |
658 // Normalize the address. | |
659 if (address_validator_->NormalizeAddress(&address_data)) { | |
660 // Set the normalized state, city and dependent locality. | |
661 profile->SetRawInfo(ADDRESS_HOME_STATE, | |
662 base::UTF8ToUTF16(address_data.administrative_area)); | |
663 profile->SetRawInfo(ADDRESS_HOME_CITY, | |
664 base::UTF8ToUTF16(address_data.locality)); | |
665 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, | |
666 base::UTF8ToUTF16(address_data.dependent_locality)); | |
667 } | |
668 | |
669 JNIEnv* env = base::android::AttachCurrentThread(); | |
670 if (weak_java_obj_.get(env).is_null()) | |
671 return; | |
672 | |
673 Java_PersonalDataManager_onAddressNormalized( | |
674 env, weak_java_obj_.get(env), CreateJavaProfileFromNative(env, *profile)); | |
675 } | |
676 | |
677 void PersonalDataManagerAndroid::OnAddressValidationRulesLoaded( | |
678 const std::string& region_code, | |
679 bool success) { | |
680 loaded_rules_[region_code] = success; | |
681 | |
682 // Check if an address normalization is pending. | |
683 if (pending_normalization_profile_guid_ != "" && | |
please use gerrit instead
2016/09/23 09:04:40
!pending_normalization_profile_guid_.empty()
sebsg
2016/09/27 18:48:38
Done.
| |
684 pending_normalization_region_code_ != "") { | |
please use gerrit instead
2016/09/23 09:04:40
!pending_normalization_region_code_.empty()
sebsg
2016/09/27 18:48:38
Done.
| |
685 NormalizeAddress(pending_normalization_profile_guid_, | |
686 pending_normalization_region_code_); | |
687 pending_normalization_profile_guid_ = ""; | |
please use gerrit instead
2016/09/23 09:04:40
.clear()
sebsg
2016/09/27 18:48:38
Done.
| |
688 pending_normalization_region_code_ = ""; | |
please use gerrit instead
2016/09/23 09:04:40
.clear()
sebsg
2016/09/27 18:48:38
Done.
| |
689 } | |
690 } | |
691 | |
604 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs( | 692 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs( |
605 JNIEnv* env, | 693 JNIEnv* env, |
606 const std::vector<AutofillProfile*>& profiles) { | 694 const std::vector<AutofillProfile*>& profiles) { |
607 std::vector<base::string16> guids; | 695 std::vector<base::string16> guids; |
608 for (AutofillProfile* profile : profiles) | 696 for (AutofillProfile* profile : profiles) |
609 guids.push_back(base::UTF8ToUTF16(profile->guid())); | 697 guids.push_back(base::UTF8ToUTF16(profile->guid())); |
610 | 698 |
611 return base::android::ToJavaArrayOfStrings(env, guids); | 699 return base::android::ToJavaArrayOfStrings(env, guids); |
612 } | 700 } |
613 | 701 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 ServerFieldType excluded_field = include_name ? UNKNOWN_TYPE : NAME_FULL; | 735 ServerFieldType excluded_field = include_name ? UNKNOWN_TYPE : NAME_FULL; |
648 | 736 |
649 std::vector<base::string16> labels; | 737 std::vector<base::string16> labels; |
650 AutofillProfile::CreateInferredLabels( | 738 AutofillProfile::CreateInferredLabels( |
651 profiles, suggested_fields.get(), excluded_field, minimal_fields_shown, | 739 profiles, suggested_fields.get(), excluded_field, minimal_fields_shown, |
652 g_browser_process->GetApplicationLocale(), &labels); | 740 g_browser_process->GetApplicationLocale(), &labels); |
653 | 741 |
654 return base::android::ToJavaArrayOfStrings(env, labels); | 742 return base::android::ToJavaArrayOfStrings(env, labels); |
655 } | 743 } |
656 | 744 |
745 bool PersonalDataManagerAndroid::AreRulesDoneLoadingForRegion( | |
746 std::string region_code) { | |
747 return loaded_rules_.find(region_code) != loaded_rules_.end(); | |
748 } | |
749 | |
750 bool PersonalDataManagerAndroid::AreRulesSuccessfullyLoadedForRegion( | |
751 std::string region_code) { | |
752 std::map<std::string, bool>::iterator it = loaded_rules_.find(region_code); | |
753 return it != loaded_rules_.end() ? it->second : false; | |
754 } | |
755 | |
657 // Returns whether the Autofill feature is enabled. | 756 // Returns whether the Autofill feature is enabled. |
658 static jboolean IsAutofillEnabled(JNIEnv* env, | 757 static jboolean IsAutofillEnabled(JNIEnv* env, |
659 const JavaParamRef<jclass>& clazz) { | 758 const JavaParamRef<jclass>& clazz) { |
660 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled); | 759 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled); |
661 } | 760 } |
662 | 761 |
663 // Enables or disables the Autofill feature. | 762 // Enables or disables the Autofill feature. |
664 static void SetAutofillEnabled(JNIEnv* env, | 763 static void SetAutofillEnabled(JNIEnv* env, |
665 const JavaParamRef<jclass>& clazz, | 764 const JavaParamRef<jclass>& clazz, |
666 jboolean enable) { | 765 jboolean enable) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
698 base::android::ConvertJavaStringToUTF16(env, jcountry_name))); | 797 base::android::ConvertJavaStringToUTF16(env, jcountry_name))); |
699 } | 798 } |
700 | 799 |
701 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 800 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
702 PersonalDataManagerAndroid* personal_data_manager_android = | 801 PersonalDataManagerAndroid* personal_data_manager_android = |
703 new PersonalDataManagerAndroid(env, obj); | 802 new PersonalDataManagerAndroid(env, obj); |
704 return reinterpret_cast<intptr_t>(personal_data_manager_android); | 803 return reinterpret_cast<intptr_t>(personal_data_manager_android); |
705 } | 804 } |
706 | 805 |
707 } // namespace autofill | 806 } // namespace autofill |
OLD | NEW |