Bug 1494896 - Make hybrid content telemetry init reject on older versions of Frefox. r=chutten
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Fri, 05 Oct 2018 18:48:24 +0000
changeset 498392 6a61b59ee9931e5868320c6fdda7a0f677c9b71a
parent 498391 4dbc5f131040f3df6ac99dd42574fa4c8d7a00bd
child 498393 935a286582d0bfe2e4f3d877dd8b6d9081590382
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1494896
milestone64.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 1494896 - Make hybrid content telemetry init reject on older versions of Frefox. r=chutten This patch enables the HCT init promise to reject even for Firefox versions (before 63) that do not have the required chrome code to communicate back to the library the lack of privileges or that HCT is disabled. It introduces a timeout that rejects the promise after 3 seconds. Differential Revision: https://phabricator.services.mozilla.com/D7693
toolkit/components/telemetry/hybrid-content/HybridContentTelemetry-lib.js
--- a/toolkit/components/telemetry/hybrid-content/HybridContentTelemetry-lib.js
+++ b/toolkit/components/telemetry/hybrid-content/HybridContentTelemetry-lib.js
@@ -45,17 +45,17 @@ if (typeof Mozilla == "undefined") {
 
   /**
    * This internal function is used to register the policy handler. This is
    * needed by pages that do not want to use Telemetry but still need to
    * respect user Privacy choices.
    */
   function _registerInternalPolicyHandler() {
     // Create a promise to wait on for HCT to be completely initialized.
-    _initPromise = new Promise(function(resolveInit, rejectInit) {
+    var setupPromise = new Promise(function(resolveInit, rejectInit) {
       // Register the handler that will update the policy boolean.
       function policyChangeHandler(updatedPref) {
         if (!("detail" in updatedPref) ||
             !("canUpload" in updatedPref.detail) ||
             typeof updatedPref.detail.canUpload != "boolean") {
           return;
         }
         _canUpload = updatedPref.detail.canUpload;
@@ -64,16 +64,29 @@ if (typeof Mozilla == "undefined") {
         resolveInit();
       }
       document.addEventListener("mozTelemetryPolicyChange", policyChangeHandler);
       document.addEventListener("mozTelemetryUntrustedOrigin",
                                 () => rejectInit(new Error("Origin not trusted or HCT disabled.")),
                                 {once: true});
     });
 
+    // In bug 1490284 we introduced a mechanism for which the HCT library rejects if the
+    // requesting website has not enough privileges or telemetry is disabled. Unfortunately,
+    // this required a change in chrome JSMs and is not available to Firefox versions
+    // before 63. To reject on these versions as well, a fallback timeout is provided that
+    // automatically rejects the init promise after 3 seconds.
+    var timeoutPromise = new Promise((resolve, reject) => {
+      setTimeout(reject, 3000);
+    });
+
+    // Resolves or rejects as soon as one of the promises resolves or rejects: the
+    // rejection timeout has no effect if |setupPromise| resolves before it.
+    _initPromise = Promise.race([setupPromise, timeoutPromise]);
+
     // Make sure the chrome is initialized.
     _sendMessageToChrome("init");
   }
 
   Mozilla.ContentTelemetry.canUpload = function() {
     return _canUpload;
   };