Bug 1273689 - Rename CorePingDelegate. r=sebastian
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 31 May 2016 15:39:58 -0700
changeset 340921 8644fcfe29da58befd2c3ea28e230570570af925
parent 340920 11a6d5a59addedc2f282e9c4d33cc2884dad00cf
child 340922 7c0dce574b3f2d5e8f6e4e60caab5532f8e13fd6
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1273689
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1273689 - Rename CorePingDelegate. r=sebastian We are doing more than just uploading in the delegate now. I didn't fix this in the previous commits because version control makes this non-trivial. MozReview-Commit-ID: IjXsQC19k2S
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingDelegate.java
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingUploadDelegate.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -72,17 +72,17 @@ import org.mozilla.gecko.sync.repositori
 import org.mozilla.gecko.tabqueue.TabQueueHelper;
 import org.mozilla.gecko.tabqueue.TabQueuePrompt;
 import org.mozilla.gecko.tabs.TabHistoryController;
 import org.mozilla.gecko.tabs.TabHistoryController.OnShowTabHistory;
 import org.mozilla.gecko.tabs.TabHistoryFragment;
 import org.mozilla.gecko.tabs.TabHistoryPage;
 import org.mozilla.gecko.tabs.TabsPanel;
 import org.mozilla.gecko.telemetry.TelemetryUploadService;
-import org.mozilla.gecko.telemetry.TelemetryCorePingUploadDelegate;
+import org.mozilla.gecko.telemetry.TelemetryCorePingDelegate;
 import org.mozilla.gecko.telemetry.measurements.SearchCountMeasurements;
 import org.mozilla.gecko.toolbar.AutocompleteHandler;
 import org.mozilla.gecko.toolbar.BrowserToolbar;
 import org.mozilla.gecko.toolbar.BrowserToolbar.TabEditingState;
 import org.mozilla.gecko.toolbar.ToolbarProgressView;
 import org.mozilla.gecko.trackingprotection.TrackingProtectionPrompt;
 import org.mozilla.gecko.updater.UpdateServiceHelper;
 import org.mozilla.gecko.util.ActivityUtils;
@@ -304,17 +304,17 @@ public class BrowserApp extends GeckoApp
     private final DynamicToolbar mDynamicToolbar = new DynamicToolbar();
 
     private final List<BrowserAppDelegate> delegates = Collections.unmodifiableList(Arrays.asList(
             (BrowserAppDelegate) new AddToHomeScreenPromotion(),
             (BrowserAppDelegate) new ScreenshotDelegate(),
             (BrowserAppDelegate) new BookmarkStateChangeDelegate(),
             (BrowserAppDelegate) new ReaderViewBookmarkPromotion(),
             (BrowserAppDelegate) new ContentNotificationsDelegate(),
-            new TelemetryCorePingUploadDelegate()
+            new TelemetryCorePingDelegate()
     ));
 
     @NonNull
     private SearchEngineManager mSearchEngineManager; // Contains reference to Context - DO NOT LEAK!
 
     private boolean mHasResumed;
 
     @Override
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingDelegate.java
@@ -0,0 +1,178 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.telemetry;
+
+import android.content.SharedPreferences;
+import android.support.annotation.Nullable;
+import android.support.annotation.WorkerThread;
+import android.util.Log;
+import org.mozilla.gecko.BrowserApp;
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.GeckoSharedPrefs;
+import org.mozilla.gecko.delegates.BrowserAppDelegate;
+import org.mozilla.gecko.distribution.DistributionStoreCallback;
+import org.mozilla.gecko.search.SearchEngineManager;
+import org.mozilla.gecko.sync.ExtendedJSONObject;
+import org.mozilla.gecko.telemetry.measurements.SearchCountMeasurements;
+import org.mozilla.gecko.telemetry.measurements.SessionMeasurements;
+import org.mozilla.gecko.telemetry.pingbuilders.TelemetryCorePingBuilder;
+import org.mozilla.gecko.util.StringUtils;
+import org.mozilla.gecko.util.ThreadUtils;
+
+import java.io.IOException;
+import java.lang.ref.WeakReference;
+
+/**
+ * An activity-lifecycle delegate for uploading the core ping.
+ */
+public class TelemetryCorePingDelegate extends BrowserAppDelegate {
+    private static final String LOGTAG = StringUtils.safeSubstring(
+            "Gecko" + TelemetryCorePingDelegate.class.getSimpleName(), 0, 23);
+
+    private static final String PREF_IS_FIRST_RUN = "telemetry-isFirstRun";
+
+    private TelemetryDispatcher telemetryDispatcher; // lazy
+    private final SessionMeasurements sessionMeasurements = new SessionMeasurements();
+
+    @Override
+    public void onStart(final BrowserApp browserApp) {
+        // We don't upload in onCreate because that's only called when the Activity needs to be instantiated
+        // and it's possible the system will never free the Activity from memory.
+        //
+        // We don't upload in onResume/onPause because that will be called each time the Activity is obscured,
+        // including by our own Activities/dialogs, and there is no reason to upload each time we're unobscured.
+        //
+        // We're left with onStart/onStop and we upload in onStart because onStop is not guaranteed to be called
+        // and we want to upload the first run ASAP (e.g. to get install data before the app may crash).
+        uploadPing(browserApp);
+    }
+
+    @Override
+    public void onStop(final BrowserApp browserApp) {
+        // We've decided to upload primarily in onStart (see note there). However, if it's the first run,
+        // it's possible a user used fennec and decided never to return to it again - it'd be great to get
+        // their session information before they decided to give it up so we upload here on first run.
+        //
+        // Caveats:
+        //   * onStop is not guaranteed to be called in low memory conditions so it's possible we won't upload,
+        // but it's better than it was before.
+        //   * Besides first run (because of this call), we can never get the user's *last* session data.
+        //
+        // If we are really interested in the user's last session data, we could consider uploading in onStop
+        // but it's less robust (see discussion in bug 1277091).
+        final SharedPreferences sharedPrefs = getSharedPreferences(browserApp);
+        if (sharedPrefs.getBoolean(PREF_IS_FIRST_RUN, true)) {
+            sharedPrefs.edit()
+                    .putBoolean(PREF_IS_FIRST_RUN, false)
+                    .apply();
+            uploadPing(browserApp);
+        }
+    }
+
+    private void uploadPing(final BrowserApp browserApp) {
+        final SearchEngineManager searchEngineManager = browserApp.getSearchEngineManager();
+        searchEngineManager.getEngine(new UploadTelemetryCorePingCallback(browserApp));
+    }
+
+    @Override
+    public void onResume(BrowserApp browserApp) {
+        sessionMeasurements.recordSessionStart();
+    }
+
+    @Override
+    public void onPause(BrowserApp browserApp) {
+        // onStart/onStop is ideal over onResume/onPause. However, onStop is not guaranteed to be called and
+        // dealing with that possibility adds a lot of complexity that we don't want to handle at this point.
+        sessionMeasurements.recordSessionEnd(browserApp);
+    }
+
+    @WorkerThread // via constructor
+    private TelemetryDispatcher getTelemetryDispatcher(final BrowserApp browserApp) {
+        if (telemetryDispatcher == null) {
+            final String profilePath = browserApp.getProfile().getDir().getAbsolutePath();
+            telemetryDispatcher = new TelemetryDispatcher(profilePath);
+        }
+        return telemetryDispatcher;
+    }
+
+    private SharedPreferences getSharedPreferences(final BrowserApp activity) {
+        return GeckoSharedPrefs.forProfileName(activity, activity.getProfile().getName());
+    }
+
+    private class UploadTelemetryCorePingCallback implements SearchEngineManager.SearchEngineCallback {
+        private final WeakReference<BrowserApp> activityWeakReference;
+
+        private UploadTelemetryCorePingCallback(final BrowserApp activity) {
+            this.activityWeakReference = new WeakReference<>(activity);
+        }
+
+        // May be called from any thread.
+        @Override
+        public void execute(@Nullable final org.mozilla.gecko.search.SearchEngine engine) {
+            // Don't waste resources queueing to the background thread if we don't have a reference.
+            if (this.activityWeakReference.get() == null) {
+                return;
+            }
+
+            // The containing method can be called from onStart: queue this work so that
+            // the first launch of the activity doesn't trigger profile init too early.
+            //
+            // Additionally, getAndIncrementSequenceNumber must be called from a worker thread.
+            ThreadUtils.postToBackgroundThread(new Runnable() {
+                @WorkerThread
+                @Override
+                public void run() {
+                    final BrowserApp activity = activityWeakReference.get();
+                    if (activity == null) {
+                        return;
+                    }
+
+                    final GeckoProfile profile = activity.getProfile();
+                    if (!TelemetryUploadService.isUploadEnabledByProfileConfig(activity, profile)) {
+                        Log.d(LOGTAG, "Core ping upload disabled by profile config. Returning.");
+                        return;
+                    }
+
+                    final String clientID;
+                    try {
+                        clientID = profile.getClientId();
+                    } catch (final IOException e) {
+                        Log.w(LOGTAG, "Unable to get client ID to generate core ping: " + e);
+                        return;
+                    }
+
+                    // Each profile can have different telemetry data so we intentionally grab the shared prefs for the profile.
+                    final SharedPreferences sharedPrefs = getSharedPreferences(activity);
+                    final SessionMeasurements.SessionMeasurementsContainer sessionMeasurementsContainer =
+                            sessionMeasurements.getAndResetSessionMeasurements(activity);
+                    final TelemetryCorePingBuilder pingBuilder = new TelemetryCorePingBuilder(activity)
+                            .setClientID(clientID)
+                            .setDefaultSearchEngine(TelemetryCorePingBuilder.getEngineIdentifier(engine))
+                            .setProfileCreationDate(TelemetryCorePingBuilder.getProfileCreationDate(activity, profile))
+                            .setSequenceNumber(TelemetryCorePingBuilder.getAndIncrementSequenceNumber(sharedPrefs))
+                            .setSessionCount(sessionMeasurementsContainer.sessionCount)
+                            .setSessionDuration(sessionMeasurementsContainer.elapsedSeconds);
+                    maybeSetOptionalMeasurements(sharedPrefs, pingBuilder);
+
+                    getTelemetryDispatcher(activity).queuePingForUpload(activity, pingBuilder);
+                }
+            });
+        }
+
+        private void maybeSetOptionalMeasurements(final SharedPreferences sharedPrefs, final TelemetryCorePingBuilder pingBuilder) {
+            final String distributionId = sharedPrefs.getString(DistributionStoreCallback.PREF_DISTRIBUTION_ID, null);
+            if (distributionId != null) {
+                pingBuilder.setOptDistributionID(distributionId);
+            }
+
+            final ExtendedJSONObject searchCounts = SearchCountMeasurements.getAndZeroSearch(sharedPrefs);
+            if (searchCounts.size() > 0) {
+                pingBuilder.setOptSearchCounts(searchCounts);
+            }
+        }
+    }
+}
deleted file mode 100644
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingUploadDelegate.java
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, you can obtain one at http://mozilla.org/MPL/2.0/.
- */
-
-package org.mozilla.gecko.telemetry;
-
-import android.content.SharedPreferences;
-import android.support.annotation.Nullable;
-import android.support.annotation.WorkerThread;
-import android.util.Log;
-import org.mozilla.gecko.BrowserApp;
-import org.mozilla.gecko.GeckoProfile;
-import org.mozilla.gecko.GeckoSharedPrefs;
-import org.mozilla.gecko.delegates.BrowserAppDelegate;
-import org.mozilla.gecko.distribution.DistributionStoreCallback;
-import org.mozilla.gecko.search.SearchEngineManager;
-import org.mozilla.gecko.sync.ExtendedJSONObject;
-import org.mozilla.gecko.telemetry.measurements.SearchCountMeasurements;
-import org.mozilla.gecko.telemetry.measurements.SessionMeasurements;
-import org.mozilla.gecko.telemetry.pingbuilders.TelemetryCorePingBuilder;
-import org.mozilla.gecko.util.StringUtils;
-import org.mozilla.gecko.util.ThreadUtils;
-
-import java.io.IOException;
-import java.lang.ref.WeakReference;
-
-/**
- * An activity-lifecycle delegate for uploading the core ping.
- */
-public class TelemetryCorePingUploadDelegate extends BrowserAppDelegate {
-    private static final String LOGTAG = StringUtils.safeSubstring(
-            "Gecko" + TelemetryCorePingUploadDelegate.class.getSimpleName(), 0, 23);
-
-    private static final String PREF_IS_FIRST_RUN = "telemetry-isFirstRun";
-
-    private TelemetryDispatcher telemetryDispatcher; // lazy
-    private final SessionMeasurements sessionMeasurements = new SessionMeasurements();
-
-    @Override
-    public void onStart(final BrowserApp browserApp) {
-        // We don't upload in onCreate because that's only called when the Activity needs to be instantiated
-        // and it's possible the system will never free the Activity from memory.
-        //
-        // We don't upload in onResume/onPause because that will be called each time the Activity is obscured,
-        // including by our own Activities/dialogs, and there is no reason to upload each time we're unobscured.
-        //
-        // We're left with onStart/onStop and we upload in onStart because onStop is not guaranteed to be called
-        // and we want to upload the first run ASAP (e.g. to get install data before the app may crash).
-        uploadPing(browserApp);
-    }
-
-    @Override
-    public void onStop(final BrowserApp browserApp) {
-        // We've decided to upload primarily in onStart (see note there). However, if it's the first run,
-        // it's possible a user used fennec and decided never to return to it again - it'd be great to get
-        // their session information before they decided to give it up so we upload here on first run.
-        //
-        // Caveats:
-        //   * onStop is not guaranteed to be called in low memory conditions so it's possible we won't upload,
-        // but it's better than it was before.
-        //   * Besides first run (because of this call), we can never get the user's *last* session data.
-        //
-        // If we are really interested in the user's last session data, we could consider uploading in onStop
-        // but it's less robust (see discussion in bug 1277091).
-        final SharedPreferences sharedPrefs = getSharedPreferences(browserApp);
-        if (sharedPrefs.getBoolean(PREF_IS_FIRST_RUN, true)) {
-            sharedPrefs.edit()
-                    .putBoolean(PREF_IS_FIRST_RUN, false)
-                    .apply();
-            uploadPing(browserApp);
-        }
-    }
-
-    private void uploadPing(final BrowserApp browserApp) {
-        final SearchEngineManager searchEngineManager = browserApp.getSearchEngineManager();
-        searchEngineManager.getEngine(new UploadTelemetryCorePingCallback(browserApp));
-    }
-
-    @Override
-    public void onResume(BrowserApp browserApp) {
-        sessionMeasurements.recordSessionStart();
-    }
-
-    @Override
-    public void onPause(BrowserApp browserApp) {
-        // onStart/onStop is ideal over onResume/onPause. However, onStop is not guaranteed to be called and
-        // dealing with that possibility adds a lot of complexity that we don't want to handle at this point.
-        sessionMeasurements.recordSessionEnd(browserApp);
-    }
-
-    @WorkerThread // via constructor
-    private TelemetryDispatcher getTelemetryDispatcher(final BrowserApp browserApp) {
-        if (telemetryDispatcher == null) {
-            final String profilePath = browserApp.getProfile().getDir().getAbsolutePath();
-            telemetryDispatcher = new TelemetryDispatcher(profilePath);
-        }
-        return telemetryDispatcher;
-    }
-
-    private SharedPreferences getSharedPreferences(final BrowserApp activity) {
-        return GeckoSharedPrefs.forProfileName(activity, activity.getProfile().getName());
-    }
-
-    private class UploadTelemetryCorePingCallback implements SearchEngineManager.SearchEngineCallback {
-        private final WeakReference<BrowserApp> activityWeakReference;
-
-        private UploadTelemetryCorePingCallback(final BrowserApp activity) {
-            this.activityWeakReference = new WeakReference<>(activity);
-        }
-
-        // May be called from any thread.
-        @Override
-        public void execute(@Nullable final org.mozilla.gecko.search.SearchEngine engine) {
-            // Don't waste resources queueing to the background thread if we don't have a reference.
-            if (this.activityWeakReference.get() == null) {
-                return;
-            }
-
-            // The containing method can be called from onStart: queue this work so that
-            // the first launch of the activity doesn't trigger profile init too early.
-            //
-            // Additionally, getAndIncrementSequenceNumber must be called from a worker thread.
-            ThreadUtils.postToBackgroundThread(new Runnable() {
-                @WorkerThread
-                @Override
-                public void run() {
-                    final BrowserApp activity = activityWeakReference.get();
-                    if (activity == null) {
-                        return;
-                    }
-
-                    final GeckoProfile profile = activity.getProfile();
-                    if (!TelemetryUploadService.isUploadEnabledByProfileConfig(activity, profile)) {
-                        Log.d(LOGTAG, "Core ping upload disabled by profile config. Returning.");
-                        return;
-                    }
-
-                    final String clientID;
-                    try {
-                        clientID = profile.getClientId();
-                    } catch (final IOException e) {
-                        Log.w(LOGTAG, "Unable to get client ID to generate core ping: " + e);
-                        return;
-                    }
-
-                    // Each profile can have different telemetry data so we intentionally grab the shared prefs for the profile.
-                    final SharedPreferences sharedPrefs = getSharedPreferences(activity);
-                    final SessionMeasurements.SessionMeasurementsContainer sessionMeasurementsContainer =
-                            sessionMeasurements.getAndResetSessionMeasurements(activity);
-                    final TelemetryCorePingBuilder pingBuilder = new TelemetryCorePingBuilder(activity)
-                            .setClientID(clientID)
-                            .setDefaultSearchEngine(TelemetryCorePingBuilder.getEngineIdentifier(engine))
-                            .setProfileCreationDate(TelemetryCorePingBuilder.getProfileCreationDate(activity, profile))
-                            .setSequenceNumber(TelemetryCorePingBuilder.getAndIncrementSequenceNumber(sharedPrefs))
-                            .setSessionCount(sessionMeasurementsContainer.sessionCount)
-                            .setSessionDuration(sessionMeasurementsContainer.elapsedSeconds);
-                    maybeSetOptionalMeasurements(sharedPrefs, pingBuilder);
-
-                    getTelemetryDispatcher(activity).queuePingForUpload(activity, pingBuilder);
-                }
-            });
-        }
-
-        private void maybeSetOptionalMeasurements(final SharedPreferences sharedPrefs, final TelemetryCorePingBuilder pingBuilder) {
-            final String distributionId = sharedPrefs.getString(DistributionStoreCallback.PREF_DISTRIBUTION_ID, null);
-            if (distributionId != null) {
-                pingBuilder.setOptDistributionID(distributionId);
-            }
-
-            final ExtendedJSONObject searchCounts = SearchCountMeasurements.getAndZeroSearch(sharedPrefs);
-            if (searchCounts.size() > 0) {
-                pingBuilder.setOptSearchCounts(searchCounts);
-            }
-        }
-    }
-}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -584,17 +584,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'telemetry/measurements/SessionMeasurements.java',
     'telemetry/pingbuilders/TelemetryCorePingBuilder.java',
     'telemetry/pingbuilders/TelemetryPingBuilder.java',
     'telemetry/schedulers/TelemetryUploadAllPingsImmediatelyScheduler.java',
     'telemetry/schedulers/TelemetryUploadScheduler.java',
     'telemetry/stores/TelemetryJSONFilePingStore.java',
     'telemetry/stores/TelemetryPingStore.java',
     'telemetry/TelemetryConstants.java',
-    'telemetry/TelemetryCorePingUploadDelegate.java',
+    'telemetry/TelemetryCorePingDelegate.java',
     'telemetry/TelemetryDispatcher.java',
     'telemetry/TelemetryPing.java',
     'telemetry/TelemetryUploadService.java',
     'TelemetryContract.java',
     'text/FloatingActionModeCallback.java',
     'text/FloatingToolbarTextSelection.java',
     'text/TextAction.java',
     'text/TextSelection.java',