Commit f2a67dbe authored by gogerald's avatar gogerald Committed by Commit Bot

[Autofill Assistant] Pass and store CreditCard and AutofillProfile in memory instead of its guid

This solves the problem of card autofill failure when not choosing save to Chrome

Bug: 806868
Change-Id: I80668ffe9f25661e735a1d2b7cc211f9c2c4e59c
Reviewed-on: https://chromium-review.googlesource.com/c/1306640
Commit-Queue: Ganggui Tang <gogerald@chromium.org>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604575}
parent 37893cef
...@@ -252,9 +252,8 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -252,9 +252,8 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
mUiDelegateHolder.performUiOperation( mUiDelegateHolder.performUiOperation(
uiDelegate -> mAutofillAssistantPaymentRequest.show(selectedPaymentInformation -> { uiDelegate -> mAutofillAssistantPaymentRequest.show(selectedPaymentInformation -> {
nativeOnGetPaymentInformation(mUiControllerAndroid, nativeOnGetPaymentInformation(mUiControllerAndroid,
selectedPaymentInformation.succeed, selectedPaymentInformation.cardGuid, selectedPaymentInformation.succeed, selectedPaymentInformation.card,
selectedPaymentInformation.cardIssuerNetwork, selectedPaymentInformation.address,
selectedPaymentInformation.addressGuid,
selectedPaymentInformation.payerName, selectedPaymentInformation.payerName,
selectedPaymentInformation.payerPhone, selectedPaymentInformation.payerPhone,
selectedPaymentInformation.payerEmail); selectedPaymentInformation.payerEmail);
...@@ -488,9 +487,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -488,9 +487,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
private native void nativeOnAddressSelected(long nativeUiControllerAndroid, String guid); private native void nativeOnAddressSelected(long nativeUiControllerAndroid, String guid);
private native void nativeOnCardSelected(long nativeUiControllerAndroid, String guid); private native void nativeOnCardSelected(long nativeUiControllerAndroid, String guid);
private native void nativeOnGetPaymentInformation(long nativeUiControllerAndroid, private native void nativeOnGetPaymentInformation(long nativeUiControllerAndroid,
boolean succeed, @Nullable String cardGuid, @Nullable String cardIssuerNetwork, boolean succeed, @Nullable PersonalDataManager.CreditCard card,
@Nullable String addressGuid, @Nullable String payerName, @Nullable String payerPhone, @Nullable PersonalDataManager.AutofillProfile address, @Nullable String payerName,
@Nullable String payerEmail); @Nullable String payerPhone, @Nullable String payerEmail);
private native void nativeOnAccessToken( private native void nativeOnAccessToken(
long nativeUiControllerAndroid, boolean success, String accessToken); long nativeUiControllerAndroid, boolean success, String accessToken);
private native String nativeGetPrimaryAccountName(long nativeUiControllerAndroid); private native String nativeGetPrimaryAccountName(long nativeUiControllerAndroid);
......
...@@ -14,6 +14,7 @@ import org.chromium.chrome.R; ...@@ -14,6 +14,7 @@ import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.autofill.PersonalDataManager; import org.chromium.chrome.browser.autofill.PersonalDataManager;
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.browser.favicon.FaviconHelper; import org.chromium.chrome.browser.favicon.FaviconHelper;
import org.chromium.chrome.browser.payments.ui.ContactDetailsSection; import org.chromium.chrome.browser.payments.ui.ContactDetailsSection;
import org.chromium.chrome.browser.payments.ui.PaymentInformation; import org.chromium.chrome.browser.payments.ui.PaymentInformation;
...@@ -67,14 +68,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client ...@@ -67,14 +68,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
/** Whether selection succeed. */ /** Whether selection succeed. */
public boolean succeed; public boolean succeed;
/** Selected payment card's guid in personal data manager. */ /** Selected payment card. */
public String cardGuid; public CreditCard card;
/** Selected payment card's network identifier. */ /** Selected shipping address. */
public String cardIssuerNetwork; public AutofillProfile address;
/** Selected shipping address's guid in personal data manager. */
public String addressGuid;
/** Payer's contact name. */ /** Payer's contact name. */
public String payerName; public String payerName;
...@@ -484,13 +482,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client ...@@ -484,13 +482,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
SelectedPaymentInformation selectedPaymentInformation = SelectedPaymentInformation selectedPaymentInformation =
new SelectedPaymentInformation(); new SelectedPaymentInformation();
PersonalDataManager.CreditCard creditCard = selectedPaymentInformation.card =
((AutofillPaymentInstrument) selectedPaymentMethod).getCard(); ((AutofillPaymentInstrument) selectedPaymentMethod).getCard();
selectedPaymentInformation.cardGuid = creditCard.getGUID();
selectedPaymentInformation.cardIssuerNetwork = creditCard.getBasicCardIssuerNetwork();
if (mPaymentOptions.requestShipping && selectedShippingAddress != null) { if (mPaymentOptions.requestShipping && selectedShippingAddress != null) {
selectedPaymentInformation.addressGuid = selectedPaymentInformation.address =
((AutofillAddress) selectedShippingAddress).getProfile().getGUID(); ((AutofillAddress) selectedShippingAddress).getProfile();
} }
if (mPaymentOptions.requestPayerName || mPaymentOptions.requestPayerPhone if (mPaymentOptions.requestPayerName || mPaymentOptions.requestPayerPhone
|| mPaymentOptions.requestPayerEmail) { || mPaymentOptions.requestPayerEmail) {
......
...@@ -13,11 +13,14 @@ ...@@ -13,11 +13,14 @@
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "chrome/browser/android/chrome_feature_list.h" #include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/autofill/android/personal_data_manager_android.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h" #include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/common/channel_info.h" #include "chrome/common/channel_info.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill_assistant/browser/access_token_fetcher.h" #include "components/autofill_assistant/browser/access_token_fetcher.h"
#include "components/autofill_assistant/browser/controller.h" #include "components/autofill_assistant/browser/controller.h"
#include "components/signin/core/browser/account_info.h" #include "components/signin/core/browser/account_info.h"
...@@ -167,9 +170,8 @@ void UiControllerAndroid::OnGetPaymentInformation( ...@@ -167,9 +170,8 @@ void UiControllerAndroid::OnGetPaymentInformation(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
jboolean jsucceed, jboolean jsucceed,
const base::android::JavaParamRef<jstring>& jcard_guid, const base::android::JavaParamRef<jobject>& jcard,
const base::android::JavaParamRef<jstring>& jcard_issuer_network, const base::android::JavaParamRef<jobject>& jaddress,
const base::android::JavaParamRef<jstring>& jaddress_guid,
const base::android::JavaParamRef<jstring>& jpayer_name, const base::android::JavaParamRef<jstring>& jpayer_name,
const base::android::JavaParamRef<jstring>& jpayer_phone, const base::android::JavaParamRef<jstring>& jpayer_phone,
const base::android::JavaParamRef<jstring>& jpayer_email) { const base::android::JavaParamRef<jstring>& jpayer_email) {
...@@ -179,17 +181,15 @@ void UiControllerAndroid::OnGetPaymentInformation( ...@@ -179,17 +181,15 @@ void UiControllerAndroid::OnGetPaymentInformation(
std::make_unique<PaymentInformation>(); std::make_unique<PaymentInformation>();
payment_info->succeed = jsucceed; payment_info->succeed = jsucceed;
if (payment_info->succeed) { if (payment_info->succeed) {
if (jcard_guid != nullptr) { if (jcard != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jcard_guid, payment_info->card = std::make_unique<autofill::CreditCard>();
&payment_info->card_guid); autofill::PersonalDataManagerAndroid::PopulateNativeCreditCardFromJava(
jcard, env, payment_info->card.get());
} }
if (jcard_issuer_network != nullptr) { if (jaddress != nullptr) {
base::android::ConvertJavaStringToUTF8( payment_info->address = std::make_unique<autofill::AutofillProfile>();
env, jcard_issuer_network, &payment_info->card_issuer_network); autofill::PersonalDataManagerAndroid::PopulateNativeProfileFromJava(
} jaddress, env, payment_info->address.get());
if (jaddress_guid != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jaddress_guid,
&payment_info->address_guid);
} }
if (jpayer_name != nullptr) { if (jpayer_name != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jpayer_name, base::android::ConvertJavaStringToUTF8(env, jpayer_name,
......
...@@ -84,9 +84,8 @@ class UiControllerAndroid : public UiController, ...@@ -84,9 +84,8 @@ class UiControllerAndroid : public UiController,
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
jboolean jsucceed, jboolean jsucceed,
const base::android::JavaParamRef<jstring>& jcard_guid, const base::android::JavaParamRef<jobject>& jcard,
const base::android::JavaParamRef<jstring>& jaddress_guid, const base::android::JavaParamRef<jobject>& jaddress,
const base::android::JavaParamRef<jstring>& jaddress_issuer_network,
const base::android::JavaParamRef<jstring>& jpayer_name, const base::android::JavaParamRef<jstring>& jpayer_name,
const base::android::JavaParamRef<jstring>& jpayer_phone, const base::android::JavaParamRef<jstring>& jpayer_phone,
const base::android::JavaParamRef<jstring>& jpayer_email); const base::android::JavaParamRef<jstring>& jpayer_email);
......
...@@ -101,44 +101,6 @@ void MaybeSetRawInfo(AutofillProfile* profile, ...@@ -101,44 +101,6 @@ void MaybeSetRawInfo(AutofillProfile* profile,
profile->SetRawInfo(type, ConvertJavaStringToUTF16(jstr)); profile->SetRawInfo(type, ConvertJavaStringToUTF16(jstr));
} }
void PopulateNativeProfileFromJava(const JavaParamRef<jobject>& jprofile,
JNIEnv* env,
AutofillProfile* profile) {
profile->set_origin(
ConvertJavaStringToUTF8(Java_AutofillProfile_getOrigin(env, jprofile)));
profile->SetInfo(
AutofillType(NAME_FULL),
ConvertJavaStringToUTF16(Java_AutofillProfile_getFullName(env, jprofile)),
g_browser_process->GetApplicationLocale());
MaybeSetRawInfo(profile, autofill::COMPANY_NAME,
Java_AutofillProfile_getCompanyName(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_STREET_ADDRESS,
Java_AutofillProfile_getStreetAddress(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_STATE,
Java_AutofillProfile_getRegion(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_CITY,
Java_AutofillProfile_getLocality(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
Java_AutofillProfile_getDependentLocality(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_ZIP,
Java_AutofillProfile_getPostalCode(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_SORTING_CODE,
Java_AutofillProfile_getSortingCode(env, jprofile));
ScopedJavaLocalRef<jstring> country_code =
Java_AutofillProfile_getCountryCode(env, jprofile);
if (!country_code.is_null()) {
profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
ConvertJavaStringToUTF16(country_code),
g_browser_process->GetApplicationLocale());
}
MaybeSetRawInfo(profile, autofill::PHONE_HOME_WHOLE_NUMBER,
Java_AutofillProfile_getPhoneNumber(env, jprofile));
MaybeSetRawInfo(profile, autofill::EMAIL_ADDRESS,
Java_AutofillProfile_getEmailAddress(env, jprofile));
profile->set_language_code(ConvertJavaStringToUTF8(
Java_AutofillProfile_getLanguageCode(env, jprofile)));
}
ScopedJavaLocalRef<jobject> ScopedJavaLocalRef<jobject>
JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env, JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env,
const CreditCard& card) { const CreditCard& card) {
...@@ -162,55 +124,6 @@ JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env, ...@@ -162,55 +124,6 @@ JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env,
ConvertUTF8ToJavaString(env, card.server_id())); ConvertUTF8ToJavaString(env, card.server_id()));
} }
void PopulateNativeCreditCardFromJava(const JavaRef<jobject>& jcard,
JNIEnv* env,
CreditCard* card) {
card->set_origin(
ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_NAME_FULL,
ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_NUMBER,
ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_EXP_MONTH,
ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_EXP_4_DIGIT_YEAR,
ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
card->set_billing_address_id(
ConvertJavaStringToUTF8(Java_CreditCard_getBillingAddressId(env, jcard)));
card->set_server_id(
ConvertJavaStringToUTF8(Java_CreditCard_getServerId(env, jcard)));
jint card_type = Java_CreditCard_getCardType(env, jcard);
DCHECK_GE(CreditCard::CARD_TYPE_PREPAID, card_type);
DCHECK_LE(CreditCard::CARD_TYPE_UNKNOWN, card_type);
card->set_card_type(static_cast<CreditCard::CardType>(card_type));
// Only set the guid if it is an existing card (java guid not empty).
// Otherwise, keep the generated one.
std::string guid =
ConvertJavaStringToUTF8(Java_CreditCard_getGUID(env, jcard));
if (!guid.empty())
card->set_guid(guid);
if (Java_CreditCard_getIsLocal(env, jcard)) {
card->set_record_type(CreditCard::LOCAL_CARD);
} else {
if (Java_CreditCard_getIsCached(env, jcard)) {
card->set_record_type(CreditCard::FULL_SERVER_CARD);
} else {
card->set_record_type(CreditCard::MASKED_SERVER_CARD);
card->SetNetworkForMaskedCard(
data_util::GetIssuerNetworkForBasicCardIssuerNetwork(
ConvertJavaStringToUTF8(
env, Java_CreditCard_getBasicCardIssuerNetwork(env, jcard))));
}
}
}
// Self-deleting requester of full card details, including full PAN and the CVC // Self-deleting requester of full card details, including full PAN and the CVC
// number. // number.
class FullCardRequester : public payments::FullCardRequest::ResultDelegate, class FullCardRequester : public payments::FullCardRequest::ResultDelegate,
...@@ -328,6 +241,104 @@ PersonalDataManagerAndroid::~PersonalDataManagerAndroid() { ...@@ -328,6 +241,104 @@ PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
personal_data_manager_->RemoveObserver(this); personal_data_manager_->RemoveObserver(this);
} }
// static
void PersonalDataManagerAndroid::PopulateNativeCreditCardFromJava(
const JavaRef<jobject>& jcard,
JNIEnv* env,
CreditCard* card) {
card->set_origin(
ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_NAME_FULL,
ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_NUMBER,
ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_EXP_MONTH,
ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
card->SetRawInfo(
CREDIT_CARD_EXP_4_DIGIT_YEAR,
ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
card->set_billing_address_id(
ConvertJavaStringToUTF8(Java_CreditCard_getBillingAddressId(env, jcard)));
card->set_server_id(
ConvertJavaStringToUTF8(Java_CreditCard_getServerId(env, jcard)));
jint card_type = Java_CreditCard_getCardType(env, jcard);
DCHECK_GE(CreditCard::CARD_TYPE_PREPAID, card_type);
DCHECK_LE(CreditCard::CARD_TYPE_UNKNOWN, card_type);
card->set_card_type(static_cast<CreditCard::CardType>(card_type));
// Only set the guid if it is an existing card (java guid not empty).
// Otherwise, keep the generated one.
std::string guid =
ConvertJavaStringToUTF8(Java_CreditCard_getGUID(env, jcard));
if (!guid.empty())
card->set_guid(guid);
if (Java_CreditCard_getIsLocal(env, jcard)) {
card->set_record_type(CreditCard::LOCAL_CARD);
} else {
if (Java_CreditCard_getIsCached(env, jcard)) {
card->set_record_type(CreditCard::FULL_SERVER_CARD);
} else {
card->set_record_type(CreditCard::MASKED_SERVER_CARD);
card->SetNetworkForMaskedCard(
data_util::GetIssuerNetworkForBasicCardIssuerNetwork(
ConvertJavaStringToUTF8(
env, Java_CreditCard_getBasicCardIssuerNetwork(env, jcard))));
}
}
}
// static
void PersonalDataManagerAndroid::PopulateNativeProfileFromJava(
const JavaParamRef<jobject>& jprofile,
JNIEnv* env,
AutofillProfile* profile) {
// Only set the guid if it is an existing profile (java guid not empty).
// Otherwise, keep the generated one.
std::string guid =
ConvertJavaStringToUTF8(Java_AutofillProfile_getGUID(env, jprofile));
if (!guid.empty())
profile->set_guid(guid);
profile->set_origin(
ConvertJavaStringToUTF8(Java_AutofillProfile_getOrigin(env, jprofile)));
profile->SetInfo(
AutofillType(NAME_FULL),
ConvertJavaStringToUTF16(Java_AutofillProfile_getFullName(env, jprofile)),
g_browser_process->GetApplicationLocale());
MaybeSetRawInfo(profile, autofill::COMPANY_NAME,
Java_AutofillProfile_getCompanyName(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_STREET_ADDRESS,
Java_AutofillProfile_getStreetAddress(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_STATE,
Java_AutofillProfile_getRegion(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_CITY,
Java_AutofillProfile_getLocality(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
Java_AutofillProfile_getDependentLocality(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_ZIP,
Java_AutofillProfile_getPostalCode(env, jprofile));
MaybeSetRawInfo(profile, autofill::ADDRESS_HOME_SORTING_CODE,
Java_AutofillProfile_getSortingCode(env, jprofile));
ScopedJavaLocalRef<jstring> country_code =
Java_AutofillProfile_getCountryCode(env, jprofile);
if (!country_code.is_null()) {
profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
ConvertJavaStringToUTF16(country_code),
g_browser_process->GetApplicationLocale());
}
MaybeSetRawInfo(profile, autofill::PHONE_HOME_WHOLE_NUMBER,
Java_AutofillProfile_getPhoneNumber(env, jprofile));
MaybeSetRawInfo(profile, autofill::EMAIL_ADDRESS,
Java_AutofillProfile_getEmailAddress(env, jprofile));
profile->set_language_code(ConvertJavaStringToUTF8(
Java_AutofillProfile_getLanguageCode(env, jprofile)));
}
jboolean PersonalDataManagerAndroid::IsDataLoaded( jboolean PersonalDataManagerAndroid::IsDataLoaded(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& unused_obj) const { const base::android::JavaParamRef<jobject>& unused_obj) const {
......
...@@ -24,6 +24,15 @@ class PersonalDataManagerAndroid : public PersonalDataManagerObserver { ...@@ -24,6 +24,15 @@ class PersonalDataManagerAndroid : public PersonalDataManagerObserver {
public: public:
PersonalDataManagerAndroid(JNIEnv* env, jobject obj); PersonalDataManagerAndroid(JNIEnv* env, jobject obj);
static void PopulateNativeCreditCardFromJava(
const base::android::JavaRef<jobject>& jcard,
JNIEnv* env,
CreditCard* card);
static void PopulateNativeProfileFromJava(
const base::android::JavaParamRef<jobject>& jprofile,
JNIEnv* env,
AutofillProfile* profile);
// Returns true if personal data manager has loaded the initial data. // Returns true if personal data manager has loaded the initial data.
jboolean IsDataLoaded( jboolean IsDataLoaded(
JNIEnv* env, JNIEnv* env,
......
...@@ -37,7 +37,7 @@ class SelfDeleteFullCardRequester ...@@ -37,7 +37,7 @@ class SelfDeleteFullCardRequester
base::OnceCallback<void(std::unique_ptr<autofill::CreditCard>, base::OnceCallback<void(std::unique_ptr<autofill::CreditCard>,
const base::string16& cvc)>; const base::string16& cvc)>;
void GetFullCard(content::WebContents* web_contents, void GetFullCard(content::WebContents* web_contents,
autofill::CreditCard* card, const autofill::CreditCard* card,
GetFullCardCallback callback) { GetFullCardCallback callback) {
DCHECK(card); DCHECK(card);
callback_ = std::move(callback); callback_ = std::move(callback);
...@@ -122,16 +122,16 @@ void AutofillAction::InternalProcessAction( ...@@ -122,16 +122,16 @@ void AutofillAction::InternalProcessAction(
ProcessActionCallback action_callback) { ProcessActionCallback action_callback) {
process_action_callback_ = std::move(action_callback); process_action_callback_ = std::move(action_callback);
// Check data already selected in a previous action. // Check data already selected in a previous action.
base::Optional<std::string> selected_data; bool has_selected_data =
if (is_autofill_card_) { (is_autofill_card_ && delegate->GetClientMemory()->has_selected_card()) ||
selected_data = delegate->GetClientMemory()->selected_card(); (!is_autofill_card_ &&
} else { delegate->GetClientMemory()->has_selected_address(name_));
selected_data = delegate->GetClientMemory()->selected_address(name_); if (has_selected_data) {
} bool has_valid_data =
(is_autofill_card_ && delegate->GetClientMemory()->selected_card()) ||
if (selected_data) { (!is_autofill_card_ &&
const std::string& guid = selected_data.value(); delegate->GetClientMemory()->selected_address(name_));
if (guid.empty()) { if (!has_valid_data) {
// User selected 'Fill manually'. // User selected 'Fill manually'.
delegate->StopCurrentScript(fill_form_message_); delegate->StopCurrentScript(fill_form_message_);
EndAction(/* successful= */ true); EndAction(/* successful= */ true);
...@@ -144,7 +144,7 @@ void AutofillAction::InternalProcessAction( ...@@ -144,7 +144,7 @@ void AutofillAction::InternalProcessAction(
return; return;
} }
FillFormWithData(guid, delegate); FillFormWithData(delegate);
return; return;
} }
...@@ -174,9 +174,20 @@ void AutofillAction::OnDataSelected(ActionDelegate* delegate, ...@@ -174,9 +174,20 @@ void AutofillAction::OnDataSelected(ActionDelegate* delegate,
const std::string& guid) { const std::string& guid) {
// Remember the selection. // Remember the selection.
if (is_autofill_card_) { if (is_autofill_card_) {
delegate->GetClientMemory()->set_selected_card(guid); std::unique_ptr<autofill::CreditCard> card;
if (!guid.empty()) {
card = std::make_unique<autofill::CreditCard>(
*delegate->GetPersonalDataManager()->GetCreditCardByGUID(guid));
}
delegate->GetClientMemory()->set_selected_card(std::move(card));
} else { } else {
delegate->GetClientMemory()->set_selected_address(name_, guid); std::unique_ptr<autofill::AutofillProfile> address;
if (!guid.empty()) {
address = std::make_unique<autofill::AutofillProfile>(
*delegate->GetPersonalDataManager()->GetProfileByGUID(guid));
}
delegate->GetClientMemory()->set_selected_address(name_,
std::move(address));
} }
if (selectors_.empty()) { if (selectors_.empty()) {
...@@ -193,19 +204,17 @@ void AutofillAction::OnDataSelected(ActionDelegate* delegate, ...@@ -193,19 +204,17 @@ void AutofillAction::OnDataSelected(ActionDelegate* delegate,
return; return;
} }
FillFormWithData(guid, delegate); FillFormWithData(delegate);
} }
void AutofillAction::FillFormWithData(const std::string& guid, void AutofillAction::FillFormWithData(ActionDelegate* delegate) {
ActionDelegate* delegate) {
delegate->WaitForElement(selectors_, delegate->WaitForElement(selectors_,
base::BindOnce(&AutofillAction::OnWaitForElement, base::BindOnce(&AutofillAction::OnWaitForElement,
weak_ptr_factory_.GetWeakPtr(), guid, weak_ptr_factory_.GetWeakPtr(),
base::Unretained(delegate))); base::Unretained(delegate)));
} }
void AutofillAction::OnWaitForElement(const std::string& guid, void AutofillAction::OnWaitForElement(ActionDelegate* delegate,
ActionDelegate* delegate,
bool element_found) { bool element_found) {
if (!element_found) { if (!element_found) {
EndAction(/* successful= */ false); EndAction(/* successful= */ false);
...@@ -214,25 +223,21 @@ void AutofillAction::OnWaitForElement(const std::string& guid, ...@@ -214,25 +223,21 @@ void AutofillAction::OnWaitForElement(const std::string& guid,
DCHECK(!selectors_.empty()); DCHECK(!selectors_.empty());
if (is_autofill_card_) { if (is_autofill_card_) {
autofill::CreditCard* card =
delegate->GetPersonalDataManager()->GetCreditCardByGUID(guid);
DCHECK(card);
// TODO(crbug.com/806868): Consider refactoring SelfDeleteFullCardRequester // TODO(crbug.com/806868): Consider refactoring SelfDeleteFullCardRequester
// so as to unit test it. // so as to unit test it.
DCHECK(delegate->GetClientMemory()->selected_card());
(new SelfDeleteFullCardRequester()) (new SelfDeleteFullCardRequester())
->GetFullCard(delegate->GetWebContents(), card, ->GetFullCard(delegate->GetWebContents(),
delegate->GetClientMemory()->selected_card(),
base::BindOnce(&AutofillAction::OnGetFullCard, base::BindOnce(&AutofillAction::OnGetFullCard,
weak_ptr_factory_.GetWeakPtr(), delegate)); weak_ptr_factory_.GetWeakPtr(), delegate));
return; return;
} }
const autofill::AutofillProfile* profile =
delegate->GetPersonalDataManager()->GetProfileByGUID(guid);
DCHECK(profile);
delegate->FillAddressForm( delegate->FillAddressForm(
profile, selectors_, delegate->GetClientMemory()->selected_address(name_), selectors_,
base::BindOnce(&AutofillAction::OnFormFilled, base::BindOnce(&AutofillAction::OnFormFilled,
weak_ptr_factory_.GetWeakPtr(), guid, delegate)); weak_ptr_factory_.GetWeakPtr(), delegate));
} }
void AutofillAction::OnGetFullCard(ActionDelegate* delegate, void AutofillAction::OnGetFullCard(ActionDelegate* delegate,
...@@ -245,27 +250,23 @@ void AutofillAction::OnGetFullCard(ActionDelegate* delegate, ...@@ -245,27 +250,23 @@ void AutofillAction::OnGetFullCard(ActionDelegate* delegate,
return; return;
} }
std::string guid = card->guid();
delegate->FillCardForm( delegate->FillCardForm(
std::move(card), cvc, selectors_, std::move(card), cvc, selectors_,
base::BindOnce(&AutofillAction::OnFormFilled, base::BindOnce(&AutofillAction::OnFormFilled,
weak_ptr_factory_.GetWeakPtr(), guid, delegate)); weak_ptr_factory_.GetWeakPtr(), delegate));
} }
void AutofillAction::OnFormFilled(const std::string& guid, void AutofillAction::OnFormFilled(ActionDelegate* delegate, bool successful) {
ActionDelegate* delegate,
bool successful) {
// In case Autofill failed, we fail the action. // In case Autofill failed, we fail the action.
if (!successful) { if (!successful) {
EndAction(/* successful= */ false); EndAction(/* successful= */ false);
return; return;
} }
CheckRequiredFields(guid, delegate, /* allow_fallback */ true); CheckRequiredFields(delegate, /* allow_fallback */ true);
} }
void AutofillAction::CheckRequiredFields(const std::string& guid, void AutofillAction::CheckRequiredFields(ActionDelegate* delegate,
ActionDelegate* delegate,
bool allow_fallback) { bool allow_fallback) {
if (is_autofill_card_) { if (is_autofill_card_) {
// TODO(crbug.com/806868): Implement required fields checking for cards. // TODO(crbug.com/806868): Implement required fields checking for cards.
...@@ -297,7 +298,7 @@ void AutofillAction::CheckRequiredFields(const std::string& guid, ...@@ -297,7 +298,7 @@ void AutofillAction::CheckRequiredFields(const std::string& guid,
base::BindOnce( base::BindOnce(
&AutofillAction::OnCheckRequiredFieldsDone, &AutofillAction::OnCheckRequiredFieldsDone,
base::Unretained(this), // this instance owns batch_element_checker_ base::Unretained(this), // this instance owns batch_element_checker_
guid, base::Unretained(delegate), allow_fallback)); base::Unretained(delegate), allow_fallback));
} }
void AutofillAction::OnGetRequiredFieldValue(int required_fields_index, void AutofillAction::OnGetRequiredFieldValue(int required_fields_index,
...@@ -307,8 +308,7 @@ void AutofillAction::OnGetRequiredFieldValue(int required_fields_index, ...@@ -307,8 +308,7 @@ void AutofillAction::OnGetRequiredFieldValue(int required_fields_index,
value.empty() ? EMPTY : NOT_EMPTY; value.empty() ? EMPTY : NOT_EMPTY;
} }
void AutofillAction::OnCheckRequiredFieldsDone(const std::string& guid, void AutofillAction::OnCheckRequiredFieldsDone(ActionDelegate* delegate,
ActionDelegate* delegate,
bool allow_fallback) { bool allow_fallback) {
batch_element_checker_.reset(); batch_element_checker_.reset();
...@@ -338,7 +338,7 @@ void AutofillAction::OnCheckRequiredFieldsDone(const std::string& guid, ...@@ -338,7 +338,7 @@ void AutofillAction::OnCheckRequiredFieldsDone(const std::string& guid,
// If there are any fallbacks for the empty fields, set them, otherwise fail // If there are any fallbacks for the empty fields, set them, otherwise fail
// immediately. // immediately.
bool has_fallbacks = false; bool has_fallbacks = false;
auto* profile = delegate->GetPersonalDataManager()->GetProfileByGUID(guid); auto* profile = delegate->GetClientMemory()->selected_address(name_);
DCHECK(profile); DCHECK(profile);
for (int i = 0; i < proto_.use_address().required_fields_size(); i++) { for (int i = 0; i < proto_.use_address().required_fields_size(); i++) {
if (required_fields_value_status_[i] == EMPTY && if (required_fields_value_status_[i] == EMPTY &&
...@@ -356,11 +356,10 @@ void AutofillAction::OnCheckRequiredFieldsDone(const std::string& guid, ...@@ -356,11 +356,10 @@ void AutofillAction::OnCheckRequiredFieldsDone(const std::string& guid,
} }
// Set the fallback values and check again. // Set the fallback values and check again.
SetFallbackFieldValuesSequentially(guid, delegate, 0); SetFallbackFieldValuesSequentially(delegate, 0);
} }
void AutofillAction::SetFallbackFieldValuesSequentially( void AutofillAction::SetFallbackFieldValuesSequentially(
const std::string& guid,
ActionDelegate* delegate, ActionDelegate* delegate,
int required_fields_index) { int required_fields_index) {
DCHECK_GE(required_fields_index, 0); DCHECK_GE(required_fields_index, 0);
...@@ -377,17 +376,17 @@ void AutofillAction::SetFallbackFieldValuesSequentially( ...@@ -377,17 +376,17 @@ void AutofillAction::SetFallbackFieldValuesSequentially(
if (required_fields_index >= required_fields.size()) { if (required_fields_index >= required_fields.size()) {
DCHECK_EQ(required_fields_index, required_fields.size()); DCHECK_EQ(required_fields_index, required_fields.size());
CheckRequiredFields(guid, delegate, /* allow_fallback */ false); CheckRequiredFields(delegate, /* allow_fallback */ false);
return; return;
} }
// Set the next field to its fallback value. // Set the next field to its fallback value.
std::string fallback_value = base::UTF16ToUTF8(GetAddressFieldValue( std::string fallback_value = base::UTF16ToUTF8(GetAddressFieldValue(
delegate->GetPersonalDataManager()->GetProfileByGUID(guid), delegate->GetClientMemory()->selected_address(name_),
required_fields.Get(required_fields_index).address_field())); required_fields.Get(required_fields_index).address_field()));
if (fallback_value.empty()) { if (fallback_value.empty()) {
// If there is no fallback value, we skip this failed field. // If there is no fallback value, we skip this failed field.
SetFallbackFieldValuesSequentially(guid, delegate, ++required_fields_index); SetFallbackFieldValuesSequentially(delegate, ++required_fields_index);
return; return;
} }
...@@ -399,12 +398,11 @@ void AutofillAction::SetFallbackFieldValuesSequentially( ...@@ -399,12 +398,11 @@ void AutofillAction::SetFallbackFieldValuesSequentially(
fallback_value, fallback_value,
required_fields.Get(required_fields_index).simulate_key_presses(), required_fields.Get(required_fields_index).simulate_key_presses(),
base::BindOnce(&AutofillAction::OnSetFallbackFieldValue, base::BindOnce(&AutofillAction::OnSetFallbackFieldValue,
weak_ptr_factory_.GetWeakPtr(), guid, delegate, weak_ptr_factory_.GetWeakPtr(), delegate,
required_fields_index)); required_fields_index));
} }
void AutofillAction::OnSetFallbackFieldValue(const std::string& guid, void AutofillAction::OnSetFallbackFieldValue(ActionDelegate* delegate,
ActionDelegate* delegate,
int required_fields_index, int required_fields_index,
bool successful) { bool successful) {
if (!successful) { if (!successful) {
...@@ -413,7 +411,7 @@ void AutofillAction::OnSetFallbackFieldValue(const std::string& guid, ...@@ -413,7 +411,7 @@ void AutofillAction::OnSetFallbackFieldValue(const std::string& guid,
EndAction(/* successful= */ true); EndAction(/* successful= */ true);
return; return;
} }
SetFallbackFieldValuesSequentially(guid, delegate, ++required_fields_index); SetFallbackFieldValuesSequentially(delegate, ++required_fields_index);
} }
base::string16 AutofillAction::GetAddressFieldValue( base::string16 AutofillAction::GetAddressFieldValue(
......
...@@ -40,12 +40,10 @@ class AutofillAction : public Action { ...@@ -40,12 +40,10 @@ class AutofillAction : public Action {
void OnDataSelected(ActionDelegate* delegate, void OnDataSelected(ActionDelegate* delegate,
const std::string& guid); const std::string& guid);
// Fill the form using data with GUID |guid|. Return whether filling succeeded // Fill the form using data in client memory. Return whether filling succeeded
// or not through |callback|. // or not through |callback|.
void FillFormWithData(const std::string& guid, ActionDelegate* delegate); void FillFormWithData(ActionDelegate* delegate);
void OnWaitForElement(const std::string& guid, void OnWaitForElement(ActionDelegate* delegate, bool element_found);
ActionDelegate* delegate,
bool element_found);
// Called after getting full credit card with its cvc. // Called after getting full credit card with its cvc.
void OnGetFullCard(ActionDelegate* delegate, void OnGetFullCard(ActionDelegate* delegate,
...@@ -53,21 +51,16 @@ class AutofillAction : public Action { ...@@ -53,21 +51,16 @@ class AutofillAction : public Action {
const base::string16& cvc); const base::string16& cvc);
// Called when the form has been filled. // Called when the form has been filled.
void OnFormFilled(const std::string& guid, void OnFormFilled(ActionDelegate* delegate, bool successful);
ActionDelegate* delegate,
bool successful);
// Check whether all required fields have a non-empty value. If it is the // Check whether all required fields have a non-empty value. If it is the
// case, finish the action successfully. If it's not and |allow_fallback| // case, finish the action successfully. If it's not and |allow_fallback|
// false, fail the action. If |allow_fallback| is true, try again by filling // false, fail the action. If |allow_fallback| is true, try again by filling
// the failed fields without Autofill. // the failed fields without Autofill.
void CheckRequiredFields(const std::string& guid, void CheckRequiredFields(ActionDelegate* delegate, bool allow_fallback);
ActionDelegate* delegate,
bool allow_fallback);
// Triggers the check for a specific field. // Triggers the check for a specific field.
void CheckRequiredFieldsSequentially(const std::string& guid, void CheckRequiredFieldsSequentially(ActionDelegate* delegate,
ActionDelegate* delegate,
bool allow_fallback, bool allow_fallback,
int required_fields_index); int required_fields_index);
...@@ -77,9 +70,7 @@ class AutofillAction : public Action { ...@@ -77,9 +70,7 @@ class AutofillAction : public Action {
const std::string& value); const std::string& value);
// Called when all required fields have been checked. // Called when all required fields have been checked.
void OnCheckRequiredFieldsDone(const std::string& guid, void OnCheckRequiredFieldsDone(ActionDelegate* delegate, bool allow_fallback);
ActionDelegate* delegate,
bool allow_fallback);
// Get the value of |address_field| associated to profile |profile|. Return // Get the value of |address_field| associated to profile |profile|. Return
// empty string if there is no data available. // empty string if there is no data available.
...@@ -89,14 +80,12 @@ class AutofillAction : public Action { ...@@ -89,14 +80,12 @@ class AutofillAction : public Action {
// Sets fallback field values for empty fields from // Sets fallback field values for empty fields from
// |required_fields_value_status_|. // |required_fields_value_status_|.
void SetFallbackFieldValuesSequentially(const std::string& guid, void SetFallbackFieldValuesSequentially(ActionDelegate* delegate,
ActionDelegate* delegate,
int required_fields_index); int required_fields_index);
// Called after trying to set form values without Autofill in case of fallback // Called after trying to set form values without Autofill in case of fallback
// after failed validation. // after failed validation.
void OnSetFallbackFieldValue(const std::string& guid, void OnSetFallbackFieldValue(ActionDelegate* delegate,
ActionDelegate* delegate,
int required_fields_index, int required_fields_index,
bool successful); bool successful);
......
...@@ -24,7 +24,9 @@ namespace { ...@@ -24,7 +24,9 @@ namespace {
using ::testing::_; using ::testing::_;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::InSequence; using ::testing::InSequence;
using ::testing::IsNull;
using ::testing::Not; using ::testing::Not;
using ::testing::NotNull;
using ::testing::Return; using ::testing::Return;
using ::testing::StrNe; using ::testing::StrNe;
using ::testing::Invoke; using ::testing::Invoke;
...@@ -96,13 +98,13 @@ class DirectCallback { ...@@ -96,13 +98,13 @@ class DirectCallback {
class AutofillActionTest : public testing::Test { class AutofillActionTest : public testing::Test {
public: public:
void SetUp() override { void SetUp() override {
autofill::AutofillProfile profile(base::GenerateGUID(), autofill_profile_ = std::make_unique<autofill::AutofillProfile>(
autofill::test::kEmptyOrigin); base::GenerateGUID(), autofill::test::kEmptyOrigin);
autofill::test::SetProfileInfo(&profile, kFirstName, "", kLastName, kEmail, autofill::test::SetProfileInfo(autofill_profile_.get(), kFirstName, "",
"", "", "", "", "", "", "", ""); kLastName, kEmail, "", "", "", "", "", "",
autofill_profile_guid_ = profile.guid(); "", "");
personal_data_manager_ = std::make_unique<MockPersonalDataManager>(); personal_data_manager_ = std::make_unique<MockPersonalDataManager>();
personal_data_manager_->SaveImportedProfile(profile); personal_data_manager_->SaveImportedProfile(*autofill_profile_);
ON_CALL(mock_action_delegate_, GetClientMemory) ON_CALL(mock_action_delegate_, GetClientMemory)
.WillByDefault(Return(&mock_client_memory_)); .WillByDefault(Return(&mock_client_memory_));
...@@ -175,7 +177,7 @@ class AutofillActionTest : public testing::Test { ...@@ -175,7 +177,7 @@ class AutofillActionTest : public testing::Test {
MockActionDelegate mock_action_delegate_; MockActionDelegate mock_action_delegate_;
MockWebController mock_web_controller_; MockWebController mock_web_controller_;
MockClientMemory mock_client_memory_; MockClientMemory mock_client_memory_;
std::string autofill_profile_guid_; std::unique_ptr<autofill::AutofillProfile> autofill_profile_;
std::unique_ptr<autofill::PersonalDataManager> personal_data_manager_; std::unique_ptr<autofill::PersonalDataManager> personal_data_manager_;
}; };
...@@ -186,8 +188,8 @@ TEST_F(AutofillActionTest, FillManually) { ...@@ -186,8 +188,8 @@ TEST_F(AutofillActionTest, FillManually) {
action_proto.mutable_use_address()->set_prompt(kSelectionPrompt); action_proto.mutable_use_address()->set_prompt(kSelectionPrompt);
// No selection was made previously. // No selection was made previously.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) EXPECT_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillOnce(Return(base::nullopt)); .WillOnce(Return(false));
// Expect prompt. // Expect prompt.
EXPECT_CALL(mock_action_delegate_, ShowStatusMessage(kSelectionPrompt)); EXPECT_CALL(mock_action_delegate_, ShowStatusMessage(kSelectionPrompt));
...@@ -197,7 +199,8 @@ TEST_F(AutofillActionTest, FillManually) { ...@@ -197,7 +199,8 @@ TEST_F(AutofillActionTest, FillManually) {
.WillOnce(RunOnceCallback<0>("")); .WillOnce(RunOnceCallback<0>(""));
// We save the selection in memory. // We save the selection in memory.
EXPECT_CALL(mock_client_memory_, set_selected_address(kAddressName, "")); EXPECT_CALL(mock_client_memory_,
set_selected_address(kAddressName, IsNull()));
ExpectActionToStopScript(action_proto, kFillForm); ExpectActionToStopScript(action_proto, kFillForm);
} }
...@@ -214,16 +217,14 @@ TEST_F(AutofillActionTest, ValidationSucceeds) { ...@@ -214,16 +217,14 @@ TEST_F(AutofillActionTest, ValidationSucceeds) {
"#email"); "#email");
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_)); .WillByDefault(Return(true));
ON_CALL(mock_client_memory_, selected_address(kAddressName))
.WillByDefault(Return(autofill_profile_.get()));
// Autofill succeeds. // Autofill succeeds.
const auto* expected_profile = EXPECT_CALL(mock_action_delegate_,
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_); OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
ASSERT_TRUE(expected_profile);
EXPECT_CALL(
mock_action_delegate_,
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
// Validation succeeds. // Validation succeeds.
...@@ -245,16 +246,14 @@ TEST_F(AutofillActionTest, FallbackFails) { ...@@ -245,16 +246,14 @@ TEST_F(AutofillActionTest, FallbackFails) {
"#email"); "#email");
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_)); .WillByDefault(Return(true));
ON_CALL(mock_client_memory_, selected_address(kAddressName))
.WillByDefault(Return(autofill_profile_.get()));
// Autofill succeeds. // Autofill succeeds.
const auto* expected_profile = EXPECT_CALL(mock_action_delegate_,
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_); OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
ASSERT_TRUE(expected_profile);
EXPECT_CALL(
mock_action_delegate_,
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
// Validation fails when getting FIRST_NAME. // Validation fails when getting FIRST_NAME.
...@@ -287,15 +286,14 @@ TEST_F(AutofillActionTest, FallbackSucceeds) { ...@@ -287,15 +286,14 @@ TEST_F(AutofillActionTest, FallbackSucceeds) {
"#email"); "#email");
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_)); .WillByDefault(Return(true));
const auto* expected_profile = ON_CALL(mock_client_memory_, selected_address(kAddressName))
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_); .WillByDefault(Return(autofill_profile_.get()));
ASSERT_TRUE(expected_profile);
// Autofill succeeds. // Autofill succeeds.
EXPECT_CALL( EXPECT_CALL(mock_action_delegate_,
mock_action_delegate_, OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
{ {
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill_assistant/browser/actions/action_delegate.h" #include "components/autofill_assistant/browser/actions/action_delegate.h"
#include "components/autofill_assistant/browser/client_memory.h" #include "components/autofill_assistant/browser/client_memory.h"
#include "third_party/blink/public/mojom/payments/payment_request.mojom.h" #include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
...@@ -56,21 +58,22 @@ void GetPaymentInformationAction::OnGetPaymentInformation( ...@@ -56,21 +58,22 @@ void GetPaymentInformationAction::OnGetPaymentInformation(
ProcessActionCallback callback, ProcessActionCallback callback,
std::unique_ptr<PaymentInformation> payment_information) { std::unique_ptr<PaymentInformation> payment_information) {
bool succeed = payment_information->succeed; bool succeed = payment_information->succeed;
if (succeed) { if (succeed) {
if (get_payment_information.ask_for_payment()) { if (get_payment_information.ask_for_payment()) {
DCHECK(!payment_information->card_guid.empty()); DCHECK(payment_information->card);
delegate->GetClientMemory()->set_selected_card(
payment_information->card_guid);
processed_action_proto_->set_card_issuer_network( processed_action_proto_->set_card_issuer_network(
payment_information->card_issuer_network); autofill::data_util::GetPaymentRequestData(
payment_information->card->network())
.basic_card_issuer_network);
delegate->GetClientMemory()->set_selected_card(
std::move(payment_information->card));
} }
if (!get_payment_information.shipping_address_name().empty()) { if (!get_payment_information.shipping_address_name().empty()) {
DCHECK(!payment_information->address_guid.empty()); DCHECK(payment_information->address);
delegate->GetClientMemory()->set_selected_address( delegate->GetClientMemory()->set_selected_address(
get_payment_information.shipping_address_name(), get_payment_information.shipping_address_name(),
payment_information->address_guid); std::move(payment_information->address));
} }
} }
......
...@@ -9,25 +9,37 @@ namespace autofill_assistant { ...@@ -9,25 +9,37 @@ namespace autofill_assistant {
ClientMemory::ClientMemory() = default; ClientMemory::ClientMemory() = default;
ClientMemory::~ClientMemory() = default; ClientMemory::~ClientMemory() = default;
base::Optional<std::string> ClientMemory::selected_card() { const autofill::CreditCard* ClientMemory::selected_card() {
return selected_card_; if (selected_card_.has_value())
return selected_card_->get();
return nullptr;
} }
base::Optional<std::string> ClientMemory::selected_address( bool ClientMemory::has_selected_card() {
return selected_card_.has_value();
}
const autofill::AutofillProfile* ClientMemory::selected_address(
const std::string& name) { const std::string& name) {
if (selected_addresses_.find(name) != selected_addresses_.end()) if (selected_addresses_.find(name) != selected_addresses_.end())
return selected_addresses_[name]; return selected_addresses_[name].get();
return nullptr;
}
return base::nullopt; void ClientMemory::set_selected_card(
std::unique_ptr<autofill::CreditCard> card) {
selected_card_ = std::move(card);
} }
void ClientMemory::set_selected_card(const std::string& guid) { void ClientMemory::set_selected_address(
selected_card_ = guid; const std::string& name,
std::unique_ptr<autofill::AutofillProfile> address) {
selected_addresses_[name] = std::move(address);
} }
void ClientMemory::set_selected_address(const std::string& name, bool ClientMemory::has_selected_address(const std::string& name) {
const std::string& guid) { return selected_addresses_.find(name) != selected_addresses_.end();
selected_addresses_[name] = guid;
} }
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include <string> #include <string>
#include "base/optional.h" #include "base/optional.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
namespace autofill_assistant { namespace autofill_assistant {
// Data shared between scripts and actions. // Data shared between scripts and actions.
...@@ -17,28 +19,39 @@ class ClientMemory { ...@@ -17,28 +19,39 @@ class ClientMemory {
ClientMemory(); ClientMemory();
virtual ~ClientMemory(); virtual ~ClientMemory();
// GUID of the currently selected credit card, if any. It will be an empty // Selected credit card, if any. It will be a nullptr if didn't select
// optional if user didn't select anything, empty string if user selected // anything or if selected 'Fill manually'.
// 'Fill manually', or the guid of a selected card. virtual const autofill::CreditCard* selected_card();
virtual base::Optional<std::string> selected_card();
// GUID of the currently selected address for |name|. It will be an empty // Return true if card has been selected, otherwise return false.
// optional if user didn't select anything, empty string if user selected // Note that selected_card() might return nullptr when has_selected_card() is
// 'Fill manually', or the guid of a selected address. // true because fill manually was chosen.
virtual base::Optional<std::string> selected_address(const std::string& name); virtual bool has_selected_card();
// Set the |guid| of the selected card. // Selected address for |name|. It will be a nullptr if didn't select anything
virtual void set_selected_card(const std::string& guid); // or if selected 'Fill manually'.
virtual const autofill::AutofillProfile* selected_address(
const std::string& name);
// Set the |guid| of the selected address for |name|. // Return true if address has been selected, otherwise return false.
virtual void set_selected_address(const std::string& name, // Note that selected_address() might return nullptr when
const std::string& guid); // has_selected_address() is true because fill manually was chosen.
virtual bool has_selected_address(const std::string& name);
// Set the selected card.
virtual void set_selected_card(std::unique_ptr<autofill::CreditCard> card);
// Set the selected address for |name|.
virtual void set_selected_address(
const std::string& name,
std::unique_ptr<autofill::AutofillProfile> address);
private: private:
base::Optional<std::string> selected_card_; base::Optional<std::unique_ptr<autofill::CreditCard>> selected_card_;
// GUID of the selected addresses (keyed by name). // The selected addresses (keyed by name).
std::map<std::string, std::string> selected_addresses_; std::map<std::string, std::unique_ptr<autofill::AutofillProfile>>
selected_addresses_;
}; };
} // namespace autofill_assistant } // namespace autofill_assistant
......
...@@ -290,7 +290,7 @@ TEST_F(ControllerTest, Reset) { ...@@ -290,7 +290,7 @@ TEST_F(ControllerTest, Reset) {
} }
// Resetting should clear the client memory // Resetting should clear the client memory
controller_->GetClientMemory()->set_selected_card("set"); controller_->GetClientMemory()->set_selected_card(nullptr);
SimulateNavigateToUrl(GURL("http://a.example.com/path")); SimulateNavigateToUrl(GURL("http://a.example.com/path"));
GetUiDelegate()->OnScriptSelected("reset"); GetUiDelegate()->OnScriptSelected("reset");
......
...@@ -15,12 +15,16 @@ class MockClientMemory : public ClientMemory { ...@@ -15,12 +15,16 @@ class MockClientMemory : public ClientMemory {
MockClientMemory(); MockClientMemory();
~MockClientMemory(); ~MockClientMemory();
MOCK_METHOD0(selected_card, base::Optional<std::string>()); MOCK_METHOD0(selected_card, const autofill::CreditCard*());
MOCK_METHOD0(has_selected_card, bool());
MOCK_METHOD1(selected_address, MOCK_METHOD1(selected_address,
base::Optional<std::string>(const std::string& name)); const autofill::AutofillProfile*(const std::string& name));
MOCK_METHOD1(set_selected_card, void(const std::string& guid)); MOCK_METHOD1(has_selected_address, bool(const std::string& name));
MOCK_METHOD1(set_selected_card,
void(std::unique_ptr<autofill::CreditCard> card));
MOCK_METHOD2(set_selected_address, MOCK_METHOD2(set_selected_address,
void(const std::string& name, const std::string& guid)); void(const std::string& name,
std::unique_ptr<autofill::AutofillProfile> address));
}; };
} // namespace autofill_assistant } // namespace autofill_assistant
......
...@@ -7,6 +7,11 @@ ...@@ -7,6 +7,11 @@
#include <string> #include <string>
namespace autofill {
class AutofillProfile;
class CreditCard;
} // namespace autofill
namespace autofill_assistant { namespace autofill_assistant {
// Struct for holding the payment information data. // Struct for holding the payment information data.
...@@ -15,9 +20,8 @@ struct PaymentInformation { ...@@ -15,9 +20,8 @@ struct PaymentInformation {
~PaymentInformation(); ~PaymentInformation();
bool succeed; bool succeed;
std::string card_guid; std::unique_ptr<autofill::CreditCard> card;
std::string card_issuer_network; std::unique_ptr<autofill::AutofillProfile> address;
std::string address_guid;
std::string payer_name; std::string payer_name;
std::string payer_phone; std::string payer_phone;
std::string payer_email; std::string payer_email;
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "components/autofill_assistant/browser/ui_controller.h" #include "components/autofill_assistant/browser/ui_controller.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
namespace autofill_assistant { namespace autofill_assistant {
PaymentInformation::PaymentInformation() : succeed(false) {} PaymentInformation::PaymentInformation() : succeed(false) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment