diff --git a/lib/polyfill/orientation.js b/lib/polyfill/orientation.js index bc5561f026..f8f2e0663a 100644 --- a/lib/polyfill/orientation.js +++ b/lib/polyfill/orientation.js @@ -6,6 +6,7 @@ goog.provide('shaka.polyfill.Orientation'); +goog.require('shaka.log'); goog.require('shaka.util.FakeEvent'); goog.require('shaka.util.FakeEventTarget'); goog.require('shaka.polyfill'); @@ -23,21 +24,47 @@ shaka.polyfill.Orientation = class { * @export */ static install() { - if (screen.orientation) { + if (screen.orientation && screen.orientation.unlock) { // Not needed. + shaka.log.info('Using native screen.orientation'); return; } - // There is no way to check to see if the 'orientationchange' event exists - // on window, which could theoretically lead to this making a - // screen.orientation object that doesn't actually work. - // However, it looks like all platforms that support the deprecated - // window.orientation feature also support that event. - if (window.orientation != undefined) { + if (screen.orientation != undefined) { + // There are some platforms where screen.orientation is defined but + // incomplete (e.g. Safari). + // Install a very simple polyfill in that case. + shaka.polyfill.Orientation.installBasedOnScreenMethods_(); + } else if (window.orientation != undefined) { + // There is no way to check to see if the 'orientationchange' event exists + // on window, which could theoretically lead to this making a + // screen.orientation object that doesn't actually work. + // However, it looks like all platforms that support the deprecated + // window.orientation feature also support that event. shaka.polyfill.Orientation.installBasedOnWindowMethods_(); } } + /** + * Modifies screen.orientation to add no-ops for missing methods. + * Meant for cases where screen.orientation is defined, but has missing + * methods that cannot be properly polyfilled. + * @private + */ + static installBasedOnScreenMethods_() { + if (screen.orientation.lock === undefined) { + screen.orientation.lock = (orientation) => { + shaka.log.info('screen.orientation.lock is a no-op'); + return Promise.resolve(); + }; + } + if (screen.orientation.unlock === undefined) { + screen.orientation.unlock = () => { + shaka.log.info('screen.orientation.unlock is a no-op'); + }; + } + } + /** * Makes a polyfill for orientation, based on window methods. * Note that some of the features this is based on are deprecated, so this