Bug 1670873 - Ignore DebugConfig on SDK_INT < 21. r=snorp

Differential Revision: https://phabricator.services.mozilla.com/D94481
This commit is contained in:
Agi Sferro 2020-10-29 16:18:41 +00:00
parent 9495897723
commit 1cd845aa11
3 changed files with 40 additions and 17 deletions

View File

@ -5,6 +5,7 @@
package org.mozilla.gecko.util;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.util.Log;
@ -14,6 +15,7 @@ import org.mozilla.gecko.annotation.ReflectionTarget;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File;
import java.io.FileInputStream;
@ -36,7 +38,18 @@ public class DebugConfig {
protected Map<String, String> env;
protected List<String> args;
public static class ConfigException extends RuntimeException {
public ConfigException(final String message) {
super(message);
}
}
public static @NonNull DebugConfig fromFile(final @NonNull File configFile) throws FileNotFoundException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// There are a lot of problems with SnakeYaml on older version let's just bail.
throw new ConfigException("Config version is only supported for SDK_INT >= 21.");
}
final Constructor constructor = new Constructor(DebugConfig.class);
final TypeDescription description = new TypeDescription(DebugConfig.class);
description.putMapPropertyType("prefs", String.class, Object.class);
@ -49,6 +62,8 @@ public class DebugConfig {
final FileInputStream fileInputStream = new FileInputStream(configFile);
try {
return yaml.load(fileInputStream);
} catch (YAMLException e) {
throw new ConfigException(e.getMessage());
} finally {
IOUtils.safeStreamClose(fileInputStream);
}

View File

@ -46,7 +46,6 @@ import org.mozilla.gecko.util.DebugConfig;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File;
import java.io.FileNotFoundException;
@ -346,24 +345,27 @@ public final class GeckoRuntime implements Parcelable {
info.prefs = prefMap;
// End of Bug 1605454 hack
String configFilePath = settings.getConfigFilePath();
if (configFilePath == null) {
// Default to /data/local/tmp/$PACKAGE-geckoview-config.yaml if android:debuggable="true"
// or if this application is the current Android "debug_app", and to not read configuration
// from a file otherwise.
if (isApplicationDebuggable(context) || isApplicationCurrentDebugApp(context)) {
configFilePath = String.format(CONFIG_FILE_PATH_TEMPLATE, context.getApplicationInfo().packageName);
// Older versions have problems with SnakeYaml
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String configFilePath = settings.getConfigFilePath();
if (configFilePath == null) {
// Default to /data/local/tmp/$PACKAGE-geckoview-config.yaml if android:debuggable="true"
// or if this application is the current Android "debug_app", and to not read configuration
// from a file otherwise.
if (isApplicationDebuggable(context) || isApplicationCurrentDebugApp(context)) {
configFilePath = String.format(CONFIG_FILE_PATH_TEMPLATE, context.getApplicationInfo().packageName);
}
}
}
if (configFilePath != null && !configFilePath.isEmpty()) {
try {
final DebugConfig debugConfig = DebugConfig.fromFile(new File(configFilePath));
Log.i(LOGTAG, "Adding debug configuration from: " + configFilePath);
debugConfig.mergeIntoInitInfo(info);
} catch (YAMLException e) {
Log.w(LOGTAG, "Failed to add debug configuration from: " + configFilePath, e);
} catch (FileNotFoundException e) {
if (configFilePath != null && !configFilePath.isEmpty()) {
try {
final DebugConfig debugConfig = DebugConfig.fromFile(new File(configFilePath));
Log.i(LOGTAG, "Adding debug configuration from: " + configFilePath);
debugConfig.mergeIntoInitInfo(info);
} catch (DebugConfig.ConfigException e) {
Log.w(LOGTAG, "Failed to add debug configuration from: " + configFilePath, e);
} catch (FileNotFoundException e) {
}
}
}

View File

@ -77,6 +77,10 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
* Path to configuration file from which GeckoView will read configuration options such as
* Gecko process arguments, environment variables, and preferences.
*
* Note: this feature is only available for
* <code>{@link Build.VERSION#SDK_INT} &gt; 21</code>, on older devices this will be
* silently ignored.
*
* @param configFilePath Configuration file path to read from, or <code>null</code> to use
* default location <code>/data/local/tmp/$PACKAGE-geckoview-config.yaml</code>.
* @return This Builder instance.
@ -581,6 +585,8 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
* Path to configuration file from which GeckoView will read configuration options such as
* Gecko process arguments, environment variables, and preferences.
*
* Note: this feature is only available for <code>{@link Build.VERSION#SDK_INT} &gt; 21</code>.
*
* @return Path to configuration file from which GeckoView will read configuration options,
* or <code>null</code> for default location
* <code>/data/local/tmp/$PACKAGE-geckoview-config.yaml</code>.