| 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 #ifndef CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ |
| 6 #define CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ | 6 #define CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "components/payments/payment_app.mojom.h" | 13 #include "components/payments/payment_app.mojom.h" |
| 14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 15 #include "content/public/browser/payment_app_context.h" | 15 #include "content/public/browser/payment_app_context.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 class PaymentAppDatabase; |
| 19 class PaymentAppManager; | 20 class PaymentAppManager; |
| 20 class ServiceWorkerContextWrapper; | 21 class ServiceWorkerContextWrapper; |
| 21 | 22 |
| 23 // One instance of this exists per StoragePartition, and services multiple child |
| 24 // processes/origins. Most logic is delegated to the owned PaymentAppDatabase |
| 25 // instance, which is only accessed on the IO thread. |
| 26 // |
| 27 // This class is created/destructed by StoragePartitionImpl on UI thread. |
| 28 // However, the PaymentAppDatabase that this class has internally should work on |
| 29 // IO thread. So, this class has Init() and Shutdown() methods in addition to |
| 30 // constructor and destructor. They should be called explicitly when creating |
| 31 // and destroying StoragePartitionImpl. |
| 32 // |
| 33 // Expected order of lifetime calls: |
| 34 // 1) Constructor |
| 35 // 2) Init() |
| 36 // 3) Can now call other public methods in this class in any order. |
| 37 // - Can call CreatePaymentAppManager() on UI thread. |
| 38 // - Can call GetAllManifests() on UI thread. |
| 39 // - Can call PaymentAppManagerHadConnectionError() on IO thread. |
| 40 // - Can call payment_app_database() on IO thread. |
| 41 // 4) Shutdown() |
| 42 // 5) Destructor |
| 22 class CONTENT_EXPORT PaymentAppContextImpl | 43 class CONTENT_EXPORT PaymentAppContextImpl |
| 23 : public base::RefCountedThreadSafe<PaymentAppContextImpl>, | 44 : public base::RefCountedThreadSafe<PaymentAppContextImpl>, |
| 24 NON_EXPORTED_BASE(public PaymentAppContext) { | 45 NON_EXPORTED_BASE(public PaymentAppContext) { |
| 25 public: | 46 public: |
| 26 explicit PaymentAppContextImpl( | 47 PaymentAppContextImpl(); |
| 27 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); | 48 |
| 49 // Init and Shutdown are for use on the UI thread when the |
| 50 // StoragePartition is being setup and torn down. |
| 51 void Init(scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); |
| 28 | 52 |
| 29 // Shutdown must be called before deleting this. Call on the UI thread. | 53 // Shutdown must be called before deleting this. Call on the UI thread. |
| 30 void Shutdown(); | 54 void Shutdown(); |
| 31 | 55 |
| 32 // Create a PaymentAppManager that is owned by this. Call on the UI | 56 // Create a PaymentAppManager that is owned by this. Call on the UI |
| 33 // thread. | 57 // thread. |
| 34 void CreateService( | 58 void CreatePaymentAppManager( |
| 35 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); | 59 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); |
| 36 | 60 |
| 37 // Called by PaymentAppManager objects so that they can | 61 // Called by PaymentAppManager objects so that they can |
| 38 // be deleted. Call on the IO thread. | 62 // be deleted. Call on the IO thread. |
| 39 void ServiceHadConnectionError(PaymentAppManager* service); | 63 void PaymentAppManagerHadConnectionError(PaymentAppManager* service); |
| 40 | 64 |
| 41 ServiceWorkerContextWrapper* service_worker_context() const; | 65 // Should be accessed only on the IO thread. |
| 66 PaymentAppDatabase* payment_app_database() const; |
| 42 | 67 |
| 43 // PaymentAppContext implementation: | 68 // PaymentAppContext implementation: |
| 44 void GetAllManifests(const GetAllManifestsCallback& callback) override; | 69 void GetAllManifests(const GetAllManifestsCallback& callback) override; |
| 45 | 70 |
| 46 protected: | 71 protected: |
| 47 friend class PaymentAppManagerTest; | 72 friend class PaymentAppManagerTest; |
| 48 | 73 |
| 49 private: | 74 private: |
| 50 friend class base::RefCountedThreadSafe<PaymentAppContextImpl>; | 75 friend class base::RefCountedThreadSafe<PaymentAppContextImpl>; |
| 51 ~PaymentAppContextImpl() override; | 76 ~PaymentAppContextImpl() override; |
| 52 | 77 |
| 53 void CreateServiceOnIOThread( | 78 void CreatePaymentAppDatabaseOnIO( |
| 79 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); |
| 80 |
| 81 void CreatePaymentAppManagerOnIO( |
| 54 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); | 82 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request); |
| 55 | 83 |
| 56 void ShutdownOnIO(); | 84 void ShutdownOnIO(); |
| 85 void DidShutdown(); |
| 57 | 86 |
| 58 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 87 // Only accessed on the IO thread. |
| 88 std::unique_ptr<PaymentAppDatabase> payment_app_database_; |
| 59 | 89 |
| 60 // The services are owned by this. They're either deleted | 90 // The PaymentAppManagers are owned by this. They're either deleted during |
| 61 // during ShutdownOnIO or when the channel is closed via | 91 // ShutdownOnIO or when the channel is closed via |
| 62 // ServiceHadConnectionError. Only accessed on the IO thread. | 92 // PaymentAppManagerHadConnectionError. Only accessed on the IO thread. |
| 63 std::map<PaymentAppManager*, std::unique_ptr<PaymentAppManager>> services_; | 93 std::map<PaymentAppManager*, std::unique_ptr<PaymentAppManager>> |
| 94 payment_app_managers_; |
| 95 |
| 96 // Only accessed on the UI thread. |
| 97 bool is_shutdown_; |
| 64 | 98 |
| 65 DISALLOW_COPY_AND_ASSIGN(PaymentAppContextImpl); | 99 DISALLOW_COPY_AND_ASSIGN(PaymentAppContextImpl); |
| 66 }; | 100 }; |
| 67 | 101 |
| 68 } // namespace content | 102 } // namespace content |
| 69 | 103 |
| 70 #endif // CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ | 104 #endif // CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_ |
| OLD | NEW |