Commit 49392e1b authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move IsInitialized() to base FeatureList.

This method should not be in ChromeFeatureList and can be used by
components.

Bug: 1060097
Change-Id: I954f71fd4dc0207a7f3b90ec855e2814f733475e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2111274
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757145}
parent 3fbd3e3b
......@@ -1413,6 +1413,7 @@ jumbo_component("base") {
"android/early_trace_event_binding.h",
"android/event_log.cc",
"android/event_log.h",
"android/feature_list_jni.cc",
"android/field_trial_list.cc",
"android/important_file_writer_android.cc",
"android/int_string_callback.cc",
......@@ -3187,6 +3188,7 @@ if (is_android) {
"android/java/src/org/chromium/base/CpuFeatures.java",
"android/java/src/org/chromium/base/EarlyTraceEvent.java",
"android/java/src/org/chromium/base/EventLog.java",
"android/java/src/org/chromium/base/FeatureList.java",
"android/java/src/org/chromium/base/FieldTrialList.java",
"android/java/src/org/chromium/base/ImportantFileWriterAndroid.java",
"android/java/src/org/chromium/base/IntStringCallback.java",
......
// Copyright 2020 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.
#include "base/base_jni_headers/FeatureList_jni.h"
#include "base/feature_list.h"
namespace base {
namespace android {
static jboolean JNI_FeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
} // namespace android
} // namespace base
......@@ -7,11 +7,18 @@ package org.chromium.base;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
import java.util.Map;
/**
* Provides shared capabilities for feature flag support.
*/
@JNINamespace("base::android")
@MainDex
public class FeatureList {
/** Map that stores substitution feature flags for tests. */
private static @Nullable Map<String, Boolean> sTestFeatures;
......@@ -21,6 +28,31 @@ public class FeatureList {
private FeatureList() {}
/**
* @return Whether the native FeatureList has been initialized. If this method returns false,
* none of the methods in this class that require native access should be called (except
* in tests if test features have been set).
*/
public static boolean isInitialized() {
return hasTestFeatures() || isNativeInitialized();
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
public static boolean isNativeInitialized() {
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return FeatureListJni.get().isInitialized();
}
/**
* This is called explicitly for instrumentation tests via Features#applyForInstrumentation().
* Unit tests and Robolectric tests must not invoke this and should rely on the {@link Features}
......@@ -75,4 +107,9 @@ public class FeatureList {
}
return null;
}
@NativeMethods
interface Natives {
boolean isInitialized();
}
}
......@@ -607,10 +607,6 @@ const base::Feature kVrBrowsingFeedback{"VrBrowsingFeedback",
const base::Feature kWebApkAdaptiveIcon{"WebApkAdaptiveIcon",
base::FEATURE_ENABLED_BY_DEFAULT};
static jboolean JNI_ChromeFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_ChromeFeatureList_IsEnabled(
JNIEnv* env,
const JavaParamRef<jstring>& jfeature_name) {
......
......@@ -10,7 +10,6 @@ import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
import java.util.Map;
......@@ -71,25 +70,10 @@ public abstract class ChromeFeatureList {
* none of the methods in this class that require native access should be called (except
* in tests if test features have been set).
*/
// TODO(crbug.com/1060097): Migrate callers to the FeatureList equivalent function.
@Deprecated
public static boolean isInitialized() {
if (FeatureList.hasTestFeatures()) return true;
return isNativeInitialized();
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return ChromeFeatureListJni.get().isInitialized();
return FeatureList.isInitialized();
}
/*
......@@ -99,7 +83,7 @@ public abstract class ChromeFeatureList {
* @return Whether the feature is enabled or not.
*/
private static boolean isEnabledInNative(String featureName) {
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return ChromeFeatureListJni.get().isEnabled(featureName);
}
......@@ -139,7 +123,7 @@ public abstract class ChromeFeatureList {
*/
public static String getFieldTrialParamByFeature(String featureName, String paramName) {
if (FeatureList.hasTestFeatures()) return "";
assert isInitialized();
assert FeatureList.isInitialized();
return ChromeFeatureListJni.get().getFieldTrialParamByFeature(featureName, paramName);
}
......@@ -158,7 +142,7 @@ public abstract class ChromeFeatureList {
public static int getFieldTrialParamByFeatureAsInt(
String featureName, String paramName, int defaultValue) {
if (FeatureList.hasTestFeatures()) return defaultValue;
assert isInitialized();
assert FeatureList.isInitialized();
return ChromeFeatureListJni.get().getFieldTrialParamByFeatureAsInt(
featureName, paramName, defaultValue);
}
......@@ -178,7 +162,7 @@ public abstract class ChromeFeatureList {
public static double getFieldTrialParamByFeatureAsDouble(
String featureName, String paramName, double defaultValue) {
if (FeatureList.hasTestFeatures()) return defaultValue;
assert isInitialized();
assert FeatureList.isInitialized();
return ChromeFeatureListJni.get().getFieldTrialParamByFeatureAsDouble(
featureName, paramName, defaultValue);
}
......@@ -198,7 +182,7 @@ public abstract class ChromeFeatureList {
public static boolean getFieldTrialParamByFeatureAsBoolean(
String featureName, String paramName, boolean defaultValue) {
if (FeatureList.hasTestFeatures()) return defaultValue;
assert isInitialized();
assert FeatureList.isInitialized();
return ChromeFeatureListJni.get().getFieldTrialParamByFeatureAsBoolean(
featureName, paramName, defaultValue);
}
......@@ -437,7 +421,6 @@ public abstract class ChromeFeatureList {
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
String getFieldTrialParamByFeature(String featureName, String paramName);
int getFieldTrialParamByFeatureAsInt(
......
......@@ -8,7 +8,6 @@ import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
/**
* Provides an API for querying the status of Site Settings features.
......@@ -34,31 +33,12 @@ public class SiteSettingsFeatureList {
public static boolean isEnabled(String featureName) {
Boolean testValue = FeatureList.getTestValueForFeature(featureName);
if (testValue != null) return testValue;
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return SiteSettingsFeatureListJni.get().isEnabled(featureName);
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (FeatureList.hasTestFeatures()) return true;
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return SiteSettingsFeatureListJni.get().isInitialized();
}
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
}
}
......@@ -36,10 +36,6 @@ const base::Feature* FindFeatureExposedToJava(const std::string& feature_name) {
} // namespace
static jboolean JNI_SiteSettingsFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_SiteSettingsFeatureList_IsEnabled(
JNIEnv* env,
const JavaParamRef<jstring>& jfeature_name) {
......
......@@ -36,10 +36,6 @@ const base::Feature* FindFeatureExposedToJava(const std::string& feature_name) {
} // namespace
static jboolean JNI_ContentSettingsFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_ContentSettingsFeatureList_IsEnabled(
JNIEnv* env,
const JavaParamRef<jstring>& jfeature_name) {
......
......@@ -8,7 +8,6 @@ import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
/**
* Provides an API for querying the status of Content Settings features.
......@@ -24,12 +23,14 @@ public class ContentSettingsFeatureList {
private ContentSettingsFeatureList() {}
/**
* @deprecated Use {@link FeatureList#isInitialized()} instead
* @return Whether the native FeatureList has been initialized. If this method returns false,
* none of the methods in this class that require native access should be called.
*/
// TODO(crbug.com/1060097): Migrate callers to the FeatureList equivalent function.
@Deprecated
public static boolean isInitialized() {
if (FeatureList.hasTestFeatures()) return true;
return isNativeInitialized();
return FeatureList.isInitialized();
}
/**
......@@ -45,31 +46,12 @@ public class ContentSettingsFeatureList {
public static boolean isEnabled(String featureName) {
Boolean testValue = FeatureList.getTestValueForFeature(featureName);
if (testValue != null) return testValue;
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return ContentSettingsFeatureListJni.get().isEnabled(featureName);
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (FeatureList.hasTestFeatures()) return true;
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return ContentSettingsFeatureListJni.get().isInitialized();
}
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
}
}
......@@ -667,6 +667,7 @@ _jar_excluded_patterns = [
"*/multidex/*.class",
"*/process_launcher/*.class",
"*/SysUtils*.class",
"org/chromium/base/FeatureList*.class",
"org/chromium/base/memory/MemoryPressureMonitor*.class",
]
......
......@@ -38,10 +38,6 @@ const base::Feature kIntentBlockExternalFormRedirectsNoGesture{
"IntentBlockExternalFormRedirectsNoGesture",
base::FEATURE_DISABLED_BY_DEFAULT};
static jboolean JNI_ExternalIntentsFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_ExternalIntentsFeatureList_IsEnabled(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& jfeature_name) {
......
......@@ -4,10 +4,10 @@
package org.chromium.components.external_intents;
import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
/**
* Java accessor for base/feature_list.h state.
......@@ -22,22 +22,6 @@ public abstract class ExternalIntentsFeatureList {
/** Prevent instantiation. */
private ExternalIntentsFeatureList() {}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return ExternalIntentsFeatureListJni.get().isInitialized();
}
/**
* Returns whether the specified feature is enabled or not.
*
......@@ -54,7 +38,7 @@ public abstract class ExternalIntentsFeatureList {
* @return Whether the feature is enabled or not.
*/
public static boolean isEnabled(String featureName) {
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return ExternalIntentsFeatureListJni.get().isEnabled(featureName);
}
......@@ -64,7 +48,6 @@ public abstract class ExternalIntentsFeatureList {
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
}
}
......@@ -4,10 +4,10 @@
package org.chromium.components.subresource_filter;
import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
/**
* Provides an API for querying the status of subresource_filter component Features.
......@@ -31,29 +31,12 @@ public class SubresourceFilterFeatureList {
* @return Whether the feature is enabled or not.
*/
public static boolean isEnabled(String featureName) {
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return SubresourceFilterFeatureListJni.get().isEnabled(featureName);
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return SubresourceFilterFeatureListJni.get().isInitialized();
}
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
}
}
......@@ -35,10 +35,6 @@ const base::Feature* FindFeatureExposedToJava(const std::string& feature_name) {
} // namespace
static jboolean JNI_SubresourceFilterFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_SubresourceFilterFeatureList_IsEnabled(
JNIEnv* env,
const JavaParamRef<jstring>& jfeature_name) {
......
......@@ -35,10 +35,6 @@ const base::Feature* FindFeatureExposedToJava(const std::string& feature_name) {
} // namespace
static jboolean JNI_DeviceFeatureList_IsInitialized(JNIEnv* env) {
return !!base::FeatureList::GetInstance();
}
static jboolean JNI_DeviceFeatureList_IsEnabled(
JNIEnv* env,
const JavaParamRef<jstring>& jfeature_name) {
......
......@@ -8,7 +8,6 @@ import org.chromium.base.FeatureList;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
/**
* Provides an API for querying the status of Device Service Features.
......@@ -33,31 +32,12 @@ public class DeviceFeatureList {
public static boolean isEnabled(String featureName) {
Boolean testValue = FeatureList.getTestValueForFeature(featureName);
if (testValue != null) return testValue;
assert isNativeInitialized();
assert FeatureList.isNativeInitialized();
return DeviceFeatureListJni.get().isEnabled(featureName);
}
/**
* @return Whether the native FeatureList is initialized or not.
*/
private static boolean isNativeInitialized() {
if (FeatureList.hasTestFeatures()) return true;
if (!LibraryLoader.getInstance().isInitialized()) return false;
// Even if the native library is loaded, the C++ FeatureList might not be initialized yet.
// In that case, accessing it will not immediately fail, but instead cause a crash later
// when it is initialized. Return whether the native FeatureList has been initialized,
// so the return value can be tested, or asserted for a more actionable stack trace
// on failure.
//
// The FeatureList is however guaranteed to be initialized by the time
// AsyncInitializationActivity#finishNativeInitialization is called.
return DeviceFeatureListJni.get().isInitialized();
}
@NativeMethods
interface Natives {
boolean isInitialized();
boolean isEnabled(String featureName);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment