| Index: android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java
|
| diff --git a/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java b/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java
|
| index 09173a39b45da3a2d45903427ec6c20f4a294cf2..f3a48ba25d882d751e0f349caa22ff5fef7bc7a7 100644
|
| --- a/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java
|
| +++ b/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java
|
| @@ -10,12 +10,25 @@ import android.content.pm.PackageManager;
|
| import android.webkit.ValueCallback;
|
|
|
| import org.chromium.base.Log;
|
| +import org.chromium.base.ThreadUtils;
|
| +import org.chromium.base.annotations.CalledByNative;
|
| import org.chromium.base.annotations.JNINamespace;
|
|
|
| /**
|
| - * Java twin of the homonymous C++ class. The Java side is only responsible for
|
| - * switching metrics on and off. Since the setting is a platform feature, it
|
| - * must be obtained through PlatformServiceBridge.
|
| + * Determines whether metrics should be enabled.
|
| + *
|
| + * This requires the following steps:
|
| + * 1) Check if the app has opted out.
|
| + * 2) Check the platform's metrics consent setting.
|
| + * 3) Wait for the native AwMetricsServiceClient to call nativeInitialized.
|
| + * 4) If enabled, inform the native AwMetricsServiceClient via nativeSetMetricsEnabled.
|
| + *
|
| + * Steps 1 and 2 are started on the Java side by calling queryMetricsSetting. This happens in
|
| + * parallel with native AwMetricsServiceClient initialization. Either nativeInitialized happens
|
| + * before onReceiveValue comes back with the platform setting, in which case we should set
|
| + * sIsClientReady and wait for onReceiveValue, or onReceiveValue happens before nativeInitialized,
|
| + * in which case we should record the setting in sShouldEnable and wait for nativeInitialized.
|
| + * Whichever fires last should call nativeSetMetricsEnabled.
|
| */
|
| @JNINamespace("android_webview")
|
| public class AwMetricsServiceClient {
|
| @@ -25,10 +38,13 @@ public class AwMetricsServiceClient {
|
| // reporting. See https://developer.android.com/reference/android/webkit/WebView.html
|
| private static final String OPT_OUT_META_DATA_STR = "android.webkit.WebView.MetricsOptOut";
|
|
|
| - private static boolean isAppOptedOut(Context applicationContext) {
|
| + private static boolean sIsClientReady; // Is the native AwMetricsServiceClient initialized?
|
| + private static boolean sShouldEnable; // Have steps 1 and 2 passed?
|
| +
|
| + private static boolean isAppOptedOut(Context appContext) {
|
| try {
|
| - ApplicationInfo info = applicationContext.getPackageManager().getApplicationInfo(
|
| - applicationContext.getPackageName(), PackageManager.GET_META_DATA);
|
| + ApplicationInfo info = appContext.getPackageManager().getApplicationInfo(
|
| + appContext.getPackageName(), PackageManager.GET_META_DATA);
|
| if (info.metaData == null) {
|
| // null means no such tag was found.
|
| return false;
|
| @@ -43,19 +59,37 @@ public class AwMetricsServiceClient {
|
| }
|
| }
|
|
|
| - public AwMetricsServiceClient(Context applicationContext) {
|
| - if (isAppOptedOut(applicationContext)) {
|
| + public static void queryMetricsSetting(Context appContext) {
|
| + ThreadUtils.assertOnUiThread();
|
| +
|
| + // Metrics defaults to off, so only call nativeSetMetricsEnabled if it's true.
|
| + if (isAppOptedOut(appContext)) {
|
| return;
|
| }
|
|
|
| // Check if the user has consented.
|
| - PlatformServiceBridge.getInstance(applicationContext)
|
| - .setMetricsSettingListener(new ValueCallback<Boolean>() {
|
| + PlatformServiceBridge.getInstance(appContext)
|
| + .queryMetricsSetting(new ValueCallback<Boolean>() {
|
| public void onReceiveValue(Boolean enabled) {
|
| - nativeSetMetricsEnabled(enabled);
|
| + ThreadUtils.assertOnUiThread();
|
| + if (enabled) {
|
| + sShouldEnable = true;
|
| + if (sIsClientReady) {
|
| + nativeSetMetricsEnabled(true);
|
| + }
|
| + }
|
| }
|
| });
|
| }
|
|
|
| + @CalledByNative
|
| + public static void nativeInitialized() {
|
| + ThreadUtils.assertOnUiThread();
|
| + sIsClientReady = true;
|
| + if (sShouldEnable) {
|
| + nativeSetMetricsEnabled(true);
|
| + }
|
| + }
|
| +
|
| public static native void nativeSetMetricsEnabled(boolean enabled);
|
| }
|
|
|