Bug 815042 - leaving private browsing mode with social enabled now resets all social components. r=gavin
authorMark Hammond <mhammond@skippinet.com.au>
Wed, 28 Nov 2012 11:30:09 +1100
changeset 114311 db5b030c100a1a92dbe74cf1fc0d8579b6187259
parent 114310 d9634ee8c1cc2aeedecef274a4d3d36585da886d
child 114312 3f225d53e909d8820c308bf42fc0e07f8d68d2f4
push id23913
push useremorley@mozilla.com
push dateWed, 28 Nov 2012 17:11:31 +0000
treeherdermozilla-central@17c267a881cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs815042
milestone20.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 815042 - leaving private browsing mode with social enabled now resets all social components. r=gavin
browser/base/content/test/browser_social.js
browser/modules/Social.jsm
--- a/browser/base/content/test/browser_social.js
+++ b/browser/base/content/test/browser_social.js
@@ -3,16 +3,31 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // a place for miscellaneous social tests
 
 
 const pb = Cc["@mozilla.org/privatebrowsing;1"].
            getService(Ci.nsIPrivateBrowsingService);
 
+function waitForPortMessage(port, topic, callback) {
+  port.onmessage = function(evt) {
+    if (evt.data.topic == topic)
+      callback(evt.data);
+  }
+}
+
+function portClosed(port) {
+  try {
+    port.postMessage({topic: "ping"});
+    return false; // worked - port can't be closed!
+  } catch (ex) {
+    return true;
+  }
+}
 
 function test() {
   waitForExplicitFinish();
 
   let manifest = { // normal provider
     name: "provider 1",
     origin: "https://example.com",
     sidebarURL: "https://example.com/browser/browser/base/content/test/social_sidebar.html",
@@ -69,22 +84,60 @@ var tests = {
     // disabled through these tests
     Services.obs.addObserver(function observer(aSubject, aTopic) {
       Services.obs.removeObserver(observer, aTopic);
       ok(!Social.enabled, "Social is not enabled");
       togglePrivateBrowsing(function () {
         ok(!Social.enabled, "Social not available during private browsing");
         togglePrivateBrowsing(function () {
           ok(!Social.enabled, "Social is not enabled after private browsing");
-          // social will be reenabled on start of next social test
+          // reenable social for next social test
+          Social.enabled = true;
           next();
         });
       });
     }, "social:pref-changed", false);
-  }
+  },
+
+  testPrivateBrowsingExitReloads: function(next) {
+    let port = Social.provider.getWorkerPort();
+    waitForPortMessage(port, "got-sidebar-message", function(data) {
+      ok(!portClosed(port), "port not closed before PB transition");
+      togglePrivateBrowsing(function () {
+        ok(!Social.enabled, "Social shuts down during private browsing");
+        // check the port we had before is dead.
+        ok(portClosed(port), "port closed after PB transition");
+        // enable it and stick a value in the window object.
+        Social.enabled = true;
+        port = Social.provider.getWorkerPort();
+        waitForPortMessage(port, "got-sidebar-message", function(data) {
+          // now just stuff a value in the sidebar - it should end
+          // up being removed when we leave PB mode via the sidebar
+          // being reloaded.
+          let sbw = document.getElementById("social-sidebar-browser").contentWindow;
+          sbw.wrappedJSObject.foo = "bar";
+          // Now toggle PB mode back to off.
+          togglePrivateBrowsing(function () {
+            ok(Social.enabled, "Social still enabled after leaving private browsing");
+            ok(portClosed(port), "port closed after PB transition");
+            port = Social.provider.getWorkerPort();
+            waitForPortMessage(port, "got-sidebar-message", function() {
+              sbw = document.getElementById("social-sidebar-browser").contentWindow;
+              is(sbw.wrappedJSObject.foo, undefined, "should have lost window variable when exiting")
+              next();
+            });
+            port.postMessage({topic: "test-init"});
+          });
+        });
+        port.postMessage({topic: "test-init"});
+      });
+    });
+    port.postMessage({topic: "test-init"});
+  },
+
 }
 
 function togglePrivateBrowsing(aCallback) {
   Services.obs.addObserver(function observe(subject, topic, data) {
     Services.obs.removeObserver(observe, topic);
     executeSoon(aCallback);
   }, "private-browsing-transition-complete", false);
 
--- a/browser/modules/Social.jsm
+++ b/browser/modules/Social.jsm
@@ -43,16 +43,22 @@ this.Social = {
   },
 
   observe: function(aSubject, aTopic, aData) {
     if (aTopic == "private-browsing") {
       if (aData == "enter") {
         this._enabledBeforePrivateBrowsing = this.enabled;
         this.enabled = false;
       } else if (aData == "exit") {
+        // if the user has explicitly re-enabled social in PB mode, then upon
+        // leaving we want to tear the world down then reenable to prevent
+        // information leaks during this transition.
+        // The next 2 lines rely on the fact that setting this.enabled to
+        // its current value doesn't actually do anything...
+        this.enabled = false;
         this.enabled = this._enabledBeforePrivateBrowsing;
       }
     }
   },
 
   get uiVisible() {
     return this.provider && this.provider.enabled;
   },