Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/payments/payment_app_manager.h" | 5 #include "content/browser/payments/payment_app_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/optional.h" | |
| 11 #include "content/browser/payments/payment_app.pb.h" | |
| 10 #include "content/browser/payments/payment_app_context.h" | 12 #include "content/browser/payments/payment_app_context.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 14 #include "content/browser/service_worker/service_worker_registration.h" | |
| 11 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 12 | 16 |
| 13 namespace content { | 17 namespace content { |
| 14 | 18 |
|
please use gerrit instead
2016/11/15 20:04:26
Usually we don't put newlines between two nested n
zino
2016/11/16 18:12:58
Done.
| |
| 19 namespace { | |
| 20 | |
| 21 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | |
| 22 | |
| 23 } | |
|
please use gerrit instead
2016/11/15 20:04:26
Anonymous namespace should end with "} // namespa
zino
2016/11/16 18:12:58
Done.
| |
| 24 | |
| 15 PaymentAppManager::~PaymentAppManager() { | 25 PaymentAppManager::~PaymentAppManager() { |
| 16 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 17 } | 27 } |
| 18 | 28 |
| 19 PaymentAppManager::PaymentAppManager( | 29 PaymentAppManager::PaymentAppManager( |
| 20 PaymentAppContext* payment_app_context, | 30 PaymentAppContext* payment_app_context, |
| 21 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) | 31 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) |
| 22 : payment_app_context_(payment_app_context), | 32 : payment_app_context_(payment_app_context), |
| 23 binding_(this, std::move(request)), | 33 binding_(this, std::move(request)), |
| 24 weak_ptr_factory_(this) { | 34 weak_ptr_factory_(this) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 35 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 DCHECK(payment_app_context); | 36 DCHECK(payment_app_context); |
| 27 | 37 |
| 28 binding_.set_connection_error_handler( | 38 binding_.set_connection_error_handler( |
| 29 base::Bind(&PaymentAppManager::OnConnectionError, | 39 base::Bind(&PaymentAppManager::OnConnectionError, |
| 30 base::Unretained(this))); | 40 base::Unretained(this))); |
| 31 } | 41 } |
| 32 | 42 |
| 33 void PaymentAppManager::OnConnectionError() { | 43 void PaymentAppManager::OnConnectionError() { |
| 34 payment_app_context_->ServiceHadConnectionError(this); | 44 payment_app_context_->ServiceHadConnectionError(this); |
| 35 } | 45 } |
| 36 | 46 |
| 37 void PaymentAppManager::SetManifest( | 47 void PaymentAppManager::SetManifest( |
| 38 const std::string& scope, | 48 const std::string& scope, |
| 39 payments::mojom::PaymentAppManifestPtr manifest, | 49 payments::mojom::PaymentAppManifestPtr manifest, |
| 40 const SetManifestCallback& callback) { | 50 const SetManifestCallback& callback) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 42 callback.Run(payments::mojom::PaymentAppManifestError::NOT_IMPLEMENTED); | 52 |
| 53 // TODO(zino): Should implement requesting a permission for users to allow | |
| 54 // the payment app to be registered. | |
|
please use gerrit instead
2016/11/15 20:04:26
All TODOs should have a bug link. The linked bug s
zino
2016/11/16 18:12:58
Done.
| |
| 55 | |
| 56 payment_app_context_->service_worker_context() | |
| 57 ->FindReadyRegistrationForPattern( | |
| 58 GURL(scope), | |
| 59 base::Bind(&PaymentAppManager::DidFindRegistrationToSetManifest, | |
| 60 weak_ptr_factory_.GetWeakPtr(), | |
| 61 base::Passed(std::move(manifest)), callback)); | |
| 62 } | |
| 63 | |
| 64 void PaymentAppManager::DidFindRegistrationToSetManifest( | |
| 65 payments::mojom::PaymentAppManifestPtr manifest, | |
| 66 const SetManifestCallback& callback, | |
| 67 ServiceWorkerStatusCode status, | |
| 68 scoped_refptr<ServiceWorkerRegistration> registration) { | |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 70 if (status != SERVICE_WORKER_OK) { | |
| 71 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 PaymentAppManifestProto manifest_proto; | |
| 76 manifest_proto.set_label(manifest->label); | |
| 77 if (manifest->icon) | |
| 78 manifest_proto.set_icon(manifest->icon.value()); | |
| 79 | |
| 80 for (const auto& option : manifest->options) { | |
| 81 PaymentAppOptionProto* option_proto = manifest_proto.add_options(); | |
| 82 option_proto->set_label(option->label); | |
| 83 if (option->icon) | |
| 84 option_proto->set_icon(option->icon.value()); | |
| 85 option_proto->set_id(option->id); | |
| 86 for (const auto& method : option->enabled_methods) { | |
| 87 option_proto->add_enabled_methods(method); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 std::string serialized; | |
| 92 bool success = manifest_proto.SerializeToString(&serialized); | |
| 93 DCHECK(success); | |
| 94 | |
| 95 payment_app_context_->service_worker_context()->StoreRegistrationUserData( | |
| 96 registration->id(), registration->pattern().GetOrigin(), | |
| 97 {{kPaymentAppManifestDataKey, serialized}}, | |
| 98 base::Bind(&PaymentAppManager::DidSetManifest, | |
| 99 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 100 } | |
| 101 | |
| 102 void PaymentAppManager::DidSetManifest(const SetManifestCallback& callback, | |
| 103 ServiceWorkerStatusCode status) { | |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 105 if (status != SERVICE_WORKER_OK) { | |
| 106 callback.Run( | |
| 107 payments::mojom::PaymentAppManifestError::STORE_MANIFEST_FAILED); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 // Success | |
| 112 callback.Run(payments::mojom::PaymentAppManifestError::NONE); | |
|
please use gerrit instead
2016/11/15 20:04:26
Cleaner to use a single statement with a ternary c
zino
2016/11/16 18:12:58
Done.
| |
| 43 } | 113 } |
| 44 | 114 |
| 45 } // namespace content | 115 } // namespace content |
| OLD | NEW |