michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #if !defined(nsMediaFragmentURIParser_h__) michael@0: #define nsMediaFragmentURIParser_h__ michael@0: michael@0: #include "mozilla/Maybe.h" michael@0: #include "nsStringFwd.h" michael@0: #include "nsRect.h" michael@0: michael@0: class nsIURI; michael@0: michael@0: // Class to handle parsing of a W3C media fragment URI as per michael@0: // spec at: http://www.w3.org/TR/media-frags/ michael@0: // Only the temporaral URI portion of the spec is implemented. michael@0: // To use: michael@0: // a) Construct an instance with the URI containing the fragment michael@0: // b) Check for the validity of the values you are interested in michael@0: // using e.g. HasStartTime(). michael@0: // c) If the values are valid, obtain them using e.g. GetStartTime(). michael@0: michael@0: namespace mozilla { namespace net { michael@0: michael@0: enum ClipUnit michael@0: { michael@0: eClipUnit_Pixel, michael@0: eClipUnit_Percent, michael@0: }; michael@0: michael@0: class nsMediaFragmentURIParser michael@0: { michael@0: public: michael@0: // Create a parser with the provided URI. michael@0: nsMediaFragmentURIParser(nsIURI* aURI); michael@0: michael@0: // Create a parser with the provided URI reference portion. michael@0: nsMediaFragmentURIParser(nsCString& aRef); michael@0: michael@0: // True if a valid temporal media fragment indicated a start time. michael@0: bool HasStartTime() const { return !mStart.empty(); } michael@0: michael@0: // If a valid temporal media fragment indicated a start time, returns michael@0: // it in units of seconds. If not, defaults to 0. michael@0: double GetStartTime() const { return mStart.ref(); } michael@0: michael@0: // True if a valid temporal media fragment indicated an end time. michael@0: bool HasEndTime() const { return !mEnd.empty(); } michael@0: michael@0: // If a valid temporal media fragment indicated an end time, returns michael@0: // it in units of seconds. If not, defaults to -1. michael@0: double GetEndTime() const { return mEnd.ref(); } michael@0: michael@0: // True if a valid spatial media fragment indicated a clipping region. michael@0: bool HasClip() const { return !mClip.empty(); } michael@0: michael@0: // True if a valid spatial media fragment indicated a resolution. michael@0: bool HasResolution() const { return !mResolution.empty(); } michael@0: michael@0: // True if a valid spatial media fragment indicated a resolution. michael@0: nsIntSize GetResolution() const { return mResolution.ref(); } michael@0: michael@0: // If a valid spatial media fragment indicated a clipping region, michael@0: // returns the region. If not, returns an empty region. The unit michael@0: // used depends on the value returned by GetClipUnit(). michael@0: nsIntRect GetClip() const { return mClip.ref(); } michael@0: michael@0: // If a valid spatial media fragment indicated a clipping region, michael@0: // returns the unit used. michael@0: ClipUnit GetClipUnit() const { return mClipUnit; } michael@0: michael@0: bool HasSampleSize() const { return !mSampleSize.empty(); } michael@0: michael@0: int GetSampleSize() const { return mSampleSize.ref(); } michael@0: michael@0: private: michael@0: // Parse the URI ref provided, looking for media fragments. This is michael@0: // the top-level parser the invokes the others below. michael@0: void Parse(nsACString& aRef); michael@0: michael@0: // The following methods parse the fragment as per the media michael@0: // fragments specification. 'aString' contains the remaining michael@0: // fragment data to be parsed. The method returns true michael@0: // if the parse was successful and leaves the remaining unparsed michael@0: // data in 'aString'. If the parse fails then false is returned michael@0: // and 'aString' is left as it was when called. michael@0: bool ParseNPT(nsDependentSubstring aString); michael@0: bool ParseNPTTime(nsDependentSubstring& aString, double& aTime); michael@0: bool ParseNPTSec(nsDependentSubstring& aString, double& aSec); michael@0: bool ParseNPTFraction(nsDependentSubstring& aString, double& aFraction); michael@0: bool ParseNPTMMSS(nsDependentSubstring& aString, double& aTime); michael@0: bool ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime); michael@0: bool ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour); michael@0: bool ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute); michael@0: bool ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond); michael@0: bool ParseXYWH(nsDependentSubstring aString); michael@0: bool ParseMozResolution(nsDependentSubstring aString); michael@0: bool ParseMozSampleSize(nsDependentSubstring aString); michael@0: michael@0: // Media fragment information. michael@0: Maybe mStart; michael@0: Maybe mEnd; michael@0: Maybe mClip; michael@0: ClipUnit mClipUnit; michael@0: Maybe mResolution; michael@0: Maybe mSampleSize; michael@0: }; michael@0: michael@0: }} // namespace mozilla::net michael@0: michael@0: #endif