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