diff options
author | [email protected] <[email protected]> | 2018-04-10 09:33:14 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2018-04-10 16:47:59 +0000 |
commit | eef3a509fa5744e5f09ec8084985e6070b78226b (patch) | |
tree | d5fe58d4e50c56a2a750bac81d79266a71e5ec01 /lang/js/src/Connection.js | |
parent | qt: Add test for resetting config value (diff) | |
download | gpgme-eef3a509fa5744e5f09ec8084985e6070b78226b.tar.gz gpgme-eef3a509fa5744e5f09ec8084985e6070b78226b.zip |
js: Initial commit for JavaScript Native Messaging API
--
Note this code misses all the legal boilerplate; please add this as
soon as possible and provide a DCO so we can merge it into master.
I also removed the dist/ directory because that was not source code.
Diffstat (limited to 'lang/js/src/Connection.js')
-rw-r--r-- | lang/js/src/Connection.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js new file mode 100644 index 00000000..e8fea542 --- /dev/null +++ b/lang/js/src/Connection.js @@ -0,0 +1,76 @@ +/** + * A connection port will be opened for each communication between gpgmejs and + * gnupg. It should be alive as long as there are additional messages to be + * expected. + */ + +export function Connection(){ + if (!this.connection){ + this.connection = connect(); + this._msg = { + 'always-trust': true, + // 'no-encrypt-to': false, + // 'no-compress': true, + // 'throw-keyids': false, + // 'wrap': false, + 'armor': true, + 'base64': false + }; + }; + + this.disconnect = function () { + if (this.connection){ + this.connection.disconnect(); + } + }; + + /** + * Sends a message and resolves with the answer. + * @param {*} operation The interaction requested from gpgme + * @param {*} message A json-capable object to pass the operation details. + * TODO: _msg should contain configurable parameters + */ + this.post = function(operation, message){ + let timeout = 5000; + let me = this; + if (!message || !operation){ + return Promise.reject('no message'); // TBD + } + + let keys = Object.keys(message); + for (let i=0; i < keys.length; i++){ + let property = keys[i]; + me._msg[property] = message[property]; + } + me._msg['op'] = operation; + // TODO fancier checks if what we want is consistent with submitted content + return new Promise(function(resolve, reject){ + me.connection.onMessage.addListener(function(msg) { + if (!msg){ + reject('empty answer.'); + } + if (msg.type === "error"){ + reject(msg.msg); + } + resolve(msg); + }); + + me.connection.postMessage(me._msg); + setTimeout( + function(){ + me.disconnect(); + reject('Timeout'); + }, timeout); + }); + }; +}; + + +function connect(){ + let connection = chrome.runtime.connectNative('gpgmejson'); + if (!connection){ + let msg = chrome.runtime.lastError || 'no message'; //TBD + throw(msg); + } + return connection; +}; |