OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.payments; | |
6 | |
7 import android.text.TextUtils; | |
8 | |
9 import org.json.JSONArray; | |
10 import org.json.JSONException; | |
11 import org.json.JSONObject; | |
12 | |
13 import java.util.ArrayList; | |
14 import java.util.HashSet; | |
15 import java.util.List; | |
16 import java.util.Set; | |
17 | |
18 /** | |
19 * Parses the payment method manifest for information specific to native Android payment apps. | |
20 * Example of a valid manifest that has no restrictions on payment apps: | |
21 * | |
22 * { | |
23 * "android": [ | |
24 * { | |
25 * "package": "*" | |
26 * } | |
27 * ] | |
28 * } | |
29 * | |
30 * Example of a valid manifest that restricts to a single app that can handle th is payment method: | |
31 * | |
32 * { | |
33 * "android": [ | |
34 * { | |
35 * "package": "com.bobpay", | |
36 * "version": 1, | |
37 * "sha256_cert_fingerprints": [ | |
38 * "146DE983C5730650146DE983C5730650146DE983C5730650146DE983C5730650", | |
39 * "F0FD6C5B410F25CBF0FD6C5B410F25CBF0FD6C5B410F25CBF0FD6C5B410F25CB"] | |
40 * } | |
41 * ] | |
42 * } | |
43 */ | |
44 public class ManifestParser { | |
45 /** A parsed manifest section. */ | |
46 public static class Manifest { | |
47 /** | |
48 * The name of the package of the native Android payment app, e.g., "*" or "com.bobpay". | |
49 * Never null. | |
50 */ | |
51 public String packageName; | |
52 | |
53 /** | |
54 * The minimum version code of the native Android payment app, e.g., 123 . By default, this | |
55 * is 0. | |
56 */ | |
57 public long version; | |
58 | |
59 /** | |
60 * The unordered collection of SHA256 certificate fingerprints, e.g., | |
61 * ["308201dd30820146020101300d06092a864886f70d01010505003037311630140"] . Null if {@see | |
62 * packageName} is "*". Otherwise, never null and never empty. Every ele ment is a non-empty | |
63 * string. | |
64 */ | |
65 public Set<String> sha256CertFingerprints; | |
66 } | |
67 | |
68 /** Interface for the callback to invoke when parsing is complete. */ | |
69 public interface ManifestParseCallback { | |
70 /** | |
71 * Called on successful parsing of a payment method manifest. | |
72 * | |
73 * @param manifests Sections of the successfully parsed manifest. | |
74 */ | |
75 void onManifestParseSuccess(List<Manifest> manifests); | |
76 | |
77 /** Called on failed parsing of a payment method manifest. */ | |
78 void onManifestParseFailure(); | |
79 } | |
80 | |
81 /** | |
82 * Parses the manifest. | |
83 * | |
84 * @param content The data to parse. | |
85 * @param callack The callback to invoke when done. | |
86 */ | |
87 public static void parse(String content, ManifestParseCallback callback) { | |
88 JSONObject json = null; | |
89 try { | |
90 json = new JSONObject(content); | |
91 } catch (JSONException e) { | |
92 callback.onManifestParseFailure(); | |
93 return; | |
94 } | |
95 | |
96 JSONArray androids = json.optJSONArray("android"); | |
97 if (androids == null) { | |
98 callback.onManifestParseFailure(); | |
99 return; | |
100 } | |
101 | |
102 List<Manifest> result = new ArrayList<>(); | |
103 for (int i = 0; i < androids.length(); i++) { | |
104 JSONObject androidSection = androids.optJSONObject(i); | |
105 if (androidSection == null) { | |
106 callback.onManifestParseFailure(); | |
107 return; | |
108 } | |
109 | |
110 Manifest manifest = new Manifest(); | |
111 | |
112 manifest.packageName = androidSection.optString("package"); | |
113 if (TextUtils.isEmpty(manifest.packageName)) { | |
114 callback.onManifestParseFailure(); | |
115 return; | |
116 } | |
117 | |
118 // All apps allowed. | |
119 if ("*".equals(manifest.packageName)) break; | |
gogerald1
2017/01/23 17:18:35
Do you need result.add(manifest) here, otherwise r
please use gerrit instead
2017/02/23 19:57:50
Done. Great catch!
| |
120 | |
121 manifest.version = androidSection.optLong("version"); | |
122 | |
123 JSONArray fingerprints = androidSection.optJSONArray("sha256_cert_fi ngerprints"); | |
124 if (fingerprints == null || fingerprints.length() == 0) { | |
125 callback.onManifestParseFailure(); | |
126 return; | |
127 } | |
128 | |
129 manifest.sha256CertFingerprints = new HashSet<>(); | |
130 for (int j = 0; j < fingerprints.length(); j++) { | |
131 String fingerprint = fingerprints.optString(j); | |
132 if (TextUtils.isEmpty(fingerprint)) { | |
133 callback.onManifestParseFailure(); | |
134 return; | |
135 } | |
136 manifest.sha256CertFingerprints.add(fingerprint); | |
137 } | |
138 | |
139 result.add(manifest); | |
140 } | |
141 | |
142 if (result.isEmpty()) { | |
143 callback.onManifestParseFailure(); | |
144 } else { | |
145 callback.onManifestParseSuccess(result); | |
146 } | |
147 } | |
148 | |
149 private ManifestParser() {} | |
150 } | |
OLD | NEW |