Bug 1299411 - Deduplicate context getter logic in ParentAPIManager r=aswan
authorRob Wu <rob@robwu.nl>
Sat, 24 Sep 2016 13:10:39 +0200
changeset 320063 57f7a5c7044db9fba2cd039773ac058b7269aac5
parent 320062 a9c19ee017a44aeb9953858b663a100a85ccffab
child 320064 2829c46a636d7a2b77a88df0b9fbe36182fa836d
push id20749
push userryanvm@gmail.com
push dateSat, 29 Oct 2016 13:21:21 +0000
treeherderfx-team@1b170b39ed6b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1299411
milestone52.0a1
Bug 1299411 - Deduplicate context getter logic in ParentAPIManager r=aswan MozReview-Commit-ID: E9MpHhal7WF
toolkit/components/extensions/Extension.jsm
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -482,21 +482,17 @@ var ParentAPIManager = {
     if (!context) {
       return;
     }
     context.unload();
     this.proxyContexts.delete(childId);
   },
 
   call(data, target) {
-    let context = this.proxyContexts.get(data.childId);
-    if (!context) {
-      Cu.reportError("WebExtension context not found!");
-      return;
-    }
+    let context = this.getContextById(data.childId);
     if (context.currentMessageManager !== target.messageManager) {
       Cu.reportError("WebExtension warning: Message manager unexpectedly changed");
     }
 
     function callback(...cbArgs) {
       let lastError = context.lastError;
 
       context.currentMessageManager.sendAsyncMessage("API:CallResult", {
@@ -520,21 +516,17 @@ var ParentAPIManager = {
         childId: data.childId,
         callId: data.callId,
         lastError: msg,
       });
     }
   },
 
   addListener(data, target) {
-    let context = this.proxyContexts.get(data.childId);
-    if (!context) {
-      Cu.reportError("WebExtension context not found!");
-      return;
-    }
+    let context = this.getContextById(data.childId);
     if (context.currentMessageManager !== target.messageManager) {
       Cu.reportError("WebExtension warning: Message manager unexpectedly changed");
     }
 
     function listener(...listenerArgs) {
       context.currentMessageManager.sendAsyncMessage("API:RunListener", {
         childId: data.childId,
         path: data.path,
@@ -544,23 +536,30 @@ var ParentAPIManager = {
 
     context.listenerProxies.set(data.path, listener);
 
     let args = Cu.cloneInto(data.args, context.sandbox);
     findPathInObject(context.apiObj, data.path).addListener(listener, ...args);
   },
 
   removeListener(data) {
-    let context = this.proxyContexts.get(data.childId);
-    if (!context) {
-      Cu.reportError("WebExtension context not found!");
-    }
+    let context = this.getContextById(data.childId);
     let listener = context.listenerProxies.get(data.path);
     findPathInObject(context.apiObj, data.path).removeListener(listener);
   },
+
+  getContextById(childId) {
+    let context = this.proxyContexts.get(childId);
+    if (!context) {
+      let error = new Error("WebExtension context not found!");
+      Cu.reportError(error);
+      throw error;
+    }
+    return context;
+  },
 };
 
 ParentAPIManager.init();
 
 // All moz-extension URIs use a machine-specific UUID rather than the
 // extension's own ID in the host component. This makes it more
 // difficult for web pages to detect whether a user has a given add-on
 // installed (by trying to load a moz-extension URI referring to a