From de5bac291960ca21645e1af26aed20d727a60fc7 Mon Sep 17 00:00:00 2001 From: Evan Theurer Date: Mon, 5 Oct 2015 12:12:12 +0200 Subject: [PATCH] Move detection of Firefox extension installation to firefoxExtension service. --- static/js/services/firefoxextension.js | 39 ++++++++++++++++++++++++-- static/js/services/screensharing.js | 26 ++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/static/js/services/firefoxextension.js b/static/js/services/firefoxextension.js index 5dbcecfe..078fcfd5 100644 --- a/static/js/services/firefoxextension.js +++ b/static/js/services/firefoxextension.js @@ -23,7 +23,7 @@ define(["underscore", "jquery", "webrtc.adapter"], function(_, $) { // firefoxExtension - return ["$window", "$q", "alertify", "translation", function($window, $q, alertify, translation) { + return ["$window", "$q", "alertify", "translation", "$interval", function($window, $q, alertify, translation, $interval) { var FirefoxExtension = function() { this.available = false; @@ -46,14 +46,49 @@ define(["underscore", "jquery", "webrtc.adapter"], function(_, $) { }; FirefoxExtension.prototype.registerAutoInstall = function(installFunc, cancelInstallFunc, force) { - this.autoinstall.install = installFunc; this.autoinstall.cancel = cancelInstallFunc; this.autoinstall.force = !!force; if (!this.available && installFunc) { this.e.triggerHandler("available", true); } + }; + + var EXTENSION_DOM_ID = 'firefoxextension-available'; + var intervalSecs = 50; + var intervalCount = 1; + var hasBeenInstalled = function() { + return $window.document.getElementById(EXTENSION_DOM_ID); + }; + + /** + * Checks for availability of the Firefox extension by looking for the id which the extension + * will append to the body of the document. Unfortunately there is no callback + * API implemented by Firefox which will allow other domains to see if an + * extension is installed using `InstallTrigger.install`. Only priviledged + * domains may use the callback. + * + * @param {int} How long of a timespan the function should check for the extension install at intervalSecs interval rate + * @return {promise} + */ + FirefoxExtension.prototype.detectInstalled = function(maxTimeout) { + var defer = $q.defer(); + var that = this; + + var intervalPromise = $interval(function() { + if (hasBeenInstalled()) { + console.log("Auto install success Firefox extension"); + $interval.cancel(intervalPromise); + that.initialize(); + defer.resolve("Auto install success Firefox extension"); + } else if (intervalCount * intervalSecs >= maxTimeout) { + $interval.cancel(intervalPromise); + defer.reject("Timeout while waiting for extension to become available"); + } + intervalCount++; + }, intervalSecs); + return defer.promise; }; // Create extension api and wait for messages. diff --git a/static/js/services/screensharing.js b/static/js/services/screensharing.js index 5d74d6e7..3bdb43c3 100644 --- a/static/js/services/screensharing.js +++ b/static/js/services/screensharing.js @@ -36,7 +36,7 @@ define(['underscore', 'text!partials/screensharedialogff.html', 'webrtc.adapter' var GLOBAL_SCREENSHARING_STOP_EVENT = new Event('webrtcStopScreensharing'); // screensharing - return ["$window", "$q", "$timeout", "$interval", "chromeExtension", "firefoxExtension", "dialogs", "$templateCache", function($window, $q, $timeout, $interval, chromeExtension, firefoxExtension, dialogs, $templateCache) { + return ["$window", "$q", "$timeout", "chromeExtension", "firefoxExtension", "dialogs", "$templateCache", function($window, $q, $timeout, chromeExtension, firefoxExtension, dialogs, $templateCache) { $templateCache.put('/dialogs/screensharedialogff.html', screenshareDialogFF); @@ -273,27 +273,13 @@ define(['underscore', 'text!partials/screensharedialogff.html', 'webrtc.adapter' d.reject(err); }); }; - var hasBeenInstalled = function() { - return $window.document.getElementById('firefoxextension-available'); - }; - var cancelInterval = function(promise) { - $interval.cancel(promise); - }; - var maxTimeout = 30000; - var intervalCount = 1; - var intervalSecs = 50; - var intervalPromise = $interval(function() { - if (that.autoinstall && that.supported && hasBeenInstalled()) { - console.log("Auto install success Firefox extension"); - cancelInterval(intervalPromise); + firefoxExtension.detectInstalled(30000).then(function() { + if (!that.autoinstall && that.supported) { starter(); - } else if (intervalCount * intervalSecs >= maxTimeout) { - cancelInterval(intervalPromise); - d.reject("Timeout while waiting for extension to become available"); } - intervalCount++; - }, intervalSecs); - + }, function(reason) { + d.reject(reason); + }); }, function(err) { console.log("Auto install of extension failed.", err); if (prepareAlternative) {