Skip to content

Commit de5bac2

Browse files
committed
Move detection of Firefox extension installation to firefoxExtension service.
1 parent 49b45f6 commit de5bac2

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

static/js/services/firefoxextension.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
define(["underscore", "jquery", "webrtc.adapter"], function(_, $) {
2424

2525
// firefoxExtension
26-
return ["$window", "$q", "alertify", "translation", function($window, $q, alertify, translation) {
26+
return ["$window", "$q", "alertify", "translation", "$interval", function($window, $q, alertify, translation, $interval) {
2727

2828
var FirefoxExtension = function() {
2929
this.available = false;
@@ -46,14 +46,49 @@ define(["underscore", "jquery", "webrtc.adapter"], function(_, $) {
4646
};
4747

4848
FirefoxExtension.prototype.registerAutoInstall = function(installFunc, cancelInstallFunc, force) {
49-
5049
this.autoinstall.install = installFunc;
5150
this.autoinstall.cancel = cancelInstallFunc;
5251
this.autoinstall.force = !!force;
5352
if (!this.available && installFunc) {
5453
this.e.triggerHandler("available", true);
5554
}
55+
};
56+
57+
var EXTENSION_DOM_ID = 'firefoxextension-available';
58+
var intervalSecs = 50;
59+
var intervalCount = 1;
60+
var hasBeenInstalled = function() {
61+
return $window.document.getElementById(EXTENSION_DOM_ID);
62+
};
63+
64+
/**
65+
* Checks for availability of the Firefox extension by looking for the id which the extension
66+
* will append to the body of the document. Unfortunately there is no callback
67+
* API implemented by Firefox which will allow other domains to see if an
68+
* extension is installed using `InstallTrigger.install`. Only priviledged
69+
* domains may use the callback.
70+
*
71+
* @param {int} How long of a timespan the function should check for the extension install at intervalSecs interval rate
72+
* @return {promise}
73+
*/
74+
FirefoxExtension.prototype.detectInstalled = function(maxTimeout) {
75+
var defer = $q.defer();
76+
var that = this;
77+
78+
var intervalPromise = $interval(function() {
79+
if (hasBeenInstalled()) {
80+
console.log("Auto install success Firefox extension");
81+
$interval.cancel(intervalPromise);
82+
that.initialize();
83+
defer.resolve("Auto install success Firefox extension");
84+
} else if (intervalCount * intervalSecs >= maxTimeout) {
85+
$interval.cancel(intervalPromise);
86+
defer.reject("Timeout while waiting for extension to become available");
87+
}
88+
intervalCount++;
89+
}, intervalSecs);
5690

91+
return defer.promise;
5792
};
5893

5994
// Create extension api and wait for messages.

static/js/services/screensharing.js

+6-20
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ define(['underscore', 'text!partials/screensharedialogff.html', 'webrtc.adapter'
3636
var GLOBAL_SCREENSHARING_STOP_EVENT = new Event('webrtcStopScreensharing');
3737

3838
// screensharing
39-
return ["$window", "$q", "$timeout", "$interval", "chromeExtension", "firefoxExtension", "dialogs", "$templateCache", function($window, $q, $timeout, $interval, chromeExtension, firefoxExtension, dialogs, $templateCache) {
39+
return ["$window", "$q", "$timeout", "chromeExtension", "firefoxExtension", "dialogs", "$templateCache", function($window, $q, $timeout, chromeExtension, firefoxExtension, dialogs, $templateCache) {
4040

4141
$templateCache.put('/dialogs/screensharedialogff.html', screenshareDialogFF);
4242

@@ -273,27 +273,13 @@ define(['underscore', 'text!partials/screensharedialogff.html', 'webrtc.adapter'
273273
d.reject(err);
274274
});
275275
};
276-
var hasBeenInstalled = function() {
277-
return $window.document.getElementById('firefoxextension-available');
278-
};
279-
var cancelInterval = function(promise) {
280-
$interval.cancel(promise);
281-
};
282-
var maxTimeout = 30000;
283-
var intervalCount = 1;
284-
var intervalSecs = 50;
285-
var intervalPromise = $interval(function() {
286-
if (that.autoinstall && that.supported && hasBeenInstalled()) {
287-
console.log("Auto install success Firefox extension");
288-
cancelInterval(intervalPromise);
276+
firefoxExtension.detectInstalled(30000).then(function() {
277+
if (!that.autoinstall && that.supported) {
289278
starter();
290-
} else if (intervalCount * intervalSecs >= maxTimeout) {
291-
cancelInterval(intervalPromise);
292-
d.reject("Timeout while waiting for extension to become available");
293279
}
294-
intervalCount++;
295-
}, intervalSecs);
296-
280+
}, function(reason) {
281+
d.reject(reason);
282+
});
297283
}, function(err) {
298284
console.log("Auto install of extension failed.", err);
299285
if (prepareAlternative) {

0 commit comments

Comments
 (0)