LP#1640255 Hatch extension ping request
authorBill Erickson <berickxx@gmail.com>
Thu, 17 Nov 2016 20:38:00 +0000 (15:38 -0500)
committerBill Erickson <berickxx@gmail.com>
Wed, 23 Nov 2016 23:06:01 +0000 (18:06 -0500)
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 <berickxx@gmail.com>
Open-ILS/web/js/ui/default/staff/services/hatch.js

index c936ef5..2983f4c 100644 (file)
@@ -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"