Commit ab430571 authored by jinho.bang's avatar jinho.bang Committed by Commit bot

PaymentApp: Introduce PaymentAppDatabase class.

Write PaymentAppDatabase class and then just move most of logics to access
database from PaymentAppManager to the new class.

The class is providing APIs to read/write payment app related data(e.g. manifest
and associated service worker). It can be shared between payment_app_manager.cc
and payment_app_context.cc.

The payment_app_manager.cc contains a actual implementation of the
PaymentAppManager interface[1] used on the page or worker.
Also, the payment_app_context.cc can be used to query all manifests data.
in order to update payment request UI in Chrome layer.

[1] https://w3c.github.io/webpayments-payment-apps-api/#payment-app-manager

BUG=661608

Review-Url: https://codereview.chromium.org/2572183002
Cr-Commit-Position: refs/heads/master@{#439100}
parent f3feff87
...@@ -981,6 +981,8 @@ source_set("browser") { ...@@ -981,6 +981,8 @@ source_set("browser") {
"notifications/type_converters.h", "notifications/type_converters.h",
"payments/payment_app_context_impl.cc", "payments/payment_app_context_impl.cc",
"payments/payment_app_context_impl.h", "payments/payment_app_context_impl.h",
"payments/payment_app_database.cc",
"payments/payment_app_database.h",
"payments/payment_app_manager.cc", "payments/payment_app_manager.cc",
"payments/payment_app_manager.h", "payments/payment_app_manager.h",
"permissions/permission_service_context.cc", "permissions/permission_service_context.cc",
......
...@@ -9,47 +9,57 @@ ...@@ -9,47 +9,57 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "content/browser/payments/payment_app_database.h"
#include "content/browser/payments/payment_app_manager.h" #include "content/browser/payments/payment_app_manager.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
namespace content { namespace content {
PaymentAppContextImpl::PaymentAppContextImpl( PaymentAppContextImpl::PaymentAppContextImpl() : is_shutdown_(false) {
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
: service_worker_context_(std::move(service_worker_context)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
} }
void PaymentAppContextImpl::Shutdown() { void PaymentAppContextImpl::Init(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!is_shutdown_);
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE, BrowserThread::IO, FROM_HERE,
base::Bind(&PaymentAppContextImpl::ShutdownOnIO, this)); base::Bind(&PaymentAppContextImpl::CreatePaymentAppDatabaseOnIO, this,
service_worker_context));
}
void PaymentAppContextImpl::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(&PaymentAppContextImpl::ShutdownOnIO, this),
base::Bind(&PaymentAppContextImpl::DidShutdown, this));
} }
void PaymentAppContextImpl::CreateService( void PaymentAppContextImpl::CreatePaymentAppManager(
mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) { mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE, BrowserThread::IO, FROM_HERE,
base::Bind(&PaymentAppContextImpl::CreateServiceOnIOThread, this, base::Bind(&PaymentAppContextImpl::CreatePaymentAppManagerOnIO, this,
base::Passed(&request))); base::Passed(&request)));
} }
void PaymentAppContextImpl::ServiceHadConnectionError( void PaymentAppContextImpl::PaymentAppManagerHadConnectionError(
PaymentAppManager* service) { PaymentAppManager* payment_app_manager) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(base::ContainsKey(services_, service)); DCHECK(base::ContainsKey(payment_app_managers_, payment_app_manager));
services_.erase(service); payment_app_managers_.erase(payment_app_manager);
} }
ServiceWorkerContextWrapper* PaymentAppContextImpl::service_worker_context() PaymentAppDatabase* PaymentAppContextImpl::payment_app_database() const {
const { DCHECK_CURRENTLY_ON(BrowserThread::IO);
return service_worker_context_.get(); return payment_app_database_.get();
} }
void PaymentAppContextImpl::GetAllManifests( void PaymentAppContextImpl::GetAllManifests(
...@@ -58,20 +68,37 @@ void PaymentAppContextImpl::GetAllManifests( ...@@ -58,20 +68,37 @@ void PaymentAppContextImpl::GetAllManifests(
} }
PaymentAppContextImpl::~PaymentAppContextImpl() { PaymentAppContextImpl::~PaymentAppContextImpl() {
DCHECK(services_.empty()); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(is_shutdown_);
}
void PaymentAppContextImpl::CreatePaymentAppDatabaseOnIO(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
payment_app_database_ =
base::MakeUnique<PaymentAppDatabase>(service_worker_context);
} }
void PaymentAppContextImpl::CreateServiceOnIOThread( void PaymentAppContextImpl::CreatePaymentAppManagerOnIO(
mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) { mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
PaymentAppManager* service = new PaymentAppManager(this, std::move(request)); PaymentAppManager* payment_app_manager =
services_[service] = base::WrapUnique(service); new PaymentAppManager(this, std::move(request));
payment_app_managers_[payment_app_manager] =
base::WrapUnique(payment_app_manager);
} }
void PaymentAppContextImpl::ShutdownOnIO() { void PaymentAppContextImpl::ShutdownOnIO() {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
services_.clear(); payment_app_managers_.clear();
payment_app_database_.reset();
}
void PaymentAppContextImpl::DidShutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
is_shutdown_ = true;
} }
} // namespace content } // namespace content
...@@ -16,29 +16,54 @@ ...@@ -16,29 +16,54 @@
namespace content { namespace content {
class PaymentAppDatabase;
class PaymentAppManager; class PaymentAppManager;
class ServiceWorkerContextWrapper; class ServiceWorkerContextWrapper;
// One instance of this exists per StoragePartition, and services multiple child
// processes/origins. Most logic is delegated to the owned PaymentAppDatabase
// instance, which is only accessed on the IO thread.
//
// This class is created/destructed by StoragePartitionImpl on UI thread.
// However, the PaymentAppDatabase that this class has internally should work on
// IO thread. So, this class has Init() and Shutdown() methods in addition to
// constructor and destructor. They should be called explicitly when creating
// and destroying StoragePartitionImpl.
//
// Expected order of lifetime calls:
// 1) Constructor
// 2) Init()
// 3) Can now call other public methods in this class in any order.
// - Can call CreatePaymentAppManager() on UI thread.
// - Can call GetAllManifests() on UI thread.
// - Can call PaymentAppManagerHadConnectionError() on IO thread.
// - Can call payment_app_database() on IO thread.
// 4) Shutdown()
// 5) Destructor
class CONTENT_EXPORT PaymentAppContextImpl class CONTENT_EXPORT PaymentAppContextImpl
: public base::RefCountedThreadSafe<PaymentAppContextImpl>, : public base::RefCountedThreadSafe<PaymentAppContextImpl>,
NON_EXPORTED_BASE(public PaymentAppContext) { NON_EXPORTED_BASE(public PaymentAppContext) {
public: public:
explicit PaymentAppContextImpl( PaymentAppContextImpl();
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
// Init and Shutdown are for use on the UI thread when the
// StoragePartition is being setup and torn down.
void Init(scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
// Shutdown must be called before deleting this. Call on the UI thread. // Shutdown must be called before deleting this. Call on the UI thread.
void Shutdown(); void Shutdown();
// Create a PaymentAppManager that is owned by this. Call on the UI // Create a PaymentAppManager that is owned by this. Call on the UI
// thread. // thread.
void CreateService( void CreatePaymentAppManager(
mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request);
// Called by PaymentAppManager objects so that they can // Called by PaymentAppManager objects so that they can
// be deleted. Call on the IO thread. // be deleted. Call on the IO thread.
void ServiceHadConnectionError(PaymentAppManager* service); void PaymentAppManagerHadConnectionError(PaymentAppManager* service);
ServiceWorkerContextWrapper* service_worker_context() const; // Should be accessed only on the IO thread.
PaymentAppDatabase* payment_app_database() const;
// PaymentAppContext implementation: // PaymentAppContext implementation:
void GetAllManifests(const GetAllManifestsCallback& callback) override; void GetAllManifests(const GetAllManifestsCallback& callback) override;
...@@ -50,17 +75,26 @@ class CONTENT_EXPORT PaymentAppContextImpl ...@@ -50,17 +75,26 @@ class CONTENT_EXPORT PaymentAppContextImpl
friend class base::RefCountedThreadSafe<PaymentAppContextImpl>; friend class base::RefCountedThreadSafe<PaymentAppContextImpl>;
~PaymentAppContextImpl() override; ~PaymentAppContextImpl() override;
void CreateServiceOnIOThread( void CreatePaymentAppDatabaseOnIO(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
void CreatePaymentAppManagerOnIO(
mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request);
void ShutdownOnIO(); void ShutdownOnIO();
void DidShutdown();
// Only accessed on the IO thread.
std::unique_ptr<PaymentAppDatabase> payment_app_database_;
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; // The PaymentAppManagers are owned by this. They're either deleted during
// ShutdownOnIO or when the channel is closed via
// PaymentAppManagerHadConnectionError. Only accessed on the IO thread.
std::map<PaymentAppManager*, std::unique_ptr<PaymentAppManager>>
payment_app_managers_;
// The services are owned by this. They're either deleted // Only accessed on the UI thread.
// during ShutdownOnIO or when the channel is closed via bool is_shutdown_;
// ServiceHadConnectionError. Only accessed on the IO thread.
std::map<PaymentAppManager*, std::unique_ptr<PaymentAppManager>> services_;
DISALLOW_COPY_AND_ASSIGN(PaymentAppContextImpl); DISALLOW_COPY_AND_ASSIGN(PaymentAppContextImpl);
}; };
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/payments/payment_app_database.h"
#include <utility>
#include "base/bind.h"
#include "base/optional.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"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/browser_thread.h"
namespace content {
namespace {
const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
} // namespace
PaymentAppDatabase::PaymentAppDatabase(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
: service_worker_context_(service_worker_context), weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
PaymentAppDatabase::~PaymentAppDatabase() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
void PaymentAppDatabase::WriteManifest(
const GURL& scope,
payments::mojom::PaymentAppManifestPtr manifest,
const WriteManifestCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->FindReadyRegistrationForPattern(
scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToWriteManifest,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(std::move(manifest)), callback));
}
void PaymentAppDatabase::ReadManifest(const GURL& scope,
const ReadManifestCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->FindReadyRegistrationForPattern(
scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void PaymentAppDatabase::DidFindRegistrationToWriteManifest(
payments::mojom::PaymentAppManifestPtr manifest,
const WriteManifestCallback& 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);
return;
}
PaymentAppManifestProto manifest_proto;
manifest_proto.set_label(manifest->label);
if (manifest->icon)
manifest_proto.set_icon(manifest->icon.value());
for (const auto& option : manifest->options) {
PaymentAppOptionProto* option_proto = manifest_proto.add_options();
option_proto->set_label(option->label);
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);
}
}
std::string serialized;
bool success = manifest_proto.SerializeToString(&serialized);
DCHECK(success);
service_worker_context_->StoreRegistrationUserData(
registration->id(), registration->pattern().GetOrigin(),
{{kPaymentAppManifestDataKey, serialized}},
base::Bind(&PaymentAppDatabase::DidWriteManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void PaymentAppDatabase::DidWriteManifest(const WriteManifestCallback& callback,
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);
}
void PaymentAppDatabase::DidFindRegistrationToReadManifest(
const ReadManifestCallback& 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);
return;
}
service_worker_context_->GetRegistrationUserData(
registration->id(), {kPaymentAppManifestDataKey},
base::Bind(&PaymentAppDatabase::DidReadManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void PaymentAppDatabase::DidReadManifest(const ReadManifestCallback& 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);
return;
}
PaymentAppManifestProto manifest_proto;
bool success = manifest_proto.ParseFromString(data[0]);
if (!success) {
callback.Run(payments::mojom::PaymentAppManifest::New(),
payments::mojom::PaymentAppManifestError::
MANIFEST_STORAGE_OPERATION_FAILED);
return;
}
payments::mojom::PaymentAppManifestPtr manifest =
payments::mojom::PaymentAppManifest::New();
manifest->label = manifest_proto.label();
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->label = option_proto.label();
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));
}
callback.Run(std::move(manifest),
payments::mojom::PaymentAppManifestError::NONE);
}
} // namespace content
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_DATABASE_H_
#define CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_DATABASE_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/payments/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 "mojo/public/cpp/bindings/binding.h"
namespace content {
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)>;
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);
private:
// WriteManifest callbacks
void DidFindRegistrationToWriteManifest(
payments::mojom::PaymentAppManifestPtr manifest,
const WriteManifestCallback& callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
void DidWriteManifest(const WriteManifestCallback& callback,
ServiceWorkerStatusCode status);
// ReadManifest callbacks
void DidFindRegistrationToReadManifest(
const ReadManifestCallback& callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
void DidReadManifest(const ReadManifestCallback& callback,
const std::vector<std::string>& data,
ServiceWorkerStatusCode status);
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
base::WeakPtrFactory<PaymentAppDatabase> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PaymentAppDatabase);
};
} // namespace content
#endif // CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_DATABASE_H_
...@@ -10,16 +10,12 @@ ...@@ -10,16 +10,12 @@
#include "base/optional.h" #include "base/optional.h"
#include "content/browser/payments/payment_app.pb.h" #include "content/browser/payments/payment_app.pb.h"
#include "content/browser/payments/payment_app_context_impl.h" #include "content/browser/payments/payment_app_context_impl.h"
#include "content/browser/payments/payment_app_database.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h" #include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
namespace content { namespace content {
namespace {
const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
} // namespace
PaymentAppManager::~PaymentAppManager() { PaymentAppManager::~PaymentAppManager() {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -39,10 +35,6 @@ PaymentAppManager::PaymentAppManager( ...@@ -39,10 +35,6 @@ PaymentAppManager::PaymentAppManager(
base::Unretained(this))); base::Unretained(this)));
} }
void PaymentAppManager::OnConnectionError() {
payment_app_context_->ServiceHadConnectionError(this);
}
void PaymentAppManager::SetManifest( void PaymentAppManager::SetManifest(
const std::string& scope, const std::string& scope,
payments::mojom::PaymentAppManifestPtr manifest, payments::mojom::PaymentAppManifestPtr manifest,
...@@ -52,128 +44,21 @@ void PaymentAppManager::SetManifest( ...@@ -52,128 +44,21 @@ void PaymentAppManager::SetManifest(
// TODO(zino): Should implement requesting a permission for users to allow // TODO(zino): Should implement requesting a permission for users to allow
// the payment app to be registered. Please see http://crbug.com/665949. // the payment app to be registered. Please see http://crbug.com/665949.
payment_app_context_->service_worker_context() payment_app_context_->payment_app_database()->WriteManifest(
->FindReadyRegistrationForPattern( GURL(scope), std::move(manifest), callback);
GURL(scope),
base::Bind(&PaymentAppManager::DidFindRegistrationToSetManifest,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(std::move(manifest)), callback));
} }
void PaymentAppManager::GetManifest(const std::string& scope, void PaymentAppManager::GetManifest(const std::string& scope,
const GetManifestCallback& callback) { const GetManifestCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
payment_app_context_->service_worker_context() payment_app_context_->payment_app_database()->ReadManifest(GURL(scope),
->FindReadyRegistrationForPattern( callback);
GURL(scope),
base::Bind(&PaymentAppManager::DidFindRegistrationToGetManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void PaymentAppManager::DidFindRegistrationToSetManifest(
payments::mojom::PaymentAppManifestPtr manifest,
const SetManifestCallback& 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);
return;
}
PaymentAppManifestProto manifest_proto;
manifest_proto.set_label(manifest->label);
if (manifest->icon)
manifest_proto.set_icon(manifest->icon.value());
for (const auto& option : manifest->options) {
PaymentAppOptionProto* option_proto = manifest_proto.add_options();
option_proto->set_label(option->label);
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);
}
}
std::string serialized;
bool success = manifest_proto.SerializeToString(&serialized);
DCHECK(success);
payment_app_context_->service_worker_context()->StoreRegistrationUserData(
registration->id(), registration->pattern().GetOrigin(),
{{kPaymentAppManifestDataKey, serialized}},
base::Bind(&PaymentAppManager::DidSetManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void PaymentAppManager::DidSetManifest(const SetManifestCallback& callback,
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);
}
void PaymentAppManager::DidFindRegistrationToGetManifest(
const GetManifestCallback& 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);
return;
}
payment_app_context_->service_worker_context()->GetRegistrationUserData(
registration->id(), {kPaymentAppManifestDataKey},
base::Bind(&PaymentAppManager::DidGetManifest,
weak_ptr_factory_.GetWeakPtr(), callback));
} }
void PaymentAppManager::DidGetManifest(const GetManifestCallback& callback, void PaymentAppManager::OnConnectionError() {
const std::vector<std::string>& data,
ServiceWorkerStatusCode status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (status != SERVICE_WORKER_OK || data.size() != 1) { payment_app_context_->PaymentAppManagerHadConnectionError(this);
callback.Run(payments::mojom::PaymentAppManifest::New(),
payments::mojom::PaymentAppManifestError::
MANIFEST_STORAGE_OPERATION_FAILED);
return;
}
PaymentAppManifestProto manifest_proto;
bool success = manifest_proto.ParseFromString(data[0]);
if (!success) {
callback.Run(payments::mojom::PaymentAppManifest::New(),
payments::mojom::PaymentAppManifestError::
MANIFEST_STORAGE_OPERATION_FAILED);
return;
}
payments::mojom::PaymentAppManifestPtr manifest =
payments::mojom::PaymentAppManifest::New();
manifest->label = manifest_proto.label();
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->label = option_proto.label();
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));
}
callback.Run(std::move(manifest),
payments::mojom::PaymentAppManifestError::NONE);
} }
} // namespace content } // namespace content
...@@ -11,13 +11,11 @@ ...@@ -11,13 +11,11 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/payments/payment_app.mojom.h" #include "components/payments/payment_app.mojom.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_status_code.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
namespace content { namespace content {
class PaymentAppContextImpl; class PaymentAppContextImpl;
class ServiceWorkerRegistration;
class CONTENT_EXPORT PaymentAppManager class CONTENT_EXPORT PaymentAppManager
: public NON_EXPORTED_BASE(payments::mojom::PaymentAppManager) { : public NON_EXPORTED_BASE(payments::mojom::PaymentAppManager) {
...@@ -38,24 +36,6 @@ class CONTENT_EXPORT PaymentAppManager ...@@ -38,24 +36,6 @@ class CONTENT_EXPORT PaymentAppManager
void GetManifest(const std::string& scope, void GetManifest(const std::string& scope,
const GetManifestCallback& callback) override; const GetManifestCallback& callback) override;
// SetManifest callbacks
void DidFindRegistrationToSetManifest(
payments::mojom::PaymentAppManifestPtr manifest,
const SetManifestCallback& callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
void DidSetManifest(const SetManifestCallback& callback,
ServiceWorkerStatusCode status);
// GetManifest callbacks
void DidFindRegistrationToGetManifest(
const GetManifestCallback& callback,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
void DidGetManifest(const GetManifestCallback& callback,
const std::vector<std::string>& data,
ServiceWorkerStatusCode status);
// Called when an error is detected on binding_. // Called when an error is detected on binding_.
void OnConnectionError(); void OnConnectionError();
......
...@@ -26,6 +26,8 @@ namespace { ...@@ -26,6 +26,8 @@ namespace {
const char kServiceWorkerPattern[] = "https://example.com/a"; const char kServiceWorkerPattern[] = "https://example.com/a";
const char kServiceWorkerScript[] = "https://example.com/a/script.js"; const char kServiceWorkerScript[] = "https://example.com/a/script.js";
const char kUnregisteredServiceWorkerPattern[] =
"https://example.com/unregistered";
void RegisterServiceWorkerCallback(bool* called, void RegisterServiceWorkerCallback(bool* called,
int64_t* store_registration_id, int64_t* store_registration_id,
...@@ -37,19 +39,40 @@ void RegisterServiceWorkerCallback(bool* called, ...@@ -37,19 +39,40 @@ void RegisterServiceWorkerCallback(bool* called,
*store_registration_id = registration_id; *store_registration_id = registration_id;
} }
void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, void SetManifestCallback(bool* called,
payments::mojom::PaymentAppManifestError* out_error,
payments::mojom::PaymentAppManifestError error) { payments::mojom::PaymentAppManifestError error) {
*called = true;
*out_error = error; *out_error = error;
} }
void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest, void GetManifestCallback(bool* called,
payments::mojom::PaymentAppManifestPtr* out_manifest,
payments::mojom::PaymentAppManifestError* out_error, payments::mojom::PaymentAppManifestError* out_error,
payments::mojom::PaymentAppManifestPtr manifest, payments::mojom::PaymentAppManifestPtr manifest,
payments::mojom::PaymentAppManifestError error) { payments::mojom::PaymentAppManifestError error) {
*called = true;
*out_manifest = std::move(manifest); *out_manifest = std::move(manifest);
*out_error = error; *out_error = error;
} }
payments::mojom::PaymentAppManifestPtr CreatePaymentAppManifestForTest() {
payments::mojom::PaymentAppOptionPtr option =
payments::mojom::PaymentAppOption::New();
option->label = "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->label = "Payment App";
manifest->options.push_back(std::move(option));
return manifest;
}
} // namespace } // namespace
class PaymentAppManagerTest : public testing::Test { class PaymentAppManagerTest : public testing::Test {
...@@ -60,13 +83,13 @@ class PaymentAppManagerTest : public testing::Test { ...@@ -60,13 +83,13 @@ class PaymentAppManagerTest : public testing::Test {
embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())),
storage_partition_impl_(new StoragePartitionImpl( storage_partition_impl_(new StoragePartitionImpl(
embedded_worker_helper_->browser_context(), embedded_worker_helper_->browser_context(),
base::FilePath(), nullptr)) { base::FilePath(), nullptr)),
sw_registration_id_(0) {
embedded_worker_helper_->context_wrapper()->set_storage_partition( embedded_worker_helper_->context_wrapper()->set_storage_partition(
storage_partition_impl_.get()); storage_partition_impl_.get());
payment_app_context_ = payment_app_context_ = new PaymentAppContextImpl();
new PaymentAppContextImpl(embedded_worker_helper_->context_wrapper()); payment_app_context_->Init(embedded_worker_helper_->context_wrapper());
bool called = false; bool called = false;
embedded_worker_helper_->context()->RegisterServiceWorker( embedded_worker_helper_->context()->RegisterServiceWorker(
...@@ -78,11 +101,11 @@ class PaymentAppManagerTest : public testing::Test { ...@@ -78,11 +101,11 @@ class PaymentAppManagerTest : public testing::Test {
mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request =
mojo::GetProxy(&service_); mojo::GetProxy(&service_);
payment_app_context_->CreateService(std::move(request)); payment_app_context_->CreatePaymentAppManager(std::move(request));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
manager_ = payment_app_context_->services_.begin()->first; manager_ = payment_app_context_->payment_app_managers_.begin()->first;
EXPECT_TRUE(manager_); EXPECT_NE(nullptr, manager_);
} }
~PaymentAppManagerTest() override { ~PaymentAppManagerTest() override {
...@@ -118,30 +141,23 @@ class PaymentAppManagerTest : public testing::Test { ...@@ -118,30 +141,23 @@ class PaymentAppManagerTest : public testing::Test {
}; };
TEST_F(PaymentAppManagerTest, SetAndGetManifest) { TEST_F(PaymentAppManagerTest, SetAndGetManifest) {
payments::mojom::PaymentAppOptionPtr option = bool called = false;
payments::mojom::PaymentAppOption::New(); payments::mojom::PaymentAppManifestError error = payments::mojom::
option->label = "Visa ****"; PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
option->id = "payment-app-id"; SetManifest(kServiceWorkerPattern, CreatePaymentAppManifestForTest(),
option->icon = std::string("payment-app-icon"); base::Bind(&SetManifestCallback, &called, &error));
option->enabled_methods.push_back("visa");
payments::mojom::PaymentAppManifestPtr manifest =
payments::mojom::PaymentAppManifest::New();
manifest->icon = std::string("payment-app-icon");
manifest->label = "Payment App";
manifest->options.push_back(std::move(option));
payments::mojom::PaymentAppManifestError error;
SetManifest(kServiceWorkerPattern, std::move(manifest),
base::Bind(&SetManifestCallback, &error));
ASSERT_TRUE(called);
ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE);
called = false;
payments::mojom::PaymentAppManifestPtr read_manifest; payments::mojom::PaymentAppManifestPtr read_manifest;
payments::mojom::PaymentAppManifestError read_error; payments::mojom::PaymentAppManifestError read_error = payments::mojom::
GetManifest(kServiceWorkerPattern, PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
base::Bind(&GetManifestCallback, &read_manifest, &read_error)); GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called,
&read_manifest, &read_error));
ASSERT_TRUE(called);
ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE);
EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon"));
EXPECT_EQ(read_manifest->label, "Payment App"); EXPECT_EQ(read_manifest->label, "Payment App");
...@@ -153,12 +169,41 @@ TEST_F(PaymentAppManagerTest, SetAndGetManifest) { ...@@ -153,12 +169,41 @@ TEST_F(PaymentAppManagerTest, SetAndGetManifest) {
EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa");
} }
TEST_F(PaymentAppManagerTest, SetManifestWithoutAssociatedServiceWorker) {
bool called = false;
payments::mojom::PaymentAppManifestError error =
payments::mojom::PaymentAppManifestError::NONE;
SetManifest(kUnregisteredServiceWorkerPattern,
CreatePaymentAppManifestForTest(),
base::Bind(&SetManifestCallback, &called, &error));
ASSERT_TRUE(called);
EXPECT_EQ(error, payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
}
TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) {
bool called = false;
payments::mojom::PaymentAppManifestPtr read_manifest;
payments::mojom::PaymentAppManifestError read_error =
payments::mojom::PaymentAppManifestError::NONE;
GetManifest(
kUnregisteredServiceWorkerPattern,
base::Bind(&GetManifestCallback, &called, &read_manifest, &read_error));
ASSERT_TRUE(called);
EXPECT_EQ(read_error,
payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
}
TEST_F(PaymentAppManagerTest, GetManifestWithNoSavedManifest) {
bool called = false;
payments::mojom::PaymentAppManifestPtr read_manifest; payments::mojom::PaymentAppManifestPtr read_manifest;
payments::mojom::PaymentAppManifestError read_error; payments::mojom::PaymentAppManifestError read_error =
GetManifest(kServiceWorkerPattern, payments::mojom::PaymentAppManifestError::NONE;
base::Bind(&GetManifestCallback, &read_manifest, &read_error)); GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called,
&read_manifest, &read_error));
ASSERT_TRUE(called);
EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError::
MANIFEST_STORAGE_OPERATION_FAILED); MANIFEST_STORAGE_OPERATION_FAILED);
} }
......
...@@ -1209,7 +1209,7 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() { ...@@ -1209,7 +1209,7 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() {
AddUIThreadInterface( AddUIThreadInterface(
registry.get(), registry.get(),
base::Bind(&PaymentAppContextImpl::CreateService, base::Bind(&PaymentAppContextImpl::CreatePaymentAppManager,
base::Unretained( base::Unretained(
storage_partition_impl_->GetPaymentAppContext()))); storage_partition_impl_->GetPaymentAppContext())));
......
...@@ -488,8 +488,8 @@ std::unique_ptr<StoragePartitionImpl> StoragePartitionImpl::Create( ...@@ -488,8 +488,8 @@ std::unique_ptr<StoragePartitionImpl> StoragePartitionImpl::Create(
partition->background_sync_context_ = new BackgroundSyncContext(); partition->background_sync_context_ = new BackgroundSyncContext();
partition->background_sync_context_->Init(partition->service_worker_context_); partition->background_sync_context_->Init(partition->service_worker_context_);
partition->payment_app_context_ = new PaymentAppContextImpl( partition->payment_app_context_ = new PaymentAppContextImpl();
partition->service_worker_context_); partition->payment_app_context_->Init(partition->service_worker_context_);
partition->broadcast_channel_provider_ = new BroadcastChannelProvider(); partition->broadcast_channel_provider_ = new BroadcastChannelProvider();
......
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