Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java

Issue 1971773002: Upstream: Add WebAPK's client library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add DEPS. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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.webapk.lib.client;
6
7 import static org.chromium.webapk.lib.common.WebApkConstants.WEBAPK_PACKAGE_PREF IX;
8
9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.pm.PackageInfo;
12 import android.content.pm.PackageManager;
13 import android.content.pm.PackageManager.NameNotFoundException;
14 import android.content.pm.ResolveInfo;
15 import android.content.pm.Signature;
16 import android.util.Log;
17
18 import java.util.Arrays;
19 import java.util.List;
20
21 /**
22 * Checks whether a URL belongs to a WebAPK, and whether a WebAPK is signed by t he WebAPK Minting
23 * Server.
24 */
25 public class WebApkValidator {
26
27 private static final String TAG = "WebApkValidator";
28 private static byte[] sExpectedSignature;
29
30 /**
31 * Queries the PackageManager to determine whether a WebAPK can handle the U RL. Ignores
32 * whether the user has selected a default handler for the URL and whether t he default
33 * handler is the WebAPK.
34 *
35 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url.
36 *
37 * @param context The application context.
38 * @param url The url to check.
39 * @return Package name of WebAPK which can handle the URL. Null if the url should not be
40 * handled by a WebAPK.
41 */
42 public static String queryWebApkPackage(Context context, String url) {
43 Intent intent;
44 try {
45 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
46 } catch (Exception e) {
47 return null;
48 }
49
50 intent.addCategory(Intent.CATEGORY_BROWSABLE);
51 intent.setComponent(null);
52 Intent selector = intent.getSelector();
53 if (selector != null) {
54 selector.addCategory(Intent.CATEGORY_BROWSABLE);
55 selector.setComponent(null);
56 }
57
58 List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntent Activities(
59 intent, PackageManager.GET_RESOLVED_FILTER);
60 return findWebApkPackage(resolveInfos);
61 }
62
63 /**
64 * @param The ResolveInfos to search.
65 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the
66 * ResolveInfos corresponds to a WebAPK.
67 */
68 public static String findWebApkPackage(List<ResolveInfo> infos) {
69 for (ResolveInfo info : infos) {
70 if (info.activityInfo != null
71 && info.activityInfo.packageName.startsWith(WEBAPK_PACKAGE_P REFIX)) {
72 return info.activityInfo.packageName;
73 }
74 }
75 return null;
76 }
77
78 /**
79 * Returns whether the provided WebAPK is installed and passes signature che cks.
80 * @param context A context
81 * @param webappPackageName The package name to check
82 * @return true iff the WebAPK is installed and passes security checks
83 */
84 public static boolean isValidWebApk(Context context, String webappPackageNam e) {
85 if (sExpectedSignature == null) {
86 Log.wtf(TAG, "WebApk validation failure - expected signature not set ."
87 + "missing call to WebApkValidator.initWithBrowserHostSignat ure");
88 }
89 if (webappPackageName != null && webappPackageName.startsWith(WEBAPK_PAC KAGE_PREFIX)) {
90 // check signature
91 PackageInfo packageInfo = null;
92 try {
93 packageInfo = context.getPackageManager().getPackageInfo(webappP ackageName,
94 PackageManager.GET_SIGNATURES);
95 } catch (NameNotFoundException e) {
96 e.printStackTrace();
97 Log.d(TAG, "WebApk not found");
98 return false;
99 }
100
101 final Signature[] arrSignatures = packageInfo.signatures;
102 if (arrSignatures != null) {
103 for (Signature signature : arrSignatures) {
104 if (Arrays.equals(sExpectedSignature, signature.toByteArray( ))) {
105 Log.d(TAG, "WebApk valid - signature match!");
106 return true;
107 }
108 }
109 }
110 }
111 Log.d(TAG, "WebApk invalid");
112 return false;
113 }
114
115 /**
116 * Initializes the WebApkValidator with the expected signature that WebAPKs must be signed
117 * with for the current host.
118 * @param expectedSignature
119 */
120 public static void initWithBrowserHostSignature(byte[] expectedSignature) {
121 sExpectedSignature = Arrays.copyOf(expectedSignature, expectedSignature. length);
122 }
123 }
OLDNEW
« no previous file with comments | « chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698