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
mUiDelegateHolder.performUiOperation(
uiDelegate -> mAutofillAssistantPaymentRequest.show(selectedPaymentInformation -> {
nativeOnGetPaymentInformation(mUiControllerAndroid,
selectedPaymentInformation.succeed, selectedPaymentInformation.cardGuid,
selectedPaymentInformation.cardIssuerNetwork,
selectedPaymentInformation.addressGuid,
selectedPaymentInformation.succeed, selectedPaymentInformation.card,
selectedPaymentInformation.address,
selectedPaymentInformation.payerName,
selectedPaymentInformation.payerPhone,
selectedPaymentInformation.payerEmail);
......@@ -488,9 +487,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
private native void nativeOnAddressSelected(long nativeUiControllerAndroid, String guid);
private native void nativeOnCardSelected(long nativeUiControllerAndroid, String guid);
private native void nativeOnGetPaymentInformation(long nativeUiControllerAndroid,
boolean succeed, @Nullable String cardGuid, @Nullable String cardIssuerNetwork,
@Nullable String addressGuid, @Nullable String payerName, @Nullable String payerPhone,
@Nullable String payerEmail);
boolean succeed, @Nullable PersonalDataManager.CreditCard card,
@Nullable PersonalDataManager.AutofillProfile address, @Nullable String payerName,
@Nullable String payerPhone, @Nullable String payerEmail);
private native void nativeOnAccessToken(
long nativeUiControllerAndroid, boolean success, String accessToken);
private native String nativeGetPrimaryAccountName(long nativeUiControllerAndroid);
......
......@@ -14,6 +14,7 @@ import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.autofill.PersonalDataManager;
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.payments.ui.ContactDetailsSection;
import org.chromium.chrome.browser.payments.ui.PaymentInformation;
......@@ -67,14 +68,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
/** Whether selection succeed. */
public boolean succeed;
/** Selected payment card's guid in personal data manager. */
public String cardGuid;
/** Selected payment card. */
public CreditCard card;
/** Selected payment card's network identifier. */
public String cardIssuerNetwork;
/** Selected shipping address's guid in personal data manager. */
public String addressGuid;
/** Selected shipping address. */
public AutofillProfile address;
/** Payer's contact name. */
public String payerName;
......@@ -484,13 +482,11 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
SelectedPaymentInformation selectedPaymentInformation =
new SelectedPaymentInformation();
PersonalDataManager.CreditCard creditCard =
selectedPaymentInformation.card =
((AutofillPaymentInstrument) selectedPaymentMethod).getCard();
selectedPaymentInformation.cardGuid = creditCard.getGUID();
selectedPaymentInformation.cardIssuerNetwork = creditCard.getBasicCardIssuerNetwork();
if (mPaymentOptions.requestShipping && selectedShippingAddress != null) {
selectedPaymentInformation.addressGuid =
((AutofillAddress) selectedShippingAddress).getProfile().getGUID();
selectedPaymentInformation.address =
((AutofillAddress) selectedShippingAddress).getProfile();
}
if (mPaymentOptions.requestPayerName || mPaymentOptions.requestPayerPhone
|| mPaymentOptions.requestPayerEmail) {
......
......@@ -13,11 +13,14 @@
#include "base/android/jni_string.h"
#include "base/command_line.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/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.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/controller.h"
#include "components/signin/core/browser/account_info.h"
......@@ -167,9 +170,8 @@ void UiControllerAndroid::OnGetPaymentInformation(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
jboolean jsucceed,
const base::android::JavaParamRef<jstring>& jcard_guid,
const base::android::JavaParamRef<jstring>& jcard_issuer_network,
const base::android::JavaParamRef<jstring>& jaddress_guid,
const base::android::JavaParamRef<jobject>& jcard,
const base::android::JavaParamRef<jobject>& jaddress,
const base::android::JavaParamRef<jstring>& jpayer_name,
const base::android::JavaParamRef<jstring>& jpayer_phone,
const base::android::JavaParamRef<jstring>& jpayer_email) {
......@@ -179,17 +181,15 @@ void UiControllerAndroid::OnGetPaymentInformation(
std::make_unique<PaymentInformation>();
payment_info->succeed = jsucceed;
if (payment_info->succeed) {
if (jcard_guid != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jcard_guid,
&payment_info->card_guid);
if (jcard != nullptr) {
payment_info->card = std::make_unique<autofill::CreditCard>();
autofill::PersonalDataManagerAndroid::PopulateNativeCreditCardFromJava(
jcard, env, payment_info->card.get());
}
if (jcard_issuer_network != nullptr) {
base::android::ConvertJavaStringToUTF8(
env, jcard_issuer_network, &payment_info->card_issuer_network);
}
if (jaddress_guid != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jaddress_guid,
&payment_info->address_guid);
if (jaddress != nullptr) {
payment_info->address = std::make_unique<autofill::AutofillProfile>();
autofill::PersonalDataManagerAndroid::PopulateNativeProfileFromJava(
jaddress, env, payment_info->address.get());
}
if (jpayer_name != nullptr) {
base::android::ConvertJavaStringToUTF8(env, jpayer_name,
......
......@@ -84,9 +84,8 @@ class UiControllerAndroid : public UiController,
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
jboolean jsucceed,
const base::android::JavaParamRef<jstring>& jcard_guid,
const base::android::JavaParamRef<jstring>& jaddress_guid,
const base::android::JavaParamRef<jstring>& jaddress_issuer_network,
const base::android::JavaParamRef<jobject>& jcard,
const base::android::JavaParamRef<jobject>& jaddress,
const base::android::JavaParamRef<jstring>& jpayer_name,
const base::android::JavaParamRef<jstring>& jpayer_phone,
const base::android::JavaParamRef<jstring>& jpayer_email);
......
......@@ -101,44 +101,6 @@ void MaybeSetRawInfo(AutofillProfile* profile,
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>
JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env,
const CreditCard& card) {
......@@ -162,55 +124,6 @@ JNI_PersonalDataManager_CreateJavaCreditCardFromNative(JNIEnv* env,
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
// number.
class FullCardRequester : public payments::FullCardRequest::ResultDelegate,
......@@ -328,6 +241,104 @@ PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
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(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& unused_obj) const {
......
......@@ -24,6 +24,15 @@ class PersonalDataManagerAndroid : public PersonalDataManagerObserver {
public:
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.
jboolean IsDataLoaded(
JNIEnv* env,
......
......@@ -40,12 +40,10 @@ class AutofillAction : public Action {
void OnDataSelected(ActionDelegate* delegate,
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|.
void FillFormWithData(const std::string& guid, ActionDelegate* delegate);
void OnWaitForElement(const std::string& guid,
ActionDelegate* delegate,
bool element_found);
void FillFormWithData(ActionDelegate* delegate);
void OnWaitForElement(ActionDelegate* delegate, bool element_found);
// Called after getting full credit card with its cvc.
void OnGetFullCard(ActionDelegate* delegate,
......@@ -53,21 +51,16 @@ class AutofillAction : public Action {
const base::string16& cvc);
// Called when the form has been filled.
void OnFormFilled(const std::string& guid,
ActionDelegate* delegate,
bool successful);
void OnFormFilled(ActionDelegate* delegate, bool successful);
// 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|
// false, fail the action. If |allow_fallback| is true, try again by filling
// the failed fields without Autofill.
void CheckRequiredFields(const std::string& guid,
ActionDelegate* delegate,
bool allow_fallback);
void CheckRequiredFields(ActionDelegate* delegate, bool allow_fallback);
// Triggers the check for a specific field.
void CheckRequiredFieldsSequentially(const std::string& guid,
ActionDelegate* delegate,
void CheckRequiredFieldsSequentially(ActionDelegate* delegate,
bool allow_fallback,
int required_fields_index);
......@@ -77,9 +70,7 @@ class AutofillAction : public Action {
const std::string& value);
// Called when all required fields have been checked.
void OnCheckRequiredFieldsDone(const std::string& guid,
ActionDelegate* delegate,
bool allow_fallback);
void OnCheckRequiredFieldsDone(ActionDelegate* delegate, bool allow_fallback);
// Get the value of |address_field| associated to profile |profile|. Return
// empty string if there is no data available.
......@@ -89,14 +80,12 @@ class AutofillAction : public Action {
// Sets fallback field values for empty fields from
// |required_fields_value_status_|.
void SetFallbackFieldValuesSequentially(const std::string& guid,
ActionDelegate* delegate,
void SetFallbackFieldValuesSequentially(ActionDelegate* delegate,
int required_fields_index);
// Called after trying to set form values without Autofill in case of fallback
// after failed validation.
void OnSetFallbackFieldValue(const std::string& guid,
ActionDelegate* delegate,
void OnSetFallbackFieldValue(ActionDelegate* delegate,
int required_fields_index,
bool successful);
......
......@@ -24,7 +24,9 @@ namespace {
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::InSequence;
using ::testing::IsNull;
using ::testing::Not;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::StrNe;
using ::testing::Invoke;
......@@ -96,13 +98,13 @@ class DirectCallback {
class AutofillActionTest : public testing::Test {
public:
void SetUp() override {
autofill::AutofillProfile profile(base::GenerateGUID(),
autofill::test::kEmptyOrigin);
autofill::test::SetProfileInfo(&profile, kFirstName, "", kLastName, kEmail,
"", "", "", "", "", "", "", "");
autofill_profile_guid_ = profile.guid();
autofill_profile_ = std::make_unique<autofill::AutofillProfile>(
base::GenerateGUID(), autofill::test::kEmptyOrigin);
autofill::test::SetProfileInfo(autofill_profile_.get(), kFirstName, "",
kLastName, kEmail, "", "", "", "", "", "",
"", "");
personal_data_manager_ = std::make_unique<MockPersonalDataManager>();
personal_data_manager_->SaveImportedProfile(profile);
personal_data_manager_->SaveImportedProfile(*autofill_profile_);
ON_CALL(mock_action_delegate_, GetClientMemory)
.WillByDefault(Return(&mock_client_memory_));
......@@ -175,7 +177,7 @@ class AutofillActionTest : public testing::Test {
MockActionDelegate mock_action_delegate_;
MockWebController mock_web_controller_;
MockClientMemory mock_client_memory_;
std::string autofill_profile_guid_;
std::unique_ptr<autofill::AutofillProfile> autofill_profile_;
std::unique_ptr<autofill::PersonalDataManager> personal_data_manager_;
};
......@@ -186,8 +188,8 @@ TEST_F(AutofillActionTest, FillManually) {
action_proto.mutable_use_address()->set_prompt(kSelectionPrompt);
// No selection was made previously.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(base::nullopt));
EXPECT_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillOnce(Return(false));
// Expect prompt.
EXPECT_CALL(mock_action_delegate_, ShowStatusMessage(kSelectionPrompt));
......@@ -197,7 +199,8 @@ TEST_F(AutofillActionTest, FillManually) {
.WillOnce(RunOnceCallback<0>(""));
// 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);
}
......@@ -214,16 +217,14 @@ TEST_F(AutofillActionTest, ValidationSucceeds) {
"#email");
// Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_));
ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillByDefault(Return(true));
ON_CALL(mock_client_memory_, selected_address(kAddressName))
.WillByDefault(Return(autofill_profile_.get()));
// Autofill succeeds.
const auto* expected_profile =
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_);
ASSERT_TRUE(expected_profile);
EXPECT_CALL(
mock_action_delegate_,
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
EXPECT_CALL(mock_action_delegate_,
OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true));
// Validation succeeds.
......@@ -245,16 +246,14 @@ TEST_F(AutofillActionTest, FallbackFails) {
"#email");
// Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_));
ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillByDefault(Return(true));
ON_CALL(mock_client_memory_, selected_address(kAddressName))
.WillByDefault(Return(autofill_profile_.get()));
// Autofill succeeds.
const auto* expected_profile =
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_);
ASSERT_TRUE(expected_profile);
EXPECT_CALL(
mock_action_delegate_,
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
EXPECT_CALL(mock_action_delegate_,
OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true));
// Validation fails when getting FIRST_NAME.
......@@ -287,15 +286,14 @@ TEST_F(AutofillActionTest, FallbackSucceeds) {
"#email");
// Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_guid_));
const auto* expected_profile =
personal_data_manager_->GetProfileByGUID(autofill_profile_guid_);
ASSERT_TRUE(expected_profile);
ON_CALL(mock_client_memory_, has_selected_address(kAddressName))
.WillByDefault(Return(true));
ON_CALL(mock_client_memory_, selected_address(kAddressName))
.WillByDefault(Return(autofill_profile_.get()));
// Autofill succeeds.
EXPECT_CALL(
mock_action_delegate_,
OnFillAddressForm(expected_profile, ElementsAre(kFakeSelector), _))
EXPECT_CALL(mock_action_delegate_,
OnFillAddressForm(NotNull(), ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true));
{
......
......@@ -8,6 +8,8 @@
#include "base/bind.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/client_memory.h"
#include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
......@@ -56,21 +58,22 @@ void GetPaymentInformationAction::OnGetPaymentInformation(
ProcessActionCallback callback,
std::unique_ptr<PaymentInformation> payment_information) {
bool succeed = payment_information->succeed;
if (succeed) {
if (get_payment_information.ask_for_payment()) {
DCHECK(!payment_information->card_guid.empty());
delegate->GetClientMemory()->set_selected_card(
payment_information->card_guid);
DCHECK(payment_information->card);
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()) {
DCHECK(!payment_information->address_guid.empty());
DCHECK(payment_information->address);
delegate->GetClientMemory()->set_selected_address(
get_payment_information.shipping_address_name(),
payment_information->address_guid);
std::move(payment_information->address));
}
}
......
......@@ -9,25 +9,37 @@ namespace autofill_assistant {
ClientMemory::ClientMemory() = default;
ClientMemory::~ClientMemory() = default;
base::Optional<std::string> ClientMemory::selected_card() {
return selected_card_;
const autofill::CreditCard* ClientMemory::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) {
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) {
selected_card_ = guid;
void ClientMemory::set_selected_address(
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,
const std::string& guid) {
selected_addresses_[name] = guid;
bool ClientMemory::has_selected_address(const std::string& name) {
return selected_addresses_.find(name) != selected_addresses_.end();
}
} // namespace autofill_assistant
......@@ -9,6 +9,8 @@
#include <string>
#include "base/optional.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
namespace autofill_assistant {
// Data shared between scripts and actions.
......@@ -17,28 +19,39 @@ class ClientMemory {
ClientMemory();
virtual ~ClientMemory();
// GUID of the currently selected credit card, if any. It will be an empty
// optional if user didn't select anything, empty string if user selected
// 'Fill manually', or the guid of a selected card.
virtual base::Optional<std::string> selected_card();
// Selected credit card, if any. It will be a nullptr if didn't select
// anything or if selected 'Fill manually'.
virtual const autofill::CreditCard* selected_card();
// GUID of the currently selected address for |name|. It will be an empty
// optional if user didn't select anything, empty string if user selected
// 'Fill manually', or the guid of a selected address.
virtual base::Optional<std::string> selected_address(const std::string& name);
// Return true if card has been selected, otherwise return false.
// Note that selected_card() might return nullptr when has_selected_card() is
// true because fill manually was chosen.
virtual bool has_selected_card();
// Set the |guid| of the selected card.
virtual void set_selected_card(const std::string& guid);
// Selected address for |name|. It will be a nullptr if didn't select anything
// or if selected 'Fill manually'.
virtual const autofill::AutofillProfile* selected_address(
const std::string& name);
// Set the |guid| of the selected address for |name|.
virtual void set_selected_address(const std::string& name,
const std::string& guid);
// Return true if address has been selected, otherwise return false.
// Note that selected_address() might return nullptr when
// 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:
base::Optional<std::string> selected_card_;
base::Optional<std::unique_ptr<autofill::CreditCard>> selected_card_;
// GUID of the selected addresses (keyed by name).
std::map<std::string, std::string> selected_addresses_;
// The selected addresses (keyed by name).
std::map<std::string, std::unique_ptr<autofill::AutofillProfile>>
selected_addresses_;
};
} // namespace autofill_assistant
......
......@@ -290,7 +290,7 @@ TEST_F(ControllerTest, Reset) {
}
// 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"));
GetUiDelegate()->OnScriptSelected("reset");
......
......@@ -15,12 +15,16 @@ class MockClientMemory : public ClientMemory {
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,
base::Optional<std::string>(const std::string& name));
MOCK_METHOD1(set_selected_card, void(const std::string& guid));
const autofill::AutofillProfile*(const std::string& name));
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,
void(const std::string& name, const std::string& guid));
void(const std::string& name,
std::unique_ptr<autofill::AutofillProfile> address));
};
} // namespace autofill_assistant
......
......@@ -7,6 +7,11 @@
#include <string>
namespace autofill {
class AutofillProfile;
class CreditCard;
} // namespace autofill
namespace autofill_assistant {
// Struct for holding the payment information data.
......@@ -15,9 +20,8 @@ struct PaymentInformation {
~PaymentInformation();
bool succeed;
std::string card_guid;
std::string card_issuer_network;
std::string address_guid;
std::unique_ptr<autofill::CreditCard> card;
std::unique_ptr<autofill::AutofillProfile> address;
std::string payer_name;
std::string payer_phone;
std::string payer_email;
......
......@@ -4,6 +4,9 @@
#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 {
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