File: android_app_communication.h

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 (126 lines) | stat: -rw-r--r-- 5,061 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
// 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.

#ifndef COMPONENTS_PAYMENTS_CONTENT_ANDROID_APP_COMMUNICATION_H_
#define COMPONENTS_PAYMENTS_CONTENT_ANDROID_APP_COMMUNICATION_H_

#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "base/unguessable_token.h"
#include "components/payments/core/android_app_description.h"

class GURL;

namespace content {
class BrowserContext;
class WebContents;
}  // namespace content

namespace payments {

// Invokes Android payment apps. This object is owned by BrowserContext, so it
// should only be accessed on UI thread, where BrowserContext lives.
class AndroidAppCommunication : public base::SupportsUserData::Data {
 public:
  using GetAppDescriptionsCallback = base::OnceCallback<void(
      const std::optional<std::string>& error_message,
      std::vector<std::unique_ptr<AndroidAppDescription>> app_descriptions)>;

  using IsReadyToPayCallback =
      base::OnceCallback<void(const std::optional<std::string>& error_message,
                              bool is_ready_to_pay)>;

  using InvokePaymentAppCallback =
      base::OnceCallback<void(const std::optional<std::string>& error_message,
                              bool is_activity_result_ok,
                              const std::string& payment_method_identifier,
                              const std::string& stringified_details)>;

  using AbortPaymentAppCallback = base::OnceCallback<void(bool)>;

  // Returns a weak pointer to the instance of AndroidAppCommunication that is
  // owned by the given |context|, which should not be null.
  static base::WeakPtr<AndroidAppCommunication> GetForBrowserContext(
      content::BrowserContext* context);

  ~AndroidAppCommunication() override;

  // Disallow copy and assign.
  AndroidAppCommunication(const AndroidAppCommunication& other) = delete;
  AndroidAppCommunication& operator=(const AndroidAppCommunication& other) =
      delete;

  // Looks up installed Android apps that support making payments. If running in
  // TWA mode, the |twa_package_name| parameter is the name of the Android
  // package of the TWA that invoked Chrome, or an empty string otherwise.
  virtual void GetAppDescriptions(const std::string& twa_package_name,
                                  GetAppDescriptionsCallback callback) = 0;

  // Queries the IS_READY_TO_PAY service to check whether the payment app can
  // perform payments.
  virtual void IsReadyToPay(const std::string& package_name,
                            const std::string& service_name,
                            const 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,
                            IsReadyToPayCallback callback) = 0;

  // Invokes the PAY activity to initiate the payment flow.
  virtual void InvokePaymentApp(
      const std::string& package_name,
      const std::string& activity_name,
      const 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,
      const base::UnguessableToken& request_token,
      content::WebContents* web_contents,
      const std::optional<base::UnguessableToken>& twa_instance_identifier,
      InvokePaymentAppCallback callback) = 0;

  // Aborts a payment flow which was previously started with InvokePaymentApp().
  virtual void AbortPaymentApp(const base::UnguessableToken& request_token,
                               AbortPaymentAppCallback callback) = 0;

  // Allows usage of a test browser context.
  virtual void SetForTesting() = 0;

  // Simulates having this payment app.
  virtual void SetAppForTesting(const std::string& package_name,
                                const std::string& method,
                                const std::string& response) = 0;

 protected:
  explicit AndroidAppCommunication(content::BrowserContext* context);

  content::BrowserContext* context() { return context_; }

 private:
  // Defined in platform-specific implementation files. See:
  // components/payments/content/android_app_communication_chromeos.cc
  // components/payments/content/android_app_communication_stub.cc
  static std::unique_ptr<AndroidAppCommunication> Create(
      content::BrowserContext* context);

  // Owns this object, so always valid.
  raw_ptr<content::BrowserContext> context_;

  base::WeakPtrFactory<AndroidAppCommunication> weak_ptr_factory_{this};
};

}  // namespace payments

#endif  // COMPONENTS_PAYMENTS_CONTENT_ANDROID_APP_COMMUNICATION_H_