Index: chrome/android/java/src/org/chromium/chrome/browser/payments/ManifestDownloader.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/ManifestDownloader.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/ManifestDownloader.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6d80a9739ada758cb40b49d38685cd0fd8a317c4 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/ManifestDownloader.java |
@@ -0,0 +1,148 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.payments; |
+ |
+import android.os.AsyncTask; |
+ |
+import java.io.IOException; |
+import java.net.HttpURLConnection; |
+import java.net.URL; |
+import java.util.List; |
+import java.util.Map; |
+import java.util.Scanner; |
+import java.util.regex.Matcher; |
+import java.util.regex.Pattern; |
+ |
+/** |
+ * Downloads the payment method manifest based on the payment method name that is a URL with HTTPS |
+ * scheme, e.g., https://bobpay.com. The download happens via two consecutive HTTP requests: |
+ * |
+ * 1) HEAD request for the payment method name. The HTTP response header is parsed for Link header |
+ * that points to the location of the payment method manifest file. Example of a relative |
+ * location: |
+ * |
+ * Link: <data/payment-manifest.json>; rel="payment-method-manifest" |
+ * |
+ * (This is relative to the payment method URL.) Example of an absolute location: |
+ * |
+ * Link: <https://bobpay.com/data/payment-manifest.json>; rel="payment-method-manifest" |
+ * |
+ * The absolute location must use HTTPS scheme. |
+ * |
+ * 2) GET request for the payment method manifest file. |
+ */ |
+public class ManifestDownloader { |
+ /** Interface for the callback to invoke when finished downloading. */ |
+ public interface ManifestDownloadCallback { |
+ /** |
+ * Called on successful download of a payment method manifest. |
+ * |
+ * @param contents The successfully downloaded payment method manifest. |
+ */ |
+ void onManifestDownloadSuccess(String contents); |
+ |
+ /** Called on failed download of a payment method manifest. */ |
+ void onManifestDownloadFailure(); |
+ } |
+ |
+ /** The prefix of a payment method name for which a manifest can be downloaded. */ |
+ public static final String MANIFEST_SCHEME = "https://"; |
+ |
+ /** HTTP HEAD method name. */ |
+ private static final String HTTP_METHOD_HEAD = "HEAD"; |
+ |
+ /** HTTP Link header name. */ |
+ private static final String HTTP_HEADER_LINK = "Link"; |
+ |
+ /** The regular expression that captures the URL of the payment method manifest. */ |
+ private static final String PATTERN_LINK = "^<(.*)>; rel=\"payment-method-manifest\"$"; |
+ |
+ /** The regular expression capture group for the URL of the payment method manifest. */ |
+ private static final int GROUP_URL = 1; |
+ |
+ /** The token for requesting the full text of the download stream. */ |
+ private static final String SCANNER_FULL_TEXT_TOKEN = "\\A"; |
+ |
+ /** |
+ * Downloads the manifest file asynchronously. |
+ * |
+ * @param methodName The payment method name that is a URL with HTTPS scheme. Must be a URL and |
+ * must start with "https://". |
+ * @param callback The callback to invoke when finished downloading. |
+ */ |
+ public static void download(final String methodName, final ManifestDownloadCallback callback) { |
+ assert methodName != null; |
+ assert callback != null; |
+ assert methodName.startsWith(MANIFEST_SCHEME); |
+ |
+ (new AsyncTask<Void, Void, String>() { |
+ @Override |
+ protected String doInBackground(Void... ignored) { |
+ HttpURLConnection httpConnection = null; |
+ try { |
+ httpConnection = (HttpURLConnection) new URL(methodName).openConnection(); |
+ if (httpConnection == null) return null; |
+ |
+ httpConnection.setRequestMethod(HTTP_METHOD_HEAD); |
+ int responseCode = httpConnection.getResponseCode(); |
+ if (responseCode != 200) { |
+ httpConnection.disconnect(); |
+ return null; |
+ } |
+ |
+ Map<String, List<String>> headers = httpConnection.getHeaderFields(); |
+ Pattern pattern = Pattern.compile(PATTERN_LINK); |
+ String manifestUrl = null; |
+ for (Map.Entry<String, List<String>> header : headers.entrySet()) { |
+ if (HTTP_HEADER_LINK.equals(header.getKey())) { |
+ List<String> values = header.getValue(); |
+ for (int i = 0; i < values.size(); i++) { |
+ Matcher matcher = pattern.matcher(values.get(i)); |
+ if (matcher.matches()) manifestUrl = matcher.group(GROUP_URL); |
+ } |
+ } |
+ } |
+ |
+ if (manifestUrl == null) { |
+ httpConnection.disconnect(); |
+ return null; |
+ } |
+ |
+ if (!manifestUrl.startsWith(MANIFEST_SCHEME)) { |
+ manifestUrl = methodName + "/" + manifestUrl; |
+ } |
+ |
+ httpConnection.disconnect(); |
+ httpConnection = (HttpURLConnection) new URL(manifestUrl).openConnection(); |
+ responseCode = httpConnection.getResponseCode(); |
+ if (responseCode != 200) { |
+ httpConnection.disconnect(); |
+ return null; |
+ } |
+ |
+ Scanner scanner = new Scanner(httpConnection.getInputStream()) |
+ .useDelimiter(SCANNER_FULL_TEXT_TOKEN); |
+ String result = scanner.hasNext() ? scanner.next() : null; |
+ httpConnection.disconnect(); |
+ return result; |
+ } catch (SecurityException | IOException e) { |
+ if (httpConnection != null) httpConnection.disconnect(); |
+ return null; |
+ } |
+ } |
+ |
+ @Override |
+ protected void onPostExecute(String data) { |
+ if (data != null) { |
+ callback.onManifestDownloadSuccess(data); |
+ } else { |
+ callback.onManifestDownloadFailure(); |
+ } |
+ } |
+ }).execute(); |
+ } |
+ |
+ private ManifestDownloader() {} |
+} |