Browse Source

Merge pull request #268 from jitsi/ss_postinstall_fix

fix(SS): implement interval to check the status of the ext after install
dev1
Любомир Маринов 9 years ago
parent
commit
2112e1b05d
1 changed files with 93 additions and 70 deletions
  1. 93
    70
      modules/RTC/ScreenObtainer.js

+ 93
- 70
modules/RTC/ScreenObtainer.js View File

@@ -1,11 +1,11 @@
1 1
 /* global chrome, $, alert */
2 2
 
3
+var AdapterJS = require("./adapter.screenshare");
4
+var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
3 5
 var logger = require("jitsi-meet-logger").getLogger(__filename);
4 6
 var RTCBrowserType = require("./RTCBrowserType");
5
-var AdapterJS = require("./adapter.screenshare");
6 7
 import JitsiTrackError from "../../JitsiTrackError";
7 8
 import * as JitsiTrackErrors from "../../JitsiTrackErrors";
8
-var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
9 9
 
10 10
 /**
11 11
  * Indicates whether the Chrome desktop sharing extension is installed.
@@ -54,6 +54,7 @@ const CHROME_NO_EXTENSION_ERROR_MSG // eslint-disable-line no-unused-vars
54 54
  */
55 55
 var ScreenObtainer = {
56 56
     obtainStream: null,
57
+
57 58
     /**
58 59
      * Initializes the function used to obtain a screen capture
59 60
      * (this.obtainStream).
@@ -68,7 +69,7 @@ var ScreenObtainer = {
68 69
      * @param options {object}
69 70
      * @param gum {Function} GUM method
70 71
      */
71
-    init: function(options, gum) {
72
+    init(options, gum) {
72 73
         var obtainDesktopStream = null;
73 74
         this.options = options = options || {};
74 75
         GUM = gum;
@@ -81,9 +82,10 @@ var ScreenObtainer = {
81 82
             (options.desktopSharingChromeMethod || options.desktopSharing);
82 83
 
83 84
         if (RTCBrowserType.isNWJS()) {
84
-            obtainDesktopStream = function (options, onSuccess, onFailure) {
85
+            obtainDesktopStream = (options, onSuccess, onFailure) => {
85 86
                 window.JitsiMeetNW.obtainDesktopStream (
86
-                    onSuccess, function (error, constraints) {
87
+                    onSuccess,
88
+                    (error, constraints) => {
87 89
                         var jitsiError;
88 90
                         // FIXME:
89 91
                         // This is very very durty fix for recognising that the
@@ -148,7 +150,6 @@ var ScreenObtainer = {
148 150
             } else {
149 151
                 obtainDesktopStream = this.obtainScreenOnFirefox;
150 152
             }
151
-
152 153
         }
153 154
 
154 155
         if (!obtainDesktopStream) {
@@ -163,17 +164,16 @@ var ScreenObtainer = {
163 164
      * environment.
164 165
      * @returns {boolean}
165 166
      */
166
-    isSupported: function() {
167
+    isSupported() {
167 168
         return !!this.obtainStream;
168 169
     },
170
+
169 171
     /**
170 172
      * Obtains a screen capture stream on Firefox.
171 173
      * @param callback
172 174
      * @param errorCallback
173 175
      */
174
-    obtainScreenOnFirefox:
175
-           function (options, callback, errorCallback) {
176
-        var self = this;
176
+    obtainScreenOnFirefox(options, callback, errorCallback) {
177 177
         var extensionRequired = false;
178 178
         if (this.options.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
179 179
             (this.options.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
@@ -198,13 +198,12 @@ var ScreenObtainer = {
198 198
         // extension if it hasn't.
199 199
         if (firefoxExtInstalled === null) {
200 200
             window.setTimeout(
201
-                function() {
201
+                () => {
202 202
                     if (firefoxExtInstalled === null)
203 203
                         firefoxExtInstalled = false;
204
-                    self.obtainScreenOnFirefox(callback, errorCallback);
204
+                    this.obtainScreenOnFirefox(callback, errorCallback);
205 205
                 },
206
-                300
207
-            );
206
+                300);
208 207
             logger.log("Waiting for detection of jidesha on firefox to " +
209 208
                 "finish.");
210 209
             return;
@@ -221,12 +220,12 @@ var ScreenObtainer = {
221 220
         errorCallback(
222 221
             new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
223 222
     },
223
+
224 224
     /**
225 225
      * Asks Chrome extension to call chooseDesktopMedia and gets chrome
226 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 229
         if (chromeExtInstalled) {
231 230
             doGetStreamFromExtension(this.options, streamCallback,
232 231
                 failCallback);
@@ -240,15 +239,19 @@ var ScreenObtainer = {
240 239
             try {
241 240
                 chrome.webstore.install(
242 241
                     getWebStoreInstallUrl(this.options),
243
-                    function (arg) {
242
+                    arg => {
244 243
                         logger.log("Extension installed successfully", arg);
245 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 256
                     this.handleExtensionInstallationError.bind(this,
254 257
                         options, streamCallback, failCallback)
@@ -259,54 +262,48 @@ var ScreenObtainer = {
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 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 283
         failCallback(new JitsiTrackError(
279 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 289
         if (options.checkAgain() === false) {
286 290
             failCallback(new JitsiTrackError(
287 291
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
288 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 308
  * Obtains a desktop stream using getUserMedia.
312 309
  * For this to work on Chrome, the
@@ -317,11 +314,7 @@ var ScreenObtainer = {
317 314
  * 'about:config'.
318 315
  */
319 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,7 +379,7 @@ function checkChromeExtInstalled(callback, options) {
386 379
         //TODO: remove chromeExtensionId (deprecated)
387 380
         (options.desktopSharingChromeExtId || options.chromeExtensionId),
388 381
         { getVersion: true },
389
-        function (response) {
382
+        response => {
390 383
             if (!response || !response.version) {
391 384
                 // Communication failure - assume that no endpoint exists
392 385
                 logger.warn(
@@ -420,7 +413,7 @@ function doGetStreamFromExtension(options, streamCallback, failCallback) {
420 413
             sources: (options.desktopSharingChromeSources ||
421 414
                 options.desktopSharingSources)
422 415
         },
423
-        function (response) {
416
+        response => {
424 417
             if (!response) {
425 418
                 // possibly re-wraping error message to make code consistent
426 419
                 var lastError = chrome.runtime.lastError;
@@ -435,11 +428,9 @@ function doGetStreamFromExtension(options, streamCallback, failCallback) {
435 428
             if (response.streamId) {
436 429
                 GUM(
437 430
                     ['desktop'],
438
-                    function (stream) {
439
-                        streamCallback(stream);
440
-                    },
431
+                    stream => streamCallback(stream),
441 432
                     failCallback,
442
-                    {desktopStream: response.streamId});
433
+                    { desktopStream: response.streamId });
443 434
             } else {
444 435
                 // As noted in Chrome Desktop Capture API:
445 436
                 // If user didn't select any source (i.e. canceled the prompt)
@@ -478,7 +469,7 @@ function initChromeExtension(options) {
478 469
     // Initialize Chrome extension inline installs
479 470
     initInlineInstalls(options);
480 471
     // Check if extension is installed
481
-    checkChromeExtInstalled(function (installed, updateRequired) {
472
+    checkChromeExtInstalled((installed, updateRequired) => {
482 473
         chromeExtInstalled = installed;
483 474
         chromeExtUpdateRequired = updateRequired;
484 475
         logger.info(
@@ -487,6 +478,38 @@ function initChromeExtension(options) {
487 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 514
  * Starts the detection of an installed jidesha extension for firefox.
492 515
  * @param options supports "desktopSharingFirefoxDisabled",
@@ -504,11 +527,11 @@ function initFirefoxExtensionDetection(options) {
504 527
     }
505 528
 
506 529
     var img = document.createElement('img');
507
-    img.onload = function(){
530
+    img.onload = () => {
508 531
         logger.log("Detected firefox screen sharing extension.");
509 532
         firefoxExtInstalled = true;
510 533
     };
511
-    img.onerror = function(){
534
+    img.onerror = () => {
512 535
         logger.log("Detected lack of firefox screen sharing extension.");
513 536
         firefoxExtInstalled = false;
514 537
     };

Loading…
Cancel
Save