OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
10 import android.webkit.ValueCallback; | 10 import android.webkit.ValueCallback; |
11 | 11 |
12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
13 import org.chromium.base.ThreadUtils; | |
14 import org.chromium.base.annotations.CalledByNative; | |
13 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
14 | 16 |
15 /** | 17 /** |
16 * Java twin of the homonymous C++ class. The Java side is only responsible for | 18 * Determines whether metrics should be enabled. |
17 * switching metrics on and off. Since the setting is a platform feature, it | 19 * |
18 * must be obtained through PlatformServiceBridge. | 20 * This requires the following steps: |
21 * 1) Check if the app has opted out. | |
22 * 2) Check the platform's metrics consent setting. | |
23 * 3) Wait for the native AwMetricsServiceClient to call nativeInitialized. | |
24 * 4) If enabled, inform the native AwMetricsServiceClient via nativeSetMetricsE nabled. | |
sgurun-gerrit only
2017/01/13 07:12:15
I think because you start it from Java (AwContents
| |
25 * | |
26 * Steps 1 and 2 are started on the Java side by calling queryMetricsSetting. Th is happens in | |
27 * parallel with native AwMetricsServiceClient initialization. Either nativeInit ialized happens | |
28 * before onReceiveValue comes back with the platform setting, in which case we should set | |
29 * sIsClientReady and wait for onReceiveValue, or onReceiveValue happens before nativeInitialized, | |
30 * in which case we should record the setting in sShouldEnable and wait for nati veInitialized. | |
31 * Whichever fires last should call nativeSetMetricsEnabled. | |
19 */ | 32 */ |
20 @JNINamespace("android_webview") | 33 @JNINamespace("android_webview") |
21 public class AwMetricsServiceClient { | 34 public class AwMetricsServiceClient { |
22 private static final String TAG = "AwMetricsServiceCli-"; | 35 private static final String TAG = "AwMetricsServiceCli-"; |
23 | 36 |
24 // Individual apps can use this meta-data tag in their manifest to opt out o f metrics | 37 // Individual apps can use this meta-data tag in their manifest to opt out o f metrics |
25 // reporting. See https://developer.android.com/reference/android/webkit/Web View.html | 38 // reporting. See https://developer.android.com/reference/android/webkit/Web View.html |
26 private static final String OPT_OUT_META_DATA_STR = "android.webkit.WebView. MetricsOptOut"; | 39 private static final String OPT_OUT_META_DATA_STR = "android.webkit.WebView. MetricsOptOut"; |
27 | 40 |
28 private static boolean isAppOptedOut(Context applicationContext) { | 41 private static boolean sIsClientReady; // Is the native AwMetricsServiceClie nt initialized? |
42 private static boolean sShouldEnable; // Have steps 1 and 2 passed? | |
43 | |
44 private static boolean isAppOptedOut(Context appContext) { | |
29 try { | 45 try { |
30 ApplicationInfo info = applicationContext.getPackageManager().getApp licationInfo( | 46 ApplicationInfo info = appContext.getPackageManager().getApplication Info( |
31 applicationContext.getPackageName(), PackageManager.GET_META _DATA); | 47 appContext.getPackageName(), PackageManager.GET_META_DATA); |
32 if (info.metaData == null) { | 48 if (info.metaData == null) { |
33 // null means no such tag was found. | 49 // null means no such tag was found. |
34 return false; | 50 return false; |
35 } | 51 } |
36 // getBoolean returns false if the key is not found, which is what w e want. | 52 // getBoolean returns false if the key is not found, which is what w e want. |
37 return info.metaData.getBoolean(OPT_OUT_META_DATA_STR); | 53 return info.metaData.getBoolean(OPT_OUT_META_DATA_STR); |
38 } catch (PackageManager.NameNotFoundException e) { | 54 } catch (PackageManager.NameNotFoundException e) { |
39 // This should never happen. | 55 // This should never happen. |
40 Log.e(TAG, "App could not find itself by package name!"); | 56 Log.e(TAG, "App could not find itself by package name!"); |
41 // The conservative thing is to assume the app HAS opted out. | 57 // The conservative thing is to assume the app HAS opted out. |
42 return true; | 58 return true; |
43 } | 59 } |
44 } | 60 } |
45 | 61 |
46 public AwMetricsServiceClient(Context applicationContext) { | 62 public static void queryMetricsSetting(Context appContext) { |
47 if (isAppOptedOut(applicationContext)) { | 63 ThreadUtils.assertOnUiThread(); |
64 | |
65 // Metrics defaults to off, so only call nativeSetMetricsEnabled if it's true. | |
66 if (isAppOptedOut(appContext)) { | |
48 return; | 67 return; |
49 } | 68 } |
50 | 69 |
51 // Check if the user has consented. | 70 // Check if the user has consented. |
52 PlatformServiceBridge.getInstance(applicationContext) | 71 PlatformServiceBridge.getInstance(appContext) |
53 .setMetricsSettingListener(new ValueCallback<Boolean>() { | 72 .queryMetricsSetting(new ValueCallback<Boolean>() { |
54 public void onReceiveValue(Boolean enabled) { | 73 public void onReceiveValue(Boolean enabled) { |
55 nativeSetMetricsEnabled(enabled); | 74 ThreadUtils.assertOnUiThread(); |
75 if (enabled) { | |
76 sShouldEnable = true; | |
77 if (sIsClientReady) { | |
78 nativeSetMetricsEnabled(true); | |
79 } | |
80 } | |
56 } | 81 } |
57 }); | 82 }); |
58 } | 83 } |
59 | 84 |
85 @CalledByNative | |
86 public static void nativeInitialized() { | |
87 ThreadUtils.assertOnUiThread(); | |
88 sIsClientReady = true; | |
89 if (sShouldEnable) { | |
90 nativeSetMetricsEnabled(true); | |
91 } | |
92 } | |
93 | |
60 public static native void nativeSetMetricsEnabled(boolean enabled); | 94 public static native void nativeSetMetricsEnabled(boolean enabled); |
61 } | 95 } |
OLD | NEW |