Bug 1471387 fix calling onBeforeRequest for data/etc urls, r?kmag draft
authorShane Caraveo <scaraveo@mozilla.com>
Mon, 06 Aug 2018 10:26:39 -0300
changeset 826948 d814dfaf0a4aa4c82e5047ebc7b6f4422bc906ca
parent 823465 87bcafe428a4ad6017e59b915581ae00aa863407
push id118419
push usermixedpuppy@gmail.com
push dateMon, 06 Aug 2018 13:27:29 +0000
reviewerskmag
bugs1471387
milestone63.0a1
Bug 1471387 fix calling onBeforeRequest for data/etc urls, r?kmag MozReview-Commit-ID: 7dKmGLzn99z
toolkit/modules/addons/WebRequest.jsm
toolkit/modules/addons/WebRequestContent.js
--- a/toolkit/modules/addons/WebRequest.jsm
+++ b/toolkit/modules/addons/WebRequest.jsm
@@ -207,61 +207,34 @@ function serializeRequestData(eventName)
 }
 
 var HttpObserverManager;
 
 var nextFakeRequestId = 1;
 
 var ContentPolicyManager = {
   policyData: new Map(),
-  policies: new Map(),
   idMap: new Map(),
   nextId: 0,
 
   init() {
     Services.ppmm.initialProcessData.webRequestContentPolicies = this.policyData;
 
     Services.ppmm.addMessageListener("WebRequest:ShouldLoad", this);
     Services.mm.addMessageListener("WebRequest:ShouldLoad", this);
   },
 
   receiveMessage(msg) {
     let browser = ChromeUtils.getClassName(msg.target) == "XULFrameElement" ? msg.target : null;
 
     let requestId = `fakeRequest-${++nextFakeRequestId}`;
-    for (let id of msg.data.ids) {
-      let callback = this.policies.get(id);
-      if (!callback) {
-        // It's possible that this listener has been removed and the
-        // child hasn't learned yet.
-        continue;
-      }
-      let response = null;
-      let listenerKind = "onStop";
-      let data = Object.assign({requestId, browser, serialize: serializeRequestData}, msg.data);
+    let data = Object.assign({requestId, browser, serialize: serializeRequestData}, msg.data);
 
-      delete data.ids;
-      try {
-        response = callback(data);
-        if (response) {
-          if (response.cancel) {
-            listenerKind = "onError";
-            data.error = "NS_ERROR_ABORT";
-            return {cancel: true};
-          }
-          // FIXME: Need to handle redirection here (for non-HTTP URIs only)
-        }
-      } catch (e) {
-        Cu.reportError(e);
-      } finally {
-        runLater(() => this.runChannelListener(listenerKind, data));
-      }
-    }
-
-    return {};
+    this.runChannelListener("opening", data);
+    runLater(() => this.runChannelListener("onStop", data));
   },
 
   shouldRunListener(policyType, url, opts) {
     let {filter} = opts;
 
     if (filter.types && !filter.types.includes(policyType)) {
       return false;
     }
@@ -277,17 +250,21 @@ var ContentPolicyManager = {
 
     return true;
   },
 
   runChannelListener(kind, data) {
     let listeners = HttpObserverManager.listeners[kind];
     for (let [callback, opts] of listeners.entries()) {
       if (this.shouldRunListener(data.type, data.url, opts)) {
-        callback(data);
+        try {
+          callback(data);
+        } catch (e) {
+          Cu.reportError(e);
+        }
       }
     }
   },
 
   addListener(callback, opts) {
     // Clone opts, since we're going to modify them for IPC.
     opts = Object.assign({}, opts);
     let id = this.nextId++;
@@ -295,27 +272,25 @@ var ContentPolicyManager = {
     if (opts.filter.urls) {
       opts.filter = Object.assign({}, opts.filter);
       opts.filter.urls = opts.filter.urls.patterns.map(url => url.pattern);
     }
     Services.ppmm.broadcastAsyncMessage("WebRequest:AddContentPolicy", opts);
 
     this.policyData.set(id, opts);
 
-    this.policies.set(id, callback);
     this.idMap.set(callback, id);
   },
 
   removeListener(callback) {
     let id = this.idMap.get(callback);
     Services.ppmm.broadcastAsyncMessage("WebRequest:RemoveContentPolicy", {id});
 
     this.policyData.delete(id);
     this.idMap.delete(callback);
-    this.policies.delete(id);
   },
 };
 ContentPolicyManager.init();
 
 var ChannelEventSink = {
   _classDescription: "WebRequest channel event sink",
   _classID: Components.ID("115062f8-92f1-11e5-8b7f-080027b0f7ec"),
   _contractID: "@mozilla.org/webrequest/channel-event-sink;1",
--- a/toolkit/modules/addons/WebRequestContent.js
+++ b/toolkit/modules/addons/WebRequestContent.js
@@ -97,25 +97,27 @@ var ContentPolicy = {
       return Ci.nsIContentPolicy.ACCEPT;
     }
     let url = contentLocation.spec;
     if (IS_HTTP.test(url)) {
       // We'll handle this in our parent process HTTP observer.
       return Ci.nsIContentPolicy.ACCEPT;
     }
 
-    let ids = [];
-    for (let [id, {filter}] of this.contentPolicies.entries()) {
+    // Break out early if there are no extension listeners.
+    let haveListeners = false;
+    for (let {filter} of this.contentPolicies.values()) {
       if (WebRequestCommon.typeMatches(policyType, filter.types) &&
           WebRequestCommon.urlMatches(contentLocation, filter.urls)) {
-        ids.push(id);
+        haveListeners = true;
+        break;
       }
     }
 
-    if (!ids.length) {
+    if (!haveListeners) {
       return Ci.nsIContentPolicy.ACCEPT;
     }
 
     let windowId = 0;
     let parentWindowId = -1;
     let frameAncestors = [];
     let mm = Services.cpmm;
 
@@ -174,18 +176,17 @@ var ContentPolicy = {
         mm = ir.getInterface(Ci.nsIContentFrameMessageManager);
       } catch (e) {
         if (e.result != Cr.NS_NOINTERFACE) {
           throw e;
         }
       }
     }
 
-    let data = {ids,
-                url,
+    let data = {url,
                 type: WebRequestCommon.typeForPolicyType(policyType),
                 windowId,
                 parentWindowId};
     if (frameAncestors.length > 0) {
       data.frameAncestors = frameAncestors;
     }
     if (requestOrigin) {
       data.documentUrl = requestOrigin.spec;