Bug 1299411 - Deduplicate context getter logic in ParentAPIManager draft
authorRob Wu <rob@robwu.nl>
Sat, 24 Sep 2016 13:10:39 +0200
changeset 430572 6130a74a09b576560d6f8361ec4c865daf7abe64
parent 430571 e6f29fea8ecd0d24e8fcb046a80ae21f256b980a
child 430573 7f42500ed69866d6514b12e4a5687828c027bf71
push id33850
push userbmo:rob@robwu.nl
push dateThu, 27 Oct 2016 23:41:10 +0000
bugs1299411
milestone52.0a1
Bug 1299411 - Deduplicate context getter logic in ParentAPIManager 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