Ver código fonte

fix(SS): implement interval to check the status of the ext after install

master
hristoterezov 9 anos atrás
pai
commit
8fe23a63bc
1 arquivos alterados com 53 adições e 21 exclusões
  1. 53
    21
      modules/RTC/ScreenObtainer.js

+ 53
- 21
modules/RTC/ScreenObtainer.js Ver arquivo

@@ -226,7 +226,6 @@ var ScreenObtainer = {
226 226
      * 'desktop' stream for returned stream token.
227 227
      */
228 228
     obtainScreenFromExtension: function(options, streamCallback, failCallback) {
229
-        var self = this;
230 229
         if (chromeExtInstalled) {
231 230
             doGetStreamFromExtension(this.options, streamCallback,
232 231
                 failCallback);
@@ -240,15 +239,21 @@ 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 245
                         // We need to give a moment for the endpoint to become
247 246
                         // available
248
-                        window.setTimeout(function () {
249
-                            doGetStreamFromExtension(self.options,
250
-                                streamCallback, failCallback);
251
-                        }, 2000);
247
+                        waitForExtensionAfterInstall(this.options, 200, 10)
248
+                            .then(() => {
249
+                                doGetStreamFromExtension(this.options,
250
+                                    streamCallback, failCallback);
251
+                            }).catch(() => {
252
+                                // options param is {} because we won't do
253
+                                // external install in this case.
254
+                                this.handleExtensionInstallationError({},
255
+                                    streamCallback, failCallback);
256
+                            });
252 257
                     },
253 258
                     this.handleExtensionInstallationError.bind(this,
254 259
                         options, streamCallback, failCallback)
@@ -287,21 +292,16 @@ var ScreenObtainer = {
287 292
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
288 293
             return;
289 294
         }
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);
295
+        waitForExtensionAfterInstall(this.options, options.interval, 1)
296
+            .then(() => {
297
+                chromeExtInstalled = true;
298
+                options.listener("extensionFound");
299
+                this.obtainScreenFromExtension(options,
300
+                    streamCallback, failCallback);
301
+            }).catch(() => {
302
+                this.checkForChromeExtensionOnInterval(options,
303
+                    streamCallback, failCallback);
304
+            });
305 305
     }
306 306
 };
307 307
 
@@ -487,6 +487,38 @@ function initChromeExtension(options) {
487 487
     }, options);
488 488
 }
489 489
 
490
+/**
491
+ * Checks "retries" times on every "waitInterval"ms whether the ext is alive.
492
+ * @param {Object} options the options passed to ScreanObtainer.obtainStream
493
+ * @param {int} waitInterval the number of ms between retries
494
+ * @param {int} retries the number of retries
495
+ * @returns {Promise} returns promise that will be resolved when the extension
496
+ * is alive and rejected if the extension is not alive even after "retries"
497
+ * checks
498
+ */
499
+function waitForExtensionAfterInstall(options, waitInterval, retries) {
500
+    if(retries === 0) {
501
+        return Promise.reject();
502
+    }
503
+    return new Promise((resolve, reject) => {
504
+        let currentRetries = retries;
505
+        let interval = window.setInterval(() => {
506
+            checkChromeExtInstalled( (installed) => {
507
+                if(installed) {
508
+                    window.clearInterval(interval);
509
+                    resolve();
510
+                } else {
511
+                    currentRetries--;
512
+                    if(currentRetries === 0) {
513
+                        reject();
514
+                        window.clearInterval(interval);
515
+                    }
516
+                }
517
+            }, options);
518
+        }, waitInterval);
519
+    });
520
+}
521
+
490 522
 /**
491 523
  * Starts the detection of an installed jidesha extension for firefox.
492 524
  * @param options supports "desktopSharingFirefoxDisabled",

Carregando…
Cancelar
Salvar