Bug 1452551 - Initialize the Telemetry core in GeckoView. r?esawin,gfritzsche,jchen draft
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 11 Apr 2018 13:11:27 +0200
changeset 784241 2f5cd9ed070f452ee64d6b6a0059975c05f6eaf0
parent 783745 26d6282bdbe03b9a125f302a3e2daeff28a9eddd
push id106889
push userbmo:alessio.placitelli@gmail.com
push dateWed, 18 Apr 2018 10:26:33 +0000
reviewersesawin, gfritzsche, jchen
bugs1452551
milestone61.0a1
Bug 1452551 - Initialize the Telemetry core in GeckoView. r?esawin,gfritzsche,jchen This additionally introduces a new pref (toolkit.telemetry.isGeckoViewMode) to discriminate, at runtime, between Fennec and GV in JavaScript code. Moreover, this disables TelemetryController initialization from content processes, which was left enabled. MozReview-Commit-ID: 7VoDorxAhvD
mobile/android/app/geckoview-prefs.js
mobile/android/components/geckoview/GeckoViewStartup.js
toolkit/components/processsingleton/ContentProcessSingleton.js
toolkit/components/telemetry/TelemetryController.jsm
toolkit/components/telemetry/TelemetryUtils.jsm
toolkit/components/telemetry/geckoview/GeckoViewTelemetryController.jsm
toolkit/components/telemetry/moz.build
--- a/mobile/android/app/geckoview-prefs.js
+++ b/mobile/android/app/geckoview-prefs.js
@@ -4,9 +4,12 @@
 
 #filter substitution
 
 #include mobile.js
 
 pref("privacy.trackingprotection.pbmode.enabled", false);
 pref("dom.ipc.processCount", 1);
 pref("dom.ipc.keepProcessesAlive.web", 1);
-pref("dom.ipc.processPrelaunch.enabled", false);
\ No newline at end of file
+pref("dom.ipc.processPrelaunch.enabled", false);
+
+// Tell Telemetry that we're in GeckoView mode.
+pref("toolkit.telemetry.isGeckoViewMode", true);
--- a/mobile/android/components/geckoview/GeckoViewStartup.js
+++ b/mobile/android/components/geckoview/GeckoViewStartup.js
@@ -1,15 +1,16 @@
 /* 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/. */
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
+  GeckoViewTelemetryController: "resource://gre/modules/GeckoViewTelemetryController.jsm",
   GeckoViewUtils: "resource://gre/modules/GeckoViewUtils.jsm",
   Services: "resource://gre/modules/Services.jsm",
 });
 
 function GeckoViewStartup() {
 }
 
 GeckoViewStartup.prototype = {
@@ -64,15 +65,20 @@ GeckoViewStartup.prototype = {
           module: "resource://gre/modules/ContentPrefServiceParent.jsm",
           init: cpsp => cpsp.alwaysInit(),
           ppmm: [
             "ContentPrefs:FunctionCall",
             "ContentPrefs:AddObserverForName",
             "ContentPrefs:RemoveObserverForName",
           ],
         });
+
+        // This initializes Telemetry for GeckoView only in the parent process.
+        // The Telemetry initialization for the content process is performed in
+        // ContentProcessSingleton.js for consistency with Desktop Telemetry.
+        GeckoViewTelemetryController.setup();
         break;
       }
     }
   },
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([GeckoViewStartup]);
--- a/toolkit/components/processsingleton/ContentProcessSingleton.js
+++ b/toolkit/components/processsingleton/ContentProcessSingleton.js
@@ -2,29 +2,38 @@
  * 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/. */
 
 "use strict";
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
-ChromeUtils.defineModuleGetter(this, "TelemetryController",
-                               "resource://gre/modules/TelemetryController.jsm");
+XPCOMUtils.defineLazyModuleGetters(this, {
+  GeckoViewTelemetryController: "resource://gre/modules/GeckoViewTelemetryController.jsm",
+  TelemetryController: "resource://gre/modules/TelemetryController.jsm",
+});
 
 function ContentProcessSingleton() {}
 ContentProcessSingleton.prototype = {
   classID: Components.ID("{ca2a8470-45c7-11e4-916c-0800200c9a66}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 
   observe(subject, topic, data) {
     switch (topic) {
     case "app-startup": {
       Services.obs.addObserver(this, "xpcom-shutdown");
+      // Initialize Telemetry in the content process: use a different
+      // controller depending on the platform.
+      if (Services.prefs.getBoolPref("toolkit.telemetry.isGeckoViewMode", false)) {
+        GeckoViewTelemetryController.setup();
+        return;
+      }
+      // Initialize Firefox Desktop Telemetry.
       TelemetryController.observe(null, topic, null);
       break;
     }
     case "xpcom-shutdown":
       Services.obs.removeObserver(this, "xpcom-shutdown");
       break;
     }
   },
--- a/toolkit/components/telemetry/TelemetryController.jsm
+++ b/toolkit/components/telemetry/TelemetryController.jsm
@@ -570,32 +570,17 @@ var Impl = {
       Telemetry.canRecordExtended = false;
       return false;
     }
 
     // Configure base Telemetry recording.
     // Unified Telemetry makes it opt-out. If extended Telemetry is enabled, base recording
     // is always on as well.
     if (IS_UNIFIED_TELEMETRY) {
-      // Enable extended Telemetry on pre-release channels and disable it
-      // on Release/ESR.
-      let prereleaseChannels = ["nightly", "aurora", "beta"];
-      if (!AppConstants.MOZILLA_OFFICIAL) {
-        // Turn extended telemetry for local developer builds.
-        prereleaseChannels.push("default");
-      }
-      const isPrereleaseChannel =
-        prereleaseChannels.includes(AppConstants.MOZ_UPDATE_CHANNEL);
-      const isReleaseCandidateOnBeta =
-        AppConstants.MOZ_UPDATE_CHANNEL === "release" &&
-        Services.prefs.getCharPref("app.update.channel", null) === "beta";
-      Telemetry.canRecordBase = true;
-      Telemetry.canRecordExtended = isPrereleaseChannel ||
-        isReleaseCandidateOnBeta ||
-        Services.prefs.getBoolPref(TelemetryUtils.Preferences.OverridePreRelease, false);
+      TelemetryUtils.setTelemetryRecordingFlags();
     } else {
       // We're not on unified Telemetry, stick to the old behaviour for
       // supporting Fennec.
       Telemetry.canRecordBase = Telemetry.canRecordExtended = Utils.isTelemetryEnabled;
     }
 
     this._log.config("enableTelemetryRecording - canRecordBase:" + Telemetry.canRecordBase +
                      ", canRecordExtended: " + Telemetry.canRecordExtended);
--- a/toolkit/components/telemetry/TelemetryUtils.jsm
+++ b/toolkit/components/telemetry/TelemetryUtils.jsm
@@ -4,16 +4,18 @@
 
 "use strict";
 
 var EXPORTED_SYMBOLS = [
   "TelemetryUtils"
 ];
 
 ChromeUtils.import("resource://gre/modules/Services.jsm", this);
+ChromeUtils.defineModuleGetter(this, "AppConstants",
+                               "resource://gre/modules/AppConstants.jsm");
 
 const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
 
 const PREF_TELEMETRY_ENABLED = "toolkit.telemetry.enabled";
 
 const IS_CONTENT_PROCESS = (function() {
   // We cannot use Services.appinfo here because in telemetry xpcshell tests,
   // appinfo is initially unavailable, and becomes available only later on.
@@ -196,10 +198,32 @@ var TelemetryUtils = {
    * or (non-monotonic) Date value if this fails back.
    */
   monotonicNow() {
     try {
       return Services.telemetry.msSinceProcessStart();
     } catch (ex) {
       return Date.now();
     }
-  }
+  },
+
+  /**
+   * Set the Telemetry core recording flag for Unified Telemetry.
+   */
+  setTelemetryRecordingFlags() {
+    // Enable extended Telemetry on pre-release channels and disable it
+    // on Release/ESR.
+    let prereleaseChannels = ["nightly", "aurora", "beta"];
+    if (!AppConstants.MOZILLA_OFFICIAL) {
+      // Turn extended telemetry for local developer builds.
+      prereleaseChannels.push("default");
+    }
+    const isPrereleaseChannel =
+      prereleaseChannels.includes(AppConstants.MOZ_UPDATE_CHANNEL);
+    const isReleaseCandidateOnBeta =
+      AppConstants.MOZ_UPDATE_CHANNEL === "release" &&
+      Services.prefs.getCharPref("app.update.channel", null) === "beta";
+    Services.telemetry.canRecordBase = true;
+    Services.telemetry.canRecordExtended = isPrereleaseChannel ||
+      isReleaseCandidateOnBeta ||
+      Services.prefs.getBoolPref(this.Preferences.OverridePreRelease, false);
+  },
 };
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/geckoview/GeckoViewTelemetryController.jsm
@@ -0,0 +1,32 @@
+/* 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/. */
+
+"use strict";
+
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+ChromeUtils.import("resource://gre/modules/GeckoViewUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  Services: "resource://gre/modules/Services.jsm",
+  TelemetryUtils: "resource://gre/modules/TelemetryUtils.jsm",
+});
+
+GeckoViewUtils.initLogging("GeckoView.TelemetryController", this);
+
+var EXPORTED_SYMBOLS = ["GeckoViewTelemetryController"];
+
+let GeckoViewTelemetryController = {
+  /**
+   * Setup the Telemetry recording flags. This must be called
+   * in all the processes that need to collect Telemetry.
+   */
+  setup() {
+    debug `setup`;
+
+    TelemetryUtils.setTelemetryRecordingFlags();
+
+    debug `setup - canRecordPrereleaseData ${Services.telemetry.canRecordPrereleaseData
+          }, canRecordReleaseData ${Services.telemetry.canRecordReleaseData}`;
+  },
+};
--- a/toolkit/components/telemetry/moz.build
+++ b/toolkit/components/telemetry/moz.build
@@ -178,10 +178,19 @@ processes_files = [
 processes_enum = GENERATED_FILES['TelemetryProcessEnums.h']
 processes_enum.script = 'gen_process_enum.py'
 processes_enum.inputs = processes_files
 
 processes_data = GENERATED_FILES['TelemetryProcessData.h']
 processes_data.script = 'gen_process_data.py'
 processes_data.inputs = processes_files
 
+# Add support for GeckoView: please note that building GeckoView
+# implies having an Android build. The packaging step decides
+# which files to include. As a consequence, we can simply only
+# include the GeckoView files on all Android builds.
+if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
+    EXTRA_JS_MODULES += [
+        'geckoview/GeckoViewTelemetryController.jsm',
+    ]
+
 with Files('**'):
     BUG_COMPONENT = ('Toolkit', 'Telemetry')