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 "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/chromium/chrome_metadata_source.h" | |
| 45 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h" | |
| 46 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" | |
| 43 | 47 |
| 44 using base::android::ConvertJavaStringToUTF8; | 48 using base::android::ConvertJavaStringToUTF8; |
| 45 using base::android::ConvertUTF16ToJavaString; | 49 using base::android::ConvertUTF16ToJavaString; |
| 46 using base::android::ConvertUTF8ToJavaString; | 50 using base::android::ConvertUTF8ToJavaString; |
| 47 using base::android::JavaParamRef; | 51 using base::android::JavaParamRef; |
| 48 using base::android::ScopedJavaGlobalRef; | 52 using base::android::ScopedJavaGlobalRef; |
| 49 using base::android::ScopedJavaLocalRef; | 53 using base::android::ScopedJavaLocalRef; |
| 50 | 54 |
| 51 namespace autofill { | 55 namespace autofill { |
| 52 namespace { | 56 namespace { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 Java_FullCardRequestDelegate_onFullCardError(env, jdelegate_); | 260 Java_FullCardRequestDelegate_onFullCardError(env, jdelegate_); |
| 257 delete this; | 261 delete this; |
| 258 } | 262 } |
| 259 | 263 |
| 260 std::unique_ptr<CreditCard> card_; | 264 std::unique_ptr<CreditCard> card_; |
| 261 ScopedJavaGlobalRef<jobject> jdelegate_; | 265 ScopedJavaGlobalRef<jobject> jdelegate_; |
| 262 | 266 |
| 263 DISALLOW_COPY_AND_ASSIGN(FullCardRequester); | 267 DISALLOW_COPY_AND_ASSIGN(FullCardRequester); |
| 264 }; | 268 }; |
| 265 | 269 |
| 270 // Self-deleting requester of address normalization. | |
| 271 class AddressNormalizationRequester | |
| 272 : public PersonalDataManagerAndroid::Delegate, | |
| 273 public base::SupportsWeakPtr<AddressNormalizationRequester> { | |
| 274 public: | |
| 275 AddressNormalizationRequester( | |
| 276 JNIEnv* env, | |
| 277 const base::android::JavaParamRef<jobject>& jdelegate, | |
| 278 const std::string& region_code, | |
| 279 const std::string& guid, | |
| 280 PersonalDataManagerAndroid* personal_data_manager_android) { | |
| 281 jdelegate_.Reset(env, jdelegate); | |
| 282 region_code_ = region_code; | |
| 283 guid_ = guid; | |
| 284 personal_data_manager_android_ = personal_data_manager_android; | |
| 285 env_ = env; | |
| 286 } | |
| 287 | |
| 288 private: | |
| 289 virtual ~AddressNormalizationRequester() {} | |
| 290 | |
| 291 void OnRulesSuccessfullyLoaded() override { | |
| 292 if (personal_data_manager_android_) { | |
| 293 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 294 Java_NormalizedAddressRequestDelegate_onAddressNormalized( | |
| 295 env, jdelegate_, personal_data_manager_android_->NormalizeAddress( | |
| 296 guid_, region_code_, env)); | |
| 297 } | |
| 298 | |
| 299 delete this; | |
| 300 } | |
| 301 | |
| 302 ScopedJavaGlobalRef<jobject> jdelegate_; | |
| 303 std::string guid_; | |
| 304 std::string region_code_; | |
| 305 PersonalDataManagerAndroid* personal_data_manager_android_; | |
| 306 JNIEnv* env_; | |
| 307 | |
| 308 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequester); | |
| 309 }; | |
| 310 | |
| 266 } // namespace | 311 } // namespace |
| 267 | 312 |
| 268 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, | 313 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, |
| 269 jobject obj) | 314 jobject obj) |
| 270 : weak_java_obj_(env, obj), | 315 : weak_java_obj_(env, obj), |
| 271 personal_data_manager_(PersonalDataManagerFactory::GetForProfile( | 316 personal_data_manager_(PersonalDataManagerFactory::GetForProfile( |
| 272 ProfileManager::GetActiveUserProfile())) { | 317 ProfileManager::GetActiveUserProfile())) { |
| 273 personal_data_manager_->AddObserver(this); | 318 personal_data_manager_->AddObserver(this); |
| 319 | |
| 320 address_validator_.reset(new AddressValidator( | |
|
vabr (Chromium)
2016/09/28 08:41:36
Unless |address_validator_| needs to be constructe
sebsg
2016/09/28 16:15:24
Thanks, coming from C# this was an interesting rea
| |
| 321 std::unique_ptr<::i18n::addressinput::Source>( | |
| 322 new autofill::ChromeMetadataSource( | |
| 323 I18N_ADDRESS_VALIDATION_DATA_URL, | |
| 324 personal_data_manager_->GetURLRequestContextGetter())), | |
| 325 ValidationRulesStorageFactory::CreateStorage(), this)); | |
| 274 } | 326 } |
| 275 | 327 |
| 276 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { | 328 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { |
| 277 personal_data_manager_->RemoveObserver(this); | 329 personal_data_manager_->RemoveObserver(this); |
| 278 } | 330 } |
| 279 | 331 |
| 280 jboolean PersonalDataManagerAndroid::IsDataLoaded( | 332 jboolean PersonalDataManagerAndroid::IsDataLoaded( |
| 281 JNIEnv* env, | 333 JNIEnv* env, |
| 282 const base::android::JavaParamRef<jobject>& unused_obj) const { | 334 const base::android::JavaParamRef<jobject>& unused_obj) const { |
| 283 return personal_data_manager_->IsDataLoaded(); | 335 return personal_data_manager_->IsDataLoaded(); |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 594 return card->use_date().ToTimeT(); | 646 return card->use_date().ToTimeT(); |
| 595 } | 647 } |
| 596 | 648 |
| 597 // TODO(crbug.com/629507): Use a mock clock for testing. | 649 // TODO(crbug.com/629507): Use a mock clock for testing. |
| 598 jlong PersonalDataManagerAndroid::GetCurrentDateForTesting( | 650 jlong PersonalDataManagerAndroid::GetCurrentDateForTesting( |
| 599 JNIEnv* env, | 651 JNIEnv* env, |
| 600 const base::android::JavaParamRef<jobject>& unused_obj) { | 652 const base::android::JavaParamRef<jobject>& unused_obj) { |
| 601 return base::Time::Now().ToTimeT(); | 653 return base::Time::Now().ToTimeT(); |
| 602 } | 654 } |
| 603 | 655 |
| 656 void PersonalDataManagerAndroid::LoadRulesForRegion( | |
| 657 JNIEnv* env, | |
| 658 const base::android::JavaParamRef<jobject>& unused_obj, | |
| 659 const base::android::JavaParamRef<jstring>& jregion_code) { | |
| 660 address_validator_->LoadRules(ConvertJavaStringToUTF8(env, jregion_code)); | |
| 661 } | |
| 662 | |
| 663 void PersonalDataManagerAndroid::OnAddressValidationRulesLoaded( | |
| 664 const std::string& region_code, | |
| 665 bool success) { | |
| 666 // Check if an address normalization is pending. | |
| 667 std::map<std::string, Delegate*>::iterator it = | |
| 668 pending_normalization_.find(region_code); | |
| 669 if (it != pending_normalization_.end()) { | |
| 670 // The AddressNormalizationRequester will self delete after normalizing. | |
|
vabr (Chromium)
2016/09/28 08:41:36
optional nit: Perhaps call it Delegate instead of
sebsg
2016/09/28 16:15:25
Done.
| |
| 671 it->second->OnRulesSuccessfullyLoaded(); | |
| 672 pending_normalization_.erase(it); | |
| 673 } | |
| 674 } | |
| 675 | |
| 676 void PersonalDataManagerAndroid::StartAddressNormalizationTask( | |
| 677 JNIEnv* env, | |
| 678 const JavaParamRef<jobject>& unused_obj, | |
| 679 const JavaParamRef<jstring>& jguid, | |
| 680 const JavaParamRef<jstring>& jregion_code, | |
| 681 const JavaParamRef<jobject>& jdelegate) { | |
| 682 std::string region_code = ConvertJavaStringToUTF8(env, jregion_code); | |
|
vabr (Chromium)
2016/09/28 08:41:36
optional: Mark both strings as const?
sebsg
2016/09/28 16:15:25
Done.
| |
| 683 std::string guid = ConvertJavaStringToUTF8(env, jguid); | |
| 684 | |
| 685 Delegate* requester = new AddressNormalizationRequester( | |
| 686 env, jdelegate, region_code, guid, this); | |
| 687 | |
| 688 // Check if the rules are already loaded. | |
| 689 if (AreRulesLoadedForRegion(region_code)) { | |
| 690 requester->OnRulesSuccessfullyLoaded(); | |
| 691 } else { | |
| 692 // Setup the variables so the profile gets normalized when the rules have | |
| 693 // finished loading. | |
| 694 pending_normalization_.insert( | |
| 695 std::pair<std::string, Delegate*>(region_code, requester)); | |
| 696 } | |
| 697 } | |
| 698 | |
| 699 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::NormalizeAddress( | |
| 700 const std::string& guid, | |
| 701 const std::string& region_code, | |
| 702 JNIEnv* env) { | |
| 703 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(guid); | |
| 704 | |
| 705 if (!profile || !AreRulesLoadedForRegion(region_code)) | |
| 706 return nullptr; | |
| 707 | |
| 708 // Create the AddressData from the profile. | |
| 709 ::i18n::addressinput::AddressData address_data = | |
| 710 *i18n::CreateAddressDataFromAutofillProfile( | |
| 711 *profile, personal_data_manager_->app_locale()); | |
| 712 | |
| 713 // Normalize the address. | |
| 714 if (address_validator_->NormalizeAddress(&address_data)) { | |
| 715 // Set the normalized state, city and dependent locality. | |
| 716 profile->SetRawInfo(ADDRESS_HOME_STATE, | |
| 717 base::UTF8ToUTF16(address_data.administrative_area)); | |
| 718 profile->SetRawInfo(ADDRESS_HOME_CITY, | |
| 719 base::UTF8ToUTF16(address_data.locality)); | |
| 720 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, | |
| 721 base::UTF8ToUTF16(address_data.dependent_locality)); | |
| 722 } | |
| 723 | |
| 724 return CreateJavaProfileFromNative(env, *profile); | |
| 725 } | |
| 726 | |
| 727 void PersonalDataManagerAndroid::CancelPendingAddressNormalization(JNIEnv* env, | |
| 728 const base::android::JavaParamRef<jobject>& unused_obj) { | |
| 729 pending_normalization_.clear(); | |
| 730 } | |
| 731 | |
| 604 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs( | 732 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs( |
| 605 JNIEnv* env, | 733 JNIEnv* env, |
| 606 const std::vector<AutofillProfile*>& profiles) { | 734 const std::vector<AutofillProfile*>& profiles) { |
| 607 std::vector<base::string16> guids; | 735 std::vector<base::string16> guids; |
| 608 for (AutofillProfile* profile : profiles) | 736 for (AutofillProfile* profile : profiles) |
| 609 guids.push_back(base::UTF8ToUTF16(profile->guid())); | 737 guids.push_back(base::UTF8ToUTF16(profile->guid())); |
| 610 | 738 |
| 611 return base::android::ToJavaArrayOfStrings(env, guids); | 739 return base::android::ToJavaArrayOfStrings(env, guids); |
| 612 } | 740 } |
| 613 | 741 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 ServerFieldType excluded_field = include_name ? UNKNOWN_TYPE : NAME_FULL; | 775 ServerFieldType excluded_field = include_name ? UNKNOWN_TYPE : NAME_FULL; |
| 648 | 776 |
| 649 std::vector<base::string16> labels; | 777 std::vector<base::string16> labels; |
| 650 AutofillProfile::CreateInferredLabels( | 778 AutofillProfile::CreateInferredLabels( |
| 651 profiles, suggested_fields.get(), excluded_field, minimal_fields_shown, | 779 profiles, suggested_fields.get(), excluded_field, minimal_fields_shown, |
| 652 g_browser_process->GetApplicationLocale(), &labels); | 780 g_browser_process->GetApplicationLocale(), &labels); |
| 653 | 781 |
| 654 return base::android::ToJavaArrayOfStrings(env, labels); | 782 return base::android::ToJavaArrayOfStrings(env, labels); |
| 655 } | 783 } |
| 656 | 784 |
| 785 bool PersonalDataManagerAndroid::AreRulesLoadedForRegion( | |
| 786 const std::string& region_code) { | |
| 787 return address_validator_->AreRulesLoadedForRegion(region_code); | |
| 788 } | |
| 789 | |
| 657 // Returns whether the Autofill feature is enabled. | 790 // Returns whether the Autofill feature is enabled. |
| 658 static jboolean IsAutofillEnabled(JNIEnv* env, | 791 static jboolean IsAutofillEnabled(JNIEnv* env, |
| 659 const JavaParamRef<jclass>& clazz) { | 792 const JavaParamRef<jclass>& clazz) { |
| 660 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled); | 793 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled); |
| 661 } | 794 } |
| 662 | 795 |
| 663 // Enables or disables the Autofill feature. | 796 // Enables or disables the Autofill feature. |
| 664 static void SetAutofillEnabled(JNIEnv* env, | 797 static void SetAutofillEnabled(JNIEnv* env, |
| 665 const JavaParamRef<jclass>& clazz, | 798 const JavaParamRef<jclass>& clazz, |
| 666 jboolean enable) { | 799 jboolean enable) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 698 base::android::ConvertJavaStringToUTF16(env, jcountry_name))); | 831 base::android::ConvertJavaStringToUTF16(env, jcountry_name))); |
| 699 } | 832 } |
| 700 | 833 |
| 701 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 834 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 702 PersonalDataManagerAndroid* personal_data_manager_android = | 835 PersonalDataManagerAndroid* personal_data_manager_android = |
| 703 new PersonalDataManagerAndroid(env, obj); | 836 new PersonalDataManagerAndroid(env, obj); |
| 704 return reinterpret_cast<intptr_t>(personal_data_manager_android); | 837 return reinterpret_cast<intptr_t>(personal_data_manager_android); |
| 705 } | 838 } |
| 706 | 839 |
| 707 } // namespace autofill | 840 } // namespace autofill |
| OLD | NEW |