From bcb4b56b8255f06a976ec78bd9b61693197e0448 Mon Sep 17 00:00:00 2001 From: Alvaro Velad Date: Tue, 15 Dec 2020 12:39:27 +0100 Subject: [PATCH 1/4] Add navigator.storage.estimate polyfill --- build/types/polyfill | 1 + lib/polyfill/storage_estimate.js | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 lib/polyfill/storage_estimate.js diff --git a/build/types/polyfill b/build/types/polyfill index 312fed509b..1476a2cdd0 100644 --- a/build/types/polyfill +++ b/build/types/polyfill @@ -13,6 +13,7 @@ +../../lib/polyfill/patchedmediakeys_nop.js +../../lib/polyfill/patchedmediakeys_webkit.js +../../lib/polyfill/pip_webkit.js ++../../lib/polyfill/storage_estimate.js +../../lib/polyfill/video_play_promise.js +../../lib/polyfill/videoplaybackquality.js +../../lib/polyfill/vttcue.js diff --git a/lib/polyfill/storage_estimate.js b/lib/polyfill/storage_estimate.js new file mode 100644 index 0000000000..4252ca3fc3 --- /dev/null +++ b/lib/polyfill/storage_estimate.js @@ -0,0 +1,54 @@ +/*! @license + * Shaka Player + * Copyright 2016 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +goog.provide('shaka.polyfill.StorageEstimate'); + +goog.require('shaka.polyfill'); + + +/** + * @summary A polyfill to provide navigator.storage.estimate in old + * webkit browsers. + */ +shaka.polyfill.StorageEstimate = class { + /** + * Install the polyfill if needed. + */ + static install() { + if ('storage' in navigator && 'estimate' in navigator.storage) { + // No need. + return; + } + + if ('webkitTemporaryStorage' in navigator && + 'queryUsageAndQuota' in navigator.webkitTemporaryStorage) { + if (!('storage' in navigator)) { + navigator.storage = /** @type {StorageManager} */ {}; + } + navigator.storage.estimate = + shaka.polyfill.StorageEstimate.storageEstimate_; + } + } + + /** + * @this {StorageManager} + * @return {!Promise} + * @private + */ + static storageEstimate_() { + return new Promise((resolve, reject) => { + navigator.webkitTemporaryStorage.queryUsageAndQuota( + (usage, quota) => { + resolve({usage: usage, quota: quota}); + }, + reject, + ); + }); + } +}; + + +shaka.polyfill.register(shaka.polyfill.StorageEstimate.install); From 3e764d661cd6283627a43ac04eafd47d4411ae2c Mon Sep 17 00:00:00 2001 From: Alvaro Velad Date: Tue, 15 Dec 2020 20:02:03 +0100 Subject: [PATCH 2/4] Fix indent --- lib/polyfill/storage_estimate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/polyfill/storage_estimate.js b/lib/polyfill/storage_estimate.js index 4252ca3fc3..5cf636d1dc 100644 --- a/lib/polyfill/storage_estimate.js +++ b/lib/polyfill/storage_estimate.js @@ -29,7 +29,7 @@ shaka.polyfill.StorageEstimate = class { navigator.storage = /** @type {StorageManager} */ {}; } navigator.storage.estimate = - shaka.polyfill.StorageEstimate.storageEstimate_; + shaka.polyfill.StorageEstimate.storageEstimate_; } } From 92f5350dc3c5688997aaea3bc348bae58ad9369e Mon Sep 17 00:00:00 2001 From: Alvaro Velad Date: Tue, 15 Dec 2020 20:02:19 +0100 Subject: [PATCH 3/4] Add link --- lib/polyfill/storage_estimate.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/polyfill/storage_estimate.js b/lib/polyfill/storage_estimate.js index 5cf636d1dc..4436bb4781 100644 --- a/lib/polyfill/storage_estimate.js +++ b/lib/polyfill/storage_estimate.js @@ -12,6 +12,7 @@ goog.require('shaka.polyfill'); /** * @summary A polyfill to provide navigator.storage.estimate in old * webkit browsers. + * See: https://developers.google.com/web/updates/2017/08/estimating-available-storage-space#the-present */ shaka.polyfill.StorageEstimate = class { /** From 9b09c1c55a90093a3b5be8649c95dc35e9405359 Mon Sep 17 00:00:00 2001 From: Alvaro Velad Date: Tue, 15 Dec 2020 20:02:42 +0100 Subject: [PATCH 4/4] Use fields directly --- lib/polyfill/storage_estimate.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/polyfill/storage_estimate.js b/lib/polyfill/storage_estimate.js index 4436bb4781..1143ecc69b 100644 --- a/lib/polyfill/storage_estimate.js +++ b/lib/polyfill/storage_estimate.js @@ -19,15 +19,15 @@ shaka.polyfill.StorageEstimate = class { * Install the polyfill if needed. */ static install() { - if ('storage' in navigator && 'estimate' in navigator.storage) { + if (navigator.storage && navigator.storage.estimate) { // No need. return; } - if ('webkitTemporaryStorage' in navigator && - 'queryUsageAndQuota' in navigator.webkitTemporaryStorage) { + if (navigator.webkitTemporaryStorage && + navigator.webkitTemporaryStorage.queryUsageAndQuota) { if (!('storage' in navigator)) { - navigator.storage = /** @type {StorageManager} */ {}; + navigator.storage = /** @type {!StorageManager} */ ({}); } navigator.storage.estimate = shaka.polyfill.StorageEstimate.storageEstimate_;