Commit 21f15872 authored by Siyu An's avatar Siyu An Committed by Commit Bot

[Autofill Offer] Fix merchant domain matching logic

Bug: 1112095
Change-Id: I3137ed5e2bda1a3408b1c08d5f163e159780a7c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2444456Reviewed-by: default avatarJared Saul <jsaul@google.com>
Commit-Queue: Siyu An <siyua@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813371}
parent 142c07b5
...@@ -25,9 +25,10 @@ namespace autofill { ...@@ -25,9 +25,10 @@ namespace autofill {
namespace { namespace {
// Ensure the offer is not expired and is valid for the current page. // Ensure the offer is not expired and is valid for the current page.
bool IsOfferEligible(const AutofillOfferData& offer, bool IsOfferEligible(const AutofillOfferData& offer,
const GURL& last_committed_url) { const GURL& last_committed_url_origin) {
bool is_eligible = (offer.expiry > AutofillClock::Now()); bool is_eligible = (offer.expiry > AutofillClock::Now());
is_eligible &= base::ranges::count(offer.merchant_domain, last_committed_url); is_eligible &=
base::ranges::count(offer.merchant_domain, last_committed_url_origin);
return is_eligible; return is_eligible;
} }
} // namespace } // namespace
...@@ -49,12 +50,13 @@ void AutofillOfferManager::OnPersonalDataChanged() { ...@@ -49,12 +50,13 @@ void AutofillOfferManager::OnPersonalDataChanged() {
void AutofillOfferManager::UpdateSuggestionsWithOffers( void AutofillOfferManager::UpdateSuggestionsWithOffers(
const GURL& last_committed_url, const GURL& last_committed_url,
std::vector<Suggestion>& suggestions) { std::vector<Suggestion>& suggestions) {
if (eligible_merchant_domains_.count(last_committed_url) == 0) { GURL last_committed_url_origin = last_committed_url.GetOrigin();
if (eligible_merchant_domains_.count(last_committed_url_origin) == 0) {
return; return;
} }
AutofillOfferManager::OffersMap eligible_offers_map = AutofillOfferManager::OffersMap eligible_offers_map =
CreateOffersMap(last_committed_url); CreateOffersMap(last_committed_url_origin);
// Update |offer_label| for each suggestion. // Update |offer_label| for each suggestion.
for (auto& suggestion : suggestions) { for (auto& suggestion : suggestions) {
...@@ -85,7 +87,7 @@ void AutofillOfferManager::UpdateEligibleMerchantDomains() { ...@@ -85,7 +87,7 @@ void AutofillOfferManager::UpdateEligibleMerchantDomains() {
} }
AutofillOfferManager::OffersMap AutofillOfferManager::CreateOffersMap( AutofillOfferManager::OffersMap AutofillOfferManager::CreateOffersMap(
const GURL& last_committed_url) const { const GURL& last_committed_url_origin) const {
AutofillOfferManager::OffersMap offers_map; AutofillOfferManager::OffersMap offers_map;
std::vector<AutofillOfferData*> offers = std::vector<AutofillOfferData*> offers =
...@@ -94,7 +96,7 @@ AutofillOfferManager::OffersMap AutofillOfferManager::CreateOffersMap( ...@@ -94,7 +96,7 @@ AutofillOfferManager::OffersMap AutofillOfferManager::CreateOffersMap(
for (auto* offer : offers) { for (auto* offer : offers) {
// Ensure the offer is valid. // Ensure the offer is valid.
if (!IsOfferEligible(*offer, last_committed_url)) { if (!IsOfferEligible(*offer, last_committed_url_origin)) {
continue; continue;
} }
......
...@@ -49,7 +49,7 @@ class AutofillOfferManager : public KeyedService, ...@@ -49,7 +49,7 @@ class AutofillOfferManager : public KeyedService,
// Creates a mapping from Suggestion Backend ID's to eligible Credit Card // Creates a mapping from Suggestion Backend ID's to eligible Credit Card
// Offers. // Offers.
OffersMap CreateOffersMap(const GURL& last_committed_url) const; OffersMap CreateOffersMap(const GURL& last_committed_url_origin) const;
PersonalDataManager* personal_data_; PersonalDataManager* personal_data_;
std::set<GURL> eligible_merchant_domains_ = {}; std::set<GURL> eligible_merchant_domains_ = {};
......
...@@ -25,6 +25,8 @@ namespace { ...@@ -25,6 +25,8 @@ namespace {
const char kTestGuid[] = "00000000-0000-0000-0000-000000000001"; const char kTestGuid[] = "00000000-0000-0000-0000-000000000001";
const char kTestNumber[] = "4234567890123456"; // Visa const char kTestNumber[] = "4234567890123456"; // Visa
const char kTestUrl[] = "http://www.example.com/"; const char kTestUrl[] = "http://www.example.com/";
const char kTestUrlWithParam[] =
"http://www.example.com/en/payments?name=checkout";
} // namespace } // namespace
...@@ -91,7 +93,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_EligibleDiscount) { ...@@ -91,7 +93,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_EligibleDiscount) {
std::vector<Suggestion> suggestions = {Suggestion()}; std::vector<Suggestion> suggestions = {Suggestion()};
suggestions[0].backend_id = kTestGuid; suggestions[0].backend_id = kTestGuid;
autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrl), autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrlWithParam),
suggestions); suggestions);
EXPECT_EQ(suggestions[0].offer_label, base::UTF8ToUTF16("$4 Off")); EXPECT_EQ(suggestions[0].offer_label, base::UTF8ToUTF16("$4 Off"));
...@@ -103,7 +105,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_EligibleCashback) { ...@@ -103,7 +105,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_EligibleCashback) {
std::vector<Suggestion> suggestions = {Suggestion()}; std::vector<Suggestion> suggestions = {Suggestion()};
suggestions[0].backend_id = kTestGuid; suggestions[0].backend_id = kTestGuid;
autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrl), autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrlWithParam),
suggestions); suggestions);
EXPECT_EQ(suggestions[0].offer_label, base::UTF8ToUTF16("5% Cash Back")); EXPECT_EQ(suggestions[0].offer_label, base::UTF8ToUTF16("5% Cash Back"));
...@@ -115,7 +117,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_ExpiredOffer) { ...@@ -115,7 +117,7 @@ TEST_F(AutofillOfferManagerTest, UpdateSuggestionsWithOffers_ExpiredOffer) {
std::vector<Suggestion> suggestions = {Suggestion()}; std::vector<Suggestion> suggestions = {Suggestion()};
suggestions[0].backend_id = kTestGuid; suggestions[0].backend_id = kTestGuid;
autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrl), autofill_offer_manager_->UpdateSuggestionsWithOffers(GURL(kTestUrlWithParam),
suggestions); suggestions);
EXPECT_TRUE(suggestions[0].offer_label.empty()); EXPECT_TRUE(suggestions[0].offer_label.empty());
......
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