| 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_database.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_impl.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 if (!manifest_proto.ParseFromString(input)) |
| 26 return nullptr; |
| 27 |
| 28 payments::mojom::PaymentAppManifestPtr manifest = |
| 29 payments::mojom::PaymentAppManifest::New(); |
| 30 manifest->name = manifest_proto.name(); |
| 31 if (manifest_proto.has_icon()) |
| 32 manifest->icon = manifest_proto.icon(); |
| 33 for (const auto& option_proto : manifest_proto.options()) { |
| 34 payments::mojom::PaymentAppOptionPtr option = |
| 35 payments::mojom::PaymentAppOption::New(); |
| 36 option->name = option_proto.name(); |
| 37 if (option_proto.has_icon()) |
| 38 option->icon = option_proto.icon(); |
| 39 option->id = option_proto.id(); |
| 40 for (const auto& method : option_proto.enabled_methods()) |
| 41 option->enabled_methods.push_back(method); |
| 42 manifest->options.push_back(std::move(option)); |
| 43 } |
| 44 |
| 45 return manifest; |
| 46 } |
| 47 |
| 22 } // namespace | 48 } // namespace |
| 23 | 49 |
| 24 PaymentAppDatabase::PaymentAppDatabase( | 50 PaymentAppDatabase::PaymentAppDatabase( |
| 25 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 51 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 26 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 52 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 28 } | 54 } |
| 29 | 55 |
| 30 PaymentAppDatabase::~PaymentAppDatabase() { | 56 PaymentAppDatabase::~PaymentAppDatabase() { |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 45 | 71 |
| 46 void PaymentAppDatabase::ReadManifest(const GURL& scope, | 72 void PaymentAppDatabase::ReadManifest(const GURL& scope, |
| 47 const ReadManifestCallback& callback) { | 73 const ReadManifestCallback& callback) { |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 | 75 |
| 50 service_worker_context_->FindReadyRegistrationForPattern( | 76 service_worker_context_->FindReadyRegistrationForPattern( |
| 51 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest, | 77 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest, |
| 52 weak_ptr_factory_.GetWeakPtr(), callback)); | 78 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 53 } | 79 } |
| 54 | 80 |
| 81 void PaymentAppDatabase::ReadAllManifests( |
| 82 const ReadAllManifestsCallback& callback) { |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 84 |
| 85 service_worker_context_->GetUserDataForAllRegistrations( |
| 86 kPaymentAppManifestDataKey, |
| 87 base::Bind(&PaymentAppDatabase::DidReadAllManifests, |
| 88 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 89 } |
| 90 |
| 55 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( | 91 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( |
| 56 payments::mojom::PaymentAppManifestPtr manifest, | 92 payments::mojom::PaymentAppManifestPtr manifest, |
| 57 const WriteManifestCallback& callback, | 93 const WriteManifestCallback& callback, |
| 58 ServiceWorkerStatusCode status, | 94 ServiceWorkerStatusCode status, |
| 59 scoped_refptr<ServiceWorkerRegistration> registration) { | 95 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 61 if (status != SERVICE_WORKER_OK) { | 97 if (status != SERVICE_WORKER_OK) { |
| 62 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | 98 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
| 63 return; | 99 return; |
| 64 } | 100 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 const std::vector<std::string>& data, | 156 const std::vector<std::string>& data, |
| 121 ServiceWorkerStatusCode status) { | 157 ServiceWorkerStatusCode status) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 123 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 159 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
| 124 callback.Run(payments::mojom::PaymentAppManifest::New(), | 160 callback.Run(payments::mojom::PaymentAppManifest::New(), |
| 125 payments::mojom::PaymentAppManifestError:: | 161 payments::mojom::PaymentAppManifestError:: |
| 126 MANIFEST_STORAGE_OPERATION_FAILED); | 162 MANIFEST_STORAGE_OPERATION_FAILED); |
| 127 return; | 163 return; |
| 128 } | 164 } |
| 129 | 165 |
| 130 PaymentAppManifestProto manifest_proto; | 166 payments::mojom::PaymentAppManifestPtr manifest = |
| 131 bool success = manifest_proto.ParseFromString(data[0]); | 167 DeserializePaymentAppManifest(data[0]); |
| 132 if (!success) { | 168 if (!manifest) { |
| 133 callback.Run(payments::mojom::PaymentAppManifest::New(), | 169 callback.Run(payments::mojom::PaymentAppManifest::New(), |
| 134 payments::mojom::PaymentAppManifestError:: | 170 payments::mojom::PaymentAppManifestError:: |
| 135 MANIFEST_STORAGE_OPERATION_FAILED); | 171 MANIFEST_STORAGE_OPERATION_FAILED); |
| 136 return; | 172 return; |
| 137 } | 173 } |
| 138 | 174 |
| 139 payments::mojom::PaymentAppManifestPtr manifest = | |
| 140 payments::mojom::PaymentAppManifest::New(); | |
| 141 manifest->name = manifest_proto.name(); | |
| 142 if (manifest_proto.has_icon()) | |
| 143 manifest->icon = manifest_proto.icon(); | |
| 144 for (const auto& option_proto : manifest_proto.options()) { | |
| 145 payments::mojom::PaymentAppOptionPtr option = | |
| 146 payments::mojom::PaymentAppOption::New(); | |
| 147 option->name = option_proto.name(); | |
| 148 if (option_proto.has_icon()) | |
| 149 option->icon = option_proto.icon(); | |
| 150 option->id = option_proto.id(); | |
| 151 for (const auto& method : option_proto.enabled_methods()) | |
| 152 option->enabled_methods.push_back(method); | |
| 153 manifest->options.push_back(std::move(option)); | |
| 154 } | |
| 155 | |
| 156 callback.Run(std::move(manifest), | 175 callback.Run(std::move(manifest), |
| 157 payments::mojom::PaymentAppManifestError::NONE); | 176 payments::mojom::PaymentAppManifestError::NONE); |
| 158 } | 177 } |
| 159 | 178 |
| 179 void PaymentAppDatabase::DidReadAllManifests( |
| 180 const ReadAllManifestsCallback& callback, |
| 181 const std::vector<std::pair<int64_t, std::string>>& raw_data, |
| 182 ServiceWorkerStatusCode status) { |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 if (status != SERVICE_WORKER_OK) { |
| 185 callback.Run(Manifests()); |
| 186 return; |
| 187 } |
| 188 |
| 189 Manifests manifests; |
| 190 for (const auto& item_of_raw_data : raw_data) { |
| 191 payments::mojom::PaymentAppManifestPtr manifest = |
| 192 DeserializePaymentAppManifest(item_of_raw_data.second); |
| 193 if (!manifest) |
| 194 continue; |
| 195 |
| 196 manifests.push_back( |
| 197 ManifestWithID(item_of_raw_data.first, std::move(manifest))); |
| 198 } |
| 199 |
| 200 callback.Run(std::move(manifests)); |
| 201 } |
| 202 |
| 160 } // namespace content | 203 } // namespace content |
| OLD | NEW |