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