From 9c3a494a2826e24a60898ae47766a7a41c4cce2d Mon Sep 17 00:00:00 2001 From: "J. Koppen" Date: Tue, 4 Oct 2022 21:05:19 +0200 Subject: [PATCH] fix: Respect existing app usage of Cast SDK (#4523) Closes #4521 --- AUTHORS | 1 + CONTRIBUTORS | 1 + lib/cast/cast_sender.js | 53 ++++++++++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/AUTHORS b/AUTHORS index cfc62d4f2e..7594b07afd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -82,3 +82,4 @@ ViacomCBS <*@viacomcbs.com> Vincent Valot Wayne Morgan Raymond Cheng +Blue Billywig <*@bluebillywig.com> diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 47fe23ff05..9c4da40aee 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -122,3 +122,4 @@ Vincent Valot Wayne Morgan Yohann Connell Raymond Cheng +Janroel Koppen diff --git a/lib/cast/cast_sender.js b/lib/cast/cast_sender.js index 95e9aec6c5..f1b5e2f96a 100644 --- a/lib/cast/cast_sender.js +++ b/lib/cast/cast_sender.js @@ -192,21 +192,14 @@ shaka.cast.CastSender = class { // If the API becomes available before this instance dies, init will be // called again. - // A note about this value: In our testing environment, we load both - // uncompiled and compiled code. This global callback in uncompiled mode - // can be overwritten by the same in compiled mode. The two versions will - // each have their own instances_ map. Therefore the callback must have a - // name, as opposed to being anonymous. This way, the CastSender tests - // can invoke the named static method instead of using a global that could - // be overwritten. - if (!window.__onGCastApiAvailable) { - window.__onGCastApiAvailable = shaka.cast.CastSender.onSdkLoaded_; - } - if (window.__onGCastApiAvailable != shaka.cast.CastSender.onSdkLoaded_) { - shaka.log.alwaysWarn('A global Cast SDK hook is already installed! ' + - 'Shaka Player will be unable to receive a notification when the ' + - 'Cast SDK is ready.'); + // Check if our callback is already installed. + if (window.__onGCastApiAvailable !== CastSender.onGCastApiAvailable_) { + // Save pre-existing __onGCastApiAvailable in order to restore later. + CastSender.__onGCastApiAvailable_ = + window.__onGCastApiAvailable || null; + window.__onGCastApiAvailable = CastSender.onGCastApiAvailable_; } + return; } @@ -757,6 +750,9 @@ shaka.cast.CastSender.hasReceivers_ = false; /** @private {chrome.cast.Session} */ shaka.cast.CastSender.session_ = null; +/** @private {?function(boolean)} */ +shaka.cast.CastSender.__onGCastApiAvailable_ = null; + /** * A set of all living CastSender instances. The constructor and destroy * methods will add and remove instances from this set. @@ -784,3 +780,32 @@ shaka.cast.CastSender.onSdkLoaded_ = (loaded) => { } } }; + +/** + * @param {boolean} available + * @private + */ +shaka.cast.CastSender.onGCastApiAvailable_ = (available) => { + // Restore callback from saved. + if (shaka.cast.CastSender.__onGCastApiAvailable_) { + window.__onGCastApiAvailable = + shaka.cast.CastSender.__onGCastApiAvailable_; + } else { + delete window.__onGCastApiAvailable; + } + shaka.cast.CastSender.__onGCastApiAvailable_ = null; + + // A note about this value: In our testing environment, we load both + // uncompiled and compiled code. This global callback in uncompiled mode + // can be overwritten by the same in compiled mode. The two versions will + // each have their own instances_ map. Therefore the callback must have a + // name, as opposed to being anonymous. This way, the CastSender tests + // can invoke the named static method instead of using a global that could + // be overwritten. + shaka.cast.CastSender.onSdkLoaded_(available); + + // call restored callback (if any) + if (typeof window.__onGCastApiAvailable === 'function') { + window.__onGCastApiAvailable(available); + } +};