Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: content/browser/payments/payment_app_database.cc

Issue 2565423002: Revert of PaymentApp: Introduce PaymentAppDatabase class. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/payments/payment_app_database.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/optional.h"
11 #include "content/browser/payments/payment_app.pb.h"
12 #include "content/browser/payments/payment_app_context_impl.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/public/browser/browser_thread.h"
16
17 namespace content {
18 namespace {
19
20 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
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
74 } // namespace
75
76 PaymentAppDatabase::PaymentAppDatabase(
77 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
78 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) {
79 DCHECK_CURRENTLY_ON(BrowserThread::IO);
80 }
81
82 PaymentAppDatabase::~PaymentAppDatabase() {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO);
84 }
85
86 void PaymentAppDatabase::WriteManifest(
87 const GURL& scope,
88 payments::mojom::PaymentAppManifestPtr manifest,
89 const WriteManifestCallback& callback) {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO);
91
92 service_worker_context_->FindReadyRegistrationForPattern(
93 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToWriteManifest,
94 weak_ptr_factory_.GetWeakPtr(),
95 base::Passed(std::move(manifest)), callback));
96 }
97
98 void PaymentAppDatabase::ReadManifest(const GURL& scope,
99 const ReadManifestCallback& callback) {
100 DCHECK_CURRENTLY_ON(BrowserThread::IO);
101
102 service_worker_context_->FindReadyRegistrationForPattern(
103 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest,
104 weak_ptr_factory_.GetWeakPtr(), callback));
105 }
106
107 void PaymentAppDatabase::DidFindRegistrationToWriteManifest(
108 payments::mojom::PaymentAppManifestPtr manifest,
109 const WriteManifestCallback& callback,
110 ServiceWorkerStatusCode status,
111 scoped_refptr<ServiceWorkerRegistration> registration) {
112 DCHECK_CURRENTLY_ON(BrowserThread::IO);
113 if (status != SERVICE_WORKER_OK) {
114 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
115 return;
116 }
117
118 std::string serialized;
119 DCHECK(SerializePaymentAppManifest(std::move(manifest), &serialized));
120
121 service_worker_context_->StoreRegistrationUserData(
122 registration->id(), registration->pattern().GetOrigin(),
123 {{kPaymentAppManifestDataKey, serialized}},
124 base::Bind(&PaymentAppDatabase::DidWriteManifest,
125 weak_ptr_factory_.GetWeakPtr(), callback));
126 }
127
128 void PaymentAppDatabase::DidWriteManifest(const WriteManifestCallback& callback,
129 ServiceWorkerStatusCode status) {
130 DCHECK_CURRENTLY_ON(BrowserThread::IO);
131 return callback.Run(status == SERVICE_WORKER_OK
132 ? payments::mojom::PaymentAppManifestError::NONE
133 : payments::mojom::PaymentAppManifestError::
134 MANIFEST_STORAGE_OPERATION_FAILED);
135 }
136
137 void PaymentAppDatabase::DidFindRegistrationToReadManifest(
138 const ReadManifestCallback& callback,
139 ServiceWorkerStatusCode status,
140 scoped_refptr<ServiceWorkerRegistration> registration) {
141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
142 if (status != SERVICE_WORKER_OK) {
143 callback.Run(payments::mojom::PaymentAppManifest::New(),
144 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
145 return;
146 }
147
148 service_worker_context_->GetRegistrationUserData(
149 registration->id(), {kPaymentAppManifestDataKey},
150 base::Bind(&PaymentAppDatabase::DidReadManifest,
151 weak_ptr_factory_.GetWeakPtr(), callback));
152 }
153
154 void PaymentAppDatabase::DidReadManifest(const ReadManifestCallback& callback,
155 const std::vector<std::string>& data,
156 ServiceWorkerStatusCode status) {
157 DCHECK_CURRENTLY_ON(BrowserThread::IO);
158 if (status != SERVICE_WORKER_OK || data.size() != 1) {
159 callback.Run(payments::mojom::PaymentAppManifest::New(),
160 payments::mojom::PaymentAppManifestError::
161 MANIFEST_STORAGE_OPERATION_FAILED);
162 return;
163 }
164
165 payments::mojom::PaymentAppManifestPtr manifest =
166 DeserializePaymentAppManifest(data[0]);
167 if (!manifest) {
168 callback.Run(payments::mojom::PaymentAppManifest::New(),
169 payments::mojom::PaymentAppManifestError::
170 MANIFEST_STORAGE_OPERATION_FAILED);
171 return;
172 }
173
174 callback.Run(std::move(manifest),
175 payments::mojom::PaymentAppManifestError::NONE);
176 }
177
178 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/payments/payment_app_database.h ('k') | content/browser/payments/payment_app_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698