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

Commit 73c06b1d authored by Chad Brubaker's avatar Chad Brubaker Committed by Gerrit Code Review
Browse files

Merge "Make NetworkSecurityConfigProvider.install lazy"

parents 7d72975c 2075a3eb
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
@@ -144,18 +144,4 @@ public final class ApplicationConfig {
            return sInstance;
        }
    }

    /** @hide */
    public static ApplicationConfig getPlatformDefault() {
        return new ApplicationConfig(new ConfigSource() {
            @Override
            public NetworkSecurityConfig getDefaultConfig() {
                return NetworkSecurityConfig.DEFAULT;
            }
            @Override
            public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() {
                return null;
            }
        });
    }
}
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.security.net.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.util.Pair;
import java.util.Set;

/** @hide */
public class ManifestConfigSource implements ConfigSource {
    public static final String META_DATA_NETWORK_SECURITY_CONFIG =
            "android.security.net.config";
    private static final boolean DBG = true;
    private static final String LOG_TAG = "NetworkSecurityConfig";

    private final Object mLock = new Object();
    private final Context mContext;

    private ConfigSource mConfigSource;

    public ManifestConfigSource(Context context) {
        mContext = context;
    }

    @Override
    public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() {
        return getConfigSource().getPerDomainConfigs();
    }

    @Override
    public NetworkSecurityConfig getDefaultConfig() {
        return getConfigSource().getDefaultConfig();
    }

    private ConfigSource getConfigSource() {
        synchronized (mLock) {
            if (mConfigSource != null) {
                return mConfigSource;
            }
            ApplicationInfo info;
            try {
                info = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(),
                        PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException("Failed to look up ApplicationInfo", e);
            }
            int configResourceId = 0;
            if (info != null && info.metaData != null) {
                configResourceId = info.metaData.getInt(META_DATA_NETWORK_SECURITY_CONFIG);
            }

            ConfigSource source;
            if (configResourceId != 0) {
                boolean debugBuild = (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                if (DBG) {
                    Log.d(LOG_TAG, "Using Network Security Config from resource "
                            + mContext.getResources().getResourceEntryName(configResourceId)
                            + " debugBuild: " + debugBuild);
                }
                source = new XmlConfigSource(mContext, configResourceId, debugBuild);
            } else {
                if (DBG) {
                    Log.d(LOG_TAG, "No Network Security Config specified, using platform default");
                }
                source = new DefaultConfigSource();
            }
            mConfigSource = source;
            return mConfigSource;
        }
    }

    private static final class DefaultConfigSource implements ConfigSource {
        @Override
        public NetworkSecurityConfig getDefaultConfig() {
            return NetworkSecurityConfig.DEFAULT;
        }

        @Override
        public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() {
            return null;
        }
    }
}
+1 −37
Original line number Diff line number Diff line
@@ -17,20 +17,13 @@
package android.security.net.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import java.security.Security;
import java.security.Provider;

/** @hide */
public final class NetworkSecurityConfigProvider extends Provider {
    private static final String LOG_TAG = "NetworkSecurityConfig";
    private static final String PREFIX =
            NetworkSecurityConfigProvider.class.getPackage().getName() + ".";
    public static final String META_DATA_NETWORK_SECURITY_CONFIG =
            "android.security.net.config";
    private static final boolean DBG = true;

    public NetworkSecurityConfigProvider() {
        // TODO: More clever name than this
@@ -40,36 +33,7 @@ public final class NetworkSecurityConfigProvider extends Provider {
    }

    public static void install(Context context) {
        ApplicationInfo info = null;
        // TODO: This lookup shouldn't be done in the app startup path, it should be done lazily.
        try {
            info = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Failed to look up ApplicationInfo", e);
        }
        int configResourceId = 0;
        if (info != null && info.metaData != null) {
            configResourceId = info.metaData.getInt(META_DATA_NETWORK_SECURITY_CONFIG);
        }

        ApplicationConfig config;
        if (configResourceId != 0) {
            boolean debugBuild = (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
            if (DBG) {
                Log.d(LOG_TAG, "Using Network Security Config from resource "
                        + context.getResources().getResourceEntryName(configResourceId)
                        + " debugBuild: " + debugBuild);
            }
            ConfigSource source = new XmlConfigSource(context, configResourceId, debugBuild);
            config = new ApplicationConfig(source);
        } else {
            if (DBG) {
                Log.d(LOG_TAG, "No Network Security Config specified, using platform default");
            }
            config = ApplicationConfig.getPlatformDefault();
        }

        ApplicationConfig config = new ApplicationConfig(new ManifestConfigSource(context));
        ApplicationConfig.setDefaultInstance(config);
        int pos = Security.insertProviderAt(new NetworkSecurityConfigProvider(), 1);
        if (pos != 1) {