Bug 1471387 fix calling onBeforeRequest for data/etc urls, r=kmag
authorShane Caraveo <scaraveo@mozilla.com>
Mon, 06 Aug 2018 10:26:39 -0300
changeset 430973 5a51b1f6d4f9e32d728912b9352a66120a8a10a5
parent 430943 2bbf606a0b6870b24dd33612dafa9a37f3bee47a
child 430974 dcab8343b3db152c68e2e981cd22b0cb77ebaf5c
push id34418
push userbtara@mozilla.com
push dateFri, 10 Aug 2018 21:58:19 +0000
treeherdermozilla-central@9a6728f46269 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1471387
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
@@ -223,61 +223,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;
     }
@@ -293,17 +266,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++;
@@ -311,27 +288,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
@@ -96,25 +96,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;
 
@@ -166,18 +168,17 @@ var ContentPolicy = {
       }
 
       let windowMM = window.docShell.messageManager;
       if (windowMM) {
         mm = windowMM;
       }
     }
 
-    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;