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

Commit ff400f32 authored by Chad Brubaker's avatar Chad Brubaker Committed by android-build-merger
Browse files

Merge "Allow debug-overrides to be specified in an extra resource" into nyc-dev

am: 8064b4a6

* commit '8064b4a6':
  Allow debug-overrides to be specified in an extra resource
parents 8b27e125 8064b4a6
Loading
Loading
Loading
Loading
+43 −1
Original line number Original line Diff line number Diff line
@@ -339,7 +339,7 @@ public class XmlConfigSource implements ConfigSource {
                }
                }
                if (mDebugBuild) {
                if (mDebugBuild) {
                    debugConfigBuilder =
                    debugConfigBuilder =
                            parseConfigEntry(parser, seenDomains, null, CONFIG_DEBUG).get(0).first;
                            parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                } else {
                    XmlUtils.skipCurrentTag(parser);
                    XmlUtils.skipCurrentTag(parser);
                }
                }
@@ -348,6 +348,11 @@ public class XmlConfigSource implements ConfigSource {
                XmlUtils.skipCurrentTag(parser);
                XmlUtils.skipCurrentTag(parser);
            }
            }
        }
        }
        // If debug is true and there was no debug-overrides in the file check for an extra
        // _debug resource.
        if (mDebugBuild && debugConfigBuilder == null) {
            debugConfigBuilder = parseDebugOverridesResource();
        }


        // Use the platform default as the parent of the base config for any values not provided
        // Use the platform default as the parent of the base config for any values not provided
        // there. If there is no base config use the platform default.
        // there. If there is no base config use the platform default.
@@ -385,6 +390,43 @@ public class XmlConfigSource implements ConfigSource {
        mDomainMap = configs;
        mDomainMap = configs;
    }
    }


    private NetworkSecurityConfig.Builder parseDebugOverridesResource()
            throws IOException, XmlPullParserException, ParserException {
        Resources resources = mContext.getResources();
        String packageName = resources.getResourcePackageName(mResourceId);
        String entryName = resources.getResourceEntryName(mResourceId);
        int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
        // No debug-overrides resource was found, nothing to parse.
        if (resId == 0) {
            return null;
        }
        NetworkSecurityConfig.Builder debugConfigBuilder = null;
        // Parse debug-overrides out of the _debug resource.
        try (XmlResourceParser parser = resources.getXml(resId)) {
            XmlUtils.beginDocument(parser, "network-security-config");
            int outerDepth = parser.getDepth();
            boolean seenDebugOverrides = false;
            while (XmlUtils.nextElementWithin(parser, outerDepth)) {
                if ("debug-overrides".equals(parser.getName())) {
                    if (seenDebugOverrides) {
                        throw new ParserException(parser, "Only one debug-overrides allowed");
                    }
                    if (mDebugBuild) {
                        debugConfigBuilder =
                                parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                    } else {
                        XmlUtils.skipCurrentTag(parser);
                    }
                    seenDebugOverrides = true;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
            }
        }

        return debugConfigBuilder;
    }

    public static class ParserException extends Exception {
    public static class ParserException extends Exception {


        public ParserException(XmlPullParser parser, String message, Throwable cause) {
        public ParserException(XmlPullParser parser, String message, Throwable cause) {
+7 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <base-config>
    <trust-anchors>
    </trust-anchors>
  </base-config>
</network-security-config>
+7 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- debug-overrides not inside network-security-config should cause a parsing error -->
<debug-overrides>
  <trust-anchors>
    <certificates src="system" />
  </trust-anchors>
</debug-overrides>
+7 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <base-config>
    <trust-anchors>
    </trust-anchors>
  </base-config>
</network-security-config>
+8 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <debug-overrides>
    <trust-anchors>
      <certificates src="system" />
    </trust-anchors>
  </debug-overrides>
</network-security-config>
Loading