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

Commit 5fb012e4 authored by Todd Kennedy's avatar Todd Kennedy Committed by android-build-merger
Browse files

Merge "Filter instant app content providers" into oc-dev am: 38916742

am: 0bb486bc

Change-Id: If96297cade9909d744eb57669aecd96841a27a31
parents ecb4a9b3 0bb486bc
Loading
Loading
Loading
Loading
+133 −14
Original line number Original line Diff line number Diff line
@@ -7100,17 +7100,23 @@ public class PackageManagerService extends IPackageManager.Stub
                // used when either 1) the service is in an instant application and the
                // used when either 1) the service is in an instant application and the
                // caller is not the same instant application or 2) the calling package is
                // caller is not the same instant application or 2) the calling package is
                // ephemeral and the activity is not visible to ephemeral applications.
                // ephemeral and the activity is not visible to ephemeral applications.
                final boolean matchInstantApp =
                        (flags & PackageManager.MATCH_INSTANT) != 0;
                final boolean matchVisibleToInstantAppOnly =
                final boolean matchVisibleToInstantAppOnly =
                        (flags & PackageManager.MATCH_VISIBLE_TO_INSTANT_APP_ONLY) != 0;
                        (flags & PackageManager.MATCH_VISIBLE_TO_INSTANT_APP_ONLY) != 0;
                final boolean isCallerInstantApp =
                final boolean isCallerInstantApp =
                        instantAppPkgName != null;
                        instantAppPkgName != null;
                final boolean isTargetSameInstantApp =
                final boolean isTargetSameInstantApp =
                        comp.getPackageName().equals(instantAppPkgName);
                        comp.getPackageName().equals(instantAppPkgName);
                final boolean isTargetInstantApp =
                        (si.applicationInfo.privateFlags
                                & ApplicationInfo.PRIVATE_FLAG_INSTANT) != 0;
                final boolean isTargetHiddenFromInstantApp =
                final boolean isTargetHiddenFromInstantApp =
                        (si.flags & ServiceInfo.FLAG_VISIBLE_TO_EPHEMERAL) == 0;
                        (si.flags & ServiceInfo.FLAG_VISIBLE_TO_EPHEMERAL) == 0;
                final boolean blockResolution =
                final boolean blockResolution =
                        !isTargetSameInstantApp
                        !isTargetSameInstantApp
                        && ((matchVisibleToInstantAppOnly && isCallerInstantApp
                        && ((!matchInstantApp && !isCallerInstantApp && isTargetInstantApp)
                                || (matchVisibleToInstantAppOnly && isCallerInstantApp
                                        && isTargetHiddenFromInstantApp));
                                        && isTargetHiddenFromInstantApp));
                if (!blockResolution) {
                if (!blockResolution) {
                    final ResolveInfo ri = new ResolveInfo();
                    final ResolveInfo ri = new ResolveInfo();
@@ -7197,6 +7203,7 @@ public class PackageManagerService extends IPackageManager.Stub
            Intent intent, String resolvedType, int flags, int userId) {
            Intent intent, String resolvedType, int flags, int userId) {
        if (!sUserManager.exists(userId)) return Collections.emptyList();
        if (!sUserManager.exists(userId)) return Collections.emptyList();
        final int callingUid = Binder.getCallingUid();
        final int callingUid = Binder.getCallingUid();
        final String instantAppPkgName = getInstantAppPackageName(callingUid);
        flags = updateFlagsForResolve(flags, userId, intent, callingUid,
        flags = updateFlagsForResolve(flags, userId, intent, callingUid,
                false /*includeInstantApps*/);
                false /*includeInstantApps*/);
        ComponentName comp = intent.getComponent();
        ComponentName comp = intent.getComponent();
@@ -7210,10 +7217,34 @@ public class PackageManagerService extends IPackageManager.Stub
            final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1);
            final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1);
            final ProviderInfo pi = getProviderInfo(comp, flags, userId);
            final ProviderInfo pi = getProviderInfo(comp, flags, userId);
            if (pi != null) {
            if (pi != null) {
                // When specifying an explicit component, we prevent the provider from being
                // used when either 1) the provider is in an instant application and the
                // caller is not the same instant application or 2) the calling package is an
                // instant application and the provider is not visible to instant applications.
                final boolean matchInstantApp =
                        (flags & PackageManager.MATCH_INSTANT) != 0;
                final boolean matchVisibleToInstantAppOnly =
                        (flags & PackageManager.MATCH_VISIBLE_TO_INSTANT_APP_ONLY) != 0;
                final boolean isCallerInstantApp =
                        instantAppPkgName != null;
                final boolean isTargetSameInstantApp =
                        comp.getPackageName().equals(instantAppPkgName);
                final boolean isTargetInstantApp =
                        (pi.applicationInfo.privateFlags
                                & ApplicationInfo.PRIVATE_FLAG_INSTANT) != 0;
                final boolean isTargetHiddenFromInstantApp =
                        (pi.flags & ProviderInfo.FLAG_VISIBLE_TO_EPHEMERAL) == 0;
                final boolean blockResolution =
                        !isTargetSameInstantApp
                        && ((!matchInstantApp && !isCallerInstantApp && isTargetInstantApp)
                                || (matchVisibleToInstantAppOnly && isCallerInstantApp
                                        && isTargetHiddenFromInstantApp));
                if (!blockResolution) {
                    final ResolveInfo ri = new ResolveInfo();
                    final ResolveInfo ri = new ResolveInfo();
                    ri.providerInfo = pi;
                    ri.providerInfo = pi;
                    list.add(ri);
                    list.add(ri);
                }
                }
            }
            return list;
            return list;
        }
        }
@@ -7221,17 +7252,67 @@ public class PackageManagerService extends IPackageManager.Stub
        synchronized (mPackages) {
        synchronized (mPackages) {
            String pkgName = intent.getPackage();
            String pkgName = intent.getPackage();
            if (pkgName == null) {
            if (pkgName == null) {
                return mProviders.queryIntent(intent, resolvedType, flags, userId);
                return applyPostContentProviderResolutionFilter(
                        mProviders.queryIntent(intent, resolvedType, flags, userId),
                        instantAppPkgName);
            }
            }
            final PackageParser.Package pkg = mPackages.get(pkgName);
            final PackageParser.Package pkg = mPackages.get(pkgName);
            if (pkg != null) {
            if (pkg != null) {
                return mProviders.queryIntentForPackage(
                return applyPostContentProviderResolutionFilter(
                        intent, resolvedType, flags, pkg.providers, userId);
                        mProviders.queryIntentForPackage(
                        intent, resolvedType, flags, pkg.providers, userId),
                        instantAppPkgName);
            }
            }
            return Collections.emptyList();
            return Collections.emptyList();
        }
        }
    }
    }
    private List<ResolveInfo> applyPostContentProviderResolutionFilter(
            List<ResolveInfo> resolveInfos, String instantAppPkgName) {
        // TODO: When adding on-demand split support for non-instant applications, remove
        // this check and always apply post filtering
        if (instantAppPkgName == null) {
            return resolveInfos;
        }
        for (int i = resolveInfos.size() - 1; i >= 0; i--) {
            final ResolveInfo info = resolveInfos.get(i);
            final boolean isEphemeralApp = info.providerInfo.applicationInfo.isInstantApp();
            // allow providers that are defined in the provided package
            if (isEphemeralApp && instantAppPkgName.equals(info.providerInfo.packageName)) {
                if (info.providerInfo.splitName != null
                        && !ArrayUtils.contains(info.providerInfo.applicationInfo.splitNames,
                                info.providerInfo.splitName)) {
                    // requested provider is defined in a split that hasn't been installed yet.
                    // add the installer to the resolve list
                    if (DEBUG_EPHEMERAL) {
                        Slog.v(TAG, "Adding ephemeral installer to the ResolveInfo list");
                    }
                    final ResolveInfo installerInfo = new ResolveInfo(mInstantAppInstallerInfo);
                    installerInfo.auxiliaryInfo = new AuxiliaryResolveInfo(
                            info.providerInfo.packageName, info.providerInfo.splitName,
                            info.providerInfo.applicationInfo.versionCode);
                    // make sure this resolver is the default
                    installerInfo.isDefault = true;
                    installerInfo.match = IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART
                            | IntentFilter.MATCH_ADJUSTMENT_NORMAL;
                    // add a non-generic filter
                    installerInfo.filter = new IntentFilter();
                    // load resources from the correct package
                    installerInfo.resolvePackageName = info.getComponentInfo().packageName;
                    resolveInfos.set(i, installerInfo);
                }
                continue;
            }
            // allow providers that have been explicitly exposed to instant applications
            if (!isEphemeralApp
                    && ((info.providerInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_EPHEMERAL) != 0)) {
                continue;
            }
            resolveInfos.remove(i);
        }
        return resolveInfos;
    }
    @Override
    @Override
    public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, int userId) {
    public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, int userId) {
        if (!sUserManager.exists(userId)) return ParceledListSlice.emptyList();
        if (!sUserManager.exists(userId)) return ParceledListSlice.emptyList();
@@ -7563,17 +7644,38 @@ public class PackageManagerService extends IPackageManager.Stub
    public ProviderInfo resolveContentProvider(String name, int flags, int userId) {
    public ProviderInfo resolveContentProvider(String name, int flags, int userId) {
        if (!sUserManager.exists(userId)) return null;
        if (!sUserManager.exists(userId)) return null;
        flags = updateFlagsForComponent(flags, userId, name);
        flags = updateFlagsForComponent(flags, userId, name);
        final String instantAppPkgName = getInstantAppPackageName(Binder.getCallingUid());
        // reader
        // reader
        synchronized (mPackages) {
        synchronized (mPackages) {
            final PackageParser.Provider provider = mProvidersByAuthority.get(name);
            final PackageParser.Provider provider = mProvidersByAuthority.get(name);
            PackageSetting ps = provider != null
            PackageSetting ps = provider != null
                    ? mSettings.mPackages.get(provider.owner.packageName)
                    ? mSettings.mPackages.get(provider.owner.packageName)
                    : null;
                    : null;
            return ps != null
            if (ps != null) {
                    && mSettings.isEnabledAndMatchLPr(provider.info, flags, userId)
                final boolean isInstantApp = ps.getInstantApp(userId);
                    ? PackageParser.generateProviderInfo(provider, flags,
                // normal application; filter out instant application provider
                            ps.readUserState(userId), userId)
                if (instantAppPkgName == null && isInstantApp) {
                    : null;
                    return null;
                }
                // instant application; filter out other instant applications
                if (instantAppPkgName != null
                        && isInstantApp
                        && !provider.owner.packageName.equals(instantAppPkgName)) {
                    return null;
                }
                // instant application; filter out non-exposed provider
                if (instantAppPkgName != null
                        && (provider.info.flags & ProviderInfo.FLAG_VISIBLE_TO_EPHEMERAL) == 0) {
                    return null;
                }
                // provider not enabled
                if (!mSettings.isEnabledAndMatchLPr(provider.info, flags, userId)) {
                    return null;
                }
                return PackageParser.generateProviderInfo(
                        provider, flags, ps.readUserState(userId), userId);
            }
            return null;
        }
        }
    }
    }
@@ -12777,8 +12879,26 @@ public class PackageManagerService extends IPackageManager.Stub
            if (ps == null) {
            if (ps == null) {
                return null;
                return null;
            }
            }
            final PackageUserState userState = ps.readUserState(userId);
            final boolean matchVisibleToInstantApp =
                    (mFlags & PackageManager.MATCH_VISIBLE_TO_INSTANT_APP_ONLY) != 0;
            final boolean isInstantApp = (mFlags & PackageManager.MATCH_INSTANT) != 0;
            // throw out filters that aren't visible to instant applications
            if (matchVisibleToInstantApp
                    && !(info.isVisibleToInstantApp() || userState.instantApp)) {
                return null;
            }
            // throw out instant application filters if we're not explicitly requesting them
            if (!isInstantApp && userState.instantApp) {
                return null;
            }
            // throw out instant application filters if updates are available; will trigger
            // instant application resolution
            if (userState.instantApp && ps.isUpdateAvailable()) {
                return null;
            }
            ProviderInfo pi = PackageParser.generateProviderInfo(provider, mFlags,
            ProviderInfo pi = PackageParser.generateProviderInfo(provider, mFlags,
                    ps.readUserState(userId), userId);
                    userState, userId);
            if (pi == null) {
            if (pi == null) {
                return null;
                return null;
            }
            }
@@ -23373,7 +23493,6 @@ Slog.v(TAG, ":: stepped forward, applying functor at tag " + parser.getName());
                    intent, resolvedType, flags, userId, callingUid, true /*includeInstantApps*/);
                    intent, resolvedType, flags, userId, callingUid, true /*includeInstantApps*/);
        }
        }
        @Override
        @Override
        public void addIsolatedUid(int isolatedUid, int ownerUid) {
        public void addIsolatedUid(int isolatedUid, int ownerUid) {
            synchronized (mPackages) {
            synchronized (mPackages) {