File: chrome_payment_request_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (324 lines) | stat: -rw-r--r-- 12,465 bytes parent folder | download | duplicates (6)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/payments/chrome_payment_request_delegate.h"

#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/browser_instance/browser_app_instance.h"
#include "chrome/browser/apps/browser_instance/browser_app_instance_tracker.h"
#include "chrome/browser/autofill/address_normalizer_factory.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/autofill/validation_rules_storage_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/payments/payment_request_display_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog_view.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_quality/addresses/address_normalizer_impl.h"
#include "components/autofill/core/browser/geo/region_data_loader_impl.h"
#include "components/autofill/core/browser/ui/region_combobox_model.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/payments/content/payment_manifest_web_data_service.h"
#include "components/payments/content/payment_request.h"
#include "components/payments/content/payment_request_dialog.h"
#include "components/payments/content/ssl_validity_checker.h"
#include "components/payments/core/payment_prefs.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/webauthn/content/browser/internal_authenticator_impl.h"
#include "components/webdata_services/web_data_service_wrapper_factory.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom-shared.h"
#include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
#include "third_party/libaddressinput/chromium/chrome_storage_impl.h"

namespace payments {

namespace {

std::unique_ptr<::i18n::addressinput::Source> GetAddressInputSource() {
  return std::unique_ptr<::i18n::addressinput::Source>(
      new autofill::ChromeMetadataSource(
          I18N_ADDRESS_VALIDATION_DATA_URL,
          g_browser_process->system_network_context_manager()
              ->GetSharedURLLoaderFactory()));
}

std::unique_ptr<::i18n::addressinput::Storage> GetAddressInputStorage() {
  return autofill::ValidationRulesStorageFactory::CreateStorage();
}

bool FrameSupportsPayments(content::RenderFrameHost* rfh) {
  return rfh && rfh->IsActive() && rfh->IsRenderFrameLive() &&
         rfh->IsFeatureEnabled(
             network::mojom::PermissionsPolicyFeature::kPayment);
}

}  // namespace

ChromePaymentRequestDelegate::ChromePaymentRequestDelegate(
    content::RenderFrameHost* render_frame_host)
    : shown_dialog_(nullptr),
      frame_routing_id_(render_frame_host->GetGlobalId()),
      twa_package_helper_(FrameSupportsPayments(render_frame_host)
                              ? render_frame_host
                              : nullptr) {}

ChromePaymentRequestDelegate::~ChromePaymentRequestDelegate() = default;

void ChromePaymentRequestDelegate::ShowDialog(
    base::WeakPtr<PaymentRequest> request) {
  DCHECK_EQ(nullptr, shown_dialog_.get());
  DCHECK_EQ(nullptr, spc_dialog_.get());

  switch (dialog_type_) {
    case DialogType::PAYMENT_REQUEST:
      shown_dialog_ = PaymentRequestDialogView::Create(request, nullptr);
      break;
    case DialogType::SECURE_PAYMENT_CONFIRMATION:
      spc_dialog_ =
          std::make_unique<SecurePaymentConfirmationController>(request);
      shown_dialog_ = spc_dialog_->GetWeakPtr();
      break;
  }

  shown_dialog_->ShowDialog();
}

void ChromePaymentRequestDelegate::RetryDialog() {
  if (shown_dialog_)
    shown_dialog_->RetryDialog();
}

void ChromePaymentRequestDelegate::CloseDialog() {
  if (shown_dialog_) {
    shown_dialog_->CloseDialog();
    shown_dialog_ = nullptr;
  }

  // The shown_dialog_ may have been an SPC dialog, in which case we own the
  // object directly and need to clean it up here.
  spc_dialog_.reset();

  // The 'no-credentials' dialog for SPC is currently handled separately from
  // spc_dialog_ (and shown_dialog_), and so needs to separately be closed and
  // cleaned up.
  if (spc_no_creds_dialog_) {
    spc_no_creds_dialog_->CloseDialog();
    spc_no_creds_dialog_.reset();
  }
}

void ChromePaymentRequestDelegate::ShowErrorMessage() {
  if (shown_dialog_)
    shown_dialog_->ShowErrorMessage();
}

void ChromePaymentRequestDelegate::ShowProcessingSpinner() {
  if (shown_dialog_)
    shown_dialog_->ShowProcessingSpinner();
}

autofill::PersonalDataManager*
ChromePaymentRequestDelegate::GetPersonalDataManager() {
  return autofill::PersonalDataManagerFactory::GetForBrowserContext(
      GetBrowserContextOrNull());
}

const std::string& ChromePaymentRequestDelegate::GetApplicationLocale() const {
  return g_browser_process->GetApplicationLocale();
}

bool ChromePaymentRequestDelegate::IsOffTheRecord() const {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  if (!rfh)
    return false;
  Profile* profile = Profile::FromBrowserContext(rfh->GetBrowserContext());
  return profile && profile->IsOffTheRecord();
}

const GURL& ChromePaymentRequestDelegate::GetLastCommittedURL() const {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  return FrameSupportsPayments(rfh) ? rfh->GetMainFrame()->GetLastCommittedURL()
                                    : GURL::EmptyGURL();
}

autofill::AddressNormalizer*
ChromePaymentRequestDelegate::GetAddressNormalizer() {
  return autofill::AddressNormalizerFactory::GetInstance();
}

autofill::RegionDataLoader*
ChromePaymentRequestDelegate::GetRegionDataLoader() {
  return new autofill::RegionDataLoaderImpl(GetAddressInputSource().release(),
                                            GetAddressInputStorage().release(),
                                            GetApplicationLocale());
}

ukm::UkmRecorder* ChromePaymentRequestDelegate::GetUkmRecorder() {
  return ukm::UkmRecorder::Get();
}

std::string ChromePaymentRequestDelegate::GetAuthenticatedEmail() const {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  if (!rfh)
    return std::string();

  // Check if the profile is authenticated.  Guest profiles or incognito
  // windows may not have a sign in manager, and are considered not
  // authenticated.
  Profile* profile = Profile::FromBrowserContext(rfh->GetBrowserContext());
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);
  if (identity_manager &&
      identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync)) {
    return identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSync)
        .email;
  }

  return std::string();
}

PrefService* ChromePaymentRequestDelegate::GetPrefService() {
  return Profile::FromBrowserContext(GetBrowserContextOrNull())->GetPrefs();
}

bool ChromePaymentRequestDelegate::IsBrowserWindowActive() const {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  if (!FrameSupportsPayments(rfh))
    return false;

  Browser* browser = chrome::FindBrowserWithTab(
      content::WebContents::FromRenderFrameHost(rfh));
  return browser && browser->window() && browser->window()->IsActive();
}

void ChromePaymentRequestDelegate::ShowNoMatchingPaymentCredentialDialog(
    const std::u16string& merchant_name,
    const std::string& rp_id,
    base::OnceClosure response_callback,
    base::OnceClosure opt_out_callback) {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  if (!FrameSupportsPayments(rfh))
    return;
  content::WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(rfh);
  if (!web_contents)
    return;
  spc_no_creds_dialog_ = SecurePaymentConfirmationNoCreds::Create();
  spc_no_creds_dialog_->ShowDialog(web_contents, merchant_name, rp_id,
                                   std::move(response_callback),
                                   std::move(opt_out_callback));
}

content::RenderFrameHost* ChromePaymentRequestDelegate::GetRenderFrameHost()
    const {
  return content::RenderFrameHost::FromID(frame_routing_id_);
}

std::unique_ptr<webauthn::InternalAuthenticator>
ChromePaymentRequestDelegate::CreateInternalAuthenticator() const {
  // This authenticator can be used in a cross-origin iframe only if the
  // top-level frame allowed it with Permissions Policy, e.g., with
  // allow="payment" iframe attribute. The secure payment confirmation dialog
  // displays the top-level origin in its UI before the user can click on the
  // [Verify] button to invoke this authenticator.
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  // Lifetime of the created authenticator is externally managed by the
  // authenticator factory, but is generally tied to the RenderFrame by
  // listening for `RenderFrameDeleted()`. `FrameSupportsPayments()` already
  // performs this check on our behalf, so the DCHECK() here is just for
  // documentation purposes: this ensures that `RenderFrameDeleted()` will be
  // called at some point.
  if (!FrameSupportsPayments(rfh))
    return nullptr;
  DCHECK(rfh->IsRenderFrameLive());
  return std::make_unique<content::InternalAuthenticatorImpl>(rfh);
}

scoped_refptr<PaymentManifestWebDataService>
ChromePaymentRequestDelegate::GetPaymentManifestWebDataService() const {
  return webdata_services::WebDataServiceWrapperFactory::
      GetPaymentManifestWebDataServiceForBrowserContext(
          GetBrowserContextOrNull(), ServiceAccessType::EXPLICIT_ACCESS);
}

PaymentRequestDisplayManager*
ChromePaymentRequestDelegate::GetDisplayManager() {
  return PaymentRequestDisplayManagerFactory::GetForBrowserContext(
      GetBrowserContextOrNull());
}

void ChromePaymentRequestDelegate::EmbedPaymentHandlerWindow(
    const GURL& url,
    PaymentHandlerOpenWindowCallback callback) {
  if (shown_dialog_) {
    shown_dialog_->ShowPaymentHandlerScreen(url, std::move(callback));
  } else {
    std::move(callback).Run(/*success=*/false,
                            /*render_process_id=*/0,
                            /*render_frame_id=*/0);
  }
}

bool ChromePaymentRequestDelegate::IsInteractive() const {
  return shown_dialog_ && shown_dialog_->IsInteractive();
}

std::string
ChromePaymentRequestDelegate::GetInvalidSslCertificateErrorMessage() {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  return FrameSupportsPayments(rfh)
             ? SslValidityChecker::GetInvalidSslCertificateErrorMessage(
                   content::WebContents::FromRenderFrameHost(rfh))
             : "";
}

void ChromePaymentRequestDelegate::GetTwaPackageName(
    GetTwaPackageNameCallback callback) const {
  twa_package_helper_.GetTwaPackageName(std::move(callback));
}

PaymentRequestDialog* ChromePaymentRequestDelegate::GetDialogForTesting() {
  return shown_dialog_.get();
}

SecurePaymentConfirmationNoCreds*
ChromePaymentRequestDelegate::GetNoMatchingCredentialsDialogForTesting() {
  return spc_no_creds_dialog_.get();
}

std::optional<base::UnguessableToken>
ChromePaymentRequestDelegate::GetChromeOSTWAInstanceId() const {
  return std::nullopt;
}

const base::WeakPtr<PaymentUIObserver>
ChromePaymentRequestDelegate::GetPaymentUIObserver() const {
  return nullptr;
}

content::BrowserContext* ChromePaymentRequestDelegate::GetBrowserContextOrNull()
    const {
  auto* rfh = content::RenderFrameHost::FromID(frame_routing_id_);
  return rfh ? rfh->GetBrowserContext() : nullptr;
}

}  // namespace payments