Source: lib/polyfill/storage_estimate.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.StorageEstimate');
  7. goog.require('shaka.polyfill');
  8. /**
  9. * @summary A polyfill to provide navigator.storage.estimate in old
  10. * webkit browsers.
  11. * See: https://developers.google.com/web/updates/2017/08/estimating-available-storage-space#the-present
  12. * @export
  13. */
  14. shaka.polyfill.StorageEstimate = class {
  15. /**
  16. * Install the polyfill if needed.
  17. * @export
  18. */
  19. static install() {
  20. if (navigator.storage && navigator.storage.estimate) {
  21. // No need.
  22. return;
  23. }
  24. if (navigator.webkitTemporaryStorage &&
  25. navigator.webkitTemporaryStorage.queryUsageAndQuota) {
  26. if (!('storage' in navigator)) {
  27. navigator.storage = /** @type {!StorageManager} */ ({});
  28. }
  29. navigator.storage.estimate =
  30. shaka.polyfill.StorageEstimate.storageEstimate_;
  31. }
  32. }
  33. /**
  34. * @this {StorageManager}
  35. * @return {!Promise}
  36. * @private
  37. */
  38. static storageEstimate_() {
  39. return new Promise((resolve, reject) => {
  40. navigator.webkitTemporaryStorage.queryUsageAndQuota(
  41. (usage, quota) => {
  42. resolve({usage: usage, quota: quota});
  43. },
  44. reject,
  45. );
  46. });
  47. }
  48. };
  49. shaka.polyfill.register(shaka.polyfill.StorageEstimate.install);