Bug 1254204: Part 3 - Refactor runChennelListener to decrease cyclomatic complexity. r=mixedpuppy
authorKris Maglione <maglione.k@gmail.com>
Tue, 08 Nov 2016 21:51:23 -0800
changeset 351998 b913f0ce1e3e3bc94ce444b286768aed15efbfd4
parent 351997 07b39b68b0844b18d16747dd9a9502d47d2c487a
child 351999 fc55188f14b6819fbf34a425d7af2a2c73ded730
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1254204
milestone52.0a1
Bug 1254204: Part 3 - Refactor runChennelListener to decrease cyclomatic complexity. r=mixedpuppy MozReview-Commit-ID: udmh8O94Jv
toolkit/modules/addons/WebRequest.jsm
--- a/toolkit/modules/addons/WebRequest.jsm
+++ b/toolkit/modules/addons/WebRequest.jsm
@@ -555,123 +555,116 @@ 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) {
       // Just ignore this for the request phases where response headers
       // aren't yet available.
     }
 
     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);