Bug 1254204: Part 3 - Refactor runChennelListener to decrease cyclomatic complexity. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 08 Nov 2016 21:51:23 -0800
changeset 435758 963f981367100b3f7b5b62e42042ac906a711d6c
parent 435756 b7800bc6e407a8c262f73efcfa5dc7ad99d36a31
child 536375 bbd98a509249051b66365bd411ce13a7d41645e4
push id35118
push usermaglione.k@gmail.com
push dateWed, 09 Nov 2016 05:51:52 +0000
reviewersaswan
bugs1254204
milestone52.0a1
Bug 1254204: Part 3 - Refactor runChennelListener to decrease cyclomatic complexity. r?aswan MozReview-Commit-ID: udmh8O94Jv
toolkit/modules/addons/WebRequest.jsm
--- a/toolkit/modules/addons/WebRequest.jsm
+++ b/toolkit/modules/addons/WebRequest.jsm
@@ -558,122 +558,115 @@ HttpObserverManager = {
   errorCheck(channel, loadContext, channelData = null) {
     let errorData = this.maybeError(channel, null, channelData);
     if (errorData) {
       this.runChannelListener(channel, loadContext, "onError", errorData);
     }
     return errorData;
   },
 
-  runChannelListener(channel, loadContext, kind, extraData = null) {
+  runChannelListener(channel, loadContext = null, kind, extraData = null) {
     if (this.activityInitialized) {
       let channelData = getData(channel);
       if (kind === "onError") {
         if (channelData.errorNotified) {
           return;
         }
         channelData.errorNotified = true;
       } else if (this.errorCheck(channel, loadContext, channelData)) {
         return;
       }
     }
     let listeners = this.listeners[kind];
-    let browser = loadContext ? loadContext.topFrameElement : null;
+    let browser = loadContext  && loadContext.topFrameElement;
     let loadInfo = channel.loadInfo;
-    let policyType = loadInfo ?
-                     loadInfo.externalContentPolicyType :
-                     Ci.nsIContentPolicy.TYPE_OTHER;
+    let policyType = (loadInfo ? loadInfo.externalContentPolicyType
+                               : Ci.nsIContentPolicy.TYPE_OTHER);
 
-    let requestBody;
-
-    let includeStatus = (
-                          kind === "headersReceived" ||
-                          kind === "onRedirect" ||
-                          kind === "onStart" ||
-                          kind === "onStop"
-                        ) && channel instanceof Ci.nsIHttpChannel;
+    let includeStatus = (["headersReceived", "onRedirect", "onStart", "onStop"].includes(kind) &&
+                         channel instanceof Ci.nsIHttpChannel);
 
     let requestHeaders = new RequestHeaderChanger(channel);
     let responseHeaders;
     try {
       responseHeaders = new ResponseHeaderChanger(channel);
     } catch (e) {
       // Meh.
     }
 
     let commonData = null;
     let uri = channel.URI;
     let handlerResults = [];
+    let requestBody;
     for (let [callback, opts] of listeners.entries()) {
       if (!this.shouldRunListener(policyType, uri, opts.filter)) {
         continue;
       }
 
       if (!commonData) {
         commonData = {
           requestId: RequestId.get(channel),
           url: uri.spec,
           method: channel.requestMethod,
           browser: browser,
           type: WebRequestCommon.typeForPolicyType(policyType),
           fromCache: getData(channel).fromCache,
+          windowId: 0,
+          parentWindowId: 0,
         };
 
         if (loadInfo) {
-          let originPrincipal = loadInfo.triggeringPrincipal || loadInfo.loadingPrincipal;
+          let originPrincipal = loadInfo.triggeringPrincipal;
           if (originPrincipal.URI) {
             commonData.originUrl = originPrincipal.URI.spec;
           }
-          Object.assign(commonData, {
-            windowId: loadInfo.frameOuterWindowID ?
-                        loadInfo.frameOuterWindowID : loadInfo.outerWindowID,
-            parentWindowId: loadInfo.frameOuterWindowID ?
-                              loadInfo.outerWindowID : loadInfo.parentOuterWindowID,
-            isSystemPrincipal: Services.scriptSecurityManager
-                                       .isSystemPrincipal(loadInfo.triggeringPrincipal) ||
-                               Services.scriptSecurityManager
-                                       .isSystemPrincipal(loadInfo.loadingPrincipal),
-          });
-        } else {
-          Object.assign(commonData, {
-            windowId: 0,
-            parentWindowId: 0,
-          });
+
+          let {isSystemPrincipal} = Services.scriptSecurityManager;
+
+          commonData.isSystemPrincipal = (isSystemPrincipal(loadInfo.triggeringPrincipal) ||
+                                          isSystemPrincipal(loadInfo.loadingPrincipal));
+
+          if (loadInfo.frameOuterWindowID) {
+            Object.assign(commonData, {
+              windowId: loadInfo.frameOuterWindowID,
+              parentWindowId: loadInfo.outerWindowID,
+            });
+          } else {
+            Object.assign(commonData, {
+              windowId: loadInfo.outerWindowID,
+              parentWindowId: loadInfo.parentOuterWindowID,
+            });
+          }
         }
 
         if (channel instanceof Ci.nsIHttpChannelInternal) {
           try {
             commonData.ip = channel.remoteAddress;
           } catch (e) {
             // The remoteAddress getter throws if the address is unavailable,
             // but ip is an optional property so just ignore the exception.
           }
         }
-        if (extraData) {
-          Object.assign(commonData, extraData);
-        }
+
+        Object.assign(commonData, extraData);
       }
 
       let data = Object.assign({}, commonData);
 
       if (opts.requestHeaders) {
         data.requestHeaders = requestHeaders.toArray();
       }
 
       if (opts.responseHeaders && responseHeaders) {
         data.responseHeaders = responseHeaders.toArray();
       }
 
       if (opts.requestBody) {
-        if (requestBody === undefined) {
-          requestBody = WebRequestUpload.createRequestBody(channel);
-        }
-        if (requestBody) {
-          data.requestBody = requestBody;
-        }
+        requestBody = requestBody || WebRequestUpload.createRequestBody(channel);
+        data.requestBody = requestBody;
       }
 
       if (includeStatus) {
         mergeStatus(data, channel, kind);
       }
 
       try {
         let result = callback(data);