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_database.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" | 10 #include "base/optional.h" |
11 #include "content/browser/payments/payment_app.pb.h" | 11 #include "content/browser/payments/payment_app.pb.h" |
12 #include "content/browser/payments/payment_app_context.h" | 12 #include "content/browser/payments/payment_app_context_impl.h" |
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | 20 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
21 | 21 |
| 22 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( |
| 23 const std::string& input) { |
| 24 PaymentAppManifestProto manifest_proto; |
| 25 bool success = manifest_proto.ParseFromString(input); |
| 26 if (!success) |
| 27 return nullptr; |
| 28 |
| 29 payments::mojom::PaymentAppManifestPtr manifest = |
| 30 payments::mojom::PaymentAppManifest::New(); |
| 31 |
| 32 manifest->label = manifest_proto.label(); |
| 33 if (manifest_proto.has_icon()) |
| 34 manifest->icon = manifest_proto.icon(); |
| 35 for (const auto& option_proto : manifest_proto.options()) { |
| 36 payments::mojom::PaymentAppOptionPtr option = |
| 37 payments::mojom::PaymentAppOption::New(); |
| 38 option->label = option_proto.label(); |
| 39 if (option_proto.has_icon()) |
| 40 option->icon = option_proto.icon(); |
| 41 option->id = option_proto.id(); |
| 42 for (const auto& method : option_proto.enabled_methods()) |
| 43 option->enabled_methods.push_back(method); |
| 44 manifest->options.push_back(std::move(option)); |
| 45 } |
| 46 |
| 47 return manifest; |
| 48 } |
| 49 |
| 50 bool SerializePaymentAppManifest( |
| 51 payments::mojom::PaymentAppManifestPtr manifest, |
| 52 std::string* output) { |
| 53 DCHECK(manifest); |
| 54 |
| 55 PaymentAppManifestProto manifest_proto; |
| 56 manifest_proto.set_label(manifest->label); |
| 57 if (manifest->icon) |
| 58 manifest_proto.set_icon(manifest->icon.value()); |
| 59 |
| 60 for (const auto& option : manifest->options) { |
| 61 PaymentAppOptionProto* option_proto = manifest_proto.add_options(); |
| 62 option_proto->set_label(option->label); |
| 63 if (option->icon) |
| 64 option_proto->set_icon(option->icon.value()); |
| 65 option_proto->set_id(option->id); |
| 66 for (const auto& method : option->enabled_methods) { |
| 67 option_proto->add_enabled_methods(method); |
| 68 } |
| 69 } |
| 70 |
| 71 return manifest_proto.SerializeToString(output); |
| 72 } |
| 73 |
22 } // namespace | 74 } // namespace |
23 | 75 |
24 PaymentAppManager::~PaymentAppManager() { | 76 PaymentAppDatabase::PaymentAppDatabase( |
| 77 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 78 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
26 } | 80 } |
27 | 81 |
28 PaymentAppManager::PaymentAppManager( | 82 PaymentAppDatabase::~PaymentAppDatabase() { |
29 PaymentAppContext* payment_app_context, | |
30 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) | |
31 : payment_app_context_(payment_app_context), | |
32 binding_(this, std::move(request)), | |
33 weak_ptr_factory_(this) { | |
34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
35 DCHECK(payment_app_context); | |
36 | |
37 binding_.set_connection_error_handler( | |
38 base::Bind(&PaymentAppManager::OnConnectionError, | |
39 base::Unretained(this))); | |
40 } | 84 } |
41 | 85 |
42 void PaymentAppManager::OnConnectionError() { | 86 void PaymentAppDatabase::WriteManifest( |
43 payment_app_context_->ServiceHadConnectionError(this); | 87 const std::string& scope, |
| 88 payments::mojom::PaymentAppManifestPtr manifest, |
| 89 const WriteManifestCallback& callback) { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 |
| 92 service_worker_context_->FindReadyRegistrationForPattern( |
| 93 GURL(scope), |
| 94 base::Bind(&PaymentAppDatabase::DidFindRegistrationToWriteManifest, |
| 95 weak_ptr_factory_.GetWeakPtr(), |
| 96 base::Passed(std::move(manifest)), callback)); |
44 } | 97 } |
45 | 98 |
46 void PaymentAppManager::SetManifest( | 99 void PaymentAppDatabase::ReadManifest(const std::string& scope, |
47 const std::string& scope, | 100 const ReadManifestCallback& callback) { |
48 payments::mojom::PaymentAppManifestPtr manifest, | |
49 const SetManifestCallback& callback) { | |
50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
51 | 102 |
52 // TODO(zino): Should implement requesting a permission for users to allow | 103 service_worker_context_->FindReadyRegistrationForPattern( |
53 // the payment app to be registered. Please see http://crbug.com/665949. | 104 GURL(scope), |
54 | 105 base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest, |
55 payment_app_context_->service_worker_context() | 106 weak_ptr_factory_.GetWeakPtr(), callback)); |
56 ->FindReadyRegistrationForPattern( | |
57 GURL(scope), | |
58 base::Bind(&PaymentAppManager::DidFindRegistrationToSetManifest, | |
59 weak_ptr_factory_.GetWeakPtr(), | |
60 base::Passed(std::move(manifest)), callback)); | |
61 } | 107 } |
62 | 108 |
63 void PaymentAppManager::GetManifest(const std::string& scope, | 109 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( |
64 const GetManifestCallback& callback) { | |
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
66 | |
67 payment_app_context_->service_worker_context() | |
68 ->FindReadyRegistrationForPattern( | |
69 GURL(scope), | |
70 base::Bind(&PaymentAppManager::DidFindRegistrationToGetManifest, | |
71 weak_ptr_factory_.GetWeakPtr(), callback)); | |
72 } | |
73 | |
74 void PaymentAppManager::DidFindRegistrationToSetManifest( | |
75 payments::mojom::PaymentAppManifestPtr manifest, | 110 payments::mojom::PaymentAppManifestPtr manifest, |
76 const SetManifestCallback& callback, | 111 const WriteManifestCallback& callback, |
77 ServiceWorkerStatusCode status, | 112 ServiceWorkerStatusCode status, |
78 scoped_refptr<ServiceWorkerRegistration> registration) { | 113 scoped_refptr<ServiceWorkerRegistration> registration) { |
79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
80 if (status != SERVICE_WORKER_OK) { | 115 if (status != SERVICE_WORKER_OK) { |
81 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | 116 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
82 return; | 117 return; |
83 } | 118 } |
84 | 119 |
85 PaymentAppManifestProto manifest_proto; | 120 std::string serialized; |
86 manifest_proto.set_label(manifest->label); | 121 DCHECK(SerializePaymentAppManifest(std::move(manifest), &serialized)); |
87 if (manifest->icon) | |
88 manifest_proto.set_icon(manifest->icon.value()); | |
89 | 122 |
90 for (const auto& option : manifest->options) { | 123 service_worker_context_->StoreRegistrationUserData( |
91 PaymentAppOptionProto* option_proto = manifest_proto.add_options(); | |
92 option_proto->set_label(option->label); | |
93 if (option->icon) | |
94 option_proto->set_icon(option->icon.value()); | |
95 option_proto->set_id(option->id); | |
96 for (const auto& method : option->enabled_methods) { | |
97 option_proto->add_enabled_methods(method); | |
98 } | |
99 } | |
100 | |
101 std::string serialized; | |
102 bool success = manifest_proto.SerializeToString(&serialized); | |
103 DCHECK(success); | |
104 | |
105 payment_app_context_->service_worker_context()->StoreRegistrationUserData( | |
106 registration->id(), registration->pattern().GetOrigin(), | 124 registration->id(), registration->pattern().GetOrigin(), |
107 {{kPaymentAppManifestDataKey, serialized}}, | 125 {{kPaymentAppManifestDataKey, serialized}}, |
108 base::Bind(&PaymentAppManager::DidSetManifest, | 126 base::Bind(&PaymentAppDatabase::DidWriteManifest, |
109 weak_ptr_factory_.GetWeakPtr(), callback)); | 127 weak_ptr_factory_.GetWeakPtr(), callback)); |
110 } | 128 } |
111 | 129 |
112 void PaymentAppManager::DidSetManifest(const SetManifestCallback& callback, | 130 void PaymentAppDatabase::DidWriteManifest(const WriteManifestCallback& callback, |
113 ServiceWorkerStatusCode status) { | 131 ServiceWorkerStatusCode status) { |
114 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 132 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
115 return callback.Run(status == SERVICE_WORKER_OK | 133 return callback.Run(status == SERVICE_WORKER_OK |
116 ? payments::mojom::PaymentAppManifestError::NONE | 134 ? payments::mojom::PaymentAppManifestError::NONE |
117 : payments::mojom::PaymentAppManifestError:: | 135 : payments::mojom::PaymentAppManifestError:: |
118 MANIFEST_STORAGE_OPERATION_FAILED); | 136 MANIFEST_STORAGE_OPERATION_FAILED); |
119 } | 137 } |
120 | 138 |
121 void PaymentAppManager::DidFindRegistrationToGetManifest( | 139 void PaymentAppDatabase::DidFindRegistrationToReadManifest( |
122 const GetManifestCallback& callback, | 140 const ReadManifestCallback& callback, |
123 ServiceWorkerStatusCode status, | 141 ServiceWorkerStatusCode status, |
124 scoped_refptr<ServiceWorkerRegistration> registration) { | 142 scoped_refptr<ServiceWorkerRegistration> registration) { |
125 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 143 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
126 if (status != SERVICE_WORKER_OK) { | 144 if (status != SERVICE_WORKER_OK) { |
127 callback.Run(payments::mojom::PaymentAppManifest::New(), | 145 callback.Run(payments::mojom::PaymentAppManifest::New(), |
128 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | 146 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
129 return; | 147 return; |
130 } | 148 } |
131 | 149 |
132 payment_app_context_->service_worker_context()->GetRegistrationUserData( | 150 service_worker_context_->GetRegistrationUserData( |
133 registration->id(), {kPaymentAppManifestDataKey}, | 151 registration->id(), {kPaymentAppManifestDataKey}, |
134 base::Bind(&PaymentAppManager::DidGetManifest, | 152 base::Bind(&PaymentAppDatabase::DidReadManifest, |
135 weak_ptr_factory_.GetWeakPtr(), callback)); | 153 weak_ptr_factory_.GetWeakPtr(), callback)); |
136 } | 154 } |
137 | 155 |
138 void PaymentAppManager::DidGetManifest(const GetManifestCallback& callback, | 156 void PaymentAppDatabase::DidReadManifest(const ReadManifestCallback& callback, |
139 const std::vector<std::string>& data, | 157 const std::vector<std::string>& data, |
140 ServiceWorkerStatusCode status) { | 158 ServiceWorkerStatusCode status) { |
141 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
142 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 160 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
143 callback.Run(payments::mojom::PaymentAppManifest::New(), | 161 callback.Run(payments::mojom::PaymentAppManifest::New(), |
144 payments::mojom::PaymentAppManifestError:: | 162 payments::mojom::PaymentAppManifestError:: |
145 MANIFEST_STORAGE_OPERATION_FAILED); | 163 MANIFEST_STORAGE_OPERATION_FAILED); |
146 return; | 164 return; |
147 } | 165 } |
148 | 166 |
149 PaymentAppManifestProto manifest_proto; | 167 payments::mojom::PaymentAppManifestPtr manifest = |
150 bool success = manifest_proto.ParseFromString(data[0]); | 168 DeserializePaymentAppManifest(data[0]); |
151 if (!success) { | 169 if (!manifest) { |
152 callback.Run(payments::mojom::PaymentAppManifest::New(), | 170 callback.Run(payments::mojom::PaymentAppManifest::New(), |
153 payments::mojom::PaymentAppManifestError:: | 171 payments::mojom::PaymentAppManifestError:: |
154 MANIFEST_STORAGE_OPERATION_FAILED); | 172 MANIFEST_STORAGE_OPERATION_FAILED); |
155 return; | 173 return; |
156 } | 174 } |
157 | 175 |
158 payments::mojom::PaymentAppManifestPtr manifest = | |
159 payments::mojom::PaymentAppManifest::New(); | |
160 manifest->label = manifest_proto.label(); | |
161 if (manifest_proto.has_icon()) | |
162 manifest->icon = manifest_proto.icon(); | |
163 for (const auto& option_proto : manifest_proto.options()) { | |
164 payments::mojom::PaymentAppOptionPtr option = | |
165 payments::mojom::PaymentAppOption::New(); | |
166 option->label = option_proto.label(); | |
167 if (option_proto.has_icon()) | |
168 option->icon = option_proto.icon(); | |
169 option->id = option_proto.id(); | |
170 for (const auto& method : option_proto.enabled_methods()) | |
171 option->enabled_methods.push_back(method); | |
172 manifest->options.push_back(std::move(option)); | |
173 } | |
174 | |
175 callback.Run(std::move(manifest), | 176 callback.Run(std::move(manifest), |
176 payments::mojom::PaymentAppManifestError::NONE); | 177 payments::mojom::PaymentAppManifestError::NONE); |
177 } | 178 } |
178 | 179 |
179 } // namespace content | 180 } // namespace content |
OLD | NEW |