-
Notifications
You must be signed in to change notification settings - Fork 272
Auto Upgrade DRM Embeds #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto Upgrade DRM Embeds #1095
Changes from all commits
752992d
84df165
e706e4c
7c4d6d1
e76227a
a7fdb32
a443596
74d3a99
5bc81f3
810b039
5a01123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |||||
| */ | ||||||
|
|
||||||
| import Player from '../player'; | ||||||
| import { isVimeoUrl, isVimeoEmbed, getVimeoUrl, getOembedDomain } from './functions'; | ||||||
| import { isVimeoUrl, getVimeoUrl, getOembedDomain, isVimeoEmbed, findIframeBySourceWindow } from './functions'; | ||||||
| import { parseMessageData } from './postmessage'; | ||||||
|
|
||||||
| const oEmbedParameters = [ | ||||||
|
|
@@ -224,19 +224,13 @@ export function resizeEmbeds(parent = document) { | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const iframes = parent.querySelectorAll('iframe'); | ||||||
|
|
||||||
| for (let i = 0; i < iframes.length; i++) { | ||||||
| if (iframes[i].contentWindow !== event.source) { | ||||||
| continue; | ||||||
| } | ||||||
| const senderIFrame = event.source ? findIframeBySourceWindow(event.source, parent) : null; | ||||||
|
|
||||||
| if (senderIFrame) { | ||||||
| // Change padding-bottom of the enclosing div to accommodate | ||||||
| // card carousel without distorting aspect ratio | ||||||
| const space = iframes[i].parentElement; | ||||||
| const space = senderIFrame.parentElement; | ||||||
| space.style.paddingBottom = `${event.data.data[0].bottom}px`; | ||||||
|
|
||||||
| break; | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -266,16 +260,12 @@ export function initAppendVideoMetadata(parent = document) { | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const iframes = parent.querySelectorAll('iframe'); | ||||||
| for (let i = 0; i < iframes.length; i++) { | ||||||
| const iframe = iframes[i]; | ||||||
| const senderIFrame = event.source ? findIframeBySourceWindow(event.source, parent) : null; | ||||||
|
|
||||||
| // Initiate appendVideoMetadata if iframe is a Vimeo embed | ||||||
| const isValidMessageSource = iframe.contentWindow === event.source; | ||||||
| if (isVimeoEmbed(iframe.src) && isValidMessageSource) { | ||||||
| const player = new Player(iframe); | ||||||
| player.callMethod('appendVideoMetadata', window.location.href); | ||||||
| } | ||||||
| // Initiate appendVideoMetadata if iframe is a Vimeo embed | ||||||
| if (senderIFrame && isVimeoEmbed(senderIFrame.src)) { | ||||||
| const player = new Player(senderIFrame); | ||||||
| player.callMethod('appendVideoMetadata', window.location.href); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -311,25 +301,75 @@ export function checkUrlTimeParam(parent = document) { | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const iframes = parent.querySelectorAll('iframe'); | ||||||
| for (let i = 0; i < iframes.length; i++) { | ||||||
| const iframe = iframes[i]; | ||||||
| const isValidMessageSource = iframe.contentWindow === event.source; | ||||||
|
|
||||||
| if (isVimeoEmbed(iframe.src) && isValidMessageSource) { | ||||||
| const player = new Player(iframe); | ||||||
| player | ||||||
| .getVideoId() | ||||||
| .then((videoId) => { | ||||||
| const matches = new RegExp(`[?&]vimeo_t_${videoId}=([^&#]*)`).exec(window.location.href); | ||||||
| if (matches && matches[1]) { | ||||||
| const sec = decodeURI(matches[1]); | ||||||
| player.setCurrentTime(sec); | ||||||
| } | ||||||
| return; | ||||||
| }) | ||||||
| .catch(handleError); | ||||||
| } | ||||||
| const senderIFrame = event.source ? findIframeBySourceWindow(event.source, parent) : null; | ||||||
|
|
||||||
| if (senderIFrame && isVimeoEmbed(senderIFrame.src)) { | ||||||
| const player = new Player(senderIFrame); | ||||||
| player | ||||||
| .getVideoId() | ||||||
| .then((videoId) => { | ||||||
| const matches = new RegExp(`[?&]vimeo_t_${videoId}=([^&#]*)`).exec(window.location.href); | ||||||
| if (matches && matches[1]) { | ||||||
| const sec = decodeURI(matches[1]); | ||||||
| player.setCurrentTime(sec); | ||||||
| } | ||||||
| return; | ||||||
| }) | ||||||
| .catch(handleError); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| window.addEventListener('message', onMessage); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Updates iframe embeds to support DRM content playback by adding the 'encrypted-media' permission | ||||||
| * to the iframe's allow attribute when DRM initialization fails. This function acts as a fallback | ||||||
| * mechanism to enable playback of DRM-protected content in embeds that weren't properly configured. | ||||||
| * | ||||||
| * @return {void} | ||||||
| */ | ||||||
| export function updateDRMEmbeds() { | ||||||
| if (window.VimeoDRMEmbedsUpdated) { | ||||||
| return; | ||||||
| } | ||||||
| window.VimeoDRMEmbedsUpdated = true; | ||||||
|
|
||||||
| /** | ||||||
| * Handle message events for DRM initialization failures | ||||||
| * @param {MessageEvent} event - The message event from the iframe | ||||||
| */ | ||||||
| const onMessage = (event) => { | ||||||
| if (!isVimeoUrl(event.origin)) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const data = parseMessageData(event.data); | ||||||
| if (!data || data.event !== 'drminitfailed') { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const senderIFrame = event.source ? findIframeBySourceWindow(event.source) : null; | ||||||
|
|
||||||
| if (!senderIFrame) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const currentAllow = senderIFrame.getAttribute('allow') || ''; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit. |
||||||
| const allowSupportsDRM = currentAllow.includes('encrypted-media'); | ||||||
|
|
||||||
| if (!allowSupportsDRM) { | ||||||
| // For DRM playback to successfully occur, the iframe `allow` attribute must include 'encrypted-media'. | ||||||
| // If the video requires DRM but doesn't have the attribute, we try to add on behalf of the embed owner | ||||||
| // as a temporary measure to enable playback until they're able to update their embeds. | ||||||
| senderIFrame.setAttribute('allow', `${currentAllow}; encrypted-media`); | ||||||
| const currentUrl = new URL(senderIFrame.getAttribute('src')); | ||||||
|
|
||||||
| // Adding this forces the embed to reload once `allow` has been updated with `encrypted-media`. | ||||||
| currentUrl.searchParams.set('forcereload', 'drm'); | ||||||
| senderIFrame.setAttribute('src', currentUrl.toString()); | ||||||
| return; | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,3 +171,26 @@ export const logSurveyLink = () => { | |
| 'color:#aaa;font-size:0.8em;' | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Find the iframe element that contains a specific source window | ||
| * | ||
| * @param {Window} sourceWindow The source window to find the iframe for | ||
| * @param {Document} [doc=document] The document to search within | ||
| * @return {HTMLIFrameElement|null} The iframe element if found, otherwise null | ||
| */ | ||
| export function findIframeBySourceWindow(sourceWindow, doc = document) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| if (!sourceWindow || !doc || typeof doc.querySelectorAll !== 'function') { | ||
| return null; | ||
| } | ||
|
|
||
| const iframes = doc.querySelectorAll('iframe'); | ||
|
|
||
| for (let i = 0; i < iframes.length; i++) { | ||
| if (iframes[i] && iframes[i].contentWindow === sourceWindow) { | ||
| return iframes[i]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * Helper function to trigger a message handler for testing | ||
| * This allows testing of functions that add message event listeners to the window | ||
| * without having to mock the entire MessageEvent infrastructure | ||
| * | ||
| * @param {Function} setupListener - Function that registers a message event handler (e.g., updateDRMEmbeds) | ||
| * @param {Object} mockEvent - The mock event object to pass to the handler | ||
| */ | ||
| export function triggerMessageHandler(setupListener, mockEvent) { | ||
| const originalAddEventListener = window.addEventListener; | ||
|
|
||
| let messageHandler; | ||
|
|
||
| window.addEventListener = function(eventName, handler) { | ||
| if (eventName === 'message') { | ||
| messageHandler = handler; | ||
| } | ||
| return originalAddEventListener.apply(this, arguments); | ||
| }; | ||
|
|
||
| setupListener(); | ||
|
|
||
| window.addEventListener = originalAddEventListener; | ||
|
|
||
| if (messageHandler) { | ||
| messageHandler(mockEvent); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a mock DRM initialization failure event | ||
| * | ||
| * @param {string} origin - The origin of the message | ||
| * @param {Window} sourceWindow - The source window (iframe.contentWindow) | ||
| * @returns {Object} - A mock message event object | ||
| */ | ||
| export function createDRMInitFailedEvent(origin, sourceWindow) { | ||
| return { | ||
| origin, | ||
| source: sourceWindow, | ||
| data: JSON.stringify({ | ||
| event: 'drminitfailed' | ||
| }) | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit.
findIframeBySourceWindowalready returns null if event.source is falsy.