Skip to content

Commit

Permalink
fix: Respect existing app usage of Cast SDK (#4523)
Browse files Browse the repository at this point in the history
Closes #4521
  • Loading branch information
jkoppen-bluebillywig authored and JulianDomingo committed Oct 7, 2022
1 parent deda68f commit d2258d6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ ViacomCBS <*@viacomcbs.com>
Vincent Valot <valot.vince@gmail.com>
Wayne Morgan <wayne.morgan.dev@gmail.com>
Raymond Cheng <raycheng100@gmail.com>
Blue Billywig <*@bluebillywig.com>
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ Vincent Valot <valot.vince@gmail.com>
Wayne Morgan <wayne.morgan.dev@gmail.com>
Yohann Connell <robinconnell@google.com>
Raymond Cheng <raycheng100@gmail.com>
Janroel Koppen <j.koppen@bluebillywig.com>
53 changes: 39 additions & 14 deletions lib/cast/cast_sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,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;
}

Expand Down Expand Up @@ -747,6 +740,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.
Expand Down Expand Up @@ -774,3 +770,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);
}
};

0 comments on commit d2258d6

Please sign in to comment.