Bug 1435100 - Ensure preloaded css and script caches are cleared when a WebExtension is shutting down. r=mixedpuppy, a=RyanVM
authorLuca Greco <lgreco@mozilla.com>
Wed, 21 Feb 2018 12:53:56 +0100
changeset 455013 575e33458d1b599df7d5ac28f25fafe3b3e38cee
parent 455012 17c5d6591ddef2166d1fe7785d9f9f859e79ac98
child 455014 e9b426dbf63304b94b5192eaea0931b078fe7313
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy, RyanVM
bugs1435100
milestone59.0
Bug 1435100 - Ensure preloaded css and script caches are cleared when a WebExtension is shutting down. r=mixedpuppy, a=RyanVM MozReview-Commit-ID: IHK7hBYVLMj
toolkit/components/extensions/ExtensionChild.jsm
toolkit/components/extensions/ExtensionContent.jsm
--- a/toolkit/components/extensions/ExtensionChild.jsm
+++ b/toolkit/components/extensions/ExtensionChild.jsm
@@ -669,16 +669,17 @@ class BrowserExtensionContent extends Ev
 
   shutdown() {
     ExtensionManager.extensions.delete(this.id);
     ExtensionContent.shutdownExtension(this);
     Services.cpmm.removeMessageListener(this.MESSAGE_EMIT_EVENT, this);
     if (isContentProcess) {
       MessageChannel.abortResponses({extensionId: this.id});
     }
+    this.emit("shutdown");
   }
 
   getContext(window) {
     return ExtensionContent.getContext(this, window);
   }
 
   emit(event, ...args) {
     Services.cpmm.sendAsyncMessage(this.MESSAGE_EMIT_EVENT, {event, args});
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -94,22 +94,29 @@ const SCRIPT_CLEAR_TIMEOUT_MS = 5 * 1000
 
 const CSS_EXPIRY_TIMEOUT_MS = 30 * 60 * 1000;
 const CSSCODE_EXPIRY_TIMEOUT_MS = 10 * 60 * 1000;
 
 const scriptCaches = new WeakSet();
 const sheetCacheDocuments = new DefaultWeakMap(() => new WeakSet());
 
 class CacheMap extends DefaultMap {
-  constructor(timeout, getter) {
+  constructor(timeout, getter, extension) {
     super(getter);
 
     this.expiryTimeout = timeout;
 
     scriptCaches.add(this);
+
+    // This ensures that all the cached scripts and stylesheets are deleted
+    // from the cache and the xpi is no longer actively used.
+    // See Bug 1435100 for rationale.
+    extension.once("shutdown", () => {
+      this.clear(-1);
+    });
   }
 
   get(url) {
     let promise = super.get(url);
 
     promise.lastUsed = Date.now();
     if (promise.timer) {
       promise.timer.cancel();
@@ -127,26 +134,29 @@ class CacheMap extends DefaultMap {
     }
 
     super.delete(url);
   }
 
   clear(timeout = SCRIPT_CLEAR_TIMEOUT_MS) {
     let now = Date.now();
     for (let [url, promise] of this.entries()) {
-      if (now - promise.lastUsed >= timeout) {
+      // Delete the entry if expired or if clear has been called with timeout -1
+      // (which is used to force the cache to clear all the entries, e.g. when the
+      // extension is shutting down).
+      if (timeout === -1 || (now - promise.lastUsed >= timeout)) {
         this.delete(url);
       }
     }
   }
 }
 
 class ScriptCache extends CacheMap {
-  constructor(options) {
-    super(SCRIPT_EXPIRY_TIMEOUT_MS);
+  constructor(options, extension) {
+    super(SCRIPT_EXPIRY_TIMEOUT_MS, null, extension);
     this.options = options;
   }
 
   defaultConstructor(url) {
     let promise = ChromeUtils.compileScript(url, this.options);
     promise.then(script => {
       promise.script = script;
     });
@@ -155,16 +165,20 @@ class ScriptCache extends CacheMap {
 }
 
 /**
  * Shared base class for the two specialized CSS caches:
  * CSSCache (for the "url"-based stylesheets) and CSSCodeCache
  * (for the stylesheet defined by plain CSS content as a string).
  */
 class BaseCSSCache extends CacheMap {
+  constructor(expiryTimeout, defaultConstructor, extension) {
+    super(expiryTimeout, defaultConstructor, extension);
+  }
+
   addDocument(key, document) {
     sheetCacheDocuments.get(this.get(key)).add(document);
   }
 
   deleteDocument(key, document) {
     sheetCacheDocuments.get(this.get(key)).delete(document);
   }
 
@@ -185,23 +199,23 @@ class BaseCSSCache extends CacheMap {
     super.delete(key);
   }
 }
 
 /**
  * Cache of the preloaded stylesheet defined by url.
  */
 class CSSCache extends BaseCSSCache {
-  constructor(sheetType) {
+  constructor(sheetType, extension) {
     super(CSS_EXPIRY_TIMEOUT_MS, url => {
       let uri = Services.io.newURI(url);
       return styleSheetService.preloadSheetAsync(uri, sheetType).then(sheet => {
         return {url, sheet};
       });
-    });
+    }, extension);
   }
 }
 
 /**
  * Cache of the preloaded stylesheet defined by plain CSS content as a string,
  * the key of the cached stylesheet is the hash of its "CSSCode" string.
  */
 class CSSCodeCache extends BaseCSSCache {
@@ -209,17 +223,17 @@ class CSSCodeCache extends BaseCSSCache 
     super(CSSCODE_EXPIRY_TIMEOUT_MS, (hash) => {
       if (!this.has(hash)) {
         // Do not allow the getter to be used to lazily create the cached stylesheet,
         // the cached CSSCode stylesheet has to be explicitly set.
         throw new Error("Unexistent cached cssCode stylesheet: " + Error().stack);
       }
 
       return super.get(hash);
-    });
+    }, extension);
 
     // Store the preferred sheetType (used to preload the expected stylesheet type in
     // the addCSSCode method).
     this.sheetType = sheetType;
   }
 
   addCSSCode(hash, cssCode) {
     if (this.has(hash)) {
@@ -230,30 +244,30 @@ class CSSCodeCache extends BaseCSSCache 
     const value = styleSheetService.preloadSheetAsync(uri, this.sheetType).then(sheet => {
       return {sheet, uri};
     });
 
     super.set(hash, value);
   }
 }
 
-defineLazyGetter(BrowserExtensionContent.prototype, "staticScripts", () => {
-  return new ScriptCache({hasReturnValue: false});
+defineLazyGetter(BrowserExtensionContent.prototype, "staticScripts", function() {
+  return new ScriptCache({hasReturnValue: false}, this);
 });
 
-defineLazyGetter(BrowserExtensionContent.prototype, "dynamicScripts", () => {
-  return new ScriptCache({hasReturnValue: true});
+defineLazyGetter(BrowserExtensionContent.prototype, "dynamicScripts", function() {
+  return new ScriptCache({hasReturnValue: true}, this);
 });
 
-defineLazyGetter(BrowserExtensionContent.prototype, "userCSS", () => {
-  return new CSSCache(Ci.nsIStyleSheetService.USER_SHEET);
+defineLazyGetter(BrowserExtensionContent.prototype, "userCSS", function() {
+  return new CSSCache(Ci.nsIStyleSheetService.USER_SHEET, this);
 });
 
-defineLazyGetter(BrowserExtensionContent.prototype, "authorCSS", () => {
-  return new CSSCache(Ci.nsIStyleSheetService.AUTHOR_SHEET);
+defineLazyGetter(BrowserExtensionContent.prototype, "authorCSS", function() {
+  return new CSSCache(Ci.nsIStyleSheetService.AUTHOR_SHEET, this);
 });
 
 // These two caches are similar to the above but specialized to cache the cssCode
 // using an hash computed from the cssCode string as the key (instead of the generated data
 // URI which can be pretty long for bigger injected cssCode).
 defineLazyGetter(BrowserExtensionContent.prototype, "userCSSCode", function() {
   return new CSSCodeCache(Ci.nsIStyleSheetService.USER_SHEET, this);
 });
@@ -316,17 +330,17 @@ class Script {
     return this.css.map(url => this.cssCache.get(url));
   }
 
   preload() {
     this.loadCSS();
     this.compileScripts();
   }
 
-  cleanup(window, forceCacheClear = false) {
+  cleanup(window) {
     if (this.requiresCleanup) {
       if (window) {
         let winUtils = getWinUtils(window);
 
         let type = this.cssOrigin === "user" ? winUtils.USER_SHEET : winUtils.AUTHOR_SHEET;
 
         for (let url of this.css) {
           this.cssCache.deleteDocument(url, window.document);
@@ -340,18 +354,18 @@ class Script {
             runSafeSyncWithoutClone(winUtils.removeSheet, uri, type);
           });
           this.cssCodeCache.deleteDocument(cssCodeHash, window.document);
         }
       }
 
       // Clear any sheets that were kept alive past their timeout as
       // a result of living in this document.
-      this.cssCodeCache.clear(forceCacheClear ? 0 : CSSCODE_EXPIRY_TIMEOUT_MS);
-      this.cssCache.clear(forceCacheClear ? 0 : CSS_EXPIRY_TIMEOUT_MS);
+      this.cssCodeCache.clear(CSSCODE_EXPIRY_TIMEOUT_MS);
+      this.cssCache.clear(CSS_EXPIRY_TIMEOUT_MS);
     }
   }
 
   matchesWindow(window) {
     return this.matcher.matchesWindow(window);
   }
 
   async injectInto(window) {
@@ -605,29 +619,23 @@ class ContentScriptContextChild extends 
   }
 
   addScript(script) {
     if (script.requiresCleanup) {
       this.scripts.push(script);
     }
   }
 
-  cleanupScripts(forceCacheClear = false) {
-    // Cleanup the scripts (even if the contentWindow have been destroyed) and their
-    // related CSS and Script caches.
-    for (let script of this.scripts) {
-      script.cleanup(this.contentWindow, forceCacheClear);
-    }
-  }
-
   close() {
     super.unload();
 
     // Cleanup the scripts even if the contentWindow have been destroyed.
-    this.cleanupScripts();
+    for (let script of this.scripts) {
+      script.cleanup(this.contentWindow);
+    }
 
     if (this.contentWindow) {
       // Overwrite the content script APIs with an empty object if the APIs objects are still
       // defined in the content window (See Bug 1214658).
       if (this.isExtensionPage) {
         Cu.createObjectIn(this.contentWindow, {defineAs: "browser"});
         Cu.createObjectIn(this.contentWindow, {defineAs: "chrome"});
       }
@@ -713,20 +721,16 @@ DocumentManager = {
   observe(subject, topic, data) {
     this.observers[topic].call(this, subject, topic, data);
   },
 
   shutdownExtension(extension) {
     for (let extensions of this.contexts.values()) {
       let context = extensions.get(extension);
       if (context) {
-        // Passing true to context.cleanupScripts causes the caches for this context
-        // to be cleared.
-        context.cleanupScripts(true);
-
         context.close();
         extensions.delete(extension);
       }
     }
   },
 
   getContexts(window) {
     let winId = getInnerWindowID(window);