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

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

Issue 1965583002: Move //webapk to //chrome/android/webapk (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
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.WEB_APK_PACKAGE_PRE FIX;
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.
23 */
24 public class WebApkValidator {
25
26 private static final String TAG = "WebApkValidator";
27 private static byte[] sExpectedSignature;
28
29 /**
30 * Queries the PackageManager to determine whether a WebAPK can handle the U RL. Ignores
31 * whether the user has selected a default handler for the URL and whether t he default
32 * handler is the WebAPK.
33 *
34 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url.
35 *
36 * @param context The application context.
37 * @param url The url to check.
38 * @return Package name of WebAPK which can handle the URL. Null if the url should not be
39 * handled by a WebAPK.
40 */
41 public static String queryWebAPKPackage(Context context, String url) {
42 Intent intent;
43 try {
44 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
45 } catch (Exception e) {
46 return null;
47 }
48
49 intent.addCategory(Intent.CATEGORY_BROWSABLE);
50 intent.setComponent(null);
51 Intent selector = intent.getSelector();
52 if (selector != null) {
53 selector.addCategory(Intent.CATEGORY_BROWSABLE);
54 selector.setComponent(null);
55 }
56
57 List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntent Activities(
58 intent, PackageManager.GET_RESOLVED_FILTER);
59 return findWebAPKPackage(resolveInfos);
60 }
61
62 /**
63 * @param The ResolveInfos to search.
64 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the
65 * ResolveInfos corresponds to a WebAPK.
66 */
67 public static String findWebAPKPackage(List<ResolveInfo> infos) {
68 for (ResolveInfo info : infos) {
69 if (info.activityInfo != null
70 && info.activityInfo.packageName.startsWith(WEB_APK_PACKAGE_ PREFIX)) {
71 return info.activityInfo.packageName;
72 }
73 }
74 return null;
75 }
76
77 /**
78 * Returns whether the provided WebAPK is installed and passes signature che cks.
79 * @param context A context
80 * @param webappPackageName The package name to check
81 * @return true iff the WebAPK is installed and passes security checks
82 */
83 public static boolean isValidWebApk(Context context, String webappPackageNam e) {
84 if (sExpectedSignature == null) {
85 Log.wtf(TAG, "WebApk validation failure - expected signature not set ."
86 + "missing call to WebApkValidator.initWithBrowserHostSignat ure");
87 }
88 if (webappPackageName != null && webappPackageName.startsWith(WEB_APK_PA CKAGE_PREFIX)) {
89 // check signature
90 PackageInfo packageInfo = null;
91 try {
92 packageInfo = context.getPackageManager().getPackageInfo(webappP ackageName,
93 PackageManager.GET_SIGNATURES);
94 } catch (NameNotFoundException e) {
95 e.printStackTrace();
96 Log.d(TAG, "WebApk not found");
97 return false;
98 }
99
100 final Signature[] arrSignatures = packageInfo.signatures;
101 if (arrSignatures != null) {
102 for (Signature signature : arrSignatures) {
103 if (Arrays.equals(sExpectedSignature, signature.toByteArray( ))) {
104 Log.d(TAG, "WebApk valid - signature match!");
105 return true;
106 }
107 }
108 }
109 }
110 Log.d(TAG, "WebApk invalid");
111 return false;
112 }
113
114 /**
115 * Initializes the WebApkValidator with the expected signature that WebAPKs must be signed
116 * with for the current host.
117 * @param expectedSignature
118 */
119 public static void initWithBrowserHostSignature(byte[] expectedSignature) {
120 sExpectedSignature = expectedSignature;
121 }
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698