Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java

Issue 2611883002: Prepare to call GMS APIs from WebView (Closed)
Patch Set: explicit destructor for style checker Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
11 10
12 import org.chromium.base.Log; 11 import org.chromium.base.Log;
12 import org.chromium.base.ThreadUtils;
13 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNINamespace; 14 import org.chromium.base.annotations.JNINamespace;
14 15
15 /** 16 /**
16 * Java twin of the homonymous C++ class. The Java side is only responsible for 17 * Determines whether metrics should be enabled.
17 * switching metrics on and off. Since the setting is a platform feature, it 18 *
18 * must be obtained through PlatformServiceBridge. 19 * This requires the following steps:
20 * 1) Check the platform's metrics consent setting.
21 * 2) Check if the app has opted out.
22 * 3) Wait for the native AwMetricsServiceClient to call nativeInitialized.
23 * 4) If enabled, inform the native AwMetricsServiceClient via nativeSetMetricsE nabled.
24 *
25 * Step 1 is done asynchronously and the result is passed to setConsentSetting, which does step 2.
26 * This happens in parallel with native AwMetricsServiceClient initialization; e ither
27 * nativeInitialized or setConsentSetting might fire first. Whichever fires seco nd should call
28 * nativeSetMetricsEnabled.
19 */ 29 */
20 @JNINamespace("android_webview") 30 @JNINamespace("android_webview")
21 public class AwMetricsServiceClient { 31 public class AwMetricsServiceClient {
22 private static final String TAG = "AwMetricsServiceCli-"; 32 private static final String TAG = "AwMetricsServiceCli-";
23 33
24 // Individual apps can use this meta-data tag in their manifest to opt out o f metrics 34 // 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 35 // 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"; 36 private static final String OPT_OUT_META_DATA_STR = "android.webkit.WebView. MetricsOptOut";
27 37
28 private static boolean isAppOptedOut(Context applicationContext) { 38 private static boolean sIsClientReady; // Is the native AwMetricsServiceClie nt initialized?
39 private static boolean sShouldEnable; // Have steps 1 and 2 passed?
40
41 private static boolean isAppOptedOut(Context appContext) {
29 try { 42 try {
30 ApplicationInfo info = applicationContext.getPackageManager().getApp licationInfo( 43 ApplicationInfo info = appContext.getPackageManager().getApplication Info(
31 applicationContext.getPackageName(), PackageManager.GET_META _DATA); 44 appContext.getPackageName(), PackageManager.GET_META_DATA);
32 if (info.metaData == null) { 45 if (info.metaData == null) {
33 // null means no such tag was found. 46 // null means no such tag was found.
34 return false; 47 return false;
35 } 48 }
36 // getBoolean returns false if the key is not found, which is what w e want. 49 // 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); 50 return info.metaData.getBoolean(OPT_OUT_META_DATA_STR);
38 } catch (PackageManager.NameNotFoundException e) { 51 } catch (PackageManager.NameNotFoundException e) {
39 // This should never happen. 52 // This should never happen.
40 Log.e(TAG, "App could not find itself by package name!"); 53 Log.e(TAG, "App could not find itself by package name!");
41 // The conservative thing is to assume the app HAS opted out. 54 // The conservative thing is to assume the app HAS opted out.
42 return true; 55 return true;
43 } 56 }
44 } 57 }
45 58
46 public AwMetricsServiceClient(Context applicationContext) { 59 public static void setConsentSetting(Context appContext, boolean userConsent ) {
47 if (isAppOptedOut(applicationContext)) { 60 ThreadUtils.assertOnUiThread();
61
62 if (!userConsent || isAppOptedOut(appContext)) {
63 // Metrics defaults to off, so no need to call nativeSetMetricsEnabl ed(false).
48 return; 64 return;
49 } 65 }
50 66
51 // Check if the user has consented. 67 sShouldEnable = true;
52 PlatformServiceBridge.getInstance(applicationContext) 68 if (sIsClientReady) {
53 .setMetricsSettingListener(new ValueCallback<Boolean>() { 69 nativeSetMetricsEnabled(true);
54 public void onReceiveValue(Boolean enabled) { 70 }
55 nativeSetMetricsEnabled(enabled); 71 }
56 } 72
57 }); 73 @CalledByNative
74 public static void nativeInitialized() {
75 ThreadUtils.assertOnUiThread();
76 sIsClientReady = true;
77 if (sShouldEnable) {
78 nativeSetMetricsEnabled(true);
79 }
58 } 80 }
59 81
60 public static native void nativeSetMetricsEnabled(boolean enabled); 82 public static native void nativeSetMetricsEnabled(boolean enabled);
61 } 83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698