diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
commit | f164d9124708b50789dbb6959e1de96cc5697c48 (patch) | |
tree | 6dffd12e08c5383130df0252fb69cd6d6330794f /toolkit/components/webextensions/ext-c-runtime.js | |
parent | 30de4018913f0cdaea19d1dd12ecd8209e2ed08e (diff) | |
download | UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.gz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.lz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.xz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.zip |
Rename Toolkit's webextensions component directory to better reflect what it is.
Diffstat (limited to 'toolkit/components/webextensions/ext-c-runtime.js')
-rw-r--r-- | toolkit/components/webextensions/ext-c-runtime.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/toolkit/components/webextensions/ext-c-runtime.js b/toolkit/components/webextensions/ext-c-runtime.js new file mode 100644 index 000000000..8adca60ca --- /dev/null +++ b/toolkit/components/webextensions/ext-c-runtime.js @@ -0,0 +1,93 @@ +"use strict"; + +function runtimeApiFactory(context) { + let {extension} = context; + + return { + runtime: { + onConnect: context.messenger.onConnect("runtime.onConnect"), + + onMessage: context.messenger.onMessage("runtime.onMessage"), + + connect: function(extensionId, connectInfo) { + let name = connectInfo !== null && connectInfo.name || ""; + extensionId = extensionId || extension.id; + let recipient = {extensionId}; + + return context.messenger.connect(context.messageManager, name, recipient); + }, + + sendMessage: function(...args) { + let options; // eslint-disable-line no-unused-vars + let extensionId, message, responseCallback; + if (typeof args[args.length - 1] == "function") { + responseCallback = args.pop(); + } + if (!args.length) { + return Promise.reject({message: "runtime.sendMessage's message argument is missing"}); + } else if (args.length == 1) { + message = args[0]; + } else if (args.length == 2) { + if (typeof args[0] == "string" && args[0]) { + [extensionId, message] = args; + } else { + [message, options] = args; + } + } else if (args.length == 3) { + [extensionId, message, options] = args; + } else if (args.length == 4 && !responseCallback) { + return Promise.reject({message: "runtime.sendMessage's last argument is not a function"}); + } else { + return Promise.reject({message: "runtime.sendMessage received too many arguments"}); + } + + if (extensionId != null && typeof extensionId != "string") { + return Promise.reject({message: "runtime.sendMessage's extensionId argument is invalid"}); + } + if (options != null && typeof options != "object") { + return Promise.reject({message: "runtime.sendMessage's options argument is invalid"}); + } + // TODO(robwu): Validate option keys and values when we support it. + + extensionId = extensionId || extension.id; + let recipient = {extensionId}; + + return context.messenger.sendMessage(context.messageManager, message, recipient, responseCallback); + }, + + connectNative(application) { + let recipient = { + childId: context.childManager.id, + toNativeApp: application, + }; + + return context.messenger.connectNative(context.messageManager, "", recipient); + }, + + sendNativeMessage(application, message) { + let recipient = { + childId: context.childManager.id, + toNativeApp: application, + }; + return context.messenger.sendNativeMessage(context.messageManager, message, recipient); + }, + + get lastError() { + return context.lastError; + }, + + getManifest() { + return Cu.cloneInto(extension.manifest, context.cloneScope); + }, + + id: extension.id, + + getURL: function(url) { + return extension.baseURI.resolve(url); + }, + }, + }; +} + +extensions.registerSchemaAPI("runtime", "addon_child", runtimeApiFactory); +extensions.registerSchemaAPI("runtime", "content_child", runtimeApiFactory); |