WebVTTParserWrapper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  5. Components.utils.import("resource://gre/modules/vtt.jsm");
  6. var Ci = Components.interfaces;
  7. var WEBVTTPARSERWRAPPER_CID = "{acf6e493-0092-4b26-b172-241e375c57ab}";
  8. var WEBVTTPARSERWRAPPER_CONTRACTID = "@mozilla.org/webvttParserWrapper;1";
  9. function WebVTTParserWrapper()
  10. {
  11. // Nothing
  12. }
  13. WebVTTParserWrapper.prototype =
  14. {
  15. loadParser: function(window)
  16. {
  17. this.parser = new WebVTT.Parser(window, new TextDecoder("utf8"));
  18. },
  19. parse: function(data)
  20. {
  21. // We can safely translate the string data to a Uint8Array as we are
  22. // guaranteed character codes only from \u0000 => \u00ff
  23. var buffer = new Uint8Array(data.length);
  24. for (var i = 0; i < data.length; i++) {
  25. buffer[i] = data.charCodeAt(i);
  26. }
  27. this.parser.parse(buffer);
  28. },
  29. flush: function()
  30. {
  31. this.parser.flush();
  32. },
  33. watch: function(callback)
  34. {
  35. this.parser.oncue = callback.onCue;
  36. this.parser.onregion = callback.onRegion;
  37. this.parser.onparsingerror = function(e) {
  38. // Passing the just the error code back is enough for our needs.
  39. callback.onParsingError(("code" in e) ? e.code : -1);
  40. };
  41. },
  42. convertCueToDOMTree: function(window, cue)
  43. {
  44. return WebVTT.convertCueToDOMTree(window, cue.text);
  45. },
  46. processCues: function(window, cues, overlay, controls)
  47. {
  48. WebVTT.processCues(window, cues, overlay, controls);
  49. },
  50. classDescription: "Wrapper for the JS WebVTT implementation (vtt.js)",
  51. classID: Components.ID(WEBVTTPARSERWRAPPER_CID),
  52. QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebVTTParserWrapper]),
  53. classInfo: XPCOMUtils.generateCI({
  54. classID: WEBVTTPARSERWRAPPER_CID,
  55. contractID: WEBVTTPARSERWRAPPER_CONTRACTID,
  56. interfaces: [Ci.nsIWebVTTParserWrapper]
  57. })
  58. };
  59. this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WebVTTParserWrapper]);