Bug 1348442: Part 2c - Refactor Script class into ES6 class. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 18 Mar 2017 15:19:29 -0700
changeset 501332 8998d4d545be46f0d84a0d32e73b5699db6750d1
parent 501331 2791d9758ecf7906eea334be29f9b38f7daa67d1
child 501333 c9f6e9af5aade1054a4cef1d82f56d93466243fa
push id49931
push usermaglione.k@gmail.com
push dateSun, 19 Mar 2017 23:22:27 +0000
reviewersaswan
bugs1348442
milestone55.0a1
Bug 1348442: Part 2c - Refactor Script class into ES6 class. r?aswan MozReview-Commit-ID: Dtci1mfQts0
toolkit/components/extensions/ExtensionContent.jsm
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -172,69 +172,69 @@ class CSSCache extends CacheMap {
       return styleSheetService.preloadSheetAsync(uri, sheetType).then(sheet => {
         return {url, sheet};
       });
     });
   }
 }
 
 // Represents a content script.
-function Script(extension, options, deferred = PromiseUtils.defer()) {
-  this.extension = extension;
-  this.options = options;
-  this.run_at = this.options.run_at;
-  this.js = this.options.js || [];
-  this.css = this.options.css || [];
-  this.remove_css = this.options.remove_css;
-  this.match_about_blank = this.options.match_about_blank;
-  this.css_origin = this.options.css_origin;
+class Script {
+  constructor(extension, options, deferred = PromiseUtils.defer()) {
+    this.extension = extension;
+    this.options = options;
+    this.run_at = this.options.run_at;
+    this.js = this.options.js || [];
+    this.css = this.options.css || [];
+    this.remove_css = this.options.remove_css;
+    this.match_about_blank = this.options.match_about_blank;
+    this.css_origin = this.options.css_origin;
+
+    this.deferred = deferred;
 
-  this.deferred = deferred;
+    this.cssCache = extension[this.css_origin === "user" ? "userCSS"
+                                                         : "authorCSS"];
+    this.scriptCache = extension[options.wantReturnValue ? "dynamicScripts"
+                                                         : "staticScripts"];
+
+    if (options.wantReturnValue) {
+      this.compileScripts();
+      this.loadCSS();
+    }
 
-  this.cssCache = extension[this.css_origin === "user" ? "userCSS"
-                                                       : "authorCSS"];
-  this.scriptCache = extension[options.wantReturnValue ? "dynamicScripts"
-                                                       : "staticScripts"];
+    this.matches_ = new MatchPattern(this.options.matches);
+    this.exclude_matches_ = new MatchPattern(this.options.exclude_matches || null);
+    // TODO: MatchPattern should pre-mangle host-only patterns so that we
+    // don't need to call a separate match function.
+    this.matches_host_ = new MatchPattern(this.options.matchesHost || null);
+    this.include_globs_ = new MatchGlobs(this.options.include_globs);
+    this.exclude_globs_ = new MatchGlobs(this.options.exclude_globs);
 
-  if (options.wantReturnValue) {
-    this.compileScripts();
-    this.loadCSS();
+    this.requiresCleanup = !this.remove_css && (this.css.length > 0 || options.cssCode);
   }
 
-  this.matches_ = new MatchPattern(this.options.matches);
-  this.exclude_matches_ = new MatchPattern(this.options.exclude_matches || null);
-  // TODO: MatchPattern should pre-mangle host-only patterns so that we
-  // don't need to call a separate match function.
-  this.matches_host_ = new MatchPattern(this.options.matchesHost || null);
-  this.include_globs_ = new MatchGlobs(this.options.include_globs);
-  this.exclude_globs_ = new MatchGlobs(this.options.exclude_globs);
-
-  this.requiresCleanup = !this.remove_css && (this.css.length > 0 || options.cssCode);
-}
-
-Script.prototype = {
   compileScripts() {
     return this.js.map(url => this.scriptCache.get(url));
-  },
+  }
 
   loadCSS() {
     return this.cssURLs.map(url => this.cssCache.get(url));
-  },
+  }
 
   matchesLoadInfo(uri, loadInfo) {
     if (!this.matchesURI(uri)) {
       return false;
     }
 
     if (!this.options.all_frames && !loadInfo.isTopLevelLoad) {
       return false;
     }
 
     return true;
-  },
+  }
 
   matchesURI(uri) {
     if (!(this.matches_.matches(uri) || this.matches_host_.matchesIgnoringPath(uri))) {
       return false;
     }
 
     if (this.exclude_matches_.matches(uri)) {
       return false;
@@ -246,17 +246,17 @@ Script.prototype = {
       }
     }
 
     if (this.exclude_globs_.matches(uri.spec)) {
       return false;
     }
 
     return true;
-  },
+  }
 
   matches(window) {
     let uri = window.document.documentURIObject;
     let principal = window.document.nodePrincipal;
 
     // If mozAddonManager is present on this page, don't allow
     // content scripts.
     if (window.navigator.mozAddonManager !== undefined) {
@@ -293,28 +293,28 @@ Script.prototype = {
       if (WebNavigationFrames.getFrameId(window) != this.options.frame_id) {
         return false;
       }
     } else if (!this.options.all_frames && window.top != window) {
       return false;
     }
 
     return true;
-  },
+  }
 
   cleanup(window) {
     if (!this.remove_css) {
       let winUtils = getWinUtils(window);
 
       let type = this.css_origin === "user" ? winUtils.USER_SHEET : winUtils.AUTHOR_SHEET;
       for (let url of this.cssURLs) {
         runSafeSyncWithoutClone(winUtils.removeSheetUsingURIString, url, type);
       }
     }
-  },
+  }
 
   /**
    * Tries to inject this script into the given window and sandbox, if
    * there are pending operations for the window's current load state.
    *
    * @param {Window} window
    *        The DOM Window to inject the scripts and CSS into.
    * @param {Sandbox} sandbox
@@ -386,18 +386,18 @@ Script.prototype = {
 
         if (this.options.jsCode) {
           result = Cu.evalInSandbox(this.options.jsCode, sandbox, "latest");
         }
 
         return result;
       }));
     }
-  },
-};
+  }
+}
 
 defineLazyGetter(Script.prototype, "cssURLs", function() {
   // We can handle CSS urls (css) and CSS code (cssCode).
   let urls = this.css.slice();
 
   if (this.options.cssCode) {
     urls.push("data:text/css;charset=utf-8," + encodeURIComponent(this.options.cssCode));
   }