Index: content/browser/payments/payment_app_manager.cc |
diff --git a/content/browser/payments/payment_app_manager.cc b/content/browser/payments/payment_app_manager.cc |
index 0f0c9ecde16b864bfae94a229dd81287e03d1b5a..e5e3df9b606fc4a1305d5d5a9883b6a3f7c43e1a 100644 |
--- a/content/browser/payments/payment_app_manager.cc |
+++ b/content/browser/payments/payment_app_manager.cc |
@@ -7,11 +7,20 @@ |
#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.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 { |
+ |
nhiroki
2016/11/15 02:32:19
nit: remove this blank line or add one after line
zino
2016/11/15 17:57:37
Done.
|
+const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
+} |
+ |
PaymentAppManager::~PaymentAppManager() { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
} |
@@ -39,7 +48,67 @@ void PaymentAppManager::SetManifest( |
payments::mojom::PaymentAppManifestPtr manifest, |
const SetManifestCallback& callback) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- callback.Run(payments::mojom::PaymentAppManifestError::NOT_IMPLEMENTED); |
+ |
+ // TODO(zino): Should implement requesting a permission for users to allow |
+ // the payment app to be registered. |
+ |
+ payment_app_context_->service_worker_context() |
+ ->FindReadyRegistrationForPattern( |
+ GURL(scope), |
+ base::Bind(&PaymentAppManager::DidFindRegistrationToSetManifest, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Passed(std::move(manifest)), 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); |
+ if (status != SERVICE_WORKER_OK) { |
+ callback.Run( |
+ payments::mojom::PaymentAppManifestError::STORE_MANIFEST_FAILED); |
+ return; |
+ } |
+ |
+ // Success |
+ callback.Run(payments::mojom::PaymentAppManifestError::NONE); |
} |
} // namespace content |