Merge "Add ConfigNetworkSecurityPolicy"

am: 2786002bd5

* commit '2786002bd51b229d9a4672e3c43f835796de06ea':
  Add ConfigNetworkSecurityPolicy
This commit is contained in:
Chad Brubaker
2016-01-04 20:49:29 +00:00
committed by android-build-merger
3 changed files with 67 additions and 0 deletions

View File

@@ -120,6 +120,32 @@ public final class ApplicationConfig {
return mTrustManager;
}
/**
* Returns {@code true} if cleartext traffic is permitted for this application, which is the
* case only if all configurations permit cleartext traffic. For finer-grained policy use
* {@link #isCleartextTrafficPermitted(String)}.
*/
public boolean isCleartextTrafficPermitted() {
ensureInitialized();
if (mConfigs != null) {
for (Pair<Domain, NetworkSecurityConfig> entry : mConfigs) {
if (!entry.second.isCleartextTrafficPermitted()) {
return false;
}
}
}
return mDefaultConfig.isCleartextTrafficPermitted();
}
/**
* Returns {@code true} if cleartext traffic is permitted for this application when connecting
* to {@code hostname}.
*/
public boolean isCleartextTrafficPermitted(String hostname) {
return getConfigForHostname(hostname).isCleartextTrafficPermitted();
}
private void ensureInitialized() {
synchronized(mLock) {
if (mInitialized) {

View File

@@ -0,0 +1,40 @@
/**
* 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;
/**
* {@link libcore.net.NetworkSecurityPolicy} based on an {@link ApplicationConfig}.
*
* @hide
*/
public class ConfigNetworkSecurityPolicy extends libcore.net.NetworkSecurityPolicy {
private final ApplicationConfig mConfig;
public ConfigNetworkSecurityPolicy(ApplicationConfig config) {
mConfig = config;
}
@Override
public boolean isCleartextTrafficPermitted() {
return mConfig.isCleartextTrafficPermitted();
}
@Override
public boolean isCleartextTrafficPermitted(String hostname) {
return mConfig.isCleartextTrafficPermitted(hostname);
}
}

View File

@@ -40,5 +40,6 @@ public final class NetworkSecurityConfigProvider extends Provider {
throw new RuntimeException("Failed to install provider as highest priority provider."
+ " Provider was installed at position " + pos);
}
libcore.net.NetworkSecurityPolicy.setInstance(new ConfigNetworkSecurityPolicy(config));
}
}