2323define ( [ "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.
0 commit comments