Bug 1362224: Cached normalized object for simple icon URLs. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 05 May 2017 12:53:30 -0700
changeset 573491 88da9878aadb1c432c550f2ab037e360830d41a3
parent 573490 0a53bfb0eadd41c90680390727c42cf35caa0bfb
child 573492 dbaf63bed6403023ea5ad162fd0808056886dc79
push id57408
push usermaglione.k@gmail.com
push dateFri, 05 May 2017 21:03:51 +0000
reviewersaswan
bugs1362224
milestone55.0a1
Bug 1362224: Cached normalized object for simple icon URLs. r?aswan MozReview-Commit-ID: ErFFdfKAtuz
toolkit/components/extensions/ExtensionUtils.jsm
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -318,26 +318,46 @@ class SpreadArgs extends Array {
     super();
     this.push(...args);
   }
 }
 
 // Manages icon details for toolbar buttons in the |pageAction| and
 // |browserAction| APIs.
 let IconDetails = {
+  iconCache: new DefaultWeakMap(() => new Map()),
+
   // Normalizes the various acceptable input formats into an object
   // with icon size as key and icon URL as value.
   //
   // If a context is specified (function is called from an extension):
   // Throws an error if an invalid icon size was provided or the
   // extension is not allowed to load the specified resources.
   //
   // If no context is specified, instead of throwing an error, this
   // function simply logs a warning message.
   normalize(details, extension, context = null) {
+    if (!details.imageData && typeof details.path === "string") {
+      let icons = this.iconCache.get(extension);
+
+      let baseURI = context ? context.uri : extension.baseURI;
+      let url = baseURI.resolve(details.path);
+
+      let icon = icons.get(url);
+      if (!icon) {
+        icon = this._normalize(details, extension, context);
+        icons.set(url, icon);
+      }
+      return icon;
+    }
+
+    return this._normalize(details, extension, context);
+  },
+
+  _normalize(details, extension, context = null) {
     let result = {};
 
     try {
       if (details.imageData) {
         let imageData = details.imageData;
 
         if (typeof imageData == "string") {
           imageData = {"19": imageData};