Bug 1226322 - (WIP) Add 'restricted' field to core telemetry ping. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 10 Feb 2016 17:09:48 -0800
changeset 330203 01f1bffb515667f7732b6601cbb49aaf03465a93
parent 329332 103f2dcf2778d0c22db916e96a962bc0d4eb7ac0
child 514132 e267da8c68feed269878c1fe10edf232e9bfe6d2
push id10712
push usermichael.l.comella@gmail.com
push dateThu, 11 Feb 2016 01:23:21 +0000
reviewerssebastian
bugs1226322, 1244295
milestone47.0a1
Bug 1226322 - (WIP) Add 'restricted' field to core telemetry ping. r=sebastian This patch seems simple enough to work but I can't generate a client ID on this new profile (see bug 1244295) which prevents us from uploading the telemetry. Data choices are disabled in the restricted profile so I presume we don't upload telemetry via gecko and thus I assume this client ID will never be created without bug 1244295. Still TODO: * Update core ping docs * If we're sending core pings from this profile, we should probably give it an opt-out data choice. * Probably fix bug 1244295 first MozReview-Commit-ID: K4VWe4h3uzU
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -3941,16 +3941,17 @@ public class BrowserApp extends GeckoApp
         }
 
         final SharedPreferences sharedPrefs = GeckoSharedPrefs.forProfileName(this, profile.getName());
         final int seq = sharedPrefs.getInt(TelemetryConstants.PREF_SEQ_COUNT, 1);
 
         final Intent i = new Intent(TelemetryConstants.ACTION_UPLOAD_CORE);
         i.setClass(this, TelemetryUploadService.class);
         i.putExtra(TelemetryConstants.EXTRA_DOC_ID, UUID.randomUUID().toString());
+        i.putExtra(TelemetryConstants.EXTRA_IS_RESTRICTED_PROFILE, Restrictions.isRestrictedProfile(this));
         i.putExtra(TelemetryConstants.EXTRA_PROFILE_NAME, profile.getName());
         i.putExtra(TelemetryConstants.EXTRA_PROFILE_PATH, profile.getDir().toString());
         i.putExtra(TelemetryConstants.EXTRA_SEQ, seq);
         startService(i);
 
         // Intent redelivery will ensure this value gets used - see TelemetryUploadService class comments for details.
         sharedPrefs.edit().putInt(TelemetryConstants.PREF_SEQ_COUNT, seq + 1).apply();
     }
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java
@@ -12,16 +12,17 @@ public class TelemetryConstants {
     public static final boolean UPLOAD_ENABLED = AppConstants.MOZILLA_OFFICIAL; // Disabled for developer builds.
     public static final String DEFAULT_SERVER_URL = "https://incoming.telemetry.mozilla.org";
 
     public static final String USER_AGENT =
             "Firefox-Android-Telemetry/" + AppConstants.MOZ_APP_VERSION + " (" + AppConstants.MOZ_APP_UA_NAME + ")";
 
     public static final String ACTION_UPLOAD_CORE = "uploadCore";
     public static final String EXTRA_DOC_ID = "docId";
+    public static final String EXTRA_IS_RESTRICTED_PROFILE = "isRestrictedProfile";
     public static final String EXTRA_PROFILE_NAME = "geckoProfileName";
     public static final String EXTRA_PROFILE_PATH = "geckoProfilePath";
     public static final String EXTRA_SEQ = "seq";
 
     public static final String PREF_SERVER_URL = "telemetry-serverUrl";
     public static final String PREF_SEQ_COUNT = "telemetry-seqCount";
 
     public static class CorePing {
@@ -30,15 +31,16 @@ public class TelemetryConstants {
         public static final String NAME = "core";
         public static final int VERSION_VALUE = 1;
         public static final String OS_VALUE = "Android";
 
         public static final String ARCHITECTURE = "arch";
         public static final String CLIENT_ID = "clientId";
         public static final String DEVICE = "device";
         public static final String EXPERIMENTS = "experiments";
+        public static final String IS_RESTRICTED_PROFILE = "restricted";
         public static final String LOCALE = "locale";
         public static final String OS_ATTR = "os";
         public static final String OS_VERSION = "osversion";
         public static final String SEQ = "seq";
         public static final String VERSION_ATTR = "v";
     }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
@@ -56,24 +56,24 @@ public class TelemetryPingGenerator {
 
     /**
      * @param docId A unique document ID for the ping associated with the upload to this server
      * @param clientId The client ID of this profile (from Gecko)
      * @param serverURLSchemeHostPort The server url with the scheme, host, and port (e.g. "http://mozilla.org:80")
      * @throws IOException when client ID could not be created
      */
     public static TelemetryPing createCorePing(final Context context, final String docId, final String clientId,
-            final String serverURLSchemeHostPort, final int seq) {
+            final String serverURLSchemeHostPort, final int seq, final boolean isRestrictedProfile) {
         final String serverURL = getTelemetryServerURL(docId, serverURLSchemeHostPort, CorePing.NAME);
-        final ExtendedJSONObject payload = createCorePingPayload(context, clientId, seq);
+        final ExtendedJSONObject payload = createCorePingPayload(context, clientId, seq, isRestrictedProfile);
         return new TelemetryPing(serverURL, payload);
     }
 
     private static ExtendedJSONObject createCorePingPayload(final Context context, final String clientId,
-            final int seq) {
+            final int seq, final boolean isRestrictedProfile) {
         final ExtendedJSONObject ping = new ExtendedJSONObject();
         ping.put(CorePing.VERSION_ATTR, CorePing.VERSION_VALUE);
         ping.put(CorePing.OS_ATTR, CorePing.OS_VALUE);
 
         // We limit the device descriptor to 32 characters because it can get long. We give fewer characters to the
         // manufacturer because we're less likely to have manufacturers with similar names than we are for a
         // manufacturer to have two devices with the similar names (e.g. Galaxy S6 vs. Galaxy Note 6).
         final String deviceDescriptor =
@@ -83,16 +83,19 @@ public class TelemetryPingGenerator {
         ping.put(CorePing.CLIENT_ID, clientId);
         ping.put(CorePing.DEVICE, deviceDescriptor);
         ping.put(CorePing.LOCALE, Locales.getLanguageTag(Locale.getDefault()));
         ping.put(CorePing.OS_VERSION, Integer.toString(Build.VERSION.SDK_INT)); // A String for cross-platform reasons.
         ping.put(CorePing.SEQ, seq);
         if (AppConstants.MOZ_SWITCHBOARD) {
             ping.put(CorePing.EXPERIMENTS, getActiveExperiments(context));
         }
+        if (isRestrictedProfile) { // Optional: if omitted, assumed false. Thus only added if it's true to save data.
+            ping.put(CorePing.IS_RESTRICTED_PROFILE, isRestrictedProfile);
+        }
         return ping;
     }
 
     private static JSONArray getActiveExperiments(final Context context) {
         if (!AppConstants.MOZ_SWITCHBOARD) {
             throw new IllegalStateException("This method should not be called with switchboard disabled");
         }
         return new JSONArray(SwitchBoard.getActiveExperiments(context));
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java
@@ -74,22 +74,23 @@ public class TelemetryUploadService exte
         }
 
         if (!TelemetryConstants.ACTION_UPLOAD_CORE.equals(intent.getAction())) {
             Log.w(LOGTAG, "Unknown action: " + intent.getAction() + ". Returning");
             return;
         }
 
         final String docId = intent.getStringExtra(TelemetryConstants.EXTRA_DOC_ID);
+        final boolean isRestrictedProfile = intent.getBooleanExtra(TelemetryConstants.EXTRA_IS_RESTRICTED_PROFILE, false);
         final int seq = intent.getIntExtra(TelemetryConstants.EXTRA_SEQ, -1);
 
         final String profileName = intent.getStringExtra(TelemetryConstants.EXTRA_PROFILE_NAME);
         final String profilePath = intent.getStringExtra(TelemetryConstants.EXTRA_PROFILE_PATH);
 
-        uploadCorePing(docId, seq, profileName, profilePath);
+        uploadCorePing(docId, seq, profileName, profilePath, isRestrictedProfile);
     }
 
     /**
      * Determines if the telemetry upload feature is enabled via the application configuration. Prefer to use
      * {@link #isUploadEnabledByProfileConfig(Context, GeckoProfile)} if the profile is available as it takes into
      * account more information.
      *
      * Note that this method logs debug statements when upload is disabled.
@@ -156,17 +157,17 @@ public class TelemetryUploadService exte
             Log.d(LOGTAG, "Received invalid profile path in Intent");
             return false;
         }
 
         return true;
     }
 
     private void uploadCorePing(@NonNull final String docId, final int seq, @NonNull final String profileName,
-                @NonNull final String profilePath) {
+                @NonNull final String profilePath, final boolean isRestrictedProfile) {
         final GeckoProfile profile = GeckoProfile.get(this, profileName, profilePath);
 
         final String clientId;
         try {
             clientId = profile.getClientId();
         } catch (final IOException e) {
             // Don't log the exception to avoid leaking the profile path.
             Log.w(LOGTAG, "Unable to get client ID to generate core ping: returning.");
@@ -174,18 +175,18 @@ public class TelemetryUploadService exte
         }
 
         // Each profile can have different telemetry data so we intentionally grab the shared prefs for the profile.
         final SharedPreferences sharedPrefs = GeckoSharedPrefs.forProfileName(this, profileName);
         // TODO (bug 1241685): Sync this preference with the gecko preference.
         final String serverURLSchemeHostPort =
                 sharedPrefs.getString(TelemetryConstants.PREF_SERVER_URL, TelemetryConstants.DEFAULT_SERVER_URL);
 
-        final TelemetryPing corePing =
-                TelemetryPingGenerator.createCorePing(this, docId, clientId, serverURLSchemeHostPort, seq);
+        final TelemetryPing corePing = TelemetryPingGenerator.createCorePing(this, docId, clientId,
+                serverURLSchemeHostPort, seq, isRestrictedProfile);
         final CorePingResultDelegate resultDelegate = new CorePingResultDelegate();
         uploadPing(corePing, resultDelegate);
     }
 
     private void uploadPing(final TelemetryPing ping, final ResultDelegate delegate) {
         final BaseResource resource;
         try {
             resource = new BaseResource(ping.getURL());