File: payment_manifest_parser.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (170 lines) | stat: -rw-r--r-- 5,665 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef COMPONENTS_PAYMENTS_CONTENT_UTILITY_PAYMENT_MANIFEST_PARSER_H_
#define COMPONENTS_PAYMENTS_CONTENT_UTILITY_PAYMENT_MANIFEST_PARSER_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/payments/content/web_app_manifest.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace payments {

class ErrorLogger;

// Parser for payment method manifests and web app manifests.
//
// Example 1 of valid payment method manifest structure:
//
// {
//   "default_applications": ["payment-app.json"],
//   "supported_origins": ["https://alicepay.com"]
// }
//
// Example 2 of valid payment method manifest structure:
//
// {
//   "default_applications": ["payment-app.json"],
//   "supported_origins": "*"
// }
//
// Example valid web app manifest structure:
//
// {
//   "name": "bobpay",
//   "serviceworker": {
//     "src": "bobpay.js",
//     "scope": "/pay",
//     "use_cache": false
//   },
//  "icons": [{
//    "src": "icon/bobpay.png",
//    "sizes": "48x48",
//    "type": "image/png"
//   },{
//    "src": "icon/lowres",
//    "sizes": "48x48"
//   }],
//   "related_applications": [{
//     "platform": "play",
//     "id": "com.bobpay.app",
//     "min_version": "1",
//     "fingerprint": [{
//       "type": "sha256_cert",
//       "value": "91:5C:88:65:FF:C4:E8:20:CF:F7:3E:C8:64:D0:95:F0:06:19:2E:A6"
//     }]
//   }]
// }
//
// Specs:
// https://developers.google.com/web/fundamentals/payments/payment-apps-developer-guide/web-payment-apps
// https://developers.google.com/web/fundamentals/payments/payment-apps-developer-guide/android-payment-apps
// https://w3c.github.io/payment-method-manifest/
// https://w3c.github.io/manifest/
//
// Note the JSON parsing is done using the DataDecoder (either OOP or in a safe
// environment).
//
// The command line must be initialized to use this class in tests, because it
// checks for --unsafely-treat-insecure-origin-as-secure=<origin> flag. For
// example:
//  base::CommandLine::Init(0, nullptr);
class PaymentManifestParser {
 public:
  // Web app icon info parsed from web app manifest.
  struct WebAppIcon {
    WebAppIcon();
    ~WebAppIcon();

    std::string src;
    std::string sizes;
    std::string type;
  };

  // TODO(crbug.com/40681786): Return manifest parser errors to caller.

  // Called on successful parsing of a payment method manifest. Parse failure
  // results in empty vectors and "false".
  using PaymentMethodCallback =
      base::OnceCallback<void(const std::vector<GURL>&,
                              const std::vector<url::Origin>&)>;
  // Called on successful parsing of a web app manifest. Parse failure results
  // in an empty vector.
  using WebAppCallback =
      base::OnceCallback<void(const std::vector<WebAppManifestSection>&)>;
  // Called on successful parsing of the installation info (name, icons,
  // and serviceworker) in the web app manifest. Parse failure results in a
  // nullptr.
  using WebAppInstallationInfoCallback =
      base::OnceCallback<void(std::unique_ptr<WebAppInstallationInfo>,
                              std::unique_ptr<std::vector<WebAppIcon>>)>;

  explicit PaymentManifestParser(std::unique_ptr<ErrorLogger> log);

  PaymentManifestParser(const PaymentManifestParser&) = delete;
  PaymentManifestParser& operator=(const PaymentManifestParser&) = delete;

  ~PaymentManifestParser();

  void ParsePaymentMethodManifest(const GURL& manifest_url,
                                  const std::string& content,
                                  PaymentMethodCallback callback);
  void ParseWebAppManifest(const std::string& content, WebAppCallback callback);

  // Parses the installation info in the web app manifest |content|. Sends the
  // result back through callback.
  // Refer to:
  // https://www.w3.org/TR/appmanifest/#webappmanifest-dictionary
  void ParseWebAppInstallationInfo(const std::string& content,
                                   WebAppInstallationInfoCallback callback);

  // Visible for tests.
  static void ParsePaymentMethodManifestIntoVectors(
      const GURL& manifest_url,
      base::Value value,
      const ErrorLogger& log,
      std::vector<GURL>* web_app_manifest_urls,
      std::vector<url::Origin>* supported_origins);

  static bool ParseWebAppManifestIntoVector(
      base::Value value,
      const ErrorLogger& log,
      std::vector<WebAppManifestSection>* output);

  static bool ParseWebAppInstallationInfoIntoStructs(
      base::Value value,
      const ErrorLogger& log,
      WebAppInstallationInfo* installation_info,
      std::vector<WebAppIcon>* icons);

 private:
  void OnPaymentMethodParse(const GURL& manifest_url,
                            PaymentMethodCallback callback,
                            data_decoder::DataDecoder::ValueOrError result);
  void OnWebAppParse(WebAppCallback callback,
                     data_decoder::DataDecoder::ValueOrError result);
  void OnWebAppParseInstallationInfo(
      WebAppInstallationInfoCallback callback,
      data_decoder::DataDecoder::ValueOrError result);

  int64_t parse_payment_callback_counter_ = 0;
  int64_t parse_webapp_callback_counter_ = 0;

  std::unique_ptr<ErrorLogger> log_;
  base::WeakPtrFactory<PaymentManifestParser> weak_factory_{this};
};

}  // namespace payments

#endif  // COMPONENTS_PAYMENTS_CONTENT_UTILITY_PAYMENT_MANIFEST_PARSER_H_