OLD | NEW |
(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_context_impl.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "content/browser/payments/payment_app_data_conversions.h" |
| 13 #include "content/browser/payments/payment_app_manager.h" |
| 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 PaymentAppContextImpl::PaymentAppContextImpl( |
| 20 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 21 : service_worker_context_(std::move(service_worker_context)), |
| 22 weak_ptr_factory_(this) { |
| 23 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 24 } |
| 25 |
| 26 PaymentAppContextImpl::~PaymentAppContextImpl() { |
| 27 DCHECK(services_.empty()); |
| 28 } |
| 29 |
| 30 void PaymentAppContextImpl::Shutdown() { |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 32 |
| 33 BrowserThread::PostTask( |
| 34 BrowserThread::IO, FROM_HERE, |
| 35 base::Bind(&PaymentAppContextImpl::ShutdownOnIO, this)); |
| 36 } |
| 37 |
| 38 void PaymentAppContextImpl::CreateService( |
| 39 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 41 |
| 42 BrowserThread::PostTask( |
| 43 BrowserThread::IO, FROM_HERE, |
| 44 base::Bind(&PaymentAppContextImpl::CreateServiceOnIOThread, this, |
| 45 base::Passed(&request))); |
| 46 } |
| 47 |
| 48 void PaymentAppContextImpl::ServiceHadConnectionError( |
| 49 PaymentAppManager* service) { |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 51 DCHECK(base::ContainsKey(services_, service)); |
| 52 |
| 53 services_.erase(service); |
| 54 } |
| 55 |
| 56 ServiceWorkerContextWrapper* PaymentAppContextImpl::service_worker_context() |
| 57 const { |
| 58 return service_worker_context_.get(); |
| 59 } |
| 60 |
| 61 void PaymentAppContextImpl::GetAllManifests( |
| 62 const GetAllManifestsCallback& callback) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 BrowserThread::PostTask( |
| 65 BrowserThread::IO, FROM_HERE, |
| 66 base::Bind(&PaymentAppContextImpl::GetAllManifestsOnIO, this, callback)); |
| 67 } |
| 68 |
| 69 void PaymentAppContextImpl::GetAllManifestsOnIO(const GetAllManifestsCallback& c
allback) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 71 service_worker_context_->GetUserDataForAllRegistrations("PaymentAppManifestDat
a", base::Bind(&PaymentAppContextImpl::DidGetAllManifests, this, callback)); |
| 72 } |
| 73 |
| 74 void PaymentAppContextImpl::DidGetAllManifests( |
| 75 const GetAllManifestsCallback& callback, |
| 76 const std::vector<std::pair<int64_t, std::string>>& raw_data, |
| 77 ServiceWorkerStatusCode status) { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 79 |
| 80 std::vector<PaymentAppContext::PaymentAppManifestWithID> manifests; |
| 81 for (auto& payment_app_raw_data : raw_data) { |
| 82 payments::mojom::PaymentAppManifestPtr manifest = |
| 83 payments::mojom::PaymentAppManifest::New(); |
| 84 if (!DeserializePaymentAppManifest(payment_app_raw_data.second, manifest)) { |
| 85 continue; |
| 86 } |
| 87 manifests.push_back(PaymentAppContext::PaymentAppManifestWithID(payment_app_
raw_data.first, std::move(manifest))); |
| 88 } |
| 89 |
| 90 BrowserThread::PostTask( |
| 91 BrowserThread::UI, FROM_HERE, |
| 92 base::Bind(&PaymentAppContextImpl::GetAll, this, callback)); |
| 93 } |
| 94 |
| 95 void PaymentAppContextImpl::GetAll( |
| 96 const GetAllManifestsCallback& callback) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 } |
| 99 |
| 100 void PaymentAppContextImpl::CreateServiceOnIOThread( |
| 101 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 PaymentAppManager* service = new PaymentAppManager(this, std::move(request)); |
| 104 services_[service] = base::WrapUnique(service); |
| 105 } |
| 106 |
| 107 void PaymentAppContextImpl::ShutdownOnIO() { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 |
| 110 services_.clear(); |
| 111 } |
| 112 |
| 113 } // namespace content |
OLD | NEW |