nsMediaFragmentURIParser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #if !defined(nsMediaFragmentURIParser_h__)
  7. #define nsMediaFragmentURIParser_h__
  8. #include "mozilla/Maybe.h"
  9. #include "nsStringFwd.h"
  10. #include "nsRect.h"
  11. class nsIURI;
  12. // Class to handle parsing of a W3C media fragment URI as per
  13. // spec at: http://www.w3.org/TR/media-frags/
  14. // Only the temporaral URI portion of the spec is implemented.
  15. // To use:
  16. // a) Construct an instance with the URI containing the fragment
  17. // b) Check for the validity of the values you are interested in
  18. // using e.g. HasStartTime().
  19. // c) If the values are valid, obtain them using e.g. GetStartTime().
  20. namespace mozilla { namespace net {
  21. enum ClipUnit
  22. {
  23. eClipUnit_Pixel,
  24. eClipUnit_Percent,
  25. };
  26. class nsMediaFragmentURIParser
  27. {
  28. public:
  29. // Create a parser with the provided URI.
  30. explicit nsMediaFragmentURIParser(nsIURI* aURI);
  31. // Create a parser with the provided URI reference portion.
  32. explicit nsMediaFragmentURIParser(nsCString& aRef);
  33. // True if a valid temporal media fragment indicated a start time.
  34. bool HasStartTime() const { return mStart.isSome(); }
  35. // If a valid temporal media fragment indicated a start time, returns
  36. // it in units of seconds. If not, defaults to 0.
  37. double GetStartTime() const { return *mStart; }
  38. // True if a valid temporal media fragment indicated an end time.
  39. bool HasEndTime() const { return mEnd.isSome(); }
  40. // If a valid temporal media fragment indicated an end time, returns
  41. // it in units of seconds. If not, defaults to -1.
  42. double GetEndTime() const { return *mEnd; }
  43. // True if a valid spatial media fragment indicated a clipping region.
  44. bool HasClip() const { return mClip.isSome(); }
  45. // If a valid spatial media fragment indicated a clipping region,
  46. // returns the region. If not, returns an empty region. The unit
  47. // used depends on the value returned by GetClipUnit().
  48. nsIntRect GetClip() const { return *mClip; }
  49. // If a valid spatial media fragment indicated a clipping region,
  50. // returns the unit used.
  51. ClipUnit GetClipUnit() const { return mClipUnit; }
  52. bool HasSampleSize() const { return mSampleSize.isSome(); }
  53. int GetSampleSize() const { return *mSampleSize; }
  54. private:
  55. // Parse the URI ref provided, looking for media fragments. This is
  56. // the top-level parser the invokes the others below.
  57. void Parse(nsACString& aRef);
  58. // The following methods parse the fragment as per the media
  59. // fragments specification. 'aString' contains the remaining
  60. // fragment data to be parsed. The method returns true
  61. // if the parse was successful and leaves the remaining unparsed
  62. // data in 'aString'. If the parse fails then false is returned
  63. // and 'aString' is left as it was when called.
  64. bool ParseNPT(nsDependentSubstring aString);
  65. bool ParseNPTTime(nsDependentSubstring& aString, double& aTime);
  66. bool ParseNPTSec(nsDependentSubstring& aString, double& aSec);
  67. bool ParseNPTFraction(nsDependentSubstring& aString, double& aFraction);
  68. bool ParseNPTMMSS(nsDependentSubstring& aString, double& aTime);
  69. bool ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime);
  70. bool ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour);
  71. bool ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute);
  72. bool ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond);
  73. bool ParseXYWH(nsDependentSubstring aString);
  74. bool ParseMozSampleSize(nsDependentSubstring aString);
  75. // Media fragment information.
  76. Maybe<double> mStart;
  77. Maybe<double> mEnd;
  78. Maybe<nsIntRect> mClip;
  79. ClipUnit mClipUnit;
  80. Maybe<int> mSampleSize;
  81. };
  82. } // namespace net
  83. } // namespace mozilla
  84. #endif