summaryrefslogtreecommitdiff
path: root/chromium/content/browser/payments
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2017-07-17 13:57:45 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2017-07-19 13:44:40 +0000
commit6ec7b8da05d21a3878bd21c691b41e675d74bb1c (patch)
treeb87f250bc19413750b9bb9cdbf2da20ef5014820 /chromium/content/browser/payments
parentec02ee4181c49b61fce1c8fb99292dbb8139cc90 (diff)
downloadqtwebengine-chromium-6ec7b8da05d21a3878bd21c691b41e675d74bb1c.tar.gz
BASELINE: Update Chromium to 60.0.3112.70
Change-Id: I9911c2280a014d4632f254857876a395d4baed2d Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/content/browser/payments')
-rw-r--r--chromium/content/browser/payments/payment_app.proto19
-rw-r--r--chromium/content/browser/payments/payment_app_browsertest.cc117
-rw-r--r--chromium/content/browser/payments/payment_app_content_unittest_base.cc64
-rw-r--r--chromium/content/browser/payments/payment_app_content_unittest_base.h9
-rw-r--r--chromium/content/browser/payments/payment_app_context_impl.cc3
-rw-r--r--chromium/content/browser/payments/payment_app_context_impl.h12
-rw-r--r--chromium/content/browser/payments/payment_app_database.cc480
-rw-r--r--chromium/content/browser/payments/payment_app_database.h138
-rw-r--r--chromium/content/browser/payments/payment_app_provider_impl.cc23
-rw-r--r--chromium/content/browser/payments/payment_app_provider_impl.h4
-rw-r--r--chromium/content/browser/payments/payment_app_provider_impl_unittest.cc159
-rw-r--r--chromium/content/browser/payments/payment_manager.cc53
-rw-r--r--chromium/content/browser/payments/payment_manager.h21
-rw-r--r--chromium/content/browser/payments/payment_manager_unittest.cc281
14 files changed, 970 insertions, 413 deletions
diff --git a/chromium/content/browser/payments/payment_app.proto b/chromium/content/browser/payments/payment_app.proto
index df8a8c2a120..c2eadb6d795 100644
--- a/chromium/content/browser/payments/payment_app.proto
+++ b/chromium/content/browser/payments/payment_app.proto
@@ -8,15 +8,16 @@ option optimize_for = LITE_RUNTIME;
package content;
-message PaymentAppManifestProto {
- optional string name = 1;
- optional string icon = 2;
- repeated PaymentAppOptionProto options = 3;
+message StoredPaymentInstrumentKeyInfoProto {
+ optional uint64 insertion_order = 1;
+ optional string key = 2;
}
-message PaymentAppOptionProto {
- optional string name = 1;
- optional string icon = 2;
- optional string id = 3;
- repeated string enabled_methods = 4;
+message StoredPaymentInstrumentProto {
+ optional int64 registration_id = 1;
+ optional string instrument_key = 2;
+ optional string origin = 3;
+ optional string name = 4;
+ repeated string enabled_methods = 5;
+ optional string stringified_capabilities = 6;
}
diff --git a/chromium/content/browser/payments/payment_app_browsertest.cc b/chromium/content/browser/payments/payment_app_browsertest.cc
index 9f840a0f575..f5e732ca7c3 100644
--- a/chromium/content/browser/payments/payment_app_browsertest.cc
+++ b/chromium/content/browser/payments/payment_app_browsertest.cc
@@ -5,7 +5,9 @@
#include "base/command_line.h"
#include "base/macros.h"
#include "base/run_loop.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
+#include "content/browser/storage_partition_impl.h"
+#include "content/public/browser/browser_context.h"
#include "content/public/browser/payment_app_provider.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
@@ -19,17 +21,25 @@
namespace content {
namespace {
-void GetAllManifestsCallback(const base::Closure& done_callback,
- PaymentAppProvider::Manifests* out_manifests,
- PaymentAppProvider::Manifests manifests) {
- *out_manifests = std::move(manifests);
+using ::payments::mojom::PaymentAppRequest;
+using ::payments::mojom::PaymentAppRequestPtr;
+using ::payments::mojom::PaymentAppResponsePtr;
+using ::payments::mojom::PaymentCurrencyAmount;
+using ::payments::mojom::PaymentDetailsModifier;
+using ::payments::mojom::PaymentDetailsModifierPtr;
+using ::payments::mojom::PaymentItem;
+using ::payments::mojom::PaymentMethodData;
+
+void GetAllPaymentAppsCallback(const base::Closure& done_callback,
+ PaymentAppProvider::PaymentApps* out_apps,
+ PaymentAppProvider::PaymentApps apps) {
+ *out_apps = std::move(apps);
done_callback.Run();
}
-void InvokePaymentAppCallback(
- const base::Closure& done_callback,
- payments::mojom::PaymentAppResponsePtr* out_response,
- payments::mojom::PaymentAppResponsePtr response) {
+void InvokePaymentAppCallback(const base::Closure& done_callback,
+ PaymentAppResponsePtr* out_response,
+ PaymentAppResponsePtr response) {
*out_response = std::move(response);
done_callback.Run();
}
@@ -76,32 +86,51 @@ class PaymentAppBrowserTest : public ContentBrowserTest {
std::vector<int64_t> GetAllPaymentAppIDs() {
base::RunLoop run_loop;
- PaymentAppProvider::Manifests manifests;
- PaymentAppProvider::GetInstance()->GetAllManifests(
+ PaymentAppProvider::PaymentApps apps;
+ PaymentAppProvider::GetInstance()->GetAllPaymentApps(
shell()->web_contents()->GetBrowserContext(),
- base::Bind(&GetAllManifestsCallback, run_loop.QuitClosure(),
- &manifests));
+ base::BindOnce(&GetAllPaymentAppsCallback, run_loop.QuitClosure(),
+ &apps));
run_loop.Run();
std::vector<int64_t> ids;
- for (const auto& manifest : manifests) {
- ids.push_back(manifest.first);
+ for (const auto& app_info : apps) {
+ for (const auto& instrument : app_info.second) {
+ ids.push_back(instrument->registration_id);
+ }
}
return ids;
}
- payments::mojom::PaymentAppResponsePtr InvokePaymentApp(
- int64_t registration_id) {
- payments::mojom::PaymentAppRequestPtr app_request =
- payments::mojom::PaymentAppRequest::New();
- app_request->methodData.push_back(
- payments::mojom::PaymentMethodData::New());
- app_request->total = payments::mojom::PaymentItem::New();
- app_request->total->amount = payments::mojom::PaymentCurrencyAmount::New();
+ PaymentAppResponsePtr InvokePaymentAppWithTestData(int64_t registration_id) {
+ PaymentAppRequestPtr app_request = PaymentAppRequest::New();
+
+ app_request->top_level_origin = GURL("https://example.com");
+
+ app_request->payment_request_origin = GURL("https://example.com");
+
+ app_request->payment_request_id = "payment-request-id";
+
+ app_request->method_data.push_back(PaymentMethodData::New());
+ app_request->method_data[0]->supported_methods = {"basic-card"};
+
+ app_request->total = PaymentItem::New();
+ app_request->total->amount = PaymentCurrencyAmount::New();
+ app_request->total->amount->currency = "USD";
+
+ PaymentDetailsModifierPtr modifier = PaymentDetailsModifier::New();
+ modifier->total = PaymentItem::New();
+ modifier->total->amount = PaymentCurrencyAmount::New();
+ modifier->total->amount->currency = "USD";
+ modifier->method_data = PaymentMethodData::New();
+ modifier->method_data->supported_methods = {"basic-card"};
+ app_request->modifiers.push_back(std::move(modifier));
+
+ app_request->instrument_key = "instrument-key";
base::RunLoop run_loop;
- payments::mojom::PaymentAppResponsePtr response;
+ PaymentAppResponsePtr response;
PaymentAppProvider::GetInstance()->InvokePaymentApp(
shell()->web_contents()->GetBrowserContext(), registration_id,
std::move(app_request),
@@ -112,6 +141,22 @@ class PaymentAppBrowserTest : public ContentBrowserTest {
return response;
}
+ void ClearStoragePartitionData() {
+ // Clear data from the storage partition. Parameters are set to clear data
+ // for service workers, for all origins, for an unbounded time range.
+ base::RunLoop run_loop;
+
+ static_cast<StoragePartitionImpl*>(
+ content::BrowserContext::GetDefaultStoragePartition(
+ shell()->web_contents()->GetBrowserContext()))
+ ->ClearData(StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS,
+ StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(),
+ StoragePartition::OriginMatcherFunction(), base::Time(),
+ base::Time::Max(), run_loop.QuitClosure());
+
+ run_loop.Run();
+ }
+
private:
std::unique_ptr<net::EmbeddedTestServer> https_server_;
@@ -124,8 +169,30 @@ IN_PROC_BROWSER_TEST_F(PaymentAppBrowserTest, PaymentAppInvocation) {
std::vector<int64_t> ids = GetAllPaymentAppIDs();
ASSERT_EQ(1U, ids.size());
- payments::mojom::PaymentAppResponsePtr response(InvokePaymentApp(ids[0]));
+ PaymentAppResponsePtr response(InvokePaymentAppWithTestData(ids[0]));
ASSERT_EQ("test", response->method_name);
+
+ ClearStoragePartitionData();
+
+ ids = GetAllPaymentAppIDs();
+ ASSERT_EQ(0U, ids.size());
+
+ EXPECT_EQ("https://example.com/", PopConsoleString() /* topLevelOrigin */);
+ EXPECT_EQ("https://example.com/",
+ PopConsoleString() /* paymentRequestOrigin */);
+ EXPECT_EQ("payment-request-id", PopConsoleString() /* paymentRequestId */);
+ EXPECT_EQ("[{\"supportedMethods\":[\"basic-card\"]}]",
+ PopConsoleString() /* methodData */);
+ EXPECT_EQ(
+ "{\"amount\":{\"currency\":\"USD\",\"currencySystem\":\"urn:iso:std:iso:"
+ "4217\",\"value\":\"\"},\"label\":\"\",\"pending\":false}",
+ PopConsoleString() /* total */);
+ EXPECT_EQ(
+ "[{\"additionalDisplayItems\":[],\"supportedMethods\":[\"basic-card\"],"
+ "\"total\":{\"amount\":{\"currency\":\"USD\",\"currencySystem\":\"urn:"
+ "iso:std:iso:4217\",\"value\":\"\"},\"label\":\"\",\"pending\":false}}]",
+ PopConsoleString() /* modifiers */);
+ EXPECT_EQ("instrument-key", PopConsoleString() /* instrumentKey */);
}
} // namespace content
diff --git a/chromium/content/browser/payments/payment_app_content_unittest_base.cc b/chromium/content/browser/payments/payment_app_content_unittest_base.cc
index 9c5a86950e6..1ce3175e55c 100644
--- a/chromium/content/browser/payments/payment_app_content_unittest_base.cc
+++ b/chromium/content/browser/payments/payment_app_content_unittest_base.cc
@@ -21,6 +21,7 @@
#include "content/public/test/test_browser_thread_bundle.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "mojo/public/cpp/bindings/interface_request.h"
+#include "services/service_manager/public/cpp/bind_source_info.h"
namespace content {
@@ -50,29 +51,31 @@ class PaymentAppContentUnitTestBase::PaymentAppForWorkerTestHelper
last_sw_registration_id_(kInvalidServiceWorkerRegistrationId) {}
~PaymentAppForWorkerTestHelper() override {}
- void OnStartWorker(
- int embedded_worker_id,
- int64_t service_worker_version_id,
- const GURL& scope,
- const GURL& script_url,
- bool pause_after_download,
- mojom::ServiceWorkerEventDispatcherRequest request) override {
+ void OnStartWorker(int embedded_worker_id,
+ int64_t service_worker_version_id,
+ const GURL& scope,
+ const GURL& script_url,
+ bool pause_after_download,
+ mojom::ServiceWorkerEventDispatcherRequest request,
+ mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
+ instance_host) override {
ServiceWorkerVersion* version =
context()->GetLiveVersion(service_worker_version_id);
last_sw_registration_id_ = version->registration_id();
last_sw_scope_ = scope;
EmbeddedWorkerTestHelper::OnStartWorker(
embedded_worker_id, service_worker_version_id, scope, script_url,
- pause_after_download, std::move(request));
+ pause_after_download, std::move(request), std::move(instance_host));
}
void OnPaymentRequestEvent(
payments::mojom::PaymentAppRequestPtr app_request,
payments::mojom::PaymentAppResponseCallbackPtr response_callback,
- const mojom::ServiceWorkerEventDispatcher::
- DispatchPaymentRequestEventCallback& callback) override {
+ mojom::ServiceWorkerEventDispatcher::DispatchPaymentRequestEventCallback
+ callback) override {
EmbeddedWorkerTestHelper::OnPaymentRequestEvent(
- std::move(app_request), std::move(response_callback), callback);
+ std::move(app_request), std::move(response_callback),
+ std::move(callback));
}
int64_t last_sw_registration_id_;
@@ -130,7 +133,8 @@ PaymentManager* PaymentAppContentUnitTestBase::CreatePaymentManager(
mojo::InterfaceRequest<payments::mojom::PaymentManager> request =
mojo::MakeRequest(&manager);
payment_managers_.push_back(std::move(manager));
- payment_app_context()->CreatePaymentManager(std::move(request));
+ payment_app_context()->CreatePaymentManager(service_manager::BindSourceInfo(),
+ std::move(request));
base::RunLoop().RunUntilIdle();
// Find a last registered payment manager.
@@ -147,42 +151,6 @@ PaymentManager* PaymentAppContentUnitTestBase::CreatePaymentManager(
return nullptr;
}
-void PaymentAppContentUnitTestBase::SetManifest(
- PaymentManager* manager,
- payments::mojom::PaymentAppManifestPtr manifest,
- const PaymentManager::SetManifestCallback& callback) {
- ASSERT_NE(nullptr, manager);
- manager->SetManifest(std::move(manifest), callback);
- base::RunLoop().RunUntilIdle();
-}
-
-void PaymentAppContentUnitTestBase::GetManifest(
- PaymentManager* manager,
- const PaymentManager::GetManifestCallback& callback) {
- ASSERT_NE(nullptr, manager);
- manager->GetManifest(callback);
- base::RunLoop().RunUntilIdle();
-}
-
-payments::mojom::PaymentAppManifestPtr
-PaymentAppContentUnitTestBase::CreatePaymentAppManifestForTest(
- const std::string& name) {
- payments::mojom::PaymentAppOptionPtr option =
- payments::mojom::PaymentAppOption::New();
- option->name = "Visa ****";
- option->id = "payment-app-id";
- option->icon = std::string("payment-app-icon");
- option->enabled_methods.push_back("visa");
-
- payments::mojom::PaymentAppManifestPtr manifest =
- payments::mojom::PaymentAppManifest::New();
- manifest->icon = std::string("payment-app-icon");
- manifest->name = name;
- manifest->options.push_back(std::move(option));
-
- return manifest;
-}
-
void PaymentAppContentUnitTestBase::UnregisterServiceWorker(
const GURL& scope_url) {
// Unregister service worker.
diff --git a/chromium/content/browser/payments/payment_app_content_unittest_base.h b/chromium/content/browser/payments/payment_app_content_unittest_base.h
index 9461e5fadd8..3204ba56c85 100644
--- a/chromium/content/browser/payments/payment_app_content_unittest_base.h
+++ b/chromium/content/browser/payments/payment_app_content_unittest_base.h
@@ -11,7 +11,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/browser/payments/payment_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
@@ -31,13 +31,6 @@ class PaymentAppContentUnitTestBase : public testing::Test {
BrowserContext* browser_context();
PaymentManager* CreatePaymentManager(const GURL& scope_url,
const GURL& sw_script_url);
- void SetManifest(PaymentManager* manager,
- payments::mojom::PaymentAppManifestPtr manifest,
- const PaymentManager::SetManifestCallback& callback);
- void GetManifest(PaymentManager* manager,
- const PaymentManager::GetManifestCallback& callback);
- payments::mojom::PaymentAppManifestPtr CreatePaymentAppManifestForTest(
- const std::string& name);
void UnregisterServiceWorker(const GURL& scope_url);
void ResetPaymentAppInvoked() const;
diff --git a/chromium/content/browser/payments/payment_app_context_impl.cc b/chromium/content/browser/payments/payment_app_context_impl.cc
index c065a8ce7be..e699660eac9 100644
--- a/chromium/content/browser/payments/payment_app_context_impl.cc
+++ b/chromium/content/browser/payments/payment_app_context_impl.cc
@@ -39,7 +39,8 @@ void PaymentAppContextImpl::Shutdown() {
}
void PaymentAppContextImpl::CreatePaymentManager(
- mojo::InterfaceRequest<payments::mojom::PaymentManager> request) {
+ const service_manager::BindSourceInfo& source_info,
+ payments::mojom::PaymentManagerRequest request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
diff --git a/chromium/content/browser/payments/payment_app_context_impl.h b/chromium/content/browser/payments/payment_app_context_impl.h
index 2b9aa8f8bf3..82d0422f21d 100644
--- a/chromium/content/browser/payments/payment_app_context_impl.h
+++ b/chromium/content/browser/payments/payment_app_context_impl.h
@@ -10,10 +10,14 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/browser/payments/payment_app_database.h"
#include "content/common/content_export.h"
+namespace service_manager {
+struct BindSourceInfo;
+}
+
namespace content {
class PaymentAppDatabase;
@@ -35,7 +39,7 @@ class ServiceWorkerContextWrapper;
// 2) Init()
// 3) Can now call other public methods in this class in any order.
// - Can call CreatePaymentManager() on UI thread.
-// - Can call GetAllManifests() on UI thread.
+// - Can call GetAllPaymentApps() on UI thread.
// - Can call PaymentManagerHadConnectionError() on IO thread.
// - Can call payment_app_database() on IO thread.
// 4) Shutdown()
@@ -54,8 +58,8 @@ class CONTENT_EXPORT PaymentAppContextImpl
// Create a PaymentManager that is owned by this. Call on the UI
// thread.
- void CreatePaymentManager(
- mojo::InterfaceRequest<payments::mojom::PaymentManager> request);
+ void CreatePaymentManager(const service_manager::BindSourceInfo& source_info,
+ payments::mojom::PaymentManagerRequest request);
// Called by PaymentManager objects so that they can
// be deleted. Call on the IO thread.
diff --git a/chromium/content/browser/payments/payment_app_database.cc b/chromium/content/browser/payments/payment_app_database.cc
index 1809cc398d9..5c58b9b6e1f 100644
--- a/chromium/content/browser/payments/payment_app_database.cc
+++ b/chromium/content/browser/payments/payment_app_database.cc
@@ -4,10 +4,13 @@
#include "content/browser/payments/payment_app_database.h"
+#include <map>
#include <utility>
#include "base/bind.h"
+#include "base/memory/ptr_util.h"
#include "base/optional.h"
+#include "base/time/time.h"
#include "content/browser/payments/payment_app.pb.h"
#include "content/browser/payments/payment_app_context_impl.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
@@ -17,32 +20,68 @@
namespace content {
namespace {
-const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
+using ::payments::mojom::PaymentHandlerStatus;
+using ::payments::mojom::PaymentInstrument;
+using ::payments::mojom::PaymentInstrumentPtr;
-payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest(
- const std::string& input) {
- PaymentAppManifestProto manifest_proto;
- if (!manifest_proto.ParseFromString(input))
- return nullptr;
+const char kPaymentInstrumentPrefix[] = "PaymentInstrument:";
+const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:";
- payments::mojom::PaymentAppManifestPtr manifest =
- payments::mojom::PaymentAppManifest::New();
- manifest->name = manifest_proto.name();
- if (manifest_proto.has_icon())
- manifest->icon = manifest_proto.icon();
- for (const auto& option_proto : manifest_proto.options()) {
- payments::mojom::PaymentAppOptionPtr option =
- payments::mojom::PaymentAppOption::New();
- option->name = option_proto.name();
- if (option_proto.has_icon())
- option->icon = option_proto.icon();
- option->id = option_proto.id();
- for (const auto& method : option_proto.enabled_methods())
- option->enabled_methods.push_back(method);
- manifest->options.push_back(std::move(option));
+std::string CreatePaymentInstrumentKey(const std::string& instrument_key) {
+ return kPaymentInstrumentPrefix + instrument_key;
+}
+
+std::string CreatePaymentInstrumentKeyInfoKey(
+ const std::string& instrument_key) {
+ return kPaymentInstrumentKeyInfoPrefix + instrument_key;
+}
+
+std::map<uint64_t, std::string> ToStoredPaymentInstrumentKeyInfos(
+ const std::vector<std::string>& inputs) {
+ std::map<uint64_t, std::string> key_info;
+ for (const auto& input : inputs) {
+ StoredPaymentInstrumentKeyInfoProto key_info_proto;
+ if (!key_info_proto.ParseFromString(input))
+ return std::map<uint64_t, std::string>();
+
+ key_info.insert(std::pair<uint64_t, std::string>(
+ key_info_proto.insertion_order(), key_info_proto.key()));
}
- return manifest;
+ return key_info;
+}
+
+PaymentInstrumentPtr ToPaymentInstrumentForMojo(const std::string& input) {
+ StoredPaymentInstrumentProto instrument_proto;
+ if (!instrument_proto.ParseFromString(input))
+ return nullptr;
+
+ PaymentInstrumentPtr instrument = PaymentInstrument::New();
+ instrument->name = instrument_proto.name();
+ for (const auto& method : instrument_proto.enabled_methods())
+ instrument->enabled_methods.push_back(method);
+ instrument->stringified_capabilities =
+ instrument_proto.stringified_capabilities();
+
+ return instrument;
+}
+
+std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument(
+ const std::string& input) {
+ StoredPaymentInstrumentProto instrument_proto;
+ if (!instrument_proto.ParseFromString(input))
+ return std::unique_ptr<StoredPaymentInstrument>();
+
+ std::unique_ptr<StoredPaymentInstrument> instrument =
+ base::MakeUnique<StoredPaymentInstrument>();
+ instrument->registration_id = instrument_proto.registration_id();
+ instrument->instrument_key = instrument_proto.instrument_key();
+ instrument->origin = GURL(instrument_proto.origin());
+ instrument->name = instrument_proto.name();
+ for (const auto& method : instrument_proto.enabled_methods())
+ instrument->enabled_methods.push_back(method);
+
+ return instrument;
}
} // namespace
@@ -57,147 +96,384 @@ PaymentAppDatabase::~PaymentAppDatabase() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
-void PaymentAppDatabase::WriteManifest(
+void PaymentAppDatabase::ReadAllPaymentApps(
+ ReadAllPaymentAppsCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix(
+ kPaymentInstrumentPrefix,
+ base::Bind(&PaymentAppDatabase::DidReadAllPaymentApps,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::DeletePaymentInstrument(
const GURL& scope,
- payments::mojom::PaymentAppManifestPtr manifest,
- const WriteManifestCallback& callback) {
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->FindReadyRegistrationForPattern(
- scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToWriteManifest,
+ scope,
+ base::Bind(
+ &PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(), instrument_key,
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::ReadPaymentInstrument(
+ const GURL& scope,
+ const std::string& instrument_key,
+ ReadPaymentInstrumentCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ service_worker_context_->FindReadyRegistrationForPattern(
+ scope,
+ base::Bind(
+ &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(), instrument_key,
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::KeysOfPaymentInstruments(
+ const GURL& scope,
+ KeysOfPaymentInstrumentsCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ service_worker_context_->FindReadyRegistrationForPattern(
+ scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToGetKeys,
weak_ptr_factory_.GetWeakPtr(),
- base::Passed(std::move(manifest)), callback));
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::ReadManifest(const GURL& scope,
- const ReadManifestCallback& callback) {
+void PaymentAppDatabase::HasPaymentInstrument(
+ const GURL& scope,
+ const std::string& instrument_key,
+ HasPaymentInstrumentCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->FindReadyRegistrationForPattern(
- scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest,
- weak_ptr_factory_.GetWeakPtr(), callback));
+ scope,
+ base::Bind(&PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(), instrument_key,
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::ReadAllManifests(
- const ReadAllManifestsCallback& callback) {
+void PaymentAppDatabase::WritePaymentInstrument(
+ const GURL& scope,
+ const std::string& instrument_key,
+ PaymentInstrumentPtr instrument,
+ WritePaymentInstrumentCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- service_worker_context_->GetUserDataForAllRegistrations(
- kPaymentAppManifestDataKey,
- base::Bind(&PaymentAppDatabase::DidReadAllManifests,
- weak_ptr_factory_.GetWeakPtr(), callback));
+ service_worker_context_->FindReadyRegistrationForPattern(
+ scope,
+ base::Bind(
+ &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(), instrument_key,
+ base::Passed(std::move(instrument)),
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::DidFindRegistrationToWriteManifest(
- payments::mojom::PaymentAppManifestPtr manifest,
- const WriteManifestCallback& callback,
+void PaymentAppDatabase::ClearPaymentInstruments(
+ const GURL& scope,
+ ClearPaymentInstrumentsCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ service_worker_context_->FindReadyRegistrationForPattern(
+ scope,
+ base::Bind(
+ &PaymentAppDatabase::DidFindRegistrationToClearPaymentInstruments,
+ weak_ptr_factory_.GetWeakPtr(), scope,
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::DidReadAllPaymentApps(
+ ReadAllPaymentAppsCallback callback,
+ const std::vector<std::pair<int64_t, std::string>>& raw_data,
+ ServiceWorkerStatusCode status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK) {
+ std::move(callback).Run(PaymentApps());
+ return;
+ }
+
+ PaymentApps apps;
+ for (const auto& item_of_raw_data : raw_data) {
+ std::unique_ptr<StoredPaymentInstrument> instrument =
+ ToStoredPaymentInstrument(item_of_raw_data.second);
+ if (!instrument)
+ continue;
+ if (!base::ContainsKey(apps, instrument->origin))
+ apps.insert(std::make_pair(instrument->origin, Instruments()));
+ apps[instrument->origin].push_back(std::move(instrument));
+ }
+
+ std::move(callback).Run(std::move(apps));
+}
+
+void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument(
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (status != SERVICE_WORKER_OK) {
- callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
+ std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
return;
}
- PaymentAppManifestProto manifest_proto;
- manifest_proto.set_name(manifest->name);
- if (manifest->icon)
- manifest_proto.set_icon(manifest->icon.value());
+ service_worker_context_->GetRegistrationUserData(
+ registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
+ base::Bind(&PaymentAppDatabase::DidFindPaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(), registration->id(),
+ instrument_key, base::Passed(std::move(callback))));
+}
- for (const auto& option : manifest->options) {
- PaymentAppOptionProto* option_proto = manifest_proto.add_options();
- option_proto->set_name(option->name);
- if (option->icon)
- option_proto->set_icon(option->icon.value());
- option_proto->set_id(option->id);
- for (const auto& method : option->enabled_methods) {
- option_proto->add_enabled_methods(method);
- }
+void PaymentAppDatabase::DidFindPaymentInstrument(
+ int64_t registration_id,
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK || data.size() != 1) {
+ std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND);
+ return;
}
- std::string serialized;
- bool success = manifest_proto.SerializeToString(&serialized);
- DCHECK(success);
+ service_worker_context_->ClearRegistrationUserData(
+ registration_id,
+ {CreatePaymentInstrumentKey(instrument_key),
+ CreatePaymentInstrumentKeyInfoKey(instrument_key)},
+ base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
+}
- service_worker_context_->StoreRegistrationUserData(
- registration->id(), registration->pattern().GetOrigin(),
- {{kPaymentAppManifestDataKey, serialized}},
- base::Bind(&PaymentAppDatabase::DidWriteManifest,
- weak_ptr_factory_.GetWeakPtr(), callback));
+void PaymentAppDatabase::DidDeletePaymentInstrument(
+ DeletePaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ return std::move(callback).Run(status == SERVICE_WORKER_OK
+ ? PaymentHandlerStatus::SUCCESS
+ : PaymentHandlerStatus::NOT_FOUND);
+}
+
+void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument(
+ const std::string& instrument_key,
+ ReadPaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK) {
+ std::move(callback).Run(PaymentInstrument::New(),
+ PaymentHandlerStatus::NO_ACTIVE_WORKER);
+ return;
+ }
+
+ service_worker_context_->GetRegistrationUserData(
+ registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
+ base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::DidWriteManifest(const WriteManifestCallback& callback,
- ServiceWorkerStatusCode status) {
+void PaymentAppDatabase::DidReadPaymentInstrument(
+ ReadPaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- return callback.Run(status == SERVICE_WORKER_OK
- ? payments::mojom::PaymentAppManifestError::NONE
- : payments::mojom::PaymentAppManifestError::
- MANIFEST_STORAGE_OPERATION_FAILED);
+ if (status != SERVICE_WORKER_OK || data.size() != 1) {
+ std::move(callback).Run(PaymentInstrument::New(),
+ PaymentHandlerStatus::NOT_FOUND);
+ return;
+ }
+
+ PaymentInstrumentPtr instrument = ToPaymentInstrumentForMojo(data[0]);
+ if (!instrument) {
+ std::move(callback).Run(PaymentInstrument::New(),
+ PaymentHandlerStatus::STORAGE_OPERATION_FAILED);
+ return;
+ }
+
+ std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS);
}
-void PaymentAppDatabase::DidFindRegistrationToReadManifest(
- const ReadManifestCallback& callback,
+void PaymentAppDatabase::DidFindRegistrationToGetKeys(
+ KeysOfPaymentInstrumentsCallback callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (status != SERVICE_WORKER_OK) {
- callback.Run(payments::mojom::PaymentAppManifest::New(),
- payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
+ std::move(callback).Run(std::vector<std::string>(),
+ PaymentHandlerStatus::NO_ACTIVE_WORKER);
+ return;
+ }
+
+ service_worker_context_->GetRegistrationUserDataByKeyPrefix(
+ registration->id(), {kPaymentInstrumentKeyInfoPrefix},
+ base::Bind(&PaymentAppDatabase::DidGetKeysOfPaymentInstruments,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::DidGetKeysOfPaymentInstruments(
+ KeysOfPaymentInstrumentsCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK) {
+ std::move(callback).Run(std::vector<std::string>(),
+ PaymentHandlerStatus::NOT_FOUND);
+ return;
+ }
+
+ std::vector<std::string> keys;
+ for (const auto& key_info : ToStoredPaymentInstrumentKeyInfos(data)) {
+ keys.push_back(key_info.second);
+ }
+
+ std::move(callback).Run(keys, PaymentHandlerStatus::SUCCESS);
+}
+
+void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument(
+ const std::string& instrument_key,
+ HasPaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK) {
+ std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
return;
}
service_worker_context_->GetRegistrationUserData(
- registration->id(), {kPaymentAppManifestDataKey},
- base::Bind(&PaymentAppDatabase::DidReadManifest,
- weak_ptr_factory_.GetWeakPtr(), callback));
+ registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
+ base::Bind(&PaymentAppDatabase::DidHasPaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::DidReadManifest(const ReadManifestCallback& callback,
- const std::vector<std::string>& data,
- ServiceWorkerStatusCode status) {
+void PaymentAppDatabase::DidHasPaymentInstrument(
+ DeletePaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (status != SERVICE_WORKER_OK || data.size() != 1) {
- callback.Run(payments::mojom::PaymentAppManifest::New(),
- payments::mojom::PaymentAppManifestError::
- MANIFEST_STORAGE_OPERATION_FAILED);
+ std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND);
return;
}
- payments::mojom::PaymentAppManifestPtr manifest =
- DeserializePaymentAppManifest(data[0]);
- if (!manifest) {
- callback.Run(payments::mojom::PaymentAppManifest::New(),
- payments::mojom::PaymentAppManifestError::
- MANIFEST_STORAGE_OPERATION_FAILED);
+ std::move(callback).Run(PaymentHandlerStatus::SUCCESS);
+}
+
+void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument(
+ const std::string& instrument_key,
+ PaymentInstrumentPtr instrument,
+ WritePaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK) {
+ std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
return;
}
- callback.Run(std::move(manifest),
- payments::mojom::PaymentAppManifestError::NONE);
+ StoredPaymentInstrumentProto instrument_proto;
+ instrument_proto.set_registration_id(registration->id());
+ 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);
+ }
+ instrument_proto.set_stringified_capabilities(
+ instrument->stringified_capabilities);
+
+ std::string serialized_instrument;
+ bool success = instrument_proto.SerializeToString(&serialized_instrument);
+ DCHECK(success);
+
+ StoredPaymentInstrumentKeyInfoProto key_info_proto;
+ key_info_proto.set_key(instrument_key);
+ key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue());
+
+ std::string serialized_key_info;
+ success = key_info_proto.SerializeToString(&serialized_key_info);
+ DCHECK(success);
+
+ service_worker_context_->StoreRegistrationUserData(
+ registration->id(), registration->pattern().GetOrigin(),
+ {{CreatePaymentInstrumentKey(instrument_key), serialized_instrument},
+ {CreatePaymentInstrumentKeyInfoKey(instrument_key),
+ serialized_key_info}},
+ base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
}
-void PaymentAppDatabase::DidReadAllManifests(
- const ReadAllManifestsCallback& callback,
- const std::vector<std::pair<int64_t, std::string>>& raw_data,
+void PaymentAppDatabase::DidWritePaymentInstrument(
+ WritePaymentInstrumentCallback callback,
ServiceWorkerStatusCode status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ return std::move(callback).Run(
+ status == SERVICE_WORKER_OK
+ ? PaymentHandlerStatus::SUCCESS
+ : PaymentHandlerStatus::STORAGE_OPERATION_FAILED);
+}
+
+void PaymentAppDatabase::DidFindRegistrationToClearPaymentInstruments(
+ const GURL& scope,
+ ClearPaymentInstrumentsCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
if (status != SERVICE_WORKER_OK) {
- callback.Run(Manifests());
+ std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
return;
}
- Manifests manifests;
- for (const auto& item_of_raw_data : raw_data) {
- payments::mojom::PaymentAppManifestPtr manifest =
- DeserializePaymentAppManifest(item_of_raw_data.second);
- if (!manifest)
- continue;
+ KeysOfPaymentInstruments(
+ scope,
+ base::BindOnce(&PaymentAppDatabase::DidGetKeysToClearPaymentInstruments,
+ weak_ptr_factory_.GetWeakPtr(), registration->id(),
+ std::move(callback)));
+}
- manifests.push_back(
- ManifestWithID(item_of_raw_data.first, std::move(manifest)));
+void PaymentAppDatabase::DidGetKeysToClearPaymentInstruments(
+ int64_t registration_id,
+ ClearPaymentInstrumentsCallback callback,
+ const std::vector<std::string>& keys,
+ PaymentHandlerStatus status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ if (status != PaymentHandlerStatus::SUCCESS) {
+ std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND);
+ return;
+ }
+
+ std::vector<std::string> keys_with_prefix;
+ for (const auto& key : keys) {
+ keys_with_prefix.push_back(CreatePaymentInstrumentKey(key));
+ keys_with_prefix.push_back(CreatePaymentInstrumentKeyInfoKey(key));
}
- callback.Run(std::move(manifests));
+ service_worker_context_->ClearRegistrationUserData(
+ registration_id, keys_with_prefix,
+ base::Bind(&PaymentAppDatabase::DidClearPaymentInstruments,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(callback))));
+}
+
+void PaymentAppDatabase::DidClearPaymentInstruments(
+ ClearPaymentInstrumentsCallback callback,
+ ServiceWorkerStatusCode status) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ return std::move(callback).Run(status == SERVICE_WORKER_OK
+ ? PaymentHandlerStatus::SUCCESS
+ : PaymentHandlerStatus::NOT_FOUND);
}
} // namespace content
diff --git a/chromium/content/browser/payments/payment_app_database.h b/chromium/content/browser/payments/payment_app_database.h
index 8e83b60aeba..001685034d7 100644
--- a/chromium/content/browser/payments/payment_app_database.h
+++ b/chromium/content/browser/payments/payment_app_database.h
@@ -5,16 +5,18 @@
#ifndef CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_DATABASE_H_
#define CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_DATABASE_H_
+#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_status_code.h"
+#include "content/public/browser/stored_payment_instrument.h"
#include "mojo/public/cpp/bindings/binding.h"
namespace content {
@@ -23,50 +25,122 @@ class ServiceWorkerRegistration;
class CONTENT_EXPORT PaymentAppDatabase {
public:
- using WriteManifestCallback =
- base::Callback<void(payments::mojom::PaymentAppManifestError)>;
- using ReadManifestCallback =
- base::Callback<void(payments::mojom::PaymentAppManifestPtr,
- payments::mojom::PaymentAppManifestError)>;
- using ManifestWithID =
- std::pair<int64_t, payments::mojom::PaymentAppManifestPtr>;
- using Manifests = std::vector<ManifestWithID>;
- using ReadAllManifestsCallback = base::Callback<void(Manifests)>;
+ using Instruments = std::vector<std::unique_ptr<StoredPaymentInstrument>>;
+ using PaymentApps = std::map<GURL, Instruments>;
+ using ReadAllPaymentAppsCallback = base::OnceCallback<void(PaymentApps)>;
+
+ using DeletePaymentInstrumentCallback =
+ base::OnceCallback<void(payments::mojom::PaymentHandlerStatus)>;
+ using ReadPaymentInstrumentCallback =
+ base::OnceCallback<void(payments::mojom::PaymentInstrumentPtr,
+ payments::mojom::PaymentHandlerStatus)>;
+ using KeysOfPaymentInstrumentsCallback =
+ base::OnceCallback<void(const std::vector<std::string>&,
+ payments::mojom::PaymentHandlerStatus)>;
+ using HasPaymentInstrumentCallback =
+ base::OnceCallback<void(payments::mojom::PaymentHandlerStatus)>;
+ using WritePaymentInstrumentCallback =
+ base::OnceCallback<void(payments::mojom::PaymentHandlerStatus)>;
+ using ClearPaymentInstrumentsCallback =
+ base::OnceCallback<void(payments::mojom::PaymentHandlerStatus)>;
explicit PaymentAppDatabase(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
~PaymentAppDatabase();
- void WriteManifest(const GURL& scope,
- payments::mojom::PaymentAppManifestPtr manifest,
- const WriteManifestCallback& callback);
- void ReadManifest(const GURL& scope, const ReadManifestCallback& callback);
- void ReadAllManifests(const ReadAllManifestsCallback& callback);
+ void ReadAllPaymentApps(ReadAllPaymentAppsCallback callback);
+
+ void DeletePaymentInstrument(const GURL& scope,
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback);
+ void ReadPaymentInstrument(const GURL& scope,
+ const std::string& instrument_key,
+ ReadPaymentInstrumentCallback callback);
+ void KeysOfPaymentInstruments(const GURL& scope,
+ KeysOfPaymentInstrumentsCallback callback);
+ void HasPaymentInstrument(const GURL& scope,
+ const std::string& instrument_key,
+ HasPaymentInstrumentCallback callback);
+ void WritePaymentInstrument(const GURL& scope,
+ const std::string& instrument_key,
+ payments::mojom::PaymentInstrumentPtr instrument,
+ WritePaymentInstrumentCallback callback);
+ void ClearPaymentInstruments(const GURL& scope,
+ ClearPaymentInstrumentsCallback callback);
private:
- // WriteManifest callbacks
- void DidFindRegistrationToWriteManifest(
- payments::mojom::PaymentAppManifestPtr manifest,
- const WriteManifestCallback& callback,
+ // ReadAllPaymentApps callbacks
+ void DidReadAllPaymentApps(
+ ReadAllPaymentAppsCallback callback,
+ const std::vector<std::pair<int64_t, std::string>>& raw_data,
+ ServiceWorkerStatusCode status);
+
+ // DeletePaymentInstrument callbacks
+ void DidFindRegistrationToDeletePaymentInstrument(
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
- void DidWriteManifest(const WriteManifestCallback& callback,
- ServiceWorkerStatusCode status);
+ void DidFindPaymentInstrument(int64_t registration_id,
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status);
+ void DidDeletePaymentInstrument(DeletePaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status);
- // ReadManifest callbacks
- void DidFindRegistrationToReadManifest(
- const ReadManifestCallback& callback,
+ // ReadPaymentInstrument callbacks
+ void DidFindRegistrationToReadPaymentInstrument(
+ const std::string& instrument_key,
+ ReadPaymentInstrumentCallback callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
- void DidReadManifest(const ReadManifestCallback& callback,
- const std::vector<std::string>& data,
- ServiceWorkerStatusCode status);
+ void DidReadPaymentInstrument(ReadPaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status);
- // ReadAllManifests callbacks
- void DidReadAllManifests(
- const ReadAllManifestsCallback& callback,
- const std::vector<std::pair<int64_t, std::string>>& raw_data,
- ServiceWorkerStatusCode status);
+ // KeysOfPaymentInstruments callbacks
+ void DidFindRegistrationToGetKeys(
+ KeysOfPaymentInstrumentsCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration);
+ void DidGetKeysOfPaymentInstruments(KeysOfPaymentInstrumentsCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status);
+
+ // HasPaymentInstrument callbacks
+ void DidFindRegistrationToHasPaymentInstrument(
+ const std::string& instrument_key,
+ HasPaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration);
+ void DidHasPaymentInstrument(DeletePaymentInstrumentCallback callback,
+ const std::vector<std::string>& data,
+ ServiceWorkerStatusCode status);
+
+ // WritePaymentInstrument callbacks
+ void DidFindRegistrationToWritePaymentInstrument(
+ const std::string& instrument_key,
+ payments::mojom::PaymentInstrumentPtr instrument,
+ WritePaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration);
+ void DidWritePaymentInstrument(WritePaymentInstrumentCallback callback,
+ ServiceWorkerStatusCode status);
+
+ // ClearPaymentInstruments callbacks
+ void DidFindRegistrationToClearPaymentInstruments(
+ const GURL& scope,
+ ClearPaymentInstrumentsCallback callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration);
+ void DidGetKeysToClearPaymentInstruments(
+ int64_t registration_id,
+ ClearPaymentInstrumentsCallback callback,
+ const std::vector<std::string>& keys,
+ payments::mojom::PaymentHandlerStatus status);
+ void DidClearPaymentInstruments(ClearPaymentInstrumentsCallback callback,
+ ServiceWorkerStatusCode status);
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
base::WeakPtrFactory<PaymentAppDatabase> weak_ptr_factory_;
diff --git a/chromium/content/browser/payments/payment_app_provider_impl.cc b/chromium/content/browser/payments/payment_app_provider_impl.cc
index af8492a371b..73f3cbac668 100644
--- a/chromium/content/browser/payments/payment_app_provider_impl.cc
+++ b/chromium/content/browser/payments/payment_app_provider_impl.cc
@@ -56,21 +56,21 @@ class ResponseCallback : public payments::mojom::PaymentAppResponseCallback {
mojo::Binding<payments::mojom::PaymentAppResponseCallback> binding_;
};
-void DidGetAllManifestsOnIO(
- const PaymentAppProvider::GetAllManifestsCallback& callback,
- PaymentAppProvider::Manifests manifests) {
+void DidGetAllPaymentAppsOnIO(
+ PaymentAppProvider::GetAllPaymentAppsCallback callback,
+ PaymentAppProvider::PaymentApps apps) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
- base::Bind(callback, base::Passed(std::move(manifests))));
+ base::BindOnce(std::move(callback), base::Passed(std::move(apps))));
}
-void GetAllManifestsOnIO(
+void GetAllPaymentAppsOnIO(
scoped_refptr<PaymentAppContextImpl> payment_app_context,
- const PaymentAppProvider::GetAllManifestsCallback& callback) {
+ PaymentAppProvider::GetAllPaymentAppsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- payment_app_context->payment_app_database()->ReadAllManifests(
- base::Bind(&DidGetAllManifestsOnIO, callback));
+ payment_app_context->payment_app_database()->ReadAllPaymentApps(
+ base::BindOnce(&DidGetAllPaymentAppsOnIO, std::move(callback)));
}
void DispatchPaymentRequestEventError(
@@ -148,9 +148,9 @@ PaymentAppProviderImpl* PaymentAppProviderImpl::GetInstance() {
return base::Singleton<PaymentAppProviderImpl>::get();
}
-void PaymentAppProviderImpl::GetAllManifests(
+void PaymentAppProviderImpl::GetAllPaymentApps(
BrowserContext* browser_context,
- const GetAllManifestsCallback& callback) {
+ GetAllPaymentAppsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
@@ -160,7 +160,8 @@ void PaymentAppProviderImpl::GetAllManifests(
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
- base::Bind(&GetAllManifestsOnIO, payment_app_context, callback));
+ base::BindOnce(&GetAllPaymentAppsOnIO, payment_app_context,
+ std::move(callback)));
}
void PaymentAppProviderImpl::InvokePaymentApp(
diff --git a/chromium/content/browser/payments/payment_app_provider_impl.h b/chromium/content/browser/payments/payment_app_provider_impl.h
index a4b8278f990..6daeee99612 100644
--- a/chromium/content/browser/payments/payment_app_provider_impl.h
+++ b/chromium/content/browser/payments/payment_app_provider_impl.h
@@ -18,8 +18,8 @@ class CONTENT_EXPORT PaymentAppProviderImpl : public PaymentAppProvider {
// PaymentAppProvider implementation:
// Should be accessed only on the UI thread.
- void GetAllManifests(BrowserContext* browser_context,
- const GetAllManifestsCallback& callback) override;
+ void GetAllPaymentApps(BrowserContext* browser_context,
+ GetAllPaymentAppsCallback callback) override;
void InvokePaymentApp(BrowserContext* browser_context,
int64_t registration_id,
payments::mojom::PaymentAppRequestPtr app_request,
diff --git a/chromium/content/browser/payments/payment_app_provider_impl_unittest.cc b/chromium/content/browser/payments/payment_app_provider_impl_unittest.cc
index 1872cc8df44..fbc9fcd0a18 100644
--- a/chromium/content/browser/payments/payment_app_provider_impl_unittest.cc
+++ b/chromium/content/browser/payments/payment_app_provider_impl_unittest.cc
@@ -8,33 +8,30 @@
#include "base/macros.h"
#include "base/run_loop.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/browser/payments/payment_app_content_unittest_base.h"
#include "content/browser/payments/payment_app_provider_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
-using payments::mojom::PaymentAppManifestError;
-using payments::mojom::PaymentAppManifestPtr;
-
namespace content {
class PaymentManager;
namespace {
-void SetManifestCallback(bool* called,
- PaymentAppManifestError* out_error,
- PaymentAppManifestError error) {
- *called = true;
- *out_error = error;
+using ::payments::mojom::PaymentHandlerStatus;
+using ::payments::mojom::PaymentInstrument;
+using ::payments::mojom::PaymentInstrumentPtr;
+
+void SetPaymentInstrumentCallback(PaymentHandlerStatus* out_status,
+ PaymentHandlerStatus status) {
+ *out_status = status;
}
-void GetAllManifestsCallback(bool* called,
- PaymentAppProvider::Manifests* out_manifests,
- PaymentAppProvider::Manifests manifests) {
- *called = true;
- *out_manifests = std::move(manifests);
+void GetAllPaymentAppsCallback(PaymentAppProvider::PaymentApps* out_apps,
+ PaymentAppProvider::PaymentApps apps) {
+ *out_apps = std::move(apps);
}
void InvokePaymentAppCallback(bool* called,
@@ -49,9 +46,21 @@ class PaymentAppProviderTest : public PaymentAppContentUnitTestBase {
PaymentAppProviderTest() {}
~PaymentAppProviderTest() override {}
- void GetAllManifests(PaymentAppProvider::GetAllManifestsCallback callback) {
- PaymentAppProviderImpl::GetInstance()->GetAllManifests(browser_context(),
- callback);
+ void SetPaymentInstrument(
+ PaymentManager* manager,
+ const std::string& instrument_key,
+ PaymentInstrumentPtr instrument,
+ PaymentManager::SetPaymentInstrumentCallback callback) {
+ ASSERT_NE(nullptr, manager);
+ manager->SetPaymentInstrument(instrument_key, std::move(instrument),
+ std::move(callback));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void GetAllPaymentApps(
+ PaymentAppProvider::GetAllPaymentAppsCallback callback) {
+ PaymentAppProviderImpl::GetInstance()->GetAllPaymentApps(
+ browser_context(), std::move(callback));
base::RunLoop().RunUntilIdle();
}
@@ -63,89 +72,67 @@ class PaymentAppProviderTest : public PaymentAppContentUnitTestBase {
base::RunLoop().RunUntilIdle();
}
- void CreatePaymentApp(const GURL& scope_url, const GURL& sw_script_url) {
- PaymentManager* manager = CreatePaymentManager(scope_url, sw_script_url);
-
- PaymentAppManifestError error =
- PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
- bool called = false;
- SetManifest(manager, CreatePaymentAppManifestForTest(scope_url.spec()),
- base::Bind(&SetManifestCallback, &called, &error));
- ASSERT_TRUE(called);
-
- ASSERT_EQ(PaymentAppManifestError::NONE, error);
- }
-
private:
DISALLOW_COPY_AND_ASSIGN(PaymentAppProviderTest);
};
-TEST_F(PaymentAppProviderTest, GetAllManifestsTest) {
- static const struct {
- const char* scopeUrl;
- const char* scriptUrl;
- } kPaymentAppInfo[] = {
- {"https://example.com/a", "https://example.com/a/script.js"},
- {"https://example.com/b", "https://example.com/b/script.js"},
- {"https://example.com/c", "https://example.com/c/script.js"}};
-
- for (size_t i = 0; i < arraysize(kPaymentAppInfo); i++) {
- CreatePaymentApp(GURL(kPaymentAppInfo[i].scopeUrl),
- GURL(kPaymentAppInfo[i].scriptUrl));
- }
-
- PaymentAppProvider::Manifests manifests;
- bool called = false;
- GetAllManifests(base::Bind(&GetAllManifestsCallback, &called, &manifests));
- ASSERT_TRUE(called);
-
- ASSERT_EQ(3U, manifests.size());
- size_t i = 0;
- for (const auto& manifest : manifests) {
- EXPECT_EQ("payment-app-icon", manifest.second->icon.value());
- EXPECT_EQ(kPaymentAppInfo[i++].scopeUrl, manifest.second->name);
- ASSERT_EQ(1U, manifest.second->options.size());
- EXPECT_EQ("payment-app-icon", manifest.second->options[0]->icon.value());
- EXPECT_EQ("Visa ****", manifest.second->options[0]->name);
- EXPECT_EQ("payment-app-id", manifest.second->options[0]->id);
- ASSERT_EQ(1U, manifest.second->options[0]->enabled_methods.size());
- EXPECT_EQ("visa", manifest.second->options[0]->enabled_methods[0]);
- }
-}
-
TEST_F(PaymentAppProviderTest, InvokePaymentAppTest) {
- static const struct {
- const char* scopeUrl;
- const char* scriptUrl;
- } kPaymentAppInfo[] = {
- {"https://example.com/a", "https://example.com/a/script.js"},
- {"https://example.com/b", "https://example.com/b/script.js"},
- {"https://example.com/c", "https://example.com/c/script.js"}};
-
- for (size_t i = 0; i < arraysize(kPaymentAppInfo); i++) {
- CreatePaymentApp(GURL(kPaymentAppInfo[i].scopeUrl),
- GURL(kPaymentAppInfo[i].scriptUrl));
- }
-
- PaymentAppProvider::Manifests manifests;
- bool called = false;
- GetAllManifests(base::Bind(&GetAllManifestsCallback, &called, &manifests));
- ASSERT_TRUE(called);
- ASSERT_EQ(3U, manifests.size());
+ PaymentManager* manager1 = CreatePaymentManager(
+ GURL("https://hellopay.com/a"), GURL("https://hellopay.com/a/script.js"));
+ PaymentManager* manager2 = CreatePaymentManager(
+ GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js"));
+
+ PaymentHandlerStatus status;
+ SetPaymentInstrument(manager1, "test_key1",
+ payments::mojom::PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+ SetPaymentInstrument(manager2, "test_key2",
+ payments::mojom::PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+ SetPaymentInstrument(manager2, "test_key3",
+ payments::mojom::PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+
+ PaymentAppProvider::PaymentApps apps;
+ GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps));
+ ASSERT_EQ(2U, apps.size());
payments::mojom::PaymentAppRequestPtr app_request =
payments::mojom::PaymentAppRequest::New();
- app_request->methodData.push_back(payments::mojom::PaymentMethodData::New());
+ app_request->method_data.push_back(payments::mojom::PaymentMethodData::New());
app_request->total = payments::mojom::PaymentItem::New();
app_request->total->amount = payments::mojom::PaymentCurrencyAmount::New();
- called = false;
- InvokePaymentApp(manifests[1].first, std::move(app_request),
+ bool called = false;
+ InvokePaymentApp(apps[GURL("https://hellopay.com/")][0]->registration_id,
+ std::move(app_request),
base::Bind(&InvokePaymentAppCallback, &called));
ASSERT_TRUE(called);
- EXPECT_EQ(manifests[1].first, last_sw_registration_id());
- EXPECT_EQ(GURL(kPaymentAppInfo[1].scopeUrl), last_sw_scope_url());
+ EXPECT_EQ(apps[GURL("https://hellopay.com/")][0]->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"));
+ PaymentManager* manager2 = CreatePaymentManager(
+ GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js"));
+
+ PaymentHandlerStatus status;
+ SetPaymentInstrument(manager1, "test_key1", PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+ SetPaymentInstrument(manager2, "test_key2", PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+ SetPaymentInstrument(manager2, "test_key3", PaymentInstrument::New(),
+ base::Bind(&SetPaymentInstrumentCallback, &status));
+
+ PaymentAppProvider::PaymentApps apps;
+ GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps));
+
+ ASSERT_EQ(2U, apps.size());
+ ASSERT_EQ(1U, apps[GURL("https://hellopay.com/")].size());
+ ASSERT_EQ(2U, apps[GURL("https://bobpay.com/")].size());
}
} // namespace content
diff --git a/chromium/content/browser/payments/payment_manager.cc b/chromium/content/browser/payments/payment_manager.cc
index 6d77f7ce7be..ce9445ed27b 100644
--- a/chromium/content/browser/payments/payment_manager.cc
+++ b/chromium/content/browser/payments/payment_manager.cc
@@ -39,22 +39,57 @@ void PaymentManager::Init(const std::string& scope) {
scope_ = GURL(scope);
}
-void PaymentManager::SetManifest(
- payments::mojom::PaymentAppManifestPtr manifest,
- const SetManifestCallback& callback) {
+void PaymentManager::DeletePaymentInstrument(
+ const std::string& instrument_key,
+ PaymentManager::DeletePaymentInstrumentCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- // TODO(zino): Should implement requesting a permission for users to allow
- // the payment app to be registered. Please see http://crbug.com/665949.
+ payment_app_context_->payment_app_database()->DeletePaymentInstrument(
+ scope_, instrument_key, std::move(callback));
+}
+
+void PaymentManager::GetPaymentInstrument(
+ const std::string& instrument_key,
+ PaymentManager::GetPaymentInstrumentCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ payment_app_context_->payment_app_database()->ReadPaymentInstrument(
+ scope_, instrument_key, std::move(callback));
+}
+
+void PaymentManager::KeysOfPaymentInstruments(
+ PaymentManager::KeysOfPaymentInstrumentsCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ payment_app_context_->payment_app_database()->KeysOfPaymentInstruments(
+ scope_, std::move(callback));
+}
+
+void PaymentManager::HasPaymentInstrument(
+ const std::string& instrument_key,
+ PaymentManager::HasPaymentInstrumentCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ payment_app_context_->payment_app_database()->HasPaymentInstrument(
+ scope_, instrument_key, std::move(callback));
+}
+
+void PaymentManager::SetPaymentInstrument(
+ const std::string& instrument_key,
+ payments::mojom::PaymentInstrumentPtr details,
+ PaymentManager::SetPaymentInstrumentCallback callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
- payment_app_context_->payment_app_database()->WriteManifest(
- scope_, std::move(manifest), callback);
+ payment_app_context_->payment_app_database()->WritePaymentInstrument(
+ scope_, instrument_key, std::move(details), std::move(callback));
}
-void PaymentManager::GetManifest(const GetManifestCallback& callback) {
+void PaymentManager::ClearPaymentInstruments(
+ ClearPaymentInstrumentsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- payment_app_context_->payment_app_database()->ReadManifest(scope_, callback);
+ payment_app_context_->payment_app_database()->ClearPaymentInstruments(
+ scope_, std::move(callback));
}
void PaymentManager::OnConnectionError() {
diff --git a/chromium/content/browser/payments/payment_manager.h b/chromium/content/browser/payments/payment_manager.h
index 6872ad2ac79..ebb2f73264a 100644
--- a/chromium/content/browser/payments/payment_manager.h
+++ b/chromium/content/browser/payments/payment_manager.h
@@ -9,7 +9,7 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "url/gurl.h"
@@ -29,12 +29,25 @@ class CONTENT_EXPORT PaymentManager
private:
friend class PaymentAppContentUnitTestBase;
+ friend class PaymentAppProviderTest;
+ friend class PaymentManagerTest;
// payments::mojom::PaymentManager methods:
void Init(const std::string& scope) override;
- void SetManifest(payments::mojom::PaymentAppManifestPtr manifest,
- const SetManifestCallback& callback) override;
- void GetManifest(const GetManifestCallback& callback) override;
+ void DeletePaymentInstrument(
+ const std::string& instrument_key,
+ DeletePaymentInstrumentCallback callback) override;
+ void GetPaymentInstrument(const std::string& instrument_key,
+ GetPaymentInstrumentCallback callback) override;
+ void KeysOfPaymentInstruments(
+ KeysOfPaymentInstrumentsCallback callback) override;
+ void HasPaymentInstrument(const std::string& instrument_key,
+ HasPaymentInstrumentCallback callback) override;
+ void SetPaymentInstrument(const std::string& instrument_key,
+ payments::mojom::PaymentInstrumentPtr details,
+ SetPaymentInstrumentCallback callback) override;
+ void ClearPaymentInstruments(
+ ClearPaymentInstrumentsCallback callback) override;
// Called when an error is detected on binding_.
void OnConnectionError();
diff --git a/chromium/content/browser/payments/payment_manager_unittest.cc b/chromium/content/browser/payments/payment_manager_unittest.cc
index ebed2aebd2d..42d80cbfff9 100644
--- a/chromium/content/browser/payments/payment_manager_unittest.cc
+++ b/chromium/content/browser/payments/payment_manager_unittest.cc
@@ -5,35 +5,56 @@
#include <utility>
#include "base/macros.h"
-#include "components/payments/content/payment_app.mojom.h"
+#include "base/run_loop.h"
+#include "components/payments/mojom/payment_app.mojom.h"
#include "content/browser/payments/payment_app_content_unittest_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
-using payments::mojom::PaymentAppManifestError;
-using payments::mojom::PaymentAppManifestPtr;
-
namespace content {
namespace {
+using ::payments::mojom::PaymentHandlerStatus;
+using ::payments::mojom::PaymentInstrument;
+using ::payments::mojom::PaymentInstrumentPtr;
+
const char kServiceWorkerPattern[] = "https://example.com/a";
const char kServiceWorkerScript[] = "https://example.com/a/script.js";
-void SetManifestCallback(bool* called,
- PaymentAppManifestError* out_error,
- PaymentAppManifestError error) {
- *called = true;
- *out_error = error;
+void DeletePaymentInstrumentCallback(PaymentHandlerStatus* out_status,
+ PaymentHandlerStatus status) {
+ *out_status = status;
+}
+
+void SetPaymentInstrumentCallback(PaymentHandlerStatus* out_status,
+ PaymentHandlerStatus status) {
+ *out_status = status;
+}
+
+void KeysOfPaymentInstrumentsCallback(std::vector<std::string>* out_keys,
+ PaymentHandlerStatus* out_status,
+ const std::vector<std::string>& keys,
+ PaymentHandlerStatus status) {
+ *out_keys = keys;
+ *out_status = status;
}
-void GetManifestCallback(bool* called,
- PaymentAppManifestPtr* out_manifest,
- PaymentAppManifestError* out_error,
- PaymentAppManifestPtr manifest,
- PaymentAppManifestError error) {
- *called = true;
- *out_manifest = std::move(manifest);
- *out_error = error;
+void HasPaymentInstrumentCallback(PaymentHandlerStatus* out_status,
+ PaymentHandlerStatus status) {
+ *out_status = status;
+}
+
+void GetPaymentInstrumentCallback(PaymentInstrumentPtr* out_instrument,
+ PaymentHandlerStatus* out_status,
+ PaymentInstrumentPtr instrument,
+ PaymentHandlerStatus status) {
+ *out_instrument = std::move(instrument);
+ *out_status = status;
+}
+
+void ClearPaymentInstrumentsCallback(PaymentHandlerStatus* out_status,
+ PaymentHandlerStatus status) {
+ *out_status = status;
}
} // namespace
@@ -48,6 +69,52 @@ class PaymentManagerTest : public PaymentAppContentUnitTestBase {
PaymentManager* payment_manager() const { return manager_; }
+ void DeletePaymentInstrument(const std::string& instrument_key,
+ PaymentHandlerStatus* out_status) {
+ manager_->DeletePaymentInstrument(
+ instrument_key,
+ base::Bind(&DeletePaymentInstrumentCallback, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void SetPaymentInstrument(const std::string& instrument_key,
+ PaymentInstrumentPtr instrument,
+ PaymentHandlerStatus* out_status) {
+ manager_->SetPaymentInstrument(
+ instrument_key, std::move(instrument),
+ base::Bind(&SetPaymentInstrumentCallback, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void KeysOfPaymentInstruments(std::vector<std::string>* out_keys,
+ PaymentHandlerStatus* out_status) {
+ manager_->KeysOfPaymentInstruments(
+ base::Bind(&KeysOfPaymentInstrumentsCallback, out_keys, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void HasPaymentInstrument(const std::string& instrument_key,
+ PaymentHandlerStatus* out_status) {
+ manager_->HasPaymentInstrument(
+ instrument_key, base::Bind(&HasPaymentInstrumentCallback, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void GetPaymentInstrument(const std::string& instrument_key,
+ PaymentInstrumentPtr* out_instrument,
+ PaymentHandlerStatus* out_status) {
+ manager_->GetPaymentInstrument(
+ instrument_key,
+ base::Bind(&GetPaymentInstrumentCallback, out_instrument, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void ClearPaymentInstruments(PaymentHandlerStatus* out_status) {
+ manager_->ClearPaymentInstruments(
+ base::Bind(&ClearPaymentInstrumentsCallback, out_status));
+ base::RunLoop().RunUntilIdle();
+ }
+
private:
// Owned by payment_app_context_.
PaymentManager* manager_;
@@ -55,70 +122,140 @@ class PaymentManagerTest : public PaymentAppContentUnitTestBase {
DISALLOW_COPY_AND_ASSIGN(PaymentManagerTest);
};
-TEST_F(PaymentManagerTest, SetAndGetManifest) {
- bool called = false;
- PaymentAppManifestError error =
- PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
- SetManifest(payment_manager(),
- CreatePaymentAppManifestForTest(kServiceWorkerPattern),
- base::Bind(&SetManifestCallback, &called, &error));
- ASSERT_TRUE(called);
-
- ASSERT_EQ(PaymentAppManifestError::NONE, error);
-
- called = false;
- PaymentAppManifestPtr read_manifest;
- PaymentAppManifestError read_error =
- PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
- GetManifest(payment_manager(), base::Bind(&GetManifestCallback, &called,
- &read_manifest, &read_error));
- ASSERT_TRUE(called);
-
- ASSERT_EQ(payments::mojom::PaymentAppManifestError::NONE, read_error);
- EXPECT_EQ("payment-app-icon", read_manifest->icon.value());
- EXPECT_EQ(kServiceWorkerPattern, read_manifest->name);
- ASSERT_EQ(1U, read_manifest->options.size());
- EXPECT_EQ("payment-app-icon", read_manifest->options[0]->icon.value());
- EXPECT_EQ("Visa ****", read_manifest->options[0]->name);
- EXPECT_EQ("payment-app-id", read_manifest->options[0]->id);
- ASSERT_EQ(1U, read_manifest->options[0]->enabled_methods.size());
- EXPECT_EQ("visa", read_manifest->options[0]->enabled_methods[0]);
+TEST_F(PaymentManagerTest, SetAndGetPaymentInstrument) {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ PaymentInstrumentPtr write_details = PaymentInstrument::New();
+ write_details->name = "Visa ending ****4756",
+ write_details->enabled_methods.push_back("visa");
+ write_details->stringified_capabilities = "{}";
+ SetPaymentInstrument("test_key", std::move(write_details), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+
+ PaymentHandlerStatus read_status = PaymentHandlerStatus::NOT_FOUND;
+ PaymentInstrumentPtr read_details;
+ GetPaymentInstrument("test_key", &read_details, &read_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, read_status);
+ EXPECT_EQ("Visa ending ****4756", read_details->name);
+ ASSERT_EQ(1U, read_details->enabled_methods.size());
+ EXPECT_EQ("visa", read_details->enabled_methods[0]);
+ EXPECT_EQ("{}", read_details->stringified_capabilities);
+}
+
+TEST_F(PaymentManagerTest, GetUnstoredPaymentInstrument) {
+ PaymentHandlerStatus read_status = PaymentHandlerStatus::SUCCESS;
+ PaymentInstrumentPtr read_details;
+ GetPaymentInstrument("test_key", &read_details, &read_status);
+ ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, read_status);
+}
+
+TEST_F(PaymentManagerTest, DeletePaymentInstrument) {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ PaymentInstrumentPtr write_details = PaymentInstrument::New();
+ write_details->name = "Visa ending ****4756",
+ write_details->enabled_methods.push_back("visa");
+ write_details->stringified_capabilities = "{}";
+ SetPaymentInstrument("test_key", std::move(write_details), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+
+ PaymentHandlerStatus read_status = PaymentHandlerStatus::NOT_FOUND;
+ PaymentInstrumentPtr read_details;
+ GetPaymentInstrument("test_key", &read_details, &read_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, read_status);
+
+ PaymentHandlerStatus delete_status = PaymentHandlerStatus::NOT_FOUND;
+ DeletePaymentInstrument("test_key", &delete_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, delete_status);
+
+ read_status = PaymentHandlerStatus::NOT_FOUND;
+ GetPaymentInstrument("test_key", &read_details, &read_status);
+ ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, read_status);
}
-TEST_F(PaymentManagerTest, SetManifestWithoutAssociatedServiceWorker) {
- bool called = false;
- PaymentAppManifestError error = PaymentAppManifestError::NONE;
- UnregisterServiceWorker(GURL(kServiceWorkerPattern));
- SetManifest(payment_manager(),
- CreatePaymentAppManifestForTest(kServiceWorkerPattern),
- base::Bind(&SetManifestCallback, &called, &error));
- ASSERT_TRUE(called);
+TEST_F(PaymentManagerTest, HasPaymentInstrument) {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ PaymentInstrumentPtr write_details = PaymentInstrument::New();
+ write_details->name = "Visa ending ****4756",
+ write_details->enabled_methods.push_back("visa");
+ write_details->stringified_capabilities = "{}";
+ SetPaymentInstrument("test_key", std::move(write_details), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+
+ PaymentHandlerStatus has_status = PaymentHandlerStatus::NOT_FOUND;
+ HasPaymentInstrument("test_key", &has_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, has_status);
- EXPECT_EQ(PaymentAppManifestError::NO_ACTIVE_WORKER, error);
+ HasPaymentInstrument("unstored_test_key", &has_status);
+ ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, has_status);
}
-TEST_F(PaymentManagerTest, GetManifestWithoutAssociatedServiceWorker) {
- bool called = false;
- PaymentAppManifestPtr read_manifest;
- PaymentAppManifestError read_error = PaymentAppManifestError::NONE;
- UnregisterServiceWorker(GURL(kServiceWorkerPattern));
- GetManifest(payment_manager(), base::Bind(&GetManifestCallback, &called,
- &read_manifest, &read_error));
- ASSERT_TRUE(called);
+TEST_F(PaymentManagerTest, KeysOfPaymentInstruments) {
+ PaymentHandlerStatus keys_status = PaymentHandlerStatus::NOT_FOUND;
+ std::vector<std::string> keys;
+ KeysOfPaymentInstruments(&keys, &keys_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, keys_status);
+ ASSERT_EQ(0U, keys.size());
- EXPECT_EQ(PaymentAppManifestError::NO_ACTIVE_WORKER, read_error);
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key1", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key3", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key2", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+
+ keys_status = PaymentHandlerStatus::NOT_FOUND;
+ KeysOfPaymentInstruments(&keys, &keys_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, keys_status);
+ ASSERT_EQ(3U, keys.size());
+ ASSERT_EQ("test_key1", keys[0]);
+ ASSERT_EQ("test_key3", keys[1]);
+ ASSERT_EQ("test_key2", keys[2]);
}
-TEST_F(PaymentManagerTest, GetManifestWithNoSavedManifest) {
- bool called = false;
- PaymentAppManifestPtr read_manifest;
- PaymentAppManifestError read_error = PaymentAppManifestError::NONE;
- GetManifest(payment_manager(), base::Bind(&GetManifestCallback, &called,
- &read_manifest, &read_error));
- ASSERT_TRUE(called);
+TEST_F(PaymentManagerTest, ClearPaymentInstruments) {
+ PaymentHandlerStatus status = PaymentHandlerStatus::NOT_FOUND;
+ std::vector<std::string> keys;
+ KeysOfPaymentInstruments(&keys, &status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
+ ASSERT_EQ(0U, keys.size());
+
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key1", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key3", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+ {
+ PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
+ SetPaymentInstrument("test_key2", PaymentInstrument::New(), &write_status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
+ }
+
+ status = PaymentHandlerStatus::NOT_FOUND;
+ KeysOfPaymentInstruments(&keys, &status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
+ ASSERT_EQ(3U, keys.size());
+
+ status = PaymentHandlerStatus::NOT_FOUND;
+ ClearPaymentInstruments(&status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
- EXPECT_EQ(PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED,
- read_error);
+ status = PaymentHandlerStatus::NOT_FOUND;
+ KeysOfPaymentInstruments(&keys, &status);
+ ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
+ ASSERT_EQ(0U, keys.size());
}
} // namespace content