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;
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;
}
}
});
- console.debug('Connected to Hatch');
+ console.debug('Hatch port open, sending "init" message');
service.hatchAvailable = true;
// The first message to Hatch must be "init"