Source: lib/offline/offline_scheme.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.OfflineScheme');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.net.NetworkingEngine');
  20. goog.require('shaka.offline.OfflineUri');
  21. goog.require('shaka.offline.StorageMuxer');
  22. goog.require('shaka.util.AbortableOperation');
  23. goog.require('shaka.util.Error');
  24. goog.require('shaka.util.IDestroyable');
  25. /**
  26. * @namespace
  27. * @summary A plugin that handles requests for offline content.
  28. * @param {string} uri
  29. * @param {shakaExtern.Request} request
  30. * @param {shaka.net.NetworkingEngine.RequestType=} requestType
  31. * @return {!shakaExtern.IAbortableOperation.<shakaExtern.Response>}
  32. * @export
  33. */
  34. shaka.offline.OfflineScheme = function(uri, request, requestType) {
  35. let offlineUri = shaka.offline.OfflineUri.parse(uri);
  36. if (offlineUri && offlineUri.isManifest()) {
  37. return shaka.offline.OfflineScheme.getManifest_(uri);
  38. }
  39. if (offlineUri && offlineUri.isSegment()) {
  40. return shaka.offline.OfflineScheme.getSegment_(
  41. offlineUri.key(), offlineUri);
  42. }
  43. return shaka.util.AbortableOperation.failed(
  44. new shaka.util.Error(
  45. shaka.util.Error.Severity.CRITICAL,
  46. shaka.util.Error.Category.NETWORK,
  47. shaka.util.Error.Code.MALFORMED_OFFLINE_URI,
  48. uri));
  49. };
  50. /**
  51. * @param {string} uri
  52. * @return {!shakaExtern.IAbortableOperation.<shakaExtern.Response>}
  53. * @private
  54. */
  55. shaka.offline.OfflineScheme.getManifest_ = function(uri) {
  56. /** @type {shakaExtern.Response} */
  57. let response = {
  58. uri: uri,
  59. data: new ArrayBuffer(0),
  60. headers: {'content-type': 'application/x-offline-manifest'}
  61. };
  62. return shaka.util.AbortableOperation.completed(response);
  63. };
  64. /**
  65. * @param {number} id
  66. * @param {!shaka.offline.OfflineUri} uri
  67. * @return {!shakaExtern.IAbortableOperation.<shakaExtern.Response>}
  68. * @private
  69. */
  70. shaka.offline.OfflineScheme.getSegment_ = function(id, uri) {
  71. goog.asserts.assert(
  72. uri.isSegment(),
  73. 'Only segment uri\'s should be given to getSegment');
  74. let muxer = new shaka.offline.StorageMuxer();
  75. let promise = shaka.util.IDestroyable.with([muxer], async () => {
  76. await muxer.init();
  77. let cell = await muxer.getCell(uri.mechanism(), uri.cell());
  78. let segments = await cell.getSegments([uri.key()]);
  79. let segment = segments[0];
  80. return {
  81. uri: uri,
  82. data: segment.data,
  83. headers: {}
  84. };
  85. });
  86. // TODO: Support abort() in OfflineScheme.
  87. return shaka.util.AbortableOperation.notAbortable(promise);
  88. };
  89. shaka.net.NetworkingEngine.registerScheme(
  90. 'offline', shaka.offline.OfflineScheme);