Bug 807217 - Disable Social in private browsing mode. r=gavin,felipe a=lsblakk
authorShane Caraveo <scaraveo@mozilla.com>
Tue, 06 Nov 2012 12:50:08 -0800
changeset 109785 975a3df77b9baf17419bc56202164413b9bc7045
parent 109784 e449c8cd821a7c5511f9d364e0643841d187d1ce
child 109786 036da70b61f9b90ba7a5aaca4a844a0d78e5a3b7
child 109788 70627adcd75fbf140e80e708836548653d52135e
child 109790 25b230afe8216153c6ba80a48fd6752bb7d1e2d0
push id1671
push userfelipc@gmail.com
push dateWed, 07 Nov 2012 01:05:53 +0000
treeherdermozilla-beta@975a3df77b9b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin, felipe, lsblakk
bugs807217
milestone17.0
Bug 807217 - Disable Social in private browsing mode. r=gavin,felipe a=lsblakk
browser/base/content/test/Makefile.in
browser/base/content/test/browser_social.js
browser/modules/Social.jsm
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -258,16 +258,17 @@ endif
                  authenticate.sjs \
                  browser_minimize.js \
                  browser_aboutSyncProgress.js \
                  browser_middleMouse_inherit.js \
                  redirect_bug623155.sjs \
                  browser_tabDrop.js \
                  browser_lastAccessedTab.js \
                  browser_bug734076.js \
+                 browser_social.js \
                  browser_social_toolbar.js \
                  browser_social_shareButton.js \
                  browser_social_sidebar.js \
                  browser_social_flyout.js \
                  browser_social_mozSocial_API.js \
                  browser_social_isVisible.js \
                  browser_social_chatwindow.js \
                  social_panel.html \
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_social.js
@@ -0,0 +1,92 @@
+/* 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/. */
+
+// a place for miscellaneous social tests
+
+
+const pb = Cc["@mozilla.org/privatebrowsing;1"].
+           getService(Ci.nsIPrivateBrowsingService);
+
+
+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",
+    workerURL: "https://example.com/browser/browser/base/content/test/social_worker.js",
+    iconURL: "https://example.com/browser/browser/base/content/test/moz.png"
+  };
+  runSocialTestWithProvider(manifest, function (finishcb) {
+    runSocialTests(tests, undefined, undefined, finishcb);
+  });
+}
+
+var tests = {
+  testPrivateBrowsing: function(next) {
+    let port = Social.provider.getWorkerPort();
+    ok(port, "provider has a port");
+    port.postMessage({topic: "test-init"});
+    port.onmessage = function (e) {
+      let topic = e.data.topic;
+      switch (topic) {
+        case "got-sidebar-message":
+          ok(true, "got sidebar message");
+          port.close();
+          togglePrivateBrowsing(function () {
+            ok(!Social.enabled, "Social shuts down during private browsing");
+            togglePrivateBrowsing(function () {
+              ok(Social.enabled, "Social enabled after private browsing");
+              next();
+            });
+          });
+          break;
+      }
+    };
+  },
+
+  testPrivateBrowsingSocialDisabled: function(next) {
+    // test PB from the perspective of entering PB without social enabled
+    // we expect social to be enabled at the start of the test, we need
+    // to disable it before testing PB transitions.
+    let port = Social.provider.getWorkerPort();
+    ok(port, "provider has a port");
+    port.postMessage({topic: "test-init"});
+    port.onmessage = function (e) {
+      let topic = e.data.topic;
+      switch (topic) {
+        case "got-sidebar-message":
+          ok(true, "got sidebar message");
+          port.close();
+          Social.enabled = false;
+          break;
+      }
+    }
+
+    // wait for disable, then do some pb toggling. We expect social to remain
+    // 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
+          next();
+        });
+      });
+    }, "social:pref-changed", false);
+  }
+}
+
+function togglePrivateBrowsing(aCallback) {
+  Services.obs.addObserver(function observe(subject, topic, data) {
+    Services.obs.removeObserver(observe, topic);
+    executeSoon(aCallback);
+  }, "private-browsing-transition-complete", false);
+
+  pb.privateBrowsingEnabled = !pb.privateBrowsingEnabled;
+}
--- a/browser/modules/Social.jsm
+++ b/browser/modules/Social.jsm
@@ -23,25 +23,41 @@ let Social = {
   init: function Social_init(callback) {
     this._disabledForSafeMode = Services.appinfo.inSafeMode && this.enabled;
 
     if (this.provider) {
       schedule(callback);
       return;
     }
 
+    if (!this._addedPrivateBrowsingObserver) {
+      Services.obs.addObserver(this, "private-browsing", false);
+      this._addedPrivateBrowsingObserver = true;
+    }
+
     // Eventually this might want to retrieve a specific provider, but for now
     // just use the first available.
     SocialService.getProviderList(function (providers) {
       if (providers.length)
         this.provider = providers[0];
       callback();
     }.bind(this));
   },
 
+  observe: function(aSubject, aTopic, aData) {
+    if (aTopic == "private-browsing") {
+      if (aData == "enter") {
+        this._enabledBeforePrivateBrowsing = this.enabled;
+        this.enabled = false;
+      } else if (aData == "exit") {
+        this.enabled = this._enabledBeforePrivateBrowsing;
+      }
+    }
+  },
+
   get uiVisible() {
     return this.provider && this.provider.enabled;
   },
 
   set enabled(val) {
     if (!val) {
       delete this.errorState;
     }