Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webapk.lib.client; | 5 package org.chromium.webapk.lib.client; |
| 6 | 6 |
| 7 import static org.chromium.webapk.lib.common.WebApkConstants.WEBAPK_PACKAGE_PREF IX; | 7 import static org.chromium.webapk.lib.common.WebApkConstants.WEBAPK_PACKAGE_PREF IX; |
| 8 | 8 |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.Intent; | 10 import android.content.Intent; |
| 11 import android.content.pm.PackageInfo; | 11 import android.content.pm.PackageInfo; |
| 12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
| 13 import android.content.pm.PackageManager.NameNotFoundException; | 13 import android.content.pm.PackageManager.NameNotFoundException; |
| 14 import android.content.pm.ResolveInfo; | 14 import android.content.pm.ResolveInfo; |
| 15 import android.content.pm.Signature; | 15 import android.content.pm.Signature; |
| 16 import android.util.Log; | 16 import android.util.Log; |
| 17 | 17 |
| 18 import java.util.Arrays; | 18 import java.util.Arrays; |
| 19 import java.util.LinkedList; | |
| 19 import java.util.List; | 20 import java.util.List; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Checks whether a URL belongs to a WebAPK, and whether a WebAPK is signed by t he WebAPK Minting | 23 * Checks whether a URL belongs to a WebAPK, and whether a WebAPK is signed by t he WebAPK Minting |
| 23 * Server. | 24 * Server. |
| 24 */ | 25 */ |
| 25 public class WebApkValidator { | 26 public class WebApkValidator { |
| 26 | 27 |
| 27 private static final String TAG = "WebApkValidator"; | 28 private static final String TAG = "WebApkValidator"; |
| 28 private static byte[] sExpectedSignature; | 29 private static byte[] sExpectedSignature; |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * Queries the PackageManager to determine whether a WebAPK can handle the U RL. Ignores | 32 * 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 * whether the user has selected a default handler for the URL and whether t he default |
| 33 * handler is the WebAPK. | 34 * handler is the WebAPK. |
| 34 * | 35 * |
| 35 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url. | 36 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url. |
| 36 * | 37 * |
| 37 * @param context The application context. | 38 * @param context The application context. |
| 38 * @param url The url to check. | 39 * @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 * @return Package name of WebAPK which can handle the URL. Null if the url should not be |
| 40 * handled by a WebAPK. | 41 * handled by a WebAPK. |
| 41 */ | 42 */ |
| 42 public static String queryWebApkPackage(Context context, String url) { | 43 public static String queryWebApkPackage(Context context, String url) { |
|
pkotwicz
2017/02/22 16:50:20
Can you make this function use queryResolveInfo()?
gonzalon
2017/02/22 17:17:40
I don't understand why. Isn't that what findWebApk
pkotwicz
2017/02/22 20:26:09
I wanted to merge resolveInfosForUrl() and queryRe
| |
| 44 return findWebApkPackage(context, resolveInfosForUrl(context, url)); | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Queries the PackageManager to determine whether a WebAPK can handle the U RL. Ignores | |
| 49 * whether the user has selected a default handler for the URL and whether t he default | |
| 50 * handler is the WebAPK. | |
| 51 * | |
| 52 * NOTE: This can fail if multiple WebAPKs can match the supplied url. | |
| 53 * | |
| 54 * @param context The application context. | |
| 55 * @param url The url to check. | |
| 56 * @return Resolve Info of a WebAPK which can handle the URL. Null if the ur l should not be | |
|
pkotwicz
2017/02/22 20:26:09
Nit: "Resolve Info of a WebAPK" -> "ResolveInfo of
| |
| 57 * handled by a WebAPK. | |
| 58 */ | |
| 59 public static ResolveInfo queryWebApk(Context context, String url) { | |
|
pkotwicz
2017/02/22 16:50:20
Can you rename this function to queryResolveInfo()
gonzalon
2017/02/22 17:17:40
Done.
| |
| 60 for (ResolveInfo info : resolveInfosForUrl(context, url)) { | |
| 61 if (info.activityInfo != null | |
| 62 && isValidWebApk(context, info.activityInfo.packageName)) { | |
| 63 return info; | |
| 64 } | |
| 65 } | |
| 66 return null; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Fetches the list of resolve infos from the PackageManager that can handle the URL. | |
| 71 * | |
| 72 * @param context The application context. | |
| 73 * @param url The url to check. | |
| 74 * @return The list of resolve infos found that can handle the URL. | |
| 75 */ | |
| 76 private static List<ResolveInfo> resolveInfosForUrl(Context context, String url) { | |
| 43 Intent intent; | 77 Intent intent; |
| 44 try { | 78 try { |
| 45 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | 79 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 46 } catch (Exception e) { | 80 } catch (Exception e) { |
| 47 return null; | 81 return new LinkedList<>(); |
| 48 } | 82 } |
| 49 | 83 |
| 50 intent.addCategory(Intent.CATEGORY_BROWSABLE); | 84 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 51 intent.setComponent(null); | 85 intent.setComponent(null); |
| 52 Intent selector = intent.getSelector(); | 86 Intent selector = intent.getSelector(); |
| 53 if (selector != null) { | 87 if (selector != null) { |
| 54 selector.addCategory(Intent.CATEGORY_BROWSABLE); | 88 selector.addCategory(Intent.CATEGORY_BROWSABLE); |
| 55 selector.setComponent(null); | 89 selector.setComponent(null); |
| 56 } | 90 } |
| 57 | 91 return context.getPackageManager().queryIntentActivities( |
| 58 List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntent Activities( | |
| 59 intent, PackageManager.GET_RESOLVED_FILTER); | 92 intent, PackageManager.GET_RESOLVED_FILTER); |
| 60 return findWebApkPackage(context, resolveInfos); | |
| 61 } | 93 } |
| 62 | 94 |
| 63 /** | 95 /** |
| 64 * @param context The context to use to check whether WebAPK is valid. | 96 * @param context The context to use to check whether WebAPK is valid. |
| 65 * @param infos The ResolveInfos to search. | 97 * @param infos The ResolveInfos to search. |
| 66 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the | 98 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the |
| 67 * ResolveInfos corresponds to a WebAPK. | 99 * ResolveInfos corresponds to a WebAPK. |
| 68 */ | 100 */ |
| 69 public static String findWebApkPackage(Context context, List<ResolveInfo> in fos) { | 101 public static String findWebApkPackage(Context context, List<ResolveInfo> in fos) { |
|
pkotwicz
2017/02/22 16:50:20
Can you create a new function findResolveInfo() an
gonzalon
2017/02/22 17:17:40
Done.
| |
| 70 for (ResolveInfo info : infos) { | 102 for (ResolveInfo info : infos) { |
| 71 if (info.activityInfo != null | 103 if (info.activityInfo != null |
| 72 && isValidWebApk(context, info.activityInfo.packageName)) { | 104 && isValidWebApk(context, info.activityInfo.packageName)) { |
| 73 return info.activityInfo.packageName; | 105 return info.activityInfo.packageName; |
| 74 } | 106 } |
| 75 } | 107 } |
| 76 return null; | 108 return null; |
| 77 } | 109 } |
| 78 | 110 |
| 79 /** | 111 /** |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 * with for the current host. | 151 * with for the current host. |
| 120 * @param expectedSignature | 152 * @param expectedSignature |
| 121 */ | 153 */ |
| 122 public static void initWithBrowserHostSignature(byte[] expectedSignature) { | 154 public static void initWithBrowserHostSignature(byte[] expectedSignature) { |
| 123 if (sExpectedSignature != null) { | 155 if (sExpectedSignature != null) { |
| 124 return; | 156 return; |
| 125 } | 157 } |
| 126 sExpectedSignature = Arrays.copyOf(expectedSignature, expectedSignature. length); | 158 sExpectedSignature = Arrays.copyOf(expectedSignature, expectedSignature. length); |
| 127 } | 159 } |
| 128 } | 160 } |
| OLD | NEW |