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 { |
| 18 namespace { |
| 19 |
| 20 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
| 21 |
| 22 } // namespace |
14 | 23 |
15 PaymentAppManager::~PaymentAppManager() { | 24 PaymentAppManager::~PaymentAppManager() { |
16 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
17 } | 26 } |
18 | 27 |
19 PaymentAppManager::PaymentAppManager( | 28 PaymentAppManager::PaymentAppManager( |
20 PaymentAppContext* payment_app_context, | 29 PaymentAppContext* payment_app_context, |
21 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) | 30 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) |
22 : payment_app_context_(payment_app_context), | 31 : payment_app_context_(payment_app_context), |
23 binding_(this, std::move(request)), | 32 binding_(this, std::move(request)), |
24 weak_ptr_factory_(this) { | 33 weak_ptr_factory_(this) { |
25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 34 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
26 DCHECK(payment_app_context); | 35 DCHECK(payment_app_context); |
27 | 36 |
28 binding_.set_connection_error_handler( | 37 binding_.set_connection_error_handler( |
29 base::Bind(&PaymentAppManager::OnConnectionError, | 38 base::Bind(&PaymentAppManager::OnConnectionError, |
30 base::Unretained(this))); | 39 base::Unretained(this))); |
31 } | 40 } |
32 | 41 |
33 void PaymentAppManager::OnConnectionError() { | 42 void PaymentAppManager::OnConnectionError() { |
34 payment_app_context_->ServiceHadConnectionError(this); | 43 payment_app_context_->ServiceHadConnectionError(this); |
35 } | 44 } |
36 | 45 |
37 void PaymentAppManager::SetManifest( | 46 void PaymentAppManager::SetManifest( |
38 const std::string& scope, | 47 const std::string& scope, |
39 payments::mojom::PaymentAppManifestPtr manifest, | 48 payments::mojom::PaymentAppManifestPtr manifest, |
40 const SetManifestCallback& callback) { | 49 const SetManifestCallback& callback) { |
41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
42 callback.Run(payments::mojom::PaymentAppManifestError::NOT_IMPLEMENTED); | 51 |
| 52 // TODO(zino): Should implement requesting a permission for users to allow |
| 53 // the payment app to be registered. Please see http://crbug.com/665949. |
| 54 |
| 55 payment_app_context_->service_worker_context() |
| 56 ->FindReadyRegistrationForPattern( |
| 57 GURL(scope), |
| 58 base::Bind(&PaymentAppManager::DidFindRegistrationToSetManifest, |
| 59 weak_ptr_factory_.GetWeakPtr(), |
| 60 base::Passed(std::move(manifest)), callback)); |
| 61 } |
| 62 |
| 63 void PaymentAppManager::DidFindRegistrationToSetManifest( |
| 64 payments::mojom::PaymentAppManifestPtr manifest, |
| 65 const SetManifestCallback& callback, |
| 66 ServiceWorkerStatusCode status, |
| 67 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 69 if (status != SERVICE_WORKER_OK) { |
| 70 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
| 71 return; |
| 72 } |
| 73 |
| 74 PaymentAppManifestProto manifest_proto; |
| 75 manifest_proto.set_label(manifest->label); |
| 76 if (manifest->icon) |
| 77 manifest_proto.set_icon(manifest->icon.value()); |
| 78 |
| 79 for (const auto& option : manifest->options) { |
| 80 PaymentAppOptionProto* option_proto = manifest_proto.add_options(); |
| 81 option_proto->set_label(option->label); |
| 82 if (option->icon) |
| 83 option_proto->set_icon(option->icon.value()); |
| 84 option_proto->set_id(option->id); |
| 85 for (const auto& method : option->enabled_methods) { |
| 86 option_proto->add_enabled_methods(method); |
| 87 } |
| 88 } |
| 89 |
| 90 std::string serialized; |
| 91 bool success = manifest_proto.SerializeToString(&serialized); |
| 92 DCHECK(success); |
| 93 |
| 94 payment_app_context_->service_worker_context()->StoreRegistrationUserData( |
| 95 registration->id(), registration->pattern().GetOrigin(), |
| 96 {{kPaymentAppManifestDataKey, serialized}}, |
| 97 base::Bind(&PaymentAppManager::DidSetManifest, |
| 98 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 99 } |
| 100 |
| 101 void PaymentAppManager::DidSetManifest(const SetManifestCallback& callback, |
| 102 ServiceWorkerStatusCode status) { |
| 103 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 104 return callback.Run( |
| 105 status == SERVICE_WORKER_OK |
| 106 ? payments::mojom::PaymentAppManifestError::NONE |
| 107 : payments::mojom::PaymentAppManifestError::STORE_MANIFEST_FAILED); |
43 } | 108 } |
44 | 109 |
45 } // namespace content | 110 } // namespace content |
OLD | NEW |