Source: lib/offline/offline_uri.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.offline.OfflineUri');
  18. /**
  19. * The OfflineUri class contains all the components that make up the offline
  20. * uri. The components are:
  21. * TYPE: Used to know what type of data the uri points to. It can either
  22. * be "manifest" or "segment".
  23. * MECHANISM: The name of the mechanism that manages the storage cell that
  24. * holds the data.
  25. * CELL: The name of the cell that holds the data.
  26. * KEY: The key that the data is stored under in the cell.
  27. */
  28. shaka.offline.OfflineUri = class {
  29. /**
  30. * @param {string} type
  31. * @param {string} mechanism
  32. * @param {string} cell
  33. * @param {number} key
  34. */
  35. constructor(type, mechanism, cell, key) {
  36. /**
  37. * @private {string}
  38. * @const
  39. */
  40. this.type_ = type;
  41. /**
  42. * @private {string}
  43. * @const
  44. */
  45. this.mechanism_ = mechanism;
  46. /**
  47. * @private {string}
  48. * @const
  49. */
  50. this.cell_ = cell;
  51. /**
  52. * @private {number}
  53. * @const
  54. */
  55. this.key_ = key;
  56. /**
  57. * @private {string}
  58. * @const
  59. */
  60. this.asString_ = [
  61. 'offline:', type, '/', mechanism, '/', cell, '/', key,
  62. ].join('');
  63. }
  64. /** @return {boolean} */
  65. isManifest() { return this.type_ == 'manifest'; }
  66. /** @return {boolean} */
  67. isSegment() { return this.type_ == 'segment'; }
  68. /** @return {string} */
  69. mechanism() { return this.mechanism_; }
  70. /** @return {string} */
  71. cell() { return this.cell_; }
  72. /** @return {number} */
  73. key() { return this.key_; }
  74. /** @override */
  75. toString() { return this.asString_; }
  76. /**
  77. * @param {string} uri
  78. * @return {?shaka.offline.OfflineUri}
  79. */
  80. static parse(uri) {
  81. let parts = /^offline:([a-z]+)\/([^/]+)\/([^/]+)\/([0-9]+)$/.exec(uri);
  82. if (parts == null) { return null; }
  83. let type = parts[1];
  84. if (type != 'manifest' && type != 'segment') { return null; }
  85. let mechanism = parts[2];
  86. if (!mechanism) { return null; }
  87. let cell = parts[3];
  88. if (!cell) { return null; }
  89. let key = Number(parts[4]);
  90. if (type == null) { return null; }
  91. return new shaka.offline.OfflineUri(type, mechanism, cell, key);
  92. }
  93. /**
  94. * @param {string} mechanism
  95. * @param {string} cell
  96. * @param {number} key
  97. * @return {!shaka.offline.OfflineUri}
  98. */
  99. static manifest(mechanism, cell, key) {
  100. return new shaka.offline.OfflineUri('manifest', mechanism, cell, key);
  101. }
  102. /**
  103. * @param {string} mechanism
  104. * @param {string} cell
  105. * @param {number} key
  106. * @return {!shaka.offline.OfflineUri}
  107. */
  108. static segment(mechanism, cell, key) {
  109. return new shaka.offline.OfflineUri('segment', mechanism, cell, key);
  110. }
  111. };