Commit 0c3c73f6 authored by Rouslan Solomakhin's avatar Rouslan Solomakhin Committed by Commit Bot

[Web Payment] Requested method data filter.

This patch adds a utility function (and corresponding unit test) for
filtering the requested payment method data according to the payment
method names supported by an app. This prevents a payment app from
receiving method-specific information about methods that it does not
support.

Bug: 1061503
Change-Id: I901a2fd6166ddc031f8910816a0e3f8fdc5a7b93
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300344
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798465}
parent 0762b971
...@@ -166,5 +166,17 @@ base::string16 FormatCardNumberForDisplay(const base::string16& card_number) { ...@@ -166,5 +166,17 @@ base::string16 FormatCardNumberForDisplay(const base::string16& card_number) {
return number; return number;
} }
std::map<std::string, std::set<std::string>> FilterStringifiedMethodData(
const std::map<std::string, std::set<std::string>>& stringified_method_data,
const std::set<std::string>& supported_payment_method_names) {
std::map<std::string, std::set<std::string>> result;
for (const auto& pair : stringified_method_data) {
if (base::Contains(supported_payment_method_names, pair.first)) {
result[pair.first] = pair.second;
}
}
return result;
}
} // namespace data_util } // namespace data_util
} // namespace payments } // namespace payments
...@@ -64,6 +64,23 @@ void ParseSupportedMethods( ...@@ -64,6 +64,23 @@ void ParseSupportedMethods(
// numbers, which start with a letter. // numbers, which start with a letter.
base::string16 FormatCardNumberForDisplay(const base::string16& card_number); base::string16 FormatCardNumberForDisplay(const base::string16& card_number);
// Returns the subset of |stringified_method_data| map where the keys are in the
// |supported_payment_method_names| set. Used for ensuring that a payment app
// will not be queried about payment method names that it does not support.
//
// FilterStringifiedMethodData({"a": {"b"}: "c": {"d"}}, {"a"}) -> {"a": {"b"}}
//
// Both the return value and the first parameter to the function have the
// following format:
// Key: Payment method identifier, such as "example-test" or
// "https://example.test".
// Value: The set of all payment method specific parameters for the given
// payment method identifier, each one serialized into a JSON string,
// e.g., '{"key": "value"}'.
std::map<std::string, std::set<std::string>> FilterStringifiedMethodData(
const std::map<std::string, std::set<std::string>>& stringified_method_data,
const std::set<std::string>& supported_payment_method_names);
} // namespace data_util } // namespace data_util
} // namespace payments } // namespace payments
......
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
#include "components/payments/core/payment_request_data_util.h" #include "components/payments/core/payment_request_data_util.h"
#include <map>
#include <memory> #include <memory>
#include <set>
#include <string>
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -274,5 +277,25 @@ TEST(PaymentRequestDataUtil, ParseSupportedMethods_MultipleEntries) { ...@@ -274,5 +277,25 @@ TEST(PaymentRequestDataUtil, ParseSupportedMethods_MultipleEntries) {
UnorderedElementsAre(kBasicCardMethodName, kBobPayMethod)); UnorderedElementsAre(kBasicCardMethodName, kBobPayMethod));
} }
TEST(PaymentRequestDataUtil, FilterStringifiedMethodData) {
std::map<std::string, std::set<std::string>> requested;
std::set<std::string> supported;
EXPECT_TRUE(FilterStringifiedMethodData(requested, supported).empty());
requested["a"].insert("{\"b\": \"c\"}");
EXPECT_TRUE(FilterStringifiedMethodData(requested, supported).empty());
requested["x"].insert("{\"y\": \"z\"}");
EXPECT_TRUE(FilterStringifiedMethodData(requested, supported).empty());
supported.insert("x");
std::map<std::string, std::set<std::string>> expected;
expected["x"].insert("{\"y\": \"z\"}");
EXPECT_EQ(expected, FilterStringifiedMethodData(requested, supported));
supported.insert("g");
EXPECT_EQ(expected, FilterStringifiedMethodData(requested, supported));
}
} // namespace data_util } // namespace data_util
} // namespace payments } // namespace payments
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