File: android_payment_app.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 (206 lines) | stat: -rw-r--r-- 6,226 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/payments/content/android_payment_app.h"

#include <utility>

#include "base/check.h"
#include "components/payments/core/method_strings.h"
#include "components/payments/core/native_error_strings.h"
#include "components/payments/core/payer_data.h"
#include "content/public/browser/web_contents.h"

namespace payments {

AndroidPaymentApp::AndroidPaymentApp(
    const std::set<std::string>& payment_method_names,
    std::unique_ptr<std::map<std::string, std::set<std::string>>>
        stringified_method_data,
    const GURL& top_level_origin,
    const GURL& payment_request_origin,
    const std::string& payment_request_id,
    std::unique_ptr<AndroidAppDescription> description,
    base::WeakPtr<AndroidAppCommunication> communication,
    content::GlobalRenderFrameHostId frame_routing_id,
    const std::optional<base::UnguessableToken>& twa_instance_identifier)
    : PaymentApp(/*icon_resource_id=*/0, PaymentApp::Type::NATIVE_MOBILE_APP),
      stringified_method_data_(std::move(stringified_method_data)),
      top_level_origin_(top_level_origin),
      payment_request_origin_(payment_request_origin),
      payment_request_id_(payment_request_id),
      description_(std::move(description)),
      communication_(communication),
      frame_routing_id_(frame_routing_id),
      payment_app_token_(base::UnguessableToken::Create()),
      payment_app_open_(false),
      twa_instance_identifier_(twa_instance_identifier) {
  DCHECK(!payment_method_names.empty());
  DCHECK_EQ(payment_method_names.size(), stringified_method_data_->size());
  DCHECK_EQ(*payment_method_names.begin(),
            stringified_method_data_->begin()->first);
  DCHECK(description_);
  DCHECK(!description_->package.empty());
  DCHECK_EQ(1U, description_->activities.size());
  DCHECK(!description_->activities.front()->name.empty());

  app_method_names_ = payment_method_names;
}

AndroidPaymentApp::~AndroidPaymentApp() {
  if (payment_app_open_) {
    AbortPaymentApp(base::DoNothing());
  }
}

void AndroidPaymentApp::InvokePaymentApp(base::WeakPtr<Delegate> delegate) {
  // Browser is closing, so no need to invoke a callback.
  if (!communication_)
    return;

  content::RenderFrameHost* rfh =
      content::RenderFrameHost::FromID(frame_routing_id_);
  if (!rfh || !rfh->IsActive())
    return;
  content::WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(rfh);
  if (!web_contents)
    return;

  payment_app_open_ = true;
  communication_->InvokePaymentApp(
      description_->package, description_->activities.front()->name,
      *stringified_method_data_, top_level_origin_, payment_request_origin_,
      payment_request_id_, payment_app_token_, web_contents,
      twa_instance_identifier_,
      base::BindOnce(&AndroidPaymentApp::OnPaymentAppResponse,
                     weak_ptr_factory_.GetWeakPtr(), delegate));
}

bool AndroidPaymentApp::IsCompleteForPayment() const {
  return true;
}

bool AndroidPaymentApp::CanPreselect() const {
  return true;
}

std::u16string AndroidPaymentApp::GetMissingInfoLabel() const {
  NOTREACHED();
}

bool AndroidPaymentApp::HasEnrolledInstrument() const {
  return true;
}

bool AndroidPaymentApp::NeedsInstallation() const {
  return false;
}

std::string AndroidPaymentApp::GetId() const {
  return description_->package;
}

std::u16string AndroidPaymentApp::GetLabel() const {
  return std::u16string();
}

std::u16string AndroidPaymentApp::GetSublabel() const {
  return std::u16string();
}

const SkBitmap* AndroidPaymentApp::icon_bitmap() const {
  return nullptr;
}

bool AndroidPaymentApp::IsValidForModifier(const std::string& method) const {
  bool is_valid = false;
  IsValidForPaymentMethodIdentifier(method, &is_valid);
  return is_valid;
}

base::WeakPtr<PaymentApp> AndroidPaymentApp::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

bool AndroidPaymentApp::HandlesShippingAddress() const {
  return false;
}

bool AndroidPaymentApp::HandlesPayerName() const {
  return false;
}

bool AndroidPaymentApp::HandlesPayerEmail() const {
  return false;
}

bool AndroidPaymentApp::HandlesPayerPhone() const {
  return false;
}

bool AndroidPaymentApp::IsWaitingForPaymentDetailsUpdate() const {
  return false;
}

void AndroidPaymentApp::UpdateWith(
    mojom::PaymentRequestDetailsUpdatePtr details_update) {
  // TODO(crbug.com/40106647): Support payment method, shipping address, and
  // shipping option change events.
}

void AndroidPaymentApp::OnPaymentDetailsNotUpdated() {}

void AndroidPaymentApp::AbortPaymentApp(
    base::OnceCallback<void(bool)> abort_callback) {
  if (!communication_ || !payment_app_open_) {
    std::move(abort_callback).Run(false);
    return;
  }

  payment_app_open_ = false;

  communication_->AbortPaymentApp(payment_app_token_,
                                  std::move(abort_callback));
}

bool AndroidPaymentApp::IsPreferred() const {
  // This class used only on Chrome OS, where the only Android payment app
  // available is the trusted web application (TWA) that launched this instance
  // of Chrome with a TWA specific payment method, so this app should be
  // preferred.
#if BUILDFLAG(IS_CHROMEOS)
  DCHECK_EQ(1U, GetAppMethodNames().size());
  DCHECK_EQ(methods::kGooglePlayBilling, *GetAppMethodNames().begin());
  return true;
#else
  NOTREACHED();
#endif  // BUILDFLAG(IS_CHROMEOS)
}

void AndroidPaymentApp::OnPaymentAppResponse(
    base::WeakPtr<Delegate> delegate,
    const std::optional<std::string>& error_message,
    bool is_activity_result_ok,
    const std::string& payment_method_identifier,
    const std::string& stringified_details) {
  payment_app_open_ = false;
  if (!delegate)
    return;

  if (error_message.has_value()) {
    delegate->OnInstrumentDetailsError(error_message.value());
    return;
  }

  if (!is_activity_result_ok) {
    delegate->OnInstrumentDetailsError(errors::kUserClosedPaymentApp);
    return;
  }

  delegate->OnInstrumentDetailsReady(payment_method_identifier,
                                     stringified_details, PayerData());
}

}  // namespace payments