summaryrefslogtreecommitdiff
path: root/chromium/content/browser/payments/payment_app_context_impl.h
blob: e6f3b2c375e30d3bb09a59f91afb40b8f9e71c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_
#define CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_

#include <map>
#include <memory>

#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/atomic_flag.h"
#include "content/browser/payments/payment_app_database.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/WebKit/public/platform/modules/payments/payment_app.mojom.h"

namespace content {

class PaymentAppDatabase;
class PaymentManager;
class ServiceWorkerContextWrapper;

// One instance of this exists per StoragePartition, and services multiple child
// processes/origins. Most logic is delegated to the owned PaymentAppDatabase
// instance, which is only accessed on the IO thread.
//
// This class is created/destructed by StoragePartitionImpl on UI thread.
// However, the PaymentAppDatabase that this class has internally should work on
// IO thread. So, this class has Init() and Shutdown() methods in addition to
// constructor and destructor. They should be called explicitly when creating
// and destroying StoragePartitionImpl.
//
// Expected order of lifetime calls:
//   1) Constructor
//   2) Init()
//   3) Can now call other public methods in this class in any order.
//     - Can call CreatePaymentManager() on UI thread.
//     - Can call GetAllPaymentApps() on UI thread.
//     - Can call PaymentManagerHadConnectionError() on IO thread.
//     - Can call payment_app_database() on IO thread.
//   4) Shutdown()
//   5) Destructor
class CONTENT_EXPORT PaymentAppContextImpl
    : public base::RefCountedThreadSafe<
          PaymentAppContextImpl,
          content::BrowserThread::DeleteOnUIThread> {
 public:
  PaymentAppContextImpl();

  // Init and Shutdown are for use on the UI thread when the
  // StoragePartition is being setup and torn down.
  void Init(scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);

  // Shutdown must be called before deleting this. Call on the UI thread.
  void Shutdown();

  // Create a PaymentManager that is owned by this. Call on the UI
  // thread.
  void CreatePaymentManager(payments::mojom::PaymentManagerRequest request);

  // Called by PaymentManager objects so that they can
  // be deleted. Call on the IO thread.
  void PaymentManagerHadConnectionError(PaymentManager* service);

  // Should be accessed only on the IO thread.
  PaymentAppDatabase* payment_app_database() const;

 private:
  friend class PaymentAppContentUnitTestBase;
  friend struct content::BrowserThread::DeleteOnThread<
      content::BrowserThread::UI>;
  friend class base::DeleteHelper<PaymentAppContextImpl>;
  ~PaymentAppContextImpl();

  void CreatePaymentAppDatabaseOnIO(
      scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);

  void CreatePaymentManagerOnIO(
      mojo::InterfaceRequest<payments::mojom::PaymentManager> request);

  void ShutdownOnIO();

  // Only accessed on the IO thread.
  std::unique_ptr<PaymentAppDatabase> payment_app_database_;

  // The PaymentManagers are owned by this. They're either deleted during
  // ShutdownOnIO or when the channel is closed via
  // PaymentManagerHadConnectionError. Only accessed on the IO thread.
  std::map<PaymentManager*, std::unique_ptr<PaymentManager>> payment_managers_;

#if DCHECK_IS_ON()
  // Set after ShutdownOnIO() has run on the IO thread. |this| shouldn't be
  // deleted before this is set.
  base::AtomicFlag did_shutdown_on_io_;
#endif

  DISALLOW_COPY_AND_ASSIGN(PaymentAppContextImpl);
};

}  // namespace content

#endif  // CONTENT_BROWSER_PAYMENTS_PAYMENT_APP_CONTEXT_IMPL_H_