From: Bill Erickson Date: Thu, 17 Nov 2016 20:38:00 +0000 (-0500) Subject: LP#1640255 Hatch extension ping request X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=71ab57c42240af46e5479632378aeb1fee65f58d;p=working%2FEvergreen.git LP#1640255 Hatch extension ping request Before opening a port to Hatch, send a simple ping request to confirm the extension exists. Opening a port may succeed even if the extension is not available, whereas this new request will fail immediately, letting the browser know it can stop trying to talk to Hatch. Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/web/js/ui/default/staff/services/hatch.js b/Open-ILS/web/js/ui/default/staff/services/hatch.js index c936ef5b4e..2983f4c5f6 100644 --- a/Open-ILS/web/js/ui/default/staff/services/hatch.js +++ b/Open-ILS/web/js/ui/default/staff/services/hatch.js @@ -141,7 +141,52 @@ angular.module('egCoreMod') return service.getLocalItem('eg.hatch.required'); } + /** + * See if the Hatch extension is registered before we try to + * connect. + * + * Since chrome.runtime.connect() will return a valid Port object + * regardless of whether the requested extenion exists, first attempt + * a simple call/request that will loudly fail when the extension + * is not available. + * + * Returns a promise. + */ + service.checkHatchExtension = function() { + var deferred = $q.defer(); + try { + console.debug("Sending 'ping' request to Hatch extension"); + chrome.runtime.sendMessage( + HATCH_CONFIG.EXT_NAME_CHROME, {ping : true}, + function (reply) { + if (reply && reply.pong) { + deferred.resolve(); + } else { + deferred.reject( + "Extension failed to reply to 'ping' request"); + } + } + ); + } catch (E) { + deferred.reject(E); + } + return deferred.promise; + } + service.hatchConnect = function() { + service.checkHatchExtension().then( + service.openHatchPort, // extension is available + service.extConnectFailed // extension is not available + ); + } + + service.extConnectFailed = function(err) { + console.debug("Hatch connection failed: " + err); + service.hatchAvailable = false; + service.hatchClosed(); + } + + service.openHatchPort = function() { if (service.port) return; @@ -151,9 +196,13 @@ angular.module('egCoreMod') service.port = chrome.runtime.connect(HATCH_CONFIG.EXT_NAME_CHROME); } catch(e) { - service.hatchAvailable = false; - service.hatchClosed(); - console.debug("Hatch connection failed: " + e); + service.extConnectFailed(e); + return; + } + + if (!service.port) { + service.extConnectFailed( + 'runtime.connect() did not return a Port'); return; } @@ -181,7 +230,7 @@ angular.module('egCoreMod') } }); - console.debug('Connected to Hatch'); + console.debug('Hatch port open, sending "init" message'); service.hatchAvailable = true; // The first message to Hatch must be "init"