Bug 1490627 - browser-captivePortal.js leaks cps variable into the browser window's global scope. r=nhnt11
authorDão Gottwald <dao@mozilla.com>
Wed, 12 Sep 2018 12:37:37 +0200
changeset 435965 6c2e6f132f461b611d3927867591395ad2ba7047
parent 435964 1ef9297762c17121c7f92c45532d72aad23776a9
child 435966 357d1e66b368c5321c8f20ae70fb103f939680de
push id34625
push userdvarga@mozilla.com
push dateThu, 13 Sep 2018 02:31:40 +0000
treeherdermozilla-central@51e9e9660b3e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnhnt11
bugs1490627
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 1490627 - browser-captivePortal.js leaks cps variable into the browser window's global scope. r=nhnt11
browser/base/content/browser-captivePortal.js
--- a/browser/base/content/browser-captivePortal.js
+++ b/browser/base/content/browser-captivePortal.js
@@ -1,16 +1,12 @@
 /* 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/. */
 
-XPCOMUtils.defineLazyServiceGetter(this, "cps",
-                                   "@mozilla.org/network/captive-portal-service;1",
-                                   "nsICaptivePortalService");
-
 var CaptivePortalWatcher = {
   // This is the value used to identify the captive portal notification.
   PORTAL_NOTIFICATION_VALUE: "captive-portal-detected",
 
   // This holds a weak reference to the captive portal tab so that we
   // don't leak it if the user closes it.
   _captivePortalTab: null,
 
@@ -41,25 +37,28 @@ var CaptivePortalWatcher = {
       Services.strings.createBundle("chrome://browser/locale/browser.properties");
   },
 
   init() {
     Services.obs.addObserver(this, "captive-portal-login");
     Services.obs.addObserver(this, "captive-portal-login-abort");
     Services.obs.addObserver(this, "captive-portal-login-success");
 
-    if (cps.state == cps.LOCKED_PORTAL) {
+    this._cps = Cc["@mozilla.org/network/captive-portal-service;1"]
+                  .getService(Ci.nsICaptivePortalService);
+
+    if (this._cps.state == this._cps.LOCKED_PORTAL) {
       // A captive portal has already been detected.
       this._captivePortalDetected();
 
       // Automatically open a captive portal tab if there's no other browser window.
       if (BrowserWindowTracker.windowCount == 1) {
         this.ensureCaptivePortalTab();
       }
-    } else if (cps.state == cps.UNKNOWN) {
+    } else if (this._cps.state == this._cps.UNKNOWN) {
       // We trigger a portal check after delayed startup to avoid doing a network
       // request before first paint.
       this._delayedRecheckPending = true;
     }
 
     // This constant is chosen to be large enough for a portal recheck to complete,
     // and small enough that the delay in opening a tab isn't too noticeable.
     // Please see comments for _delayedCaptivePortalDetected for more details.
@@ -73,17 +72,17 @@ var CaptivePortalWatcher = {
     Services.obs.removeObserver(this, "captive-portal-login-success");
 
     this._cancelDelayedCaptivePortal();
   },
 
   delayedStartup() {
     if (this._delayedRecheckPending) {
       delete this._delayedRecheckPending;
-      cps.recheckCaptivePortal();
+      this._cps.recheckCaptivePortal();
     }
   },
 
   observe(aSubject, aTopic, aData) {
     switch (aTopic) {
       case "captive-portal-login":
         this._captivePortalDetected();
         break;
@@ -138,37 +137,37 @@ var CaptivePortalWatcher = {
     if (window.document.documentElement.getAttribute("ignorecaptiveportal")) {
       return;
     }
 
     Services.obs.notifyObservers(null, "delayed-captive-portal-handled");
 
     // Trigger a portal recheck. The user may have logged into the portal via
     // another client, or changed networks.
-    cps.recheckCaptivePortal();
+    this._cps.recheckCaptivePortal();
     this._waitingForRecheck = true;
     let requestTime = Date.now();
 
-    let self = this;
-    Services.obs.addObserver(function observer() {
+    let observer = () => {
       let time = Date.now() - requestTime;
       Services.obs.removeObserver(observer, "captive-portal-check-complete");
-      self._waitingForRecheck = false;
-      if (cps.state != cps.LOCKED_PORTAL) {
+      this._waitingForRecheck = false;
+      if (this._cps.state != this._cps.LOCKED_PORTAL) {
         // We're free of the portal!
         return;
       }
 
-      if (time <= self.PORTAL_RECHECK_DELAY_MS) {
+      if (time <= this.PORTAL_RECHECK_DELAY_MS) {
         // The amount of time elapsed since we requested a recheck (i.e. since
         // the browser window was focused) was small enough that we can add and
         // focus a tab with the login page with no noticeable delay.
-        self.ensureCaptivePortalTab();
+        this.ensureCaptivePortalTab();
       }
-    }, "captive-portal-check-complete");
+    };
+    Services.obs.addObserver(observer, "captive-portal-check-complete");
   },
 
   _captivePortalGone() {
     this._cancelDelayedCaptivePortal();
     this._removeNotification();
   },
 
   _cancelDelayedCaptivePortal() {