| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/NoCardAbortFieldTrial.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/NoCardAbortFieldTrial.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/NoCardAbortFieldTrial.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..57bff1c6aa4c8cf8d40219050e6d333f15807f75
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/NoCardAbortFieldTrial.java
|
| @@ -0,0 +1,101 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.payments;
|
| +
|
| +import android.text.TextUtils;
|
| +
|
| +import org.chromium.base.CommandLine;
|
| +import org.chromium.chrome.browser.ChromeSwitches;
|
| +import org.chromium.components.variations.VariationsAssociatedData;
|
| +
|
| +/**
|
| + * Provides Field Trial support for the Abort If No Card experiment within Chrome for Android.
|
| + */
|
| +public class NoCardAbortFieldTrial {
|
| + private static final String FIELD_TRIAL_NAME = "NoCardAbort";
|
| + private static final String ENABLED_PARAM = "enabled";
|
| + private static final String ENABLED_VALUE = "true";
|
| +
|
| + // Cached value to avoid repeated and redundant JNI operations.
|
| + private static Boolean sEnabled;
|
| +
|
| + /**
|
| + * Don't instantiate.
|
| + */
|
| + private NoCardAbortFieldTrial() {}
|
| +
|
| + /**
|
| + * Checks the current Variations parameters associated with the active group as well as the
|
| + * Chrome preference to determine if the service is enabled.
|
| + * @return Whether No Card Abort is enabled or not.
|
| + */
|
| + public static boolean isEnabled() {
|
| + if (sEnabled == null) {
|
| + sEnabled = detectEnabled();
|
| + }
|
| + return sEnabled.booleanValue();
|
| + }
|
| +
|
| + private static boolean detectEnabled() {
|
| + // Allow this user-flippable flag to disable the feature.
|
| + if (CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_NO_CARD_ABORT)) {
|
| + return false;
|
| + }
|
| +
|
| + // Allow this user-flippable flag to enable the feature.
|
| + if (CommandLine.getInstance().hasSwitch(ChromeSwitches.ENABLE_NO_CARD_ABORT)) {
|
| + return true;
|
| + }
|
| +
|
| + // Allow enabling the feature remotely.
|
| + if (getBooleanParam(ENABLED_PARAM)) {
|
| + return true;
|
| + }
|
| +
|
| + return false;
|
| + }
|
| +
|
| + // --------------------------------------------------------------------------------------------
|
| + // Helpers.
|
| + // --------------------------------------------------------------------------------------------
|
| +
|
| + /**
|
| + * Gets a boolean Finch parameter, assuming the <paramName>="true" format. Also checks for a
|
| + * command-line switch with the same name, for easy local testing.
|
| + * @param paramName The name of the Finch parameter (or command-line switch) to get a value for.
|
| + * @return Whether the Finch param is defined with a value "true", if there's a command-line
|
| + * flag present with any value.
|
| + */
|
| + private static boolean getBooleanParam(String paramName) {
|
| + if (CommandLine.getInstance().hasSwitch(paramName)) {
|
| + return true;
|
| + }
|
| + return TextUtils.equals(ENABLED_VALUE,
|
| + VariationsAssociatedData.getVariationParamValue(FIELD_TRIAL_NAME, paramName));
|
| + }
|
| +
|
| + /**
|
| + * Returns an integer value for a Finch parameter, or the default value if no parameter exists
|
| + * in the current configuration. Also checks for a command-line switch with the same name.
|
| + * @param paramName The name of the Finch parameter (or command-line switch) to get a value for.
|
| + * @param defaultValue The default value to return when there's no param or switch.
|
| + * @return An integer value -- either the param or the default.
|
| + */
|
| + private static int getIntParamValueOrDefault(String paramName, int defaultValue) {
|
| + String value = CommandLine.getInstance().getSwitchValue(paramName);
|
| + if (TextUtils.isEmpty(value)) {
|
| + value = VariationsAssociatedData.getVariationParamValue(FIELD_TRIAL_NAME, paramName);
|
| + }
|
| + if (!TextUtils.isEmpty(value)) {
|
| + try {
|
| + return Integer.parseInt(value);
|
| + } catch (NumberFormatException e) {
|
| + return defaultValue;
|
| + }
|
| + }
|
| +
|
| + return defaultValue;
|
| + }
|
| +}
|
|
|