23
23
define ( [ "underscore" , "jquery" , "webrtc.adapter" ] , function ( _ , $ ) {
24
24
25
25
// 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 ) {
27
27
28
28
var FirefoxExtension = function ( ) {
29
29
this . available = false ;
@@ -46,14 +46,49 @@ define(["underscore", "jquery", "webrtc.adapter"], function(_, $) {
46
46
} ;
47
47
48
48
FirefoxExtension . prototype . registerAutoInstall = function ( installFunc , cancelInstallFunc , force ) {
49
-
50
49
this . autoinstall . install = installFunc ;
51
50
this . autoinstall . cancel = cancelInstallFunc ;
52
51
this . autoinstall . force = ! ! force ;
53
52
if ( ! this . available && installFunc ) {
54
53
this . e . triggerHandler ( "available" , true ) ;
55
54
}
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 ) ;
56
90
91
+ return defer . promise ;
57
92
} ;
58
93
59
94
// Create extension api and wait for messages.
0 commit comments