Bug 520173 - Reset lightweight theme previews when leaving the source page. r=dtownsend
authorDão Gottwald <dao@mozilla.com>
Thu, 22 Oct 2009 22:47:59 +0200
changeset 33131 57aacf736fbbfc50f384bcb05e0856a52252c40c
parent 33130 ce1974a8acb6ac247da1ac28d3b0f2580b0a7371
child 33132 ffa03358306defae931a60ee867d975c79be0b6c
push id798
push userdgottwald@mozilla.com
push dateTue, 24 Nov 2009 20:42:48 +0000
reviewersdtownsend
bugs520173
milestone1.9.2b4pre
Bug 520173 - Reset lightweight theme previews when leaving the source page. r=dtownsend
browser/base/content/browser.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -7209,24 +7209,36 @@ let gURLBarEmptyText = {
     return gURLBar.getAttribute(type + "emptytext");
   }
 };
 
 var LightWeightThemeWebInstaller = {
   handleEvent: function (event) {
     switch (event.type) {
       case "InstallBrowserTheme":
+      case "PreviewBrowserTheme":
+      case "ResetBrowserThemePreview":
+        // ignore requests from background tabs
+        if (event.target.ownerDocument.defaultView.top != content)
+          return;
+    }
+    switch (event.type) {
+      case "InstallBrowserTheme":
         this._installRequest(event);
         break;
       case "PreviewBrowserTheme":
         this._preview(event);
         break;
       case "ResetBrowserThemePreview":
         this._resetPreview(event);
         break;
+      case "pagehide":
+      case "TabSelect":
+        this._resetPreview();
+        break;
     }
   },
 
   get _manager () {
     var temp = {};
     Cu.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
     delete this._manager;
     return this._manager = temp.LightweightThemeManager;
@@ -7309,31 +7321,43 @@ var LightWeightThemeWebInstaller = {
     ["lwtheme-install-request",
      "lwtheme-install-notification"].forEach(function (value) {
         var notification = box.getNotificationWithValue(value);
         if (notification)
           box.removeNotification(notification);
       });
   },
 
+  _previewWindow: null,
   _preview: function (event) {
     if (!this._isAllowed(event.target))
       return;
 
     var data = this._getThemeFromNode(event.target);
     if (!data)
       return;
 
+    this._resetPreview();
+
+    this._previewWindow = event.target.ownerDocument.defaultView;
+    this._previewWindow.addEventListener("pagehide", this, true);
+    gBrowser.tabContainer.addEventListener("TabSelect", this, false);
+
     this._manager.previewTheme(data);
   },
 
   _resetPreview: function (event) {
-    if (!this._isAllowed(event.target))
+    if (!this._previewWindow ||
+        event && !this._isAllowed(event.target))
       return;
 
+    this._previewWindow.removeEventListener("pagehide", this, true);
+    this._previewWindow = null;
+    gBrowser.tabContainer.removeEventListener("TabSelect", this, false);
+
     this._manager.resetPreview();
   },
 
   _isAllowed: function (node) {
     var pm = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
 
     var prefs = [["xpinstall.whitelist.add", pm.ALLOW_ACTION],
                  ["xpinstall.whitelist.add.36", pm.ALLOW_ACTION],