File: payment_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (238 lines) | stat: -rw-r--r-- 8,104 bytes parent folder | download | duplicates (5)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/payments/payment_manager.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "content/browser/payments/payment_app.pb.h"
#include "content/browser/payments/payment_app_context_impl.h"
#include "content/browser/payments/payment_app_database.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_worker_context.h"

namespace content {
namespace {

constexpr char kInvalidPaymentManagerStateMessage[] =
    "Scope URL not set, Init may not have been called.";

enum class ReasonCode : uint32_t {
  kInvalidContextUrl,
  kNonUnicodeScopeString,
  kInvalidScopeUrl,
  kCrossOriginContextAndScope,
  kCrossOriginDataAccess,
  kRenderProcessCannotAccessOrigin,
  kInvalidState,
};

}  // namespace

PaymentManager::PaymentManager(
    PaymentAppContextImpl* payment_app_context,
    const url::Origin& origin,
    mojo::PendingReceiver<payments::mojom::PaymentManager> receiver)
    : payment_app_context_(payment_app_context),
      origin_(origin),
      receiver_(this, std::move(receiver)) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(payment_app_context);

  receiver_.set_disconnect_handler(base::BindOnce(
      &PaymentManager::OnConnectionError, weak_ptr_factory_.GetWeakPtr()));
}

PaymentManager::~PaymentManager() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void PaymentManager::Init(const GURL& context_url, const std::string& scope) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!context_url.is_valid()) {
    receiver_.ResetWithReason(
        static_cast<uint32_t>(ReasonCode::kInvalidContextUrl),
        "Invalid context URL.");
    return;
  }
  // GURL constructors CHECKs when the input string is not unicode.
  if (!base::IsStringUTF8(scope)) {
    receiver_.ResetWithReason(
        static_cast<uint32_t>(ReasonCode::kNonUnicodeScopeString),
        "Scope string is not UTF8.");
    return;
  }
  GURL scope_url(scope);
  if (!scope_url.is_valid()) {
    receiver_.ResetWithReason(
        static_cast<uint32_t>(ReasonCode::kInvalidScopeUrl),
        "Invalid scope URL.");
    return;
  }
  if (!url::IsSameOriginWith(context_url, scope_url)) {
    receiver_.ResetWithReason(
        static_cast<uint32_t>(ReasonCode::kCrossOriginContextAndScope),
        "Scope URL is not from the same origin of the context URL.");
    return;
  }
  if (!origin_.IsSameOriginWith(context_url)) {
    receiver_.ResetWithReason(
        static_cast<uint32_t>(ReasonCode::kCrossOriginDataAccess),
        "Cross origin data access.");
    return;
  }

  should_set_payment_app_info_ = true;
  context_url_ = context_url;
  scope_ = scope_url;
}

void PaymentManager::DeletePaymentInstrument(
    const std::string& instrument_key,
    PaymentManager::DeletePaymentInstrumentCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->DeletePaymentInstrument(
      scope_, instrument_key, std::move(callback));
}

void PaymentManager::GetPaymentInstrument(
    const std::string& instrument_key,
    PaymentManager::GetPaymentInstrumentCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->ReadPaymentInstrument(
      scope_, instrument_key, std::move(callback));
}

void PaymentManager::KeysOfPaymentInstruments(
    PaymentManager::KeysOfPaymentInstrumentsCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->KeysOfPaymentInstruments(
      scope_, std::move(callback));
}

void PaymentManager::HasPaymentInstrument(
    const std::string& instrument_key,
    PaymentManager::HasPaymentInstrumentCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->HasPaymentInstrument(
      scope_, instrument_key, std::move(callback));
}

void PaymentManager::SetPaymentInstrument(
    const std::string& instrument_key,
    payments::mojom::PaymentInstrumentPtr details,
    PaymentManager::SetPaymentInstrumentCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  if (should_set_payment_app_info_) {
    payment_app_context_->payment_app_database()->WritePaymentInstrument(
        scope_, instrument_key, std::move(details),
        base::BindOnce(
            &PaymentManager::SetPaymentInstrumentIntermediateCallback,
            weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  } else {
    payment_app_context_->payment_app_database()->WritePaymentInstrument(
        scope_, instrument_key, std::move(details), std::move(callback));
  }
}

void PaymentManager::SetPaymentInstrumentIntermediateCallback(
    PaymentManager::SetPaymentInstrumentCallback callback,
    payments::mojom::PaymentHandlerStatus status) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  if (status != payments::mojom::PaymentHandlerStatus::SUCCESS ||
      !should_set_payment_app_info_) {
    std::move(callback).Run(status);
    return;
  }

  payment_app_context_->payment_app_database()->FetchAndUpdatePaymentAppInfo(
      context_url_, scope_, std::move(callback));
  should_set_payment_app_info_ = false;
}

void PaymentManager::ClearPaymentInstruments(
    ClearPaymentInstrumentsCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->ClearPaymentInstruments(
      scope_, std::move(callback));
}

void PaymentManager::SetUserHint(const std::string& user_hint) {
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->SetPaymentAppUserHint(
      scope_, user_hint);
}

void PaymentManager::EnableDelegations(
    const std::vector<payments::mojom::PaymentDelegation>& delegations,
    PaymentManager::EnableDelegationsCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (scope_.is_empty()) {
    receiver_.ResetWithReason(static_cast<uint32_t>(ReasonCode::kInvalidState),
                              kInvalidPaymentManagerStateMessage);
    return;
  }

  payment_app_context_->payment_app_database()->EnablePaymentAppDelegations(
      scope_, delegations, std::move(callback));
}

void PaymentManager::OnConnectionError() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  payment_app_context_->PaymentManagerHadConnectionError(this);
}

}  // namespace content