Bug 1277214 - Move MOZ_DISABLE_* to single MOZ_IN_AUTOMATION env var. r=grisha

We hit an issue where adding a new env var, MOZ_DISABLE_TELEMETRY, added env10
and caused crashes. I suspect the issue is that there are is now a double-digit
number of env vars (bug 1277390). Here, we do the quick fix by removing
MOZ_DISABLE_TELEMETRY & repurposing MOZ_DISABLE_SWITCHBOARD to be generic.

While we're at it, we simplify the code by making the setDisabled methods a
strict getter without checking for how many times they're called.

MozReview-Commit-ID: 19DDbVYRZ2

--HG--
extra : rebase_source : 1590ae4f49bf725ab8a3bb26f10dab324903aa8c
This commit is contained in:
Michael Comella 2016-06-01 16:46:44 -07:00
Родитель 4bbd712e84
Коммит af565b6cbb
5 изменённых файлов: 24 добавлений и 61 удалений

Просмотреть файл

@ -86,17 +86,12 @@ class RemoteAutomation(Automation):
# Don't override the user's choice here. See bug 1049688.
env.setdefault('MOZ_DISABLE_NONLOCAL_CONNECTIONS', '1')
# Disable Switchboard by default. This will prevent nonlocal
# network connections to the Switchboard server.
# Passing any value expect the empty string will disable it so to
# enable, don't pass a value.
env.setdefault('MOZ_DISABLE_SWITCHBOARD', '1')
# Disable Java telemetry by default to
# prevent network connections during testing.
# Passing any value expect the empty string will disable it so to
# enable, don't pass a value.
env.setdefault('MOZ_DISABLE_TELEMETRY', '1')
# Send an env var noting that we are in automation. Passing any
# value except the empty string will declare the value to exist.
#
# This may be used to disabled network connections during testing, e.g.
# Switchboard & telemetry uploads.
env.setdefault('MOZ_IN_AUTOMATION', '1')
# Set WebRTC logging in case it is not set yet.
# On Android, environment variables cannot contain ',' so the

Просмотреть файл

@ -769,8 +769,9 @@ public class BrowserApp extends GeckoApp
*/
private void configureForTestsBasedOnEnvironment(final Intent intent) {
final HashMap<String, String> envVars = IntentUtils.getEnvVarMap(intent);
Experiments.setDisabledFromEnvVar(envVars);
TelemetryUploadService.setDisabledFromEnvVar(envVars);
final boolean isInAutomation = !TextUtils.isEmpty(envVars.get(IntentUtils.ENV_VAR_IN_AUTOMATION));
Experiments.setIsDisabled(isInAutomation);
TelemetryUploadService.setDisabled(isInAutomation);
}
/**

Просмотреть файл

@ -8,7 +8,6 @@ import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import ch.boye.httpclientandroidlib.HttpHeaders;
import ch.boye.httpclientandroidlib.HttpResponse;
@ -31,7 +30,6 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -46,37 +44,17 @@ public class TelemetryUploadService extends IntentService {
private static final String LOGTAG = StringUtils.safeSubstring("Gecko" + TelemetryUploadService.class.getSimpleName(), 0, 23);
private static final String WORKER_THREAD_NAME = LOGTAG + "Worker";
private static final String ENV_VAR_NAME = "MOZ_DISABLE_TELEMETRY";
public static final String ACTION_UPLOAD = "upload";
public static final String EXTRA_STORE = "store";
/**
* An override for telemetry via Intents.
*
* BrowserApp.onCreate, which sets the disabled state, should run before
* TelemetryUploadService, so we don't have to synchronize/volatile.
*/
private static Boolean isDisabledByLaunchingIntent = null;
// TelemetryUploadService can run in a background thread so for future proofing, we set it volatile.
private static volatile boolean isDisabled = false;
/**
* As a sanity check, this method should only be called once.
*/
public static void setDisabledFromEnvVar(final HashMap<String, String> envVarMap) {
if (isDisabledByLaunchingIntent != null) {
throw new IllegalStateException("Disabled state already set");
public static void setDisabled(final boolean isDisabled) {
TelemetryUploadService.isDisabled = isDisabled;
if (isDisabled) {
Log.d(LOGTAG, "Telemetry upload disabled (env var?");
}
isDisabledByLaunchingIntent = !TextUtils.isEmpty(envVarMap.get(ENV_VAR_NAME));
if (isDisabledByLaunchingIntent) {
Log.d(LOGTAG, "Telemetry disabled by environment variable: " + ENV_VAR_NAME);
}
}
private static boolean isDisabledByLaunchingIntent() {
if (isDisabledByLaunchingIntent == null) {
throw new IllegalStateException("Disabled state not yet set.");
}
return isDisabledByLaunchingIntent;
}
public TelemetryUploadService() {
@ -214,7 +192,7 @@ public class TelemetryUploadService extends IntentService {
return false;
}
if (isDisabledByLaunchingIntent()) {
if (isDisabled) {
Log.d(LOGTAG, "Telemetry upload feature is disabled by intent (in testing?)");
return false;
}

Просмотреть файл

@ -6,7 +6,6 @@ package org.mozilla.gecko.util;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.text.TextUtils;
@ -16,7 +15,6 @@ import org.mozilla.gecko.GeckoSharedPrefs;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This class should reflect the experiment names found in the Switchboard experiments config here:
@ -25,8 +23,6 @@ import java.util.Map;
public class Experiments {
private static final String LOGTAG = "GeckoExperiments";
private static final String ENVVAR_DISABLED = "MOZ_DISABLE_SWITCHBOARD";
// Show a system notification linking to a "What's New" page on app update.
public static final String WHATSNEW_NOTIFICATION = "whatsnew-notification";
@ -58,26 +54,17 @@ public class Experiments {
// Show name of organization (EV cert) instead of full URL in URL bar (Bug 1249594).
public static final String URLBAR_SHOW_EV_CERT_OWNER = "urlbar-show-ev-cert-owner";
private static volatile Boolean disabled = null;
private static boolean isDisabled = false;
/**
* As a sanity check, this method may only be called once.
*/
public static void setDisabledFromEnvVar(@NonNull final Map<String, String> envVarMap) {
if (disabled != null) {
throw new IllegalStateException("Disabled state already set");
}
disabled = !TextUtils.isEmpty(envVarMap.get(ENVVAR_DISABLED));
if (disabled) {
Log.d(LOGTAG, "Switchboard disabled by environment variable: " + ENVVAR_DISABLED);
public static void setIsDisabled(final boolean isDisabled) {
Experiments.isDisabled = isDisabled;
if (isDisabled) {
Log.d(LOGTAG, "Switchboard disabled (env var?)");
}
}
public static boolean isDisabled() {
if (disabled == null) {
throw new IllegalStateException("Disabled state not yet set.");
}
return disabled;
return isDisabled;
}
/**

Просмотреть файл

@ -19,6 +19,8 @@ import java.util.regex.Pattern;
* Utilities for Intents.
*/
public class IntentUtils {
public static final String ENV_VAR_IN_AUTOMATION = "MOZ_IN_AUTOMATION";
private static final String ENV_VAR_REGEX = "(.+)=(.*)";
private IntentUtils() {}