Bug 1299411 - Deduplicate context getter logic in ParentAPIManager
MozReview-Commit-ID: E9MpHhal7WF
--- 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