Commit 29b8809e authored by gogerald's avatar gogerald Committed by Commit Bot

[Payments] Supports multiple web payment apps from the same origin

This CL changes the stored data scheme of the web payment app,
so users have to re-register the web payment app after this patch.

This should be fine since this feature is still behind the flag. 

Bug: 751737
Change-Id: I396960273dca51f82a4490fa2dd5f7756e3a0bf0
Reviewed-on: https://chromium-review.googlesource.com/598636
Commit-Queue: Ganggui Tang <gogerald@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarNasko Oskov <nasko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491742}
parent 94d9075e
......@@ -18,8 +18,8 @@ message StoredPaymentInstrumentImageObject {
}
message StoredPaymentInstrumentProto {
optional string instrument_key = 1;
optional string origin = 2;
optional int64 registration_id = 1;
optional string instrument_key = 2;
optional string name = 3;
repeated string enabled_methods = 4;
optional string stringified_capabilities = 5;
......
......@@ -33,8 +33,9 @@ const char kPaymentAppPrefix[] = "PaymentApp:";
const char kPaymentInstrumentPrefix[] = "PaymentInstrument:";
const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:";
std::string CreatePaymentAppKey(const std::string& origin) {
return kPaymentAppPrefix + origin;
// |pattern| is the scope URL of the service worker registration.
std::string CreatePaymentAppKey(const std::string& pattern) {
return kPaymentAppPrefix + pattern;
}
std::string CreatePaymentInstrumentKey(const std::string& instrument_key) {
......@@ -283,7 +284,7 @@ void PaymentAppDatabase::DidFindRegistrationToWritePaymentAppInfo(
service_worker_context_->StoreRegistrationUserData(
registration->id(), registration->pattern().GetOrigin(),
{{CreatePaymentAppKey(registration->pattern().GetOrigin().spec()),
{{CreatePaymentAppKey(registration->pattern().spec()),
serialized_payment_app}},
base::Bind(&PaymentAppDatabase::DidWritePaymentApp,
weak_ptr_factory_.GetWeakPtr(),
......@@ -335,7 +336,7 @@ void PaymentAppDatabase::DidReadAllPaymentApps(
std::unique_ptr<StoredPaymentApp> app =
ToStoredPaymentApp(item_of_raw_data.second);
if (app)
apps[app->origin.GetURL()] = std::move(app);
apps[app->registration_id] = std::move(app);
}
if (apps.size() == 0U) {
......@@ -366,12 +367,12 @@ void PaymentAppDatabase::DidReadAllPaymentInstruments(
if (!instrument_proto.ParseFromString(item_of_raw_data.second))
continue;
GURL origin = GURL(instrument_proto.origin());
if (!base::ContainsKey(apps, origin))
int64_t id = instrument_proto.registration_id();
if (!base::ContainsKey(apps, id))
continue;
for (const auto& method : instrument_proto.enabled_methods()) {
apps[origin]->enabled_methods.push_back(method);
apps[id]->enabled_methods.push_back(method);
}
}
......@@ -548,9 +549,9 @@ void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument(
}
StoredPaymentInstrumentProto instrument_proto;
instrument_proto.set_registration_id(registration->id());
instrument_proto.set_decoded_instrument_icon(decoded_instrument_icon);
instrument_proto.set_instrument_key(instrument_key);
instrument_proto.set_origin(registration->pattern().GetOrigin().spec());
instrument_proto.set_name(instrument->name);
for (const auto& method : instrument->enabled_methods) {
instrument_proto.add_enabled_methods(method);
......@@ -634,7 +635,7 @@ void PaymentAppDatabase::DidGetKeysToClearPaymentInstruments(
// Clear payment app info after clearing all payment instruments.
keys_with_prefix.push_back(
CreatePaymentAppKey(registration->pattern().GetOrigin().spec()));
CreatePaymentAppKey(registration->pattern().spec()));
service_worker_context_->ClearRegistrationUserData(
registration->id(), keys_with_prefix,
......
......@@ -27,7 +27,7 @@ class ServiceWorkerRegistration;
class CONTENT_EXPORT PaymentAppDatabase {
public:
using PaymentApps = std::map<GURL, std::unique_ptr<StoredPaymentApp>>;
using PaymentApps = std::map<int64_t, std::unique_ptr<StoredPaymentApp>>;
using ReadAllPaymentAppsCallback = base::OnceCallback<void(PaymentApps)>;
using DeletePaymentInstrumentCallback =
......
......@@ -112,8 +112,7 @@ TEST_F(PaymentAppProviderTest, CanMakePaymentTest) {
event_data->method_data.push_back(std::move(methodData));
bool can_make_payment = false;
CanMakePayment(apps[GURL("https://example.com/")]->registration_id,
std::move(event_data),
CanMakePayment(last_sw_registration_id(), std::move(event_data),
base::BindOnce(&CanMakePaymentCallback, &can_make_payment));
ASSERT_TRUE(can_make_payment);
}
......@@ -139,26 +138,62 @@ TEST_F(PaymentAppProviderTest, InvokePaymentAppTest) {
GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps));
ASSERT_EQ(2U, apps.size());
int64_t bobpay_registration_id = last_sw_registration_id();
EXPECT_EQ(apps[bobpay_registration_id]->origin.Serialize(),
"https://bobpay.com");
payments::mojom::PaymentRequestEventDataPtr event_data =
payments::mojom::PaymentRequestEventData::New();
event_data->method_data.push_back(payments::mojom::PaymentMethodData::New());
event_data->total = payments::mojom::PaymentCurrencyAmount::New();
bool called = false;
InvokePaymentApp(apps[GURL("https://hellopay.com/")]->registration_id,
std::move(event_data),
InvokePaymentApp(bobpay_registration_id, std::move(event_data),
base::Bind(&InvokePaymentAppCallback, &called));
ASSERT_TRUE(called);
EXPECT_EQ(apps[GURL("https://hellopay.com/")]->registration_id,
last_sw_registration_id());
}
TEST_F(PaymentAppProviderTest, GetAllPaymentAppsTest) {
PaymentManager* manager1 = CreatePaymentManager(
GURL("https://hellopay.com/a"), GURL("https://hellopay.com/a/script.js"));
int64_t hellopay_registration_id = last_sw_registration_id();
PaymentManager* manager2 = CreatePaymentManager(
GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js"));
int64_t bobpay_registration_id = last_sw_registration_id();
PaymentHandlerStatus status;
PaymentInstrumentPtr instrument_1 = PaymentInstrument::New();
instrument_1->enabled_methods.push_back("hellopay");
SetPaymentInstrument(manager1, "test_key1", std::move(instrument_1),
base::Bind(&SetPaymentInstrumentCallback, &status));
PaymentInstrumentPtr instrument_2 = PaymentInstrument::New();
instrument_2->enabled_methods.push_back("hellopay");
SetPaymentInstrument(manager2, "test_key2", std::move(instrument_2),
base::Bind(&SetPaymentInstrumentCallback, &status));
PaymentInstrumentPtr instrument_3 = PaymentInstrument::New();
instrument_3->enabled_methods.push_back("bobpay");
SetPaymentInstrument(manager2, "test_key3", std::move(instrument_3),
base::Bind(&SetPaymentInstrumentCallback, &status));
PaymentAppProvider::PaymentApps apps;
GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps));
ASSERT_EQ(2U, apps.size());
ASSERT_EQ(1U, apps[hellopay_registration_id]->enabled_methods.size());
ASSERT_EQ(2U, apps[bobpay_registration_id]->enabled_methods.size());
}
TEST_F(PaymentAppProviderTest, GetAllPaymentAppsFromTheSameOriginTest) {
PaymentManager* manager1 = CreatePaymentManager(
GURL("https://bobpay.com/a"), GURL("https://bobpay.com/a/script.js"));
int64_t bobpay_a_registration_id = last_sw_registration_id();
PaymentManager* manager2 = CreatePaymentManager(
GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js"));
int64_t bobpay_b_registration_id = last_sw_registration_id();
PaymentHandlerStatus status;
PaymentInstrumentPtr instrument_1 = PaymentInstrument::New();
......@@ -180,8 +215,8 @@ TEST_F(PaymentAppProviderTest, GetAllPaymentAppsTest) {
GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps));
ASSERT_EQ(2U, apps.size());
ASSERT_EQ(1U, apps[GURL("https://hellopay.com/")]->enabled_methods.size());
ASSERT_EQ(2U, apps[GURL("https://bobpay.com/")]->enabled_methods.size());
ASSERT_EQ(1U, apps[bobpay_a_registration_id]->enabled_methods.size());
ASSERT_EQ(2U, apps[bobpay_b_registration_id]->enabled_methods.size());
}
} // namespace content
......@@ -32,7 +32,7 @@ class CONTENT_EXPORT PaymentAppProvider {
// Please see: content/browser/payments/payment_app_provider_impl.cc
static PaymentAppProvider* GetInstance();
using PaymentApps = std::map<GURL, std::unique_ptr<StoredPaymentApp>>;
using PaymentApps = std::map<int64_t, std::unique_ptr<StoredPaymentApp>>;
using GetAllPaymentAppsCallback = base::OnceCallback<void(PaymentApps)>;
using InvokePaymentAppCallback =
base::OnceCallback<void(payments::mojom::PaymentHandlerResponsePtr)>;
......
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