Skip to content

Commit

Permalink
fix: Avoid setting global Cast hook
Browse files Browse the repository at this point in the history
For applications that want to handle Cast on their own, we should
avoid setting the global Cast hook.  Now it will only be set if the UI
is configured with a Cast receiver ID or if the application uses
CastProxy explicitly (with or without the UI).

If an application tried to use our Cast features after installing
their own hook, but before the Cast SDK is ready, we will now issue a
warning to alert the application developer of the issue.

Closes shaka-project#3167

Change-Id: Ic3f57ff257652c057f8322811c8db1d5ad3a8463
  • Loading branch information
joeyparrish committed Feb 26, 2021
1 parent 0f9bf71 commit ecbeae0
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions lib/cast/cast_sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,36 @@ shaka.cast.CastSender = class {
init() {
const CastSender = shaka.cast.CastSender;

// Check for the cast API and the receiver id.
if (!window.chrome || !chrome.cast || !chrome.cast.isAvailable ||
!this.receiverAppId_.length) {
// Return if either:
// 1) The API is not available on this platform or is not ready yet.
// 2) No cast receiver id has been provided.
// If the API becomes available before this instance dies, or the reveiver
// id changes, init() will be called again.
if (!this.receiverAppId_.length) {
// Return if no cast receiver id has been provided.
// Nothing will be initialized, no global hooks will be installed.
// If the receiver ID changes before this instance dies, init will be
// called again.
return;
}

// Check for the cast API.
if (!window.chrome || !chrome.cast || !chrome.cast.isAvailable) {
// If the API is not available on this platform or is not ready yet,
// install a hook to be notified when it becomes available.
// 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.');
}
return;
}

Expand Down Expand Up @@ -752,11 +774,3 @@ shaka.cast.CastSender.onSdkLoaded_ = (loaded) => {
}
}
};

// The cast SDK will invoke this global callback. Since 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 we gave the callback a name that the CastSender tests could invoke
// instead of using a global that could be overwritten.
window.__onGCastApiAvailable = shaka.cast.CastSender.onSdkLoaded_;

0 comments on commit ecbeae0

Please sign in to comment.