Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.app.AppGlobals; |
| 20 | import android.content.pm.ApplicationInfo; |
| 21 | import android.content.pm.PackageInfo; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.Signature; |
| 24 | import android.os.Build; |
| 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
| 27 | import android.util.AndroidRuntimeException; |
| 28 | import android.util.Base64; |
| 29 | |
| 30 | import java.util.Arrays; |
| 31 | |
| 32 | /** @hide */ |
| 33 | public class WebViewProviderInfo implements Parcelable { |
| 34 | |
| 35 | /** |
| 36 | * @hide |
| 37 | */ |
| 38 | public static class WebViewPackageNotFoundException extends AndroidRuntimeException { |
| 39 | public WebViewPackageNotFoundException(String message) { super(message); } |
| 40 | public WebViewPackageNotFoundException(Exception e) { super(e); } |
| 41 | } |
| 42 | |
Gustav Sennton | c83e3fa | 2016-02-18 12:19:13 +0000 | [diff] [blame] | 43 | public WebViewProviderInfo(String packageName, String description, boolean availableByDefault, |
| 44 | boolean isFallback, String[] signatures) { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 45 | this.packageName = packageName; |
| 46 | this.description = description; |
Gustav Sennton | c83e3fa | 2016-02-18 12:19:13 +0000 | [diff] [blame] | 47 | this.availableByDefault = availableByDefault; |
| 48 | this.isFallback = isFallback; |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 49 | this.signatures = signatures; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private boolean hasValidSignature() { |
| 53 | if (Build.IS_DEBUGGABLE) |
| 54 | return true; |
| 55 | Signature[] packageSignatures; |
| 56 | try { |
| 57 | // If no signature is declared, instead check whether the package is included in the |
| 58 | // system. |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 59 | if (signatures == null || signatures.length == 0) |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 60 | return getPackageInfo().applicationInfo.isSystemApp(); |
| 61 | |
| 62 | packageSignatures = getPackageInfo().signatures; |
| 63 | } catch (WebViewPackageNotFoundException e) { |
| 64 | return false; |
| 65 | } |
| 66 | if (packageSignatures.length != 1) |
| 67 | return false; |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 68 | |
| 69 | final byte[] packageSignature = packageSignatures[0].toByteArray(); |
| 70 | // Return whether the package signature matches any of the valid signatures |
| 71 | for (String signature : signatures) { |
| 72 | final byte[] validSignature = Base64.decode(signature, Base64.DEFAULT); |
| 73 | if (Arrays.equals(packageSignature, validSignature)) |
| 74 | return true; |
| 75 | } |
| 76 | return false; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns whether this provider is valid for use as a WebView provider. |
| 81 | */ |
| 82 | public boolean isValidProvider() { |
| 83 | ApplicationInfo applicationInfo; |
| 84 | try { |
| 85 | applicationInfo = getPackageInfo().applicationInfo; |
| 86 | } catch (WebViewPackageNotFoundException e) { |
| 87 | return false; |
| 88 | } |
| 89 | if (hasValidSignature() && WebViewFactory.getWebViewLibrary(applicationInfo) != null) { |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
Gustav Sennton | 27f13de | 2016-01-05 20:22:02 +0000 | [diff] [blame] | 95 | /** |
| 96 | * Returns whether this package is enabled. |
| 97 | * This state can be changed by the user from Settings->Apps |
| 98 | */ |
| 99 | public boolean isEnabled() { |
| 100 | try { |
Gustav Sennton | 14c033c | 2016-02-11 12:51:27 +0000 | [diff] [blame] | 101 | // Explicitly fetch up-to-date package info here since the enabled-state of the package |
| 102 | // might have changed since we last fetched its package info. |
| 103 | updatePackageInfo(); |
| 104 | return getPackageInfo().applicationInfo.enabled; |
Gustav Sennton | 27f13de | 2016-01-05 20:22:02 +0000 | [diff] [blame] | 105 | } catch (WebViewPackageNotFoundException e) { |
| 106 | return false; |
Gustav Sennton | 27f13de | 2016-01-05 20:22:02 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns whether the provider is always available as long as it is valid. |
| 112 | * If this returns false, the provider will only be used if the user chose this provider. |
| 113 | */ |
| 114 | public boolean isAvailableByDefault() { |
| 115 | return availableByDefault; |
| 116 | } |
| 117 | |
Gustav Sennton | c83e3fa | 2016-02-18 12:19:13 +0000 | [diff] [blame] | 118 | public boolean isFallbackPackage() { |
| 119 | return isFallback; |
| 120 | } |
| 121 | |
Gustav Sennton | 14c033c | 2016-02-11 12:51:27 +0000 | [diff] [blame] | 122 | private void updatePackageInfo() { |
| 123 | try { |
| 124 | PackageManager pm = AppGlobals.getInitialApplication().getPackageManager(); |
| 125 | packageInfo = pm.getPackageInfo(packageName, PACKAGE_FLAGS); |
| 126 | } catch (PackageManager.NameNotFoundException e) { |
| 127 | throw new WebViewPackageNotFoundException(e); |
| 128 | } |
| 129 | } |
| 130 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 131 | public PackageInfo getPackageInfo() { |
| 132 | if (packageInfo == null) { |
Gustav Sennton | 14c033c | 2016-02-11 12:51:27 +0000 | [diff] [blame] | 133 | updatePackageInfo(); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 134 | } |
| 135 | return packageInfo; |
| 136 | } |
| 137 | |
| 138 | // aidl stuff |
| 139 | public static final Parcelable.Creator<WebViewProviderInfo> CREATOR = |
| 140 | new Parcelable.Creator<WebViewProviderInfo>() { |
| 141 | public WebViewProviderInfo createFromParcel(Parcel in) { |
| 142 | return new WebViewProviderInfo(in); |
| 143 | } |
| 144 | |
| 145 | public WebViewProviderInfo[] newArray(int size) { |
| 146 | return new WebViewProviderInfo[size]; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | private WebViewProviderInfo(Parcel in) { |
| 151 | packageName = in.readString(); |
| 152 | description = in.readString(); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 153 | availableByDefault = (in.readInt() > 0); |
| 154 | isFallback = (in.readInt() > 0); |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 155 | signatures = in.createStringArray(); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 156 | packageInfo = null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public int describeContents() { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void writeToParcel(Parcel out, int flags) { |
| 166 | out.writeString(packageName); |
| 167 | out.writeString(description); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 168 | out.writeInt(availableByDefault ? 1 : 0); |
| 169 | out.writeInt(isFallback ? 1 : 0); |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 170 | out.writeStringArray(signatures); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // fields read from framework resource |
| 174 | public String packageName; |
| 175 | public String description; |
Gustav Sennton | 27f13de | 2016-01-05 20:22:02 +0000 | [diff] [blame] | 176 | private boolean availableByDefault; |
Gustav Sennton | c83e3fa | 2016-02-18 12:19:13 +0000 | [diff] [blame] | 177 | private boolean isFallback; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 178 | |
Gustav Sennton | 5c2454c | 2015-12-17 15:49:33 +0000 | [diff] [blame] | 179 | private String[] signatures; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 180 | |
| 181 | private PackageInfo packageInfo; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 182 | |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 183 | // flags declaring we want extra info from the package manager |
| 184 | private final static int PACKAGE_FLAGS = PackageManager.GET_META_DATA |
| 185 | | PackageManager.GET_SIGNATURES | PackageManager.MATCH_DEBUG_TRIAGED_MISSING; |
| 186 | } |