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

Side by Side Diff: chrome/browser/autofill/android/personal_data_manager_android.cc

Issue 2879853003: [Payments] Code Refactoring for the Subkey Request (Closed)
Patch Set: Refactoring Created 3 years, 7 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
sebsg 2017/05/18 17:50:51 nit: Please remove this empty line
Parastoo 2017/05/18 20:59:45 Done.
1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 3 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 4 // found in the LICENSE file.
4 5
5 #include "chrome/browser/autofill/android/personal_data_manager_android.h" 6 #include "chrome/browser/autofill/android/personal_data_manager_android.h"
6 7
7 #include <stddef.h> 8 #include <stddef.h>
8 #include <algorithm> 9 #include <algorithm>
9 #include <memory> 10 #include <memory>
10 #include <utility> 11 #include <utility>
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 302
302 private: 303 private:
303 ~AndroidAddressNormalizerDelegate() override {} 304 ~AndroidAddressNormalizerDelegate() override {}
304 305
305 ScopedJavaGlobalRef<jobject> jdelegate_; 306 ScopedJavaGlobalRef<jobject> jdelegate_;
306 307
307 DISALLOW_COPY_AND_ASSIGN(AndroidAddressNormalizerDelegate); 308 DISALLOW_COPY_AND_ASSIGN(AndroidAddressNormalizerDelegate);
308 }; 309 };
309 310
310 class AndroidSubKeyRequesterDelegate 311 class AndroidSubKeyRequesterDelegate
311 : public PersonalDataManagerAndroid::SubKeyRequestDelegate, 312 : public ::payments::SubKeyRequester::Delegate,
312 public base::SupportsWeakPtr<AndroidSubKeyRequesterDelegate> { 313 public base::SupportsWeakPtr<AndroidSubKeyRequesterDelegate> {
313 public: 314 public:
314 AndroidSubKeyRequesterDelegate( 315 AndroidSubKeyRequesterDelegate(
315 JNIEnv* env, 316 JNIEnv* env,
316 const base::android::JavaParamRef<jobject>& jdelegate, 317 const base::android::JavaParamRef<jobject>& jdelegate) {
317 const std::string& region_code,
318 base::WeakPtr<PersonalDataManagerAndroid> personal_data_manager_android) {
319 jdelegate_.Reset(env, jdelegate); 318 jdelegate_.Reset(env, jdelegate);
320 region_code_ = region_code;
321 personal_data_manager_android_ = personal_data_manager_android;
322 } 319 }
323 320
321 void OnSubKeysReceived(std::vector<std::string> sub_keys) override {
322 JNIEnv* env = base::android::AttachCurrentThread();
323 Java_GetSubKeysRequestDelegate_onSubKeysReceived(
324 env, jdelegate_, base::android::ToJavaArrayOfStrings(env, sub_keys));
325 delete this;
326 }
327
328 private:
324 ~AndroidSubKeyRequesterDelegate() override {} 329 ~AndroidSubKeyRequesterDelegate() override {}
325 330
326 private:
327 // PersonalDataManagerAndroid::SubKeyRequestDelegate:
328 void OnRulesSuccessfullyLoaded() override {
329 if (personal_data_manager_android_) {
330 JNIEnv* env = base::android::AttachCurrentThread();
331 Java_GetSubKeysRequestDelegate_onSubKeysReceived(
332 env, jdelegate_,
333 personal_data_manager_android_->GetSubKeys(env, region_code_));
334 }
335 }
336
337 ScopedJavaGlobalRef<jobject> jdelegate_; 331 ScopedJavaGlobalRef<jobject> jdelegate_;
338 std::string region_code_;
339 base::WeakPtr<PersonalDataManagerAndroid> personal_data_manager_android_;
340 332
341 DISALLOW_COPY_AND_ASSIGN(AndroidSubKeyRequesterDelegate); 333 DISALLOW_COPY_AND_ASSIGN(AndroidSubKeyRequesterDelegate);
342 }; 334 };
343 335
344 } // namespace 336 } // namespace
345 337
346 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, jobject obj) 338 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env, jobject obj)
347 : weak_java_obj_(env, obj), 339 : weak_java_obj_(env, obj),
348 personal_data_manager_(PersonalDataManagerFactory::GetForProfile( 340 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
349 ProfileManager::GetActiveUserProfile())), 341 ProfileManager::GetActiveUserProfile())),
350 address_normalizer_( 342 address_normalizer_(
351 std::unique_ptr<::i18n::addressinput::Source>( 343 std::unique_ptr<::i18n::addressinput::Source>(
352 new autofill::ChromeMetadataSource( 344 new autofill::ChromeMetadataSource(
353 I18N_ADDRESS_VALIDATION_DATA_URL, 345 I18N_ADDRESS_VALIDATION_DATA_URL,
354 personal_data_manager_->GetURLRequestContextGetter())), 346 personal_data_manager_->GetURLRequestContextGetter())),
355 ValidationRulesStorageFactory::CreateStorage()), 347 ValidationRulesStorageFactory::CreateStorage()),
356 address_validator_( 348 subkey_requester_(
357 base::MakeUnique<autofill::ChromeMetadataSource>( 349 std::unique_ptr<::i18n::addressinput::Source>(
sebsg 2017/05/18 17:50:51 Can you use MakeUnique? It's generally the preferr
Parastoo 2017/05/18 20:59:45 Done.
358 I18N_ADDRESS_VALIDATION_DATA_URL, 350 new autofill::ChromeMetadataSource(
359 personal_data_manager_->GetURLRequestContextGetter()), 351 I18N_ADDRESS_VALIDATION_DATA_URL,
360 ValidationRulesStorageFactory::CreateStorage(), 352 personal_data_manager_->GetURLRequestContextGetter())),
361 this) { 353 ValidationRulesStorageFactory::CreateStorage()) {
362 personal_data_manager_->AddObserver(this); 354 personal_data_manager_->AddObserver(this);
363 } 355 }
364 356
365 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { 357 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
366 personal_data_manager_->RemoveObserver(this); 358 personal_data_manager_->RemoveObserver(this);
367 } 359 }
368 360
369 jboolean PersonalDataManagerAndroid::IsDataLoaded( 361 jboolean PersonalDataManagerAndroid::IsDataLoaded(
370 JNIEnv* env, 362 JNIEnv* env,
371 const base::android::JavaParamRef<jobject>& unused_obj) const { 363 const base::android::JavaParamRef<jobject>& unused_obj) const {
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 const base::android::JavaParamRef<jobject>& unused_obj, 737 const base::android::JavaParamRef<jobject>& unused_obj,
746 const base::android::JavaParamRef<jstring>& jregion_code) { 738 const base::android::JavaParamRef<jstring>& jregion_code) {
747 address_normalizer_.LoadRulesForRegion( 739 address_normalizer_.LoadRulesForRegion(
748 ConvertJavaStringToUTF8(env, jregion_code)); 740 ConvertJavaStringToUTF8(env, jregion_code));
749 } 741 }
750 742
751 void PersonalDataManagerAndroid::LoadRulesForSubKeys( 743 void PersonalDataManagerAndroid::LoadRulesForSubKeys(
752 JNIEnv* env, 744 JNIEnv* env,
753 const base::android::JavaParamRef<jobject>& unused_obj, 745 const base::android::JavaParamRef<jobject>& unused_obj,
754 const base::android::JavaParamRef<jstring>& jregion_code) { 746 const base::android::JavaParamRef<jstring>& jregion_code) {
755 address_validator_.LoadRules(ConvertJavaStringToUTF8(env, jregion_code)); 747 subkey_requester_.LoadRulesForRegion(
748 ConvertJavaStringToUTF8(env, jregion_code));
756 } 749 }
757 750
758 void PersonalDataManagerAndroid::StartAddressNormalization( 751 void PersonalDataManagerAndroid::StartAddressNormalization(
759 JNIEnv* env, 752 JNIEnv* env,
760 const JavaParamRef<jobject>& unused_obj, 753 const JavaParamRef<jobject>& unused_obj,
761 const JavaParamRef<jobject>& jprofile, 754 const JavaParamRef<jobject>& jprofile,
762 const JavaParamRef<jstring>& jregion_code, 755 const JavaParamRef<jstring>& jregion_code,
763 jint jtimeout_seconds, 756 jint jtimeout_seconds,
764 const JavaParamRef<jobject>& jdelegate) { 757 const JavaParamRef<jobject>& jdelegate) {
765 const std::string region_code = ConvertJavaStringToUTF8(env, jregion_code); 758 const std::string region_code = ConvertJavaStringToUTF8(env, jregion_code);
(...skipping 15 matching lines...) Expand all
781 const base::android::JavaParamRef<jobject>& unused_obj) { 774 const base::android::JavaParamRef<jobject>& unused_obj) {
782 return !personal_data_manager_->GetProfiles().empty(); 775 return !personal_data_manager_->GetProfiles().empty();
783 } 776 }
784 777
785 jboolean PersonalDataManagerAndroid::HasCreditCards( 778 jboolean PersonalDataManagerAndroid::HasCreditCards(
786 JNIEnv* env, 779 JNIEnv* env,
787 const base::android::JavaParamRef<jobject>& unused_obj) { 780 const base::android::JavaParamRef<jobject>& unused_obj) {
788 return !personal_data_manager_->GetCreditCards().empty(); 781 return !personal_data_manager_->GetCreditCards().empty();
789 } 782 }
790 783
791 base::android::ScopedJavaLocalRef<jobjectArray>
792 PersonalDataManagerAndroid::GetSubKeys(JNIEnv* env,
793 const std::string& region_code) {
794 std::vector<std::string> sub_keys =
795 address_validator_.GetRegionSubKeys(region_code);
796 return base::android::ToJavaArrayOfStrings(env, sub_keys);
797 }
798
799 void PersonalDataManagerAndroid::OnAddressValidationRulesLoaded(
800 const std::string& region_code,
801 bool success) {
802 // if |success| == false, AddressValidator::GetRegionSubKeys will return an
803 // empty list of sub-keys. => No need to check for |success|.
804 // Check if there is any sub-key request for that region code.
805 if (!pending_subkey_region_code_.compare(region_code))
806 pending_subkey_request_->OnRulesSuccessfullyLoaded();
807 pending_subkey_region_code_.clear();
808 pending_subkey_request_.reset();
809 }
810
811 void PersonalDataManagerAndroid::StartRegionSubKeysRequest( 784 void PersonalDataManagerAndroid::StartRegionSubKeysRequest(
812 JNIEnv* env, 785 JNIEnv* env,
813 const JavaParamRef<jobject>& unused_obj, 786 const JavaParamRef<jobject>& unused_obj,
814 const JavaParamRef<jstring>& jregion_code, 787 const JavaParamRef<jstring>& jregion_code,
788 jint jtimeout_seconds,
815 const JavaParamRef<jobject>& jdelegate) { 789 const JavaParamRef<jobject>& jdelegate) {
816 const std::string region_code = ConvertJavaStringToUTF8(env, jregion_code); 790 const std::string region_code = ConvertJavaStringToUTF8(env, jregion_code);
817 std::unique_ptr<SubKeyRequestDelegate> requester = 791 // Self-deleting object.
818 base::MakeUnique<AndroidSubKeyRequesterDelegate>( 792 AndroidSubKeyRequesterDelegate* requester =
819 env, jdelegate, region_code, AsWeakPtr()); 793 new AndroidSubKeyRequesterDelegate(env, jdelegate);
820 794 subkey_requester_.StartRegionSubKeysRequest(region_code, jtimeout_seconds,
821 if (AreRulesLoadedForRegion(region_code)) { 795 requester);
822 requester->OnRulesSuccessfullyLoaded();
823 } else {
824 // Setup the variables so that the sub-keys request is sent, when the rules
825 // are loaded.
826 pending_subkey_region_code_ = region_code;
827 pending_subkey_request_ = std::move(requester);
828 }
829 } 796 }
830 797
831 void PersonalDataManagerAndroid::CancelPendingGetSubKeys( 798 void PersonalDataManagerAndroid::CancelPendingGetSubKeys(
832 JNIEnv* env, 799 JNIEnv* env,
833 const base::android::JavaParamRef<jobject>& unused_obj) { 800 const base::android::JavaParamRef<jobject>& unused_obj) {
834 pending_subkey_region_code_.clear(); 801 subkey_requester_.CancelPendingGetSubKeys();
835 pending_subkey_request_.reset();
836 } 802 }
837 803
838 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs( 804 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileGUIDs(
839 JNIEnv* env, 805 JNIEnv* env,
840 const std::vector<AutofillProfile*>& profiles) { 806 const std::vector<AutofillProfile*>& profiles) {
841 std::vector<base::string16> guids; 807 std::vector<base::string16> guids;
842 for (AutofillProfile* profile : profiles) 808 for (AutofillProfile* profile : profiles)
843 guids.push_back(base::UTF8ToUTF16(profile->guid())); 809 guids.push_back(base::UTF8ToUTF16(profile->guid()));
844 810
845 return base::android::ToJavaArrayOfStrings(env, guids); 811 return base::android::ToJavaArrayOfStrings(env, guids);
846 } 812 }
847 813
848 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetCreditCardGUIDs( 814 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetCreditCardGUIDs(
849 JNIEnv* env, 815 JNIEnv* env,
850 const std::vector<CreditCard*>& credit_cards) { 816 const std::vector<CreditCard*>& credit_cards) {
851 std::vector<base::string16> guids; 817 std::vector<base::string16> guids;
852 for (CreditCard* credit_card : credit_cards) 818 for (CreditCard* credit_card : credit_cards)
853 guids.push_back(base::UTF8ToUTF16(credit_card->guid())); 819 guids.push_back(base::UTF8ToUTF16(credit_card->guid()));
854 820
855 return base::android::ToJavaArrayOfStrings(env, guids); 821 return base::android::ToJavaArrayOfStrings(env, guids);
856 } 822 }
857 823
858 bool PersonalDataManagerAndroid::AreRulesLoadedForRegion(
859 const std::string& region_code) {
860 return address_validator_.AreRulesLoadedForRegion(region_code);
861 }
862
863 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels( 824 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
864 JNIEnv* env, 825 JNIEnv* env,
865 bool address_only, 826 bool address_only,
866 bool include_name_in_label, 827 bool include_name_in_label,
867 bool include_organization_in_label, 828 bool include_organization_in_label,
868 bool include_country_in_label, 829 bool include_country_in_label,
869 std::vector<AutofillProfile*> profiles) { 830 std::vector<AutofillProfile*> profiles) {
870 std::unique_ptr<std::vector<ServerFieldType>> suggested_fields; 831 std::unique_ptr<std::vector<ServerFieldType>> suggested_fields;
871 size_t minimal_fields_shown = 2; 832 size_t minimal_fields_shown = 2;
872 if (address_only) { 833 if (address_only) {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 base::android::ConvertJavaStringToUTF16(env, jcountry_name))); 931 base::android::ConvertJavaStringToUTF16(env, jcountry_name)));
971 } 932 }
972 933
973 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { 934 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
974 PersonalDataManagerAndroid* personal_data_manager_android = 935 PersonalDataManagerAndroid* personal_data_manager_android =
975 new PersonalDataManagerAndroid(env, obj); 936 new PersonalDataManagerAndroid(env, obj);
976 return reinterpret_cast<intptr_t>(personal_data_manager_android); 937 return reinterpret_cast<intptr_t>(personal_data_manager_android);
977 } 938 }
978 939
979 } // namespace autofill 940 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698