gecko-dev/toolkit/components/extensions/ext-runtime.js
2015-08-14 17:57:34 -07:00

50 lines
1.3 KiB
JavaScript

const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
let {
EventManager,
ignoreEvent,
} = ExtensionUtils;
extensions.registerAPI((extension, context) => {
return {
runtime: {
onStartup: new EventManager(context, "runtime.onStartup", fire => {
extension.onStartup = fire;
return () => {
extension.onStartup = null;
};
}).api(),
onInstalled: ignoreEvent(),
onMessage: context.messenger.onMessage("runtime.onMessage"),
onConnect: context.messenger.onConnect("runtime.onConnect"),
sendMessage: function(...args) {
let extensionId, message, options, responseCallback;
if (args.length == 1) {
message = args[0];
} else if (args.length == 2) {
[message, responseCallback] = args;
} else {
[extensionId, message, options, responseCallback] = args;
}
let recipient = {extensionId: extensionId ? extensionId : extension.id};
return context.messenger.sendMessage(Services.cpmm, message, recipient, responseCallback);
},
getManifest() {
return Cu.cloneInto(extension.manifest, context.cloneScope);
},
id: extension.id,
getURL: function(url) {
return extension.baseURI.resolve(url);
},
},
};
});