Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0404625c authored by Torne (Richard Coles)'s avatar Torne (Richard Coles)
Browse files

webview: refactor compatibility check.

Refactor the logic used to determine if a WebView implementation APK is
compatible with the current OS version:

- Move the constants defining the minimum target SDK version and class
  name to use to the WebViewFactoryProvider interface, as that's the
  "root" of what the implementation actually implements. This keeps the
  requirements separate from the code responsible for actually loading
  the implementation in order to simplify things for future Mainline
  work.

- Change some of the naming to be more generically about compatibility
  where it makes sense, instead of referring specifically to
  targetSdkVersion, as we don't intend to use targetSdkVersion as the
  gating criteria for future incompatible changes.

- Instead of using the "real" compatibility check in
  WebViewUpdateServiceTest, just have the test system interface accept
  any package as valid by default, and mock the check in the specific
  test where it matters.

Bug: 373617389
Flag: EXEMPT refactor
Test: atest WebViewUpdateServiceTest; atest CtsWebkitTestCases
Change-Id: I4b6dca9ef4fbd29a8f59802ceed489fb296e235e
parent 3b0bf852
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.UserHandle;
import android.os.UserManager;

@@ -36,8 +35,6 @@ public class UserPackage {
    private final UserHandle mUser;
    private final PackageInfo mPackageInfo;

    public static final int MINIMUM_SUPPORTED_SDK = Build.VERSION_CODES.TIRAMISU;

    public UserPackage(@NonNull UserHandle user, @Nullable PackageInfo packageInfo) {
        mUser = user;
        mPackageInfo = packageInfo;
@@ -83,14 +80,6 @@ public class UserPackage {
                        & ApplicationInfo.PRIVATE_FLAG_HIDDEN) == 0));
    }

    /**
     * Returns whether the package represented by {@param packageInfo} targets a sdk version
     * supported by the current framework version.
     */
    public static boolean hasCorrectTargetSdkVersion(PackageInfo packageInfo) {
        return packageInfo.applicationInfo.targetSdkVersion >= MINIMUM_SUPPORTED_SDK;
    }

    public UserHandle getUser() {
        return mUser;
    }
+2 −8
Original line number Diff line number Diff line
@@ -51,12 +51,6 @@ import java.lang.reflect.Method;
 */
@SystemApi
public final class WebViewFactory {

    // visible for WebViewZygoteInit to look up the class by reflection and call preloadInZygote.
    /** @hide */
    private static final String CHROMIUM_WEBVIEW_FACTORY =
            "com.android.webview.chromium.WebViewChromiumFactoryProviderForT";

    private static final String CHROMIUM_WEBVIEW_FACTORY_METHOD = "create";

    private static final String LOGTAG = "WebViewFactory";
@@ -275,8 +269,8 @@ public final class WebViewFactory {
     */
    public static Class<WebViewFactoryProvider> getWebViewProviderClass(ClassLoader clazzLoader)
            throws ClassNotFoundException {
        return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY,
                true, clazzLoader);
        return (Class<WebViewFactoryProvider>) Class.forName(
                WebViewFactoryProvider.getWebViewFactoryClassName(), true, clazzLoader);
    }

    /**
+33 −0
Original line number Diff line number Diff line
@@ -20,8 +20,11 @@ import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Network;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import java.util.List;

@@ -33,6 +36,36 @@ import java.util.List;
 */
@SystemApi
public interface WebViewFactoryProvider {
    /** @hide */
    int MINIMUM_SUPPORTED_TARGET_SDK = Build.VERSION_CODES.TIRAMISU;

    /**
     * Returns whether the WebView implementation represented by {@code packageInfo}
     * is compatible with this version of Android.
     * @hide
     */
    static boolean isCompatibleImplementationPackage(@NonNull PackageInfo packageInfo) {
        return packageInfo.applicationInfo.targetSdkVersion >= MINIMUM_SUPPORTED_TARGET_SDK;
    }

    /**
     * Returns a string describing the minimum requirement for a WebView implementation
     * to be compatible with this version of Android, for debugging purposes.
     * @hide
     */
    static @NonNull String describeCompatibleImplementationPackage() {
        return TextUtils.formatSimple("Minimum targetSdkVersion: %d", MINIMUM_SUPPORTED_TARGET_SDK);
    }

    /**
     * Returns the name of the class that should be used when loading the
     * WebView implementation on this version of Android.
     * @hide
     */
    static @NonNull String getWebViewFactoryClassName() {
        return "com.android.webview.chromium.WebViewChromiumFactoryProviderForT";
    }

    /**
     * This Interface provides glue for implementing the backend of WebView static methods which
     * cannot be implemented in-situ in the proxy class.
+6 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.util.Log;
import android.util.Slog;
import android.webkit.UserPackage;
import android.webkit.WebViewFactory;
import android.webkit.WebViewFactoryProvider;
import android.webkit.WebViewProviderInfo;
import android.webkit.WebViewZygote;

@@ -245,6 +246,11 @@ public class SystemImpl implements SystemInterface {
        return pm.getPackageInfo(configInfo.packageName, PACKAGE_FLAGS);
    }

    @Override
    public boolean isCompatibleImplementationPackage(PackageInfo packageInfo) {
        return WebViewFactoryProvider.isCompatibleImplementationPackage(packageInfo);
    }

    @Override
    public List<UserPackage> getPackageInfoForProviderAllUsers(WebViewProviderInfo configInfo) {
        return UserPackage.getPackageInfosAllUsers(mContext, configInfo.packageName, PACKAGE_FLAGS);
+3 −0
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ public interface SystemInterface {
    boolean systemIsDebuggable();
    PackageInfo getPackageInfoForProvider(WebViewProviderInfo configInfo)
            throws NameNotFoundException;
    /** Check if the given package is a compatible WebView implementation for the OS. */
    boolean isCompatibleImplementationPackage(PackageInfo packageInfo);

    /**
     * Get the PackageInfos of all users for the package represented by {@param configInfo}.
     * @return an array of UserPackages for a certain package, each UserPackage being belonging to a
Loading