From 05e644fb67b85512342ec2a7c52dce8cb73060de Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 13 Apr 2016 15:17:13 -0500 Subject: [PATCH] Bug 1260758 - Add distribution field to Fennec core ping; r=mfinkle,mkaply --- .../java/org/mozilla/gecko/BrowserApp.java | 6 +++- .../gecko/distribution/Distribution.java | 28 +++++++++++++++++++ .../gecko/telemetry/TelemetryConstants.java | 1 + .../telemetry/TelemetryPingGenerator.java | 12 ++++++-- .../telemetry/TelemetryUploadService.java | 5 ++-- mobile/android/base/moz.build | 1 + 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java index 700e1f166f6d..4ceb5a4ed355 100644 --- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java +++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java @@ -24,6 +24,8 @@ import org.mozilla.gecko.db.BrowserContract; import org.mozilla.gecko.db.BrowserDB; import org.mozilla.gecko.db.SuggestedSites; import org.mozilla.gecko.distribution.Distribution; +import org.mozilla.gecko.distribution.Distribution.DistributionDescriptor; +import org.mozilla.gecko.distribution.DistributionStoreCallback; import org.mozilla.gecko.dlc.DownloadContentService; import org.mozilla.gecko.dlc.catalog.DownloadContent; import org.mozilla.gecko.favicons.Favicons; @@ -688,7 +690,9 @@ public class BrowserApp extends GeckoApp // We want to upload the telemetry core ping as soon after startup as possible. It relies on the // Distribution being initialized. If you move this initialization, ensure it plays well with telemetry. - Distribution distribution = Distribution.init(this); + final Distribution distribution = Distribution.init(this); + distribution.addOnDistributionReadyCallback(new DistributionStoreCallback(this, getProfile().getName())); + searchEngineManager = new SearchEngineManager(this, distribution); // Init suggested sites engine in BrowserDB. diff --git a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java index 663a4fcf9d0c..03e7f17f3d5f 100644 --- a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java +++ b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java @@ -352,6 +352,34 @@ public class Distribution { return descFile; } + public DistributionDescriptor getDescriptor() { + File descFile = getDistributionFile("preferences.json"); + if (descFile == null) { + // Logging and existence checks are handled in getDistributionFile. + return null; + } + + try { + JSONObject all = new JSONObject(FileUtils.getFileContents(descFile)); + + if (!all.has("Global")) { + Log.e(LOGTAG, "Distribution preferences.json has no Global entry!"); + return null; + } + + return new DistributionDescriptor(all.getJSONObject("Global")); + + } catch (IOException e) { + Log.e(LOGTAG, "Error getting distribution descriptor file.", e); + Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION); + return null; + } catch (JSONException e) { + Log.e(LOGTAG, "Error parsing preferences.json", e); + Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION); + return null; + } + } + /** * Get the Android preferences from the preferences.json file, if any exist. * @return The preferences in a JSONObject, or an empty JSONObject if no preferences are defined. diff --git a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java index 6c80aa8a976b..7619da2b12af 100644 --- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java +++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryConstants.java @@ -36,6 +36,7 @@ public class TelemetryConstants { public static final String CLIENT_ID = "clientId"; public static final String DEFAULT_SEARCH_ENGINE = "defaultSearch"; public static final String DEVICE = "device"; + public static final String DISTRIBUTION_ID = "distributionId"; public static final String EXPERIMENTS = "experiments"; public static final String LOCALE = "locale"; public static final String OS_ATTR = "os"; diff --git a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java index 5bc1bc83506f..3e55eb5fbf90 100644 --- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java +++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java @@ -65,15 +65,16 @@ public class TelemetryPingGenerator { */ public static TelemetryPing createCorePing(final Context context, final String docId, final String clientId, final String serverURLSchemeHostPort, final int seq, final long profileCreationDateDays, - @Nullable final String defaultSearchEngine) { + @Nullable final String distributionId, @Nullable final String defaultSearchEngine) { final String serverURL = getTelemetryServerURL(docId, serverURLSchemeHostPort, CorePing.NAME); final ExtendedJSONObject payload = - createCorePingPayload(context, clientId, seq, profileCreationDateDays, defaultSearchEngine); + createCorePingPayload(context, clientId, seq, profileCreationDateDays, distributionId, defaultSearchEngine); return new TelemetryPing(serverURL, payload); } private static ExtendedJSONObject createCorePingPayload(final Context context, final String clientId, - final int seq, final long profileCreationDate, @Nullable final String defaultSearchEngine) { + final int seq, final long profileCreationDate, @Nullable final String distributionId, + @Nullable final String defaultSearchEngine) { final ExtendedJSONObject ping = new ExtendedJSONObject(); ping.put(CorePing.VERSION_ATTR, CorePing.VERSION_VALUE); ping.put(CorePing.OS_ATTR, CorePing.OS_VALUE); @@ -93,6 +94,11 @@ public class TelemetryPingGenerator { ping.put(CorePing.SEQ, seq); ping.putArray(CorePing.EXPERIMENTS, Experiments.getActiveExperiments(context)); + // Optional. + if (distributionId != null) { + ping.put(CorePing.DISTRIBUTION_ID, distributionId); + } + // `null` indicates failure more clearly than < 0. final Long finalProfileCreationDate = (profileCreationDate < 0) ? null : profileCreationDate; ping.put(CorePing.PROFILE_CREATION_DATE, finalProfileCreationDate); diff --git a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java index c7099aca22fd..76e21dba1095 100644 --- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java +++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryUploadService.java @@ -16,6 +16,7 @@ import ch.boye.httpclientandroidlib.client.ClientProtocolException; import org.mozilla.gecko.GeckoProfile; import org.mozilla.gecko.GeckoSharedPrefs; import org.mozilla.gecko.background.BackgroundService; +import org.mozilla.gecko.distribution.DistributionStoreCallback; import org.mozilla.gecko.preferences.GeckoPreferences; import org.mozilla.gecko.sync.net.BaseResource; import org.mozilla.gecko.sync.net.BaseResourceDelegate; @@ -183,9 +184,9 @@ public class TelemetryUploadService extends BackgroundService { // TODO (bug 1241685): Sync this preference with the gecko preference. final String serverURLSchemeHostPort = sharedPrefs.getString(TelemetryConstants.PREF_SERVER_URL, TelemetryConstants.DEFAULT_SERVER_URL); - + final String distributionId = sharedPrefs.getString(DistributionStoreCallback.PREF_DISTRIBUTION_ID, null); final TelemetryPing corePing = TelemetryPingGenerator.createCorePing(this, docId, clientId, - serverURLSchemeHostPort, seq, profileCreationDate, defaultSearchEngine); + serverURLSchemeHostPort, seq, profileCreationDate, distributionId, defaultSearchEngine); final CorePingResultDelegate resultDelegate = new CorePingResultDelegate(); uploadPing(corePing, resultDelegate); } diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index 0cad5d36f1df..25975d626d3b 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -245,6 +245,7 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [ 'db/URLMetadataTable.java', 'DevToolsAuthHelper.java', 'distribution/Distribution.java', + 'distribution/DistributionStoreCallback.java', 'distribution/ReferrerDescriptor.java', 'distribution/ReferrerReceiver.java', 'dlc/BaseAction.java',