Skip to content

Commit

Permalink
Move detection of Firefox extension installation to firefoxExtension …
Browse files Browse the repository at this point in the history
…service.
  • Loading branch information
theurere committed Oct 5, 2015
1 parent 625e015 commit 4af02f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
39 changes: 37 additions & 2 deletions static/js/services/firefoxextension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
26 changes: 6 additions & 20 deletions static/js/services/screensharing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4af02f5

Please sign in to comment.