[Pkg-mozext-commits] [nosquint] 02/13: Support per-window private browsing in Firefox 20

David Prévot taffit at moszumanska.debian.org
Tue Apr 28 01:41:23 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to annotated tag 2.1.7
in repository nosquint.

commit 3f7c032414627d187b245864273dd224773015fe
Author: Jason Tackaberry <tack at urandom.ca>
Date:   Sun Mar 31 19:45:13 2013 -0400

    Support per-window private browsing in Firefox 20
    
    This change also removes the old-style global private browsing mode,
    which means NoSquint is no longer compatible with Firefox versions
    before 20.
    
    Version is also bumped to 2.1.7 in this change.
---
 src/content/browser.js               | 32 ++--------------
 src/content/dlg-site.js              | 20 +++++-----
 src/content/dlg-site.xul             |  1 -
 src/content/interfaces.js            | 66 ++++++++------------------------
 src/content/lib.js                   | 16 +++++++-
 src/content/prefs.js                 | 73 +++++++++++++++++++++++-------------
 src/install.rdf                      |  6 +--
 src/locale/en-US/dlg-site.properties |  2 +-
 8 files changed, 94 insertions(+), 122 deletions(-)

diff --git a/src/content/browser.js b/src/content/browser.js
index 7da2350..b253971 100644
--- a/src/content/browser.js
+++ b/src/content/browser.js
@@ -14,39 +14,13 @@ NoSquint.browser = NoSquint.ns(function() { with (NoSquint) {
 
     this.init = function() {
         this.gBrowser = gBrowser;
+        this.isPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
+        this.prefs = NSQ.prefs;
         this.updateZoomMenu();
 
         this.observer = new NSQ.interfaces.Observer();
-        this.observer.watcher = {
-            onEnterPrivateBrowsing: function() {
-                this.closeSiteSettings();
-                // Switching the private browsing mode.  Store any current pending
-                // changes now.
-                NSQ.prefs.saveSiteList(true);
-                // Save current (non-private) site data for when we exit private
-                // browsing.
-                this.origSites = NSQ.prefs.cloneSites();
-            },
-
-            onExitPrivateBrowsing: function() {
-                this.closeSiteSettings();
-                // Restore previously saved site data and rezoom/style all tabs.
-                NSQ.prefs.sites = this.origSites;
-                this.origSites = null;
-                NSQ.browser.zoomAll();
-                NSQ.browser.styleAll();
-            },
-
-            closeSiteSettings: function() {
-                if (NSQ.storage.dialogs.site)
-                    NSQ.storage.dialogs.site.die();
-            }
-        };
-
-        if (this.observer.inPrivateBrowsing)
-            this.observer.watcher.onEnterPrivateBrowsing();
 
-        window.addEventListener('DOMMouseScroll', this.handleMouseScroll, false); 
+        window.addEventListener('DOMMouseScroll', this.handleMouseScroll, false);
         // XXX: used for image zoom, which feature is currently removed.
         //window.addEventListener("resize", this.handleResize, false);
         gBrowser.tabContainer.addEventListener('TabOpen', this.handleTabOpen, false);
diff --git a/src/content/dlg-site.js b/src/content/dlg-site.js
index 94ab491..dc2cfde 100644
--- a/src/content/dlg-site.js
+++ b/src/content/dlg-site.js
@@ -13,7 +13,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
         $('text-zoom-level').onchange = function() NSQ.dialogs.site.valueChange(this);
 
         var restyle = function() NSQ.dialogs.site.style(true, false);
-        for (let id in NSQ.prefs.defaultColors) {
+        for (let id in NSQ.browser.prefs.defaultColors) {
             $(id).addEventListener('CheckboxStateChange', this.colorChecked, false);
             $(id).parentNode.childNodes[1].onchange = restyle;
         }
@@ -59,7 +59,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
         this.site = site;
 
         var [text, full] = NSQ.browser.getZoomForBrowser(this.browser);
-        var style = NSQ.prefs.getStyleForSite(this.site);
+        var style = NSQ.browser.prefs.getStyleForSite(this.site);
 
         this.updateWarning();
 
@@ -67,7 +67,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
         $('text-zoom-slider').value = text;
         $('full-zoom-slider').value = full;
 
-        for (let [id, defcolor] in items(NSQ.prefs.defaultColors)) {
+        for (let [id, defcolor] in items(NSQ.browser.prefs.defaultColors)) {
             $(id).parentNode.childNodes[1].color = (!style || style[id] == '0' ? defcolor : style[id]);
             $(id).checked = Boolean(style && style[id] && style[id] != '0');
             this.colorChecked.apply($(id));
@@ -80,9 +80,9 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
 
     this.updateWarning = function() {
         var content = null;
-        if (NSQ.browser.observer.inPrivateBrowsing)
+        if (NSQ.browser.isPrivate)
             content = this.strings.warningPrivateBrowsing;
-        else if (!NSQ.prefs.rememberSites)
+        else if (!NSQ.browser.prefs.rememberSites)
             content = this.strings.warningForgetSites;
 
         $('warning-box-content').innerHTML = content;
@@ -117,7 +117,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
     };
 
     this.buttonUseDefault = function(target) {
-        var [text, full] = NSQ.prefs.getZoomDefaults(this.site);
+        var [text, full] = NSQ.browser.prefs.getZoomDefaults(this.site);
         var input = $(target.id.replace('button', 'level'));
         input.value = (input.id == 'text-zoom-level' ? text : full);
         input.onchange();
@@ -138,7 +138,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
         var full = fromForm ? $('full-zoom-level').value : null;
         NSQ.browser.zoom(this.browser, text, full);
         if (save)
-            NSQ.prefs.updateSiteList(this.site, [text, full]);
+            NSQ.browser.prefs.updateSiteList(this.site, [text, full]);
     };
 
 
@@ -157,7 +157,7 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
         var style = null;
         if (fromForm) {
             var style = {enabled: false};
-            for (let attr in iter(NSQ.prefs.defaultColors)) {
+            for (let attr in iter(NSQ.browser.prefs.defaultColors)) {
                 style[attr] = $(attr).checked ? $(attr).parentNode.childNodes[1].color : null;
                 style.enabled = style.enabled || Boolean(style[attr]);
             }
@@ -167,9 +167,9 @@ NoSquint.dialogs.site = NoSquint.ns(function() { with (NoSquint) {
             }
         }
         if (save)
-            NSQ.prefs.updateSiteList(this.site, null, style);
+            NSQ.browser.prefs.updateSiteList(this.site, null, style);
         if (style)
-            style = NSQ.prefs.applyStyleGlobals(style);
+            style = NSQ.browser.prefs.applyStyleGlobals(style);
 
         NSQ.browser.style(this.browser, style);
     };
diff --git a/src/content/dlg-site.xul b/src/content/dlg-site.xul
index ad81847..aa650cc 100644
--- a/src/content/dlg-site.xul
+++ b/src/content/dlg-site.xul
@@ -22,7 +22,6 @@
 
     <script type="application/x-javascript" src="chrome://nosquint/content/init.js" />
     <script type="application/x-javascript" src="chrome://nosquint/content/lib.js" />
-    <script type="application/x-javascript" src="chrome://nosquint/content/prefs.js" />
     <script type="application/x-javascript" src="chrome://nosquint/content/cmd.js" />
     <script type="application/x-javascript" src="chrome://nosquint/content/dlg-site.js" />
 
diff --git a/src/content/interfaces.js b/src/content/interfaces.js
index 3f2c09f..95f334c 100644
--- a/src/content/interfaces.js
+++ b/src/content/interfaces.js
@@ -109,7 +109,7 @@ NoSquint.interfaces = NoSquint.ns(function() { with (NoSquint) {
             } else if (state & stateFlag) {
                 if (!this.zoomApplied) {
                     this.zoomApplied = true;
-                    if (NSQ.browser.observer.inPrivateBrowsing) {
+                    if (NSQ.browser.isPrivate) {
                         /* In private browsing mode, Firefox does not honor
                          * siteSpecific=false and resets the zoom level back to
                          * 100% after every page load (bug #526828).  So we
@@ -178,45 +178,31 @@ NoSquint.interfaces = NoSquint.ns(function() { with (NoSquint) {
 
 
 
-    /* Custom observer attached to nsIObserverService.  Used to detect changes
-     * to private browsing state, and addon disable/uninstall.  Some code
-     * borrowed from https://developer.mozilla.org/En/Supporting_private_browsing_mode
+    /* Custom observer attached to nsIObserverService.  Used to detect new windows
+     * (to save pending site settings) and addon disable/uninstall.
      */
-    this.Observer = function() {  
+    this.Observer = function() {
         this.id = 'NoSquint.interfaces.Observer';
         this.init();
     };
 
-    this.Observer.prototype = {  
-        _os: null,  
-        _inPrivateBrowsing: false, // whether we are in private browsing mode  
-        watcher: {}, // the watcher object  
-       
-        init: function () {  
-            this._os = Components.classes["@mozilla.org/observer-service;1"]  
-                                 .getService(Components.interfaces.nsIObserverService);  
-            this._os.addObserver(this, "private-browsing", false);  
-            this._os.addObserver(this, "quit-application-granted", false);  
+    this.Observer.prototype = {
+        _os: null,
+
+        init: function () {
+            this._os = Components.classes["@mozilla.org/observer-service;1"]
+                                 .getService(Components.interfaces.nsIObserverService);
+            this._os.addObserver(this, "quit-application-granted", false);
             if (is3x())
-                this._os.addObserver(this, "em-action-requested", false);  
+                this._os.addObserver(this, "em-action-requested", false);
             else {
                 Components.utils.import("resource://gre/modules/AddonManager.jsm");
                 AddonManager.addAddonListener(this);
             }
-
-            try {  
-                var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]  
-                                  .getService(Components.interfaces.nsIPrivateBrowsingService);  
-                this._inPrivateBrowsing = pbs.privateBrowsingEnabled;  
-            } catch(ex) {  
-                // ignore exceptions in older versions of Firefox  
-            }
-
         },
 
         unhook: function() {
-            this._os.removeObserver(this, "quit-application-granted");  
-            this._os.removeObserver(this, "private-browsing");  
+            this._os.removeObserver(this, "quit-application-granted");
             if (is3x())
                 this._os.removeObserver(this, "em-action-requested");
             else
@@ -245,24 +231,8 @@ NoSquint.interfaces = NoSquint.ns(function() { with (NoSquint) {
             NSQ.storage.disabled = false;
         },
 
-        observe: function (subject, topic, data) {  
+        observe: function (subject, topic, data) {
             switch (topic) {
-                case "private-browsing":
-                    switch (data) {
-                        case "enter":
-                            this._inPrivateBrowsing = true;  
-                            if ("onEnterPrivateBrowsing" in this.watcher)
-                                this.watcher.onEnterPrivateBrowsing();  
-                            break;
-
-                        case "exit":
-                            this._inPrivateBrowsing = false;  
-                            if ("onExitPrivateBrowsing" in this.watcher)
-                                this.watcher.onExitPrivateBrowsing();  
-                            break;
-                    }
-                    break;
-
                 case "quit-application-granted":
                     NSQ.storage.quitting = true;
                     break;
@@ -283,10 +253,6 @@ NoSquint.interfaces = NoSquint.ns(function() { with (NoSquint) {
                     }
                     break;
             }
-        },  
-       
-        get inPrivateBrowsing() {  
-            return this._inPrivateBrowsing;  
-        }
-    }; 
+        },
+    };
 }});
diff --git a/src/content/lib.js b/src/content/lib.js
index 4de7571..f2be511 100644
--- a/src/content/lib.js
+++ b/src/content/lib.js
@@ -2,6 +2,8 @@
     // Shorter alias
     this.NSQ = NoSquint;
 
+    Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
+
     /* Setup global (spans all windows) storage object.  The storage object
      * exists once, and is referenced for each window.  (In contrast, doing
      * Application.storage.set('foo', [1,2]) will store a copy of the list.)
@@ -162,11 +164,21 @@
         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                            .getService(Components.interfaces.nsIWindowMediator);
         return function(callback) {
+            // For private windows, apply callback only to self.
+            if (PrivateBrowsingUtils.isWindowPrivate(window)) {
+                callback(this.NSQ);
+                return window;
+            }
+
+            // For non-private windows, apply callback to all other non-
+            // private windows.
             var enumerator = wm.getEnumerator("navigator:browser");
             var win;
-            while (win = enumerator.getNext())
-                if (win.NoSquint && callback(win.NoSquint) === false)
+            while ((win = enumerator.getNext())) {
+                if (win.NoSquint && !PrivateBrowsingUtils.isWindowPrivate(win) &&
+                    callback(win.NoSquint) === false)
                     break;
+            }
             return win;
         };
     })();
diff --git a/src/content/prefs.js b/src/content/prefs.js
index 239bac2..2edb489 100644
--- a/src/content/prefs.js
+++ b/src/content/prefs.js
@@ -1,18 +1,25 @@
 // chrome://browser/content/browser.xul
 
 /******************************************************************************
- * Preferences (Singleton)
+ * Preferences
  *
  * Namespace for anything pref related, including service objects, any
  * currently cached values, routines for parsing, or convenience functions
  * for accessing preferences.
+ *
+ * Each Private Browsing window gets its own NSQ.prefs which doesn't listen
+ * to sites pref changes.  Non-private windows share the same NSQ.prefs.
  */
 
 NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
-    // Namespace is a singleton, so return any previously instantiated prefs object.
-    if (NSQ.storage.prefs)
-        return NSQ.storage.prefs;
-    NSQ.storage.prefs = this;
+    this.isPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
+    if (!this.isPrivate) {
+        // For non-private windows, namespace is a singleton, so return any
+        // previously instantiated prefs object.
+        if (NSQ.storage.prefs)
+            return NSQ.storage.prefs;
+        NSQ.storage.prefs = this;
+    }
 
     this.id = 'NoSquint.prefs';
     this.defaultColors = {
@@ -145,12 +152,20 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
     };
 
     this.preload = function() {
-        // Initialize preferences in this order; some of them require other prefs
-        // have been loaded.  (e.g. forgetMonths needs rememberSites)
+        // If this is a prefs instance for a private window, clone the shared
+        // non-private site list if available.
+        if (this.isPrivate && NSQ.storage.prefs)
+            this.sites = NSQ.storage.prefs.cloneSites();
+        else
+            this.observe(null, "nsPref:changed", "sites");
+
+        // Initialize other preferences in this order; some of them require
+        // other prefs have been loaded.  (e.g. forgetMonths needs
+        // rememberSites)
         var prefs = [
             'fullZoomLevel', 'textZoomLevel', 'zoomIncrement', 'wheelZoomEnabled', 'hideStatus',
-            'action', 'sitesSaveDelay', 'rememberSites', 'exceptions', 'sites', 'forgetMonths',
-            'fullZoomPrimary', 'wheelZoomInvert', 'zoomImages', 'colorText', 'colorBackground', 
+            'action', 'sitesSaveDelay', 'rememberSites', 'exceptions', 'forgetMonths',
+            'fullZoomPrimary', 'wheelZoomInvert', 'zoomImages', 'colorText', 'colorBackground',
             'colorBackgroundImages', 'linksUnvisited', 'linksVisited', 'linksUnderline'
         ];
         for (let pref in iter(prefs))
@@ -164,7 +179,6 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
             // Not a pref change.
             return;
 
-        debug('observe(): data=' + data);
         switch (data) {
             case 'siteSpecific':
                 if (branchBZ.getBoolPref('siteSpecific') == false || NSQ.storage.disabled || NSQ.storage.quitting)
@@ -261,6 +275,11 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
                     ignoreNextSitesChange = false;
                     break;
                 }
+                if (subject != null && this.isPrivate)
+                    // Private windows don't refresh from pref changes.  (If subject is
+                    // null then this is a preload() and we don't want to skip that.)
+                    break;
+
                 this.sites = this.parseSites(branchNS.getCharPref('sites'));
                 if (saveTimer) {
                     /* FIXME: looks like the sites list pref was updated (possibly by
@@ -389,7 +408,7 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
          */
         function regexpify(pattern, re_star, re_dblstar) {
             var parts = pattern.split(/(\[\*+\]|\*+)/);
-            var pattern = [];
+            pattern = [];
             var sub = [];
             var length = 0;
 
@@ -514,9 +533,7 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
             delete this.sites[site];
 
         debug('updateSiteList(): site=' + site + ', record=' + record);
-
-        if (this.rememberSites)
-            this.queueSaveSiteList();
+        this.queueSaveSiteList(true);
     };
 
 
@@ -543,7 +560,11 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
      * else the next time a change is made in the Settings dialog, it will
      * be ignored.
      */
-    this.queueSaveSiteList = function() {
+    this.queueSaveSiteList = function(flush) {
+        if (!this.rememberSites || this.isPrivate)
+            // Now allowed to save changes, don't queue save.
+            return;
+
         this.stopQueueSaveSiteList();
 
         /* The list is actually saved (by default) 5s later, so if the user
@@ -551,13 +572,14 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
          * needlessly iterating over the sites array.
          */
         debug("queueSaveSiteList(): delay=" + this.saveDelay);
-        saveTimer = this.winFunc('setTimeout', function() NSQ.prefs.saveSiteList(), this.saveDelay);
+        saveTimer = this.winFunc('setTimeout', function() NSQ.prefs.saveSiteList(flush), this.saveDelay);
     };
 
-
-    /* Store the sites list right now. */
-    this.saveSiteList = function(force) {
-        if (!this.rememberSites || (NSQ.browser && NSQ.browser.observer.inPrivateBrowsing && !force))
+    /* Store the sites list right now.  If flush is true, the prefs file is
+     * committed to disk.
+     */
+    this.saveSiteList = function(flush) {
+        if (!this.rememberSites || (this.isPrivate && !force))
             /* Private Browsing mode is enabled or rememberSites disabled; do
              * not save site list.
              */
@@ -577,13 +599,13 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
          */
         ignoreNextSitesChange = true;
         branchNS.setCharPref('sites', sites.join(' '));
-        this.save();
-        debug("saveSiteList(): took: " + (new Date().getTime() - t0) + "ms");
+        if (flush)
+            this.save();
+        debug("saveSiteList(): took: " + (new Date().getTime() - t0) + "ms, flush=" + flush);
         this.winFunc('clearTimeout', saveTimer);
         saveTimer = null;
     };
 
-
     /* Stops a previously queued site list save.  Returns true if a save was
      * queued and aborted, or false if no save was queued.
      */
@@ -731,7 +753,7 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
     };
 
 
-    // Saves all preferences, including exceptions BUT NOT sites.
+    // Saves all global preferences, including exceptions BUT NOT sites.
     this.saveAll = function(exceptions) {
         const intPrefs = [
             'fullZoomLevel', 'textZoomLevel', 'zoomIncrement', 'forgetMonths'
@@ -858,6 +880,5 @@ NoSquint.prefs = NoSquint.ns(function() { with(NoSquint) {
             AddonManager.getAddonByID('nosquint at urandom.ca', callback);
         }
     };
-
-
+    return this;
 }});
diff --git a/src/install.rdf b/src/install.rdf
index a934aca..340896e 100644
--- a/src/install.rdf
+++ b/src/install.rdf
@@ -6,7 +6,7 @@
     
         <em:id>nosquint at urandom.ca</em:id>
         <em:name>NoSquint</em:name>
-        <em:version>2.1.6</em:version>
+        <em:version>2.1.7</em:version>
         <em:description>Manage site-specific zoom levels and color settings</em:description>
         <em:creator>Jason Tackaberry</em:creator>
         <!-- optional items -->
@@ -18,8 +18,8 @@
         <em:targetApplication>
             <Description>
                 <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
-                <em:minVersion>3.0</em:minVersion>
-                <em:maxVersion>17.*</em:maxVersion>
+                <em:minVersion>20.0</em:minVersion>
+                <em:maxVersion>20.*</em:maxVersion>
             </Description>
         </em:targetApplication>
     </Description>
diff --git a/src/locale/en-US/dlg-site.properties b/src/locale/en-US/dlg-site.properties
index 9e7f125..4c40753 100644
--- a/src/locale/en-US/dlg-site.properties
+++ b/src/locale/en-US/dlg-site.properties
@@ -1,4 +1,4 @@
 settingsFor=Settings for
 warningForgetSites=Values set here will be discarded when you quit Firefox.  This behavior can be changed in Global Settings.
-warningPrivateBrowsing=Values set here will be discarded once you quit Firefox or exit Private Browsing mode.
+warningPrivateBrowsing=Values set here are not stored, apply only to this window, and will be forgotten once you close this Private Browsing window.
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/nosquint.git



More information about the Pkg-mozext-commits mailing list