Bug 1368102: Part 3 - Use MatchPattern and MatchGlob bindings for content script matching. r?mixedpuppy,zombie draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 25 May 2017 15:42:18 -0700
changeset 585212 ac295ae8830960d2a7d0a487091329caa7e0a318
parent 585211 0645629fdb4affe2b4b2344f8ef5c056afb12b23
child 585213 e9a2a61c3aa96da5f547f976f6ba8879091e4084
push id61052
push usermaglione.k@gmail.com
push dateFri, 26 May 2017 17:14:32 +0000
reviewersmixedpuppy, zombie
bugs1368102
milestone55.0a1
Bug 1368102: Part 3 - Use MatchPattern and MatchGlob bindings for content script matching. r?mixedpuppy,zombie MozReview-Commit-ID: AvNCU77I2Je
toolkit/components/extensions/ExtensionTabs.jsm
toolkit/components/extensions/extension-process-script.js
--- a/toolkit/components/extensions/ExtensionTabs.jsm
+++ b/toolkit/components/extensions/ExtensionTabs.jsm
@@ -543,19 +543,19 @@ class TabBase {
 
     if (details.frameId !== null && details.allFrames) {
       return Promise.reject({message: `'frameId' and 'allFrames' are mutually exclusive`});
     }
 
     if (this.hasActiveTabPermission) {
       // If we have the "activeTab" permission for this tab, ignore
       // the host whitelist.
-      options.matchesHost = ["<all_urls>"];
+      options.matches = ["<all_urls>"];
     } else {
-      options.matchesHost = [this.extension.whiteListedHosts.patterns.map(host => host.pattern)];
+      options.matches = [this.extension.whiteListedHosts.patterns.map(host => host.pattern)];
     }
 
     if (details.code !== null) {
       options[`${kind}Code`] = details.code;
     }
     if (details.file !== null) {
       let url = context.uri.resolve(details.file);
       if (!this.extension.isExtensionURL(url)) {
--- a/toolkit/components/extensions/extension-process-script.js
+++ b/toolkit/components/extensions/extension-process-script.js
@@ -12,20 +12,16 @@
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "ExtensionManagement",
                                   "resource://gre/modules/ExtensionManagement.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "MatchGlobs",
-                                  "resource://gre/modules/MatchPattern.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "MatchPattern",
-                                  "resource://gre/modules/MatchPattern.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
                                   "resource://gre/modules/MessageChannel.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "WebNavigationFrames",
                                   "resource://gre/modules/WebNavigationFrames.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "ExtensionChild",
                                   "resource://gre/modules/ExtensionChild.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "ExtensionContent",
@@ -51,23 +47,20 @@ class ScriptMatcher {
 
     this._script = null;
 
     this.allFrames = options.all_frames;
     this.matchAboutBlank = options.match_about_blank;
     this.frameId = options.frame_id;
     this.runAt = options.run_at;
 
-    this.matches = new MatchPattern(options.matches);
-    this.excludeMatches = new MatchPattern(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.matchesHost = new MatchPattern(options.matchesHost || null);
-    this.includeGlobs = options.include_globs && new MatchGlobs(options.include_globs);
-    this.excludeGlobs = new MatchGlobs(options.exclude_globs);
+    this.matches = new MatchPatternSet(options.matches);
+    this.excludeMatches = new MatchPatternSet(options.exclude_matches || []);
+    this.includeGlobs = options.include_globs && options.include_globs.map(glob => new MatchGlob(glob));
+    this.excludeGlobs = options.include_globs && options.exclude_globs.map(glob => new MatchGlob(glob));
   }
 
   toString() {
     return `[Script {js: [${this.options.js}], matchAboutBlank: ${this.matchAboutBlank}, runAt: ${this.runAt}, matches: ${this.options.matches}}]`;
   }
 
   get script() {
     if (!this._script) {
@@ -92,29 +85,29 @@ class ScriptMatcher {
     if (!this.allFrames && !loadInfo.isTopLevelLoad) {
       return false;
     }
 
     return true;
   }
 
   matchesURI(uri) {
-    if (!(this.matches.matches(uri) || this.matchesHost.matchesIgnoringPath(uri))) {
+    if (!(this.matches.matches(uri))) {
       return false;
     }
 
     if (this.excludeMatches.matches(uri)) {
       return false;
     }
 
-    if (this.includeGlobs != null && !this.includeGlobs.matches(uri.spec)) {
+    if (this.includeGlobs && !this.includeGlobs.some(glob => glob.matches(uri.spec))) {
       return false;
     }
 
-    if (this.excludeGlobs.matches(uri.spec)) {
+    if (this.excludeGlobs && this.excludeGlobs.some(glob => glob.matches(uri.spec))) {
       return false;
     }
 
     return true;
   }
 
   matchesWindow(window) {
     if (!this.allFrames && this.frameId == null && window.parent !== window) {