1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base; 6 7 /** 8 * Flags of this type are un-cached flags that may be called before native, 9 * but not primarily. They have good default values to use before native is loaded, 10 * and will switch to using the native value once native is loaded. 11 * These flags replace code like: 12 * if (FeatureList.isInitialized() && SomeFeatureMap.isEnabled(featureName)) 13 * or 14 * if (!FeatureList.isInitialized() || SomeFeatureMap.isEnabled(featureName)). 15 */ 16 public class MutableFlagWithSafeDefault extends Flag { 17 private final boolean mDefaultValue; 18 private final FeatureMap mFeatureMap; 19 private Boolean mInMemoryCachedValue; 20 MutableFlagWithSafeDefault( FeatureMap featureMap, String featureName, boolean defaultValue)21 public MutableFlagWithSafeDefault( 22 FeatureMap featureMap, String featureName, boolean defaultValue) { 23 super(featureName); 24 mFeatureMap = featureMap; 25 mDefaultValue = defaultValue; 26 } 27 28 @Override isEnabled()29 public boolean isEnabled() { 30 if (mInMemoryCachedValue != null) return mInMemoryCachedValue; 31 if (FeatureList.hasTestFeature(mFeatureName)) { 32 return mFeatureMap.isEnabledInNative(mFeatureName); 33 } 34 35 if (FeatureList.isNativeInitialized()) { 36 mInMemoryCachedValue = mFeatureMap.isEnabledInNative(mFeatureName); 37 return mInMemoryCachedValue; 38 } 39 40 return mDefaultValue; 41 } 42 43 @Override clearInMemoryCachedValueForTesting()44 protected void clearInMemoryCachedValueForTesting() { 45 mInMemoryCachedValue = null; 46 } 47 } 48