Files
shaka-player/lib/polyfill/storage_estimate.js
T
vodlogic 52f18df21b chore: Add @export annotations to polyfill install methods (#3660)
This change allows polyfills to be installed individually e.g. shaka.polyfill.MediaCapabilities.install() instead of shaka.polyfill.installAll()

Related to #2625
2021-09-23 14:27:50 -07:00

58 lines
1.3 KiB
JavaScript

/*! @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.
* See: https://developers.google.com/web/updates/2017/08/estimating-available-storage-space#the-present
* @export
*/
shaka.polyfill.StorageEstimate = class {
/**
* Install the polyfill if needed.
* @export
*/
static install() {
if (navigator.storage && navigator.storage.estimate) {
// No need.
return;
}
if (navigator.webkitTemporaryStorage &&
navigator.webkitTemporaryStorage.queryUsageAndQuota) {
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);