浏览代码

Merge pull request #268 from jitsi/ss_postinstall_fix

fix(SS): implement interval to check the status of the ext after install
dev1
Любомир Маринов 9 年前
父节点
当前提交
2112e1b05d
共有 1 个文件被更改,包括 93 次插入70 次删除
  1. 93
    70
      modules/RTC/ScreenObtainer.js

+ 93
- 70
modules/RTC/ScreenObtainer.js 查看文件

1
 /* global chrome, $, alert */
1
 /* global chrome, $, alert */
2
 
2
 
3
+var AdapterJS = require("./adapter.screenshare");
4
+var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
3
 var logger = require("jitsi-meet-logger").getLogger(__filename);
5
 var logger = require("jitsi-meet-logger").getLogger(__filename);
4
 var RTCBrowserType = require("./RTCBrowserType");
6
 var RTCBrowserType = require("./RTCBrowserType");
5
-var AdapterJS = require("./adapter.screenshare");
6
 import JitsiTrackError from "../../JitsiTrackError";
7
 import JitsiTrackError from "../../JitsiTrackError";
7
 import * as JitsiTrackErrors from "../../JitsiTrackErrors";
8
 import * as JitsiTrackErrors from "../../JitsiTrackErrors";
8
-var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
9
 
9
 
10
 /**
10
 /**
11
  * Indicates whether the Chrome desktop sharing extension is installed.
11
  * Indicates whether the Chrome desktop sharing extension is installed.
54
  */
54
  */
55
 var ScreenObtainer = {
55
 var ScreenObtainer = {
56
     obtainStream: null,
56
     obtainStream: null,
57
+
57
     /**
58
     /**
58
      * Initializes the function used to obtain a screen capture
59
      * Initializes the function used to obtain a screen capture
59
      * (this.obtainStream).
60
      * (this.obtainStream).
68
      * @param options {object}
69
      * @param options {object}
69
      * @param gum {Function} GUM method
70
      * @param gum {Function} GUM method
70
      */
71
      */
71
-    init: function(options, gum) {
72
+    init(options, gum) {
72
         var obtainDesktopStream = null;
73
         var obtainDesktopStream = null;
73
         this.options = options = options || {};
74
         this.options = options = options || {};
74
         GUM = gum;
75
         GUM = gum;
81
             (options.desktopSharingChromeMethod || options.desktopSharing);
82
             (options.desktopSharingChromeMethod || options.desktopSharing);
82
 
83
 
83
         if (RTCBrowserType.isNWJS()) {
84
         if (RTCBrowserType.isNWJS()) {
84
-            obtainDesktopStream = function (options, onSuccess, onFailure) {
85
+            obtainDesktopStream = (options, onSuccess, onFailure) => {
85
                 window.JitsiMeetNW.obtainDesktopStream (
86
                 window.JitsiMeetNW.obtainDesktopStream (
86
-                    onSuccess, function (error, constraints) {
87
+                    onSuccess,
88
+                    (error, constraints) => {
87
                         var jitsiError;
89
                         var jitsiError;
88
                         // FIXME:
90
                         // FIXME:
89
                         // This is very very durty fix for recognising that the
91
                         // This is very very durty fix for recognising that the
148
             } else {
150
             } else {
149
                 obtainDesktopStream = this.obtainScreenOnFirefox;
151
                 obtainDesktopStream = this.obtainScreenOnFirefox;
150
             }
152
             }
151
-
152
         }
153
         }
153
 
154
 
154
         if (!obtainDesktopStream) {
155
         if (!obtainDesktopStream) {
163
      * environment.
164
      * environment.
164
      * @returns {boolean}
165
      * @returns {boolean}
165
      */
166
      */
166
-    isSupported: function() {
167
+    isSupported() {
167
         return !!this.obtainStream;
168
         return !!this.obtainStream;
168
     },
169
     },
170
+
169
     /**
171
     /**
170
      * Obtains a screen capture stream on Firefox.
172
      * Obtains a screen capture stream on Firefox.
171
      * @param callback
173
      * @param callback
172
      * @param errorCallback
174
      * @param errorCallback
173
      */
175
      */
174
-    obtainScreenOnFirefox:
175
-           function (options, callback, errorCallback) {
176
-        var self = this;
176
+    obtainScreenOnFirefox(options, callback, errorCallback) {
177
         var extensionRequired = false;
177
         var extensionRequired = false;
178
         if (this.options.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
178
         if (this.options.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
179
             (this.options.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
179
             (this.options.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
198
         // extension if it hasn't.
198
         // extension if it hasn't.
199
         if (firefoxExtInstalled === null) {
199
         if (firefoxExtInstalled === null) {
200
             window.setTimeout(
200
             window.setTimeout(
201
-                function() {
201
+                () => {
202
                     if (firefoxExtInstalled === null)
202
                     if (firefoxExtInstalled === null)
203
                         firefoxExtInstalled = false;
203
                         firefoxExtInstalled = false;
204
-                    self.obtainScreenOnFirefox(callback, errorCallback);
204
+                    this.obtainScreenOnFirefox(callback, errorCallback);
205
                 },
205
                 },
206
-                300
207
-            );
206
+                300);
208
             logger.log("Waiting for detection of jidesha on firefox to " +
207
             logger.log("Waiting for detection of jidesha on firefox to " +
209
                 "finish.");
208
                 "finish.");
210
             return;
209
             return;
221
         errorCallback(
220
         errorCallback(
222
             new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
221
             new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
223
     },
222
     },
223
+
224
     /**
224
     /**
225
      * Asks Chrome extension to call chooseDesktopMedia and gets chrome
225
      * Asks Chrome extension to call chooseDesktopMedia and gets chrome
226
      * 'desktop' stream for returned stream token.
226
      * 'desktop' stream for returned stream token.
227
      */
227
      */
228
-    obtainScreenFromExtension: function(options, streamCallback, failCallback) {
229
-        var self = this;
228
+    obtainScreenFromExtension(options, streamCallback, failCallback) {
230
         if (chromeExtInstalled) {
229
         if (chromeExtInstalled) {
231
             doGetStreamFromExtension(this.options, streamCallback,
230
             doGetStreamFromExtension(this.options, streamCallback,
232
                 failCallback);
231
                 failCallback);
240
             try {
239
             try {
241
                 chrome.webstore.install(
240
                 chrome.webstore.install(
242
                     getWebStoreInstallUrl(this.options),
241
                     getWebStoreInstallUrl(this.options),
243
-                    function (arg) {
242
+                    arg => {
244
                         logger.log("Extension installed successfully", arg);
243
                         logger.log("Extension installed successfully", arg);
245
                         chromeExtInstalled = true;
244
                         chromeExtInstalled = true;
246
-                        // We need to give a moment for the endpoint to become
247
-                        // available
248
-                        window.setTimeout(function () {
249
-                            doGetStreamFromExtension(self.options,
250
-                                streamCallback, failCallback);
251
-                        }, 2000);
245
+                        // We need to give a moment to the endpoint to become
246
+                        // available.
247
+                        waitForExtensionAfterInstall(this.options, 200, 10)
248
+                            .then(() => {
249
+                                doGetStreamFromExtension(this.options,
250
+                                    streamCallback, failCallback);
251
+                            }).catch(reason => {
252
+                                this.handleExtensionInstallationError(options,
253
+                                    streamCallback, failCallback, reason);
254
+                            });
252
                     },
255
                     },
253
                     this.handleExtensionInstallationError.bind(this,
256
                     this.handleExtensionInstallationError.bind(this,
254
                         options, streamCallback, failCallback)
257
                         options, streamCallback, failCallback)
259
             }
262
             }
260
         }
263
         }
261
     },
264
     },
262
-    handleExtensionInstallationError: function (options, streamCallback,
263
-        failCallback, e) {
264
-        if( CHROME_EXTENSION_POPUP_ERROR === e && options.interval > 0 &&
265
-            typeof(options.checkAgain) === "function" &&
266
-            typeof(options.listener) === "function") {
267
-            options.listener("waitingForExtension",
268
-                getWebStoreInstallUrl(this.options));
269
-            this.checkForChromeExtensionOnInterval(options,
270
-                streamCallback, failCallback, e);
265
+
266
+    handleExtensionInstallationError(options, streamCallback, failCallback, e) {
267
+        const webStoreInstallUrl = getWebStoreInstallUrl(this.options);
268
+
269
+        if (CHROME_EXTENSION_POPUP_ERROR === e
270
+                && options.interval > 0
271
+                && typeof(options.checkAgain) === "function"
272
+                && typeof(options.listener) === "function") {
273
+            options.listener("waitingForExtension", webStoreInstallUrl);
274
+            this.checkForChromeExtensionOnInterval(options, streamCallback,
275
+                failCallback, e);
271
             return;
276
             return;
272
         }
277
         }
273
-        var msg = "Failed to install the extension from "
274
-            + getWebStoreInstallUrl(this.options);
275
 
278
 
276
-        logger.log(msg, e);
279
+        const msg
280
+            = "Failed to install the extension from " + webStoreInstallUrl;
277
 
281
 
282
+        logger.log(msg, e);
278
         failCallback(new JitsiTrackError(
283
         failCallback(new JitsiTrackError(
279
             JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
284
             JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
280
-            msg
281
-        ));
285
+            msg));
282
     },
286
     },
283
-    checkForChromeExtensionOnInterval: function (options,
284
-        streamCallback, failCallback) {
287
+
288
+    checkForChromeExtensionOnInterval(options, streamCallback, failCallback) {
285
         if (options.checkAgain() === false) {
289
         if (options.checkAgain() === false) {
286
             failCallback(new JitsiTrackError(
290
             failCallback(new JitsiTrackError(
287
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
291
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
288
             return;
292
             return;
289
         }
293
         }
290
-        var self = this;
291
-        window.setTimeout(function () {
292
-            checkChromeExtInstalled(function (installed, updateRequired) {
293
-                chromeExtInstalled = installed;
294
-                chromeExtUpdateRequired = updateRequired;
295
-                if(installed) {
296
-                    options.listener("extensionFound");
297
-                    self.obtainScreenFromExtension(options,
298
-                        streamCallback, failCallback);
299
-                } else {
300
-                    self.checkForChromeExtensionOnInterval(options,
301
-                        streamCallback, failCallback);
302
-                }
303
-            }, self.options);
304
-        }, options.interval);
294
+        waitForExtensionAfterInstall(this.options, options.interval, 1)
295
+            .then(() => {
296
+                chromeExtInstalled = true;
297
+                options.listener("extensionFound");
298
+                this.obtainScreenFromExtension(options,
299
+                    streamCallback, failCallback);
300
+            }).catch(() => {
301
+                this.checkForChromeExtensionOnInterval(options,
302
+                    streamCallback, failCallback);
303
+            });
305
     }
304
     }
306
 };
305
 };
307
 
306
 
308
-
309
-
310
 /**
307
 /**
311
  * Obtains a desktop stream using getUserMedia.
308
  * Obtains a desktop stream using getUserMedia.
312
  * For this to work on Chrome, the
309
  * For this to work on Chrome, the
317
  * 'about:config'.
314
  * 'about:config'.
318
  */
315
  */
319
 function obtainWebRTCScreen(options, streamCallback, failCallback) {
316
 function obtainWebRTCScreen(options, streamCallback, failCallback) {
320
-    GUM(
321
-        ['screen'],
322
-        streamCallback,
323
-        failCallback
324
-    );
317
+    GUM(['screen'], streamCallback, failCallback);
325
 }
318
 }
326
 
319
 
327
 /**
320
 /**
386
         //TODO: remove chromeExtensionId (deprecated)
379
         //TODO: remove chromeExtensionId (deprecated)
387
         (options.desktopSharingChromeExtId || options.chromeExtensionId),
380
         (options.desktopSharingChromeExtId || options.chromeExtensionId),
388
         { getVersion: true },
381
         { getVersion: true },
389
-        function (response) {
382
+        response => {
390
             if (!response || !response.version) {
383
             if (!response || !response.version) {
391
                 // Communication failure - assume that no endpoint exists
384
                 // Communication failure - assume that no endpoint exists
392
                 logger.warn(
385
                 logger.warn(
420
             sources: (options.desktopSharingChromeSources ||
413
             sources: (options.desktopSharingChromeSources ||
421
                 options.desktopSharingSources)
414
                 options.desktopSharingSources)
422
         },
415
         },
423
-        function (response) {
416
+        response => {
424
             if (!response) {
417
             if (!response) {
425
                 // possibly re-wraping error message to make code consistent
418
                 // possibly re-wraping error message to make code consistent
426
                 var lastError = chrome.runtime.lastError;
419
                 var lastError = chrome.runtime.lastError;
435
             if (response.streamId) {
428
             if (response.streamId) {
436
                 GUM(
429
                 GUM(
437
                     ['desktop'],
430
                     ['desktop'],
438
-                    function (stream) {
439
-                        streamCallback(stream);
440
-                    },
431
+                    stream => streamCallback(stream),
441
                     failCallback,
432
                     failCallback,
442
-                    {desktopStream: response.streamId});
433
+                    { desktopStream: response.streamId });
443
             } else {
434
             } else {
444
                 // As noted in Chrome Desktop Capture API:
435
                 // As noted in Chrome Desktop Capture API:
445
                 // If user didn't select any source (i.e. canceled the prompt)
436
                 // If user didn't select any source (i.e. canceled the prompt)
478
     // Initialize Chrome extension inline installs
469
     // Initialize Chrome extension inline installs
479
     initInlineInstalls(options);
470
     initInlineInstalls(options);
480
     // Check if extension is installed
471
     // Check if extension is installed
481
-    checkChromeExtInstalled(function (installed, updateRequired) {
472
+    checkChromeExtInstalled((installed, updateRequired) => {
482
         chromeExtInstalled = installed;
473
         chromeExtInstalled = installed;
483
         chromeExtUpdateRequired = updateRequired;
474
         chromeExtUpdateRequired = updateRequired;
484
         logger.info(
475
         logger.info(
487
     }, options);
478
     }, options);
488
 }
479
 }
489
 
480
 
481
+/**
482
+ * Checks "retries" times on every "waitInterval"ms whether the ext is alive.
483
+ * @param {Object} options the options passed to ScreanObtainer.obtainStream
484
+ * @param {int} waitInterval the number of ms between retries
485
+ * @param {int} retries the number of retries
486
+ * @returns {Promise} returns promise that will be resolved when the extension
487
+ * is alive and rejected if the extension is not alive even after "retries"
488
+ * checks
489
+ */
490
+function waitForExtensionAfterInstall(options, waitInterval, retries) {
491
+    if(retries === 0) {
492
+        return Promise.reject();
493
+    }
494
+    return new Promise((resolve, reject) => {
495
+        let currentRetries = retries;
496
+        let interval = window.setInterval(() => {
497
+            checkChromeExtInstalled( (installed) => {
498
+                if(installed) {
499
+                    window.clearInterval(interval);
500
+                    resolve();
501
+                } else {
502
+                    currentRetries--;
503
+                    if(currentRetries === 0) {
504
+                        reject();
505
+                        window.clearInterval(interval);
506
+                    }
507
+                }
508
+            }, options);
509
+        }, waitInterval);
510
+    });
511
+}
512
+
490
 /**
513
 /**
491
  * Starts the detection of an installed jidesha extension for firefox.
514
  * Starts the detection of an installed jidesha extension for firefox.
492
  * @param options supports "desktopSharingFirefoxDisabled",
515
  * @param options supports "desktopSharingFirefoxDisabled",
504
     }
527
     }
505
 
528
 
506
     var img = document.createElement('img');
529
     var img = document.createElement('img');
507
-    img.onload = function(){
530
+    img.onload = () => {
508
         logger.log("Detected firefox screen sharing extension.");
531
         logger.log("Detected firefox screen sharing extension.");
509
         firefoxExtInstalled = true;
532
         firefoxExtInstalled = true;
510
     };
533
     };
511
-    img.onerror = function(){
534
+    img.onerror = () => {
512
         logger.log("Detected lack of firefox screen sharing extension.");
535
         logger.log("Detected lack of firefox screen sharing extension.");
513
         firefoxExtInstalled = false;
536
         firefoxExtInstalled = false;
514
     };
537
     };

正在加载...
取消
保存