|
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 |
|
9 #include "mozilla/Maybe.h" |
|
10 #include "nsStringFwd.h" |
|
11 #include "nsRect.h" |
|
12 |
|
13 class nsIURI; |
|
14 |
|
15 // Class to handle parsing of a W3C media fragment URI as per |
|
16 // spec at: http://www.w3.org/TR/media-frags/ |
|
17 // Only the temporaral URI portion of the spec is implemented. |
|
18 // To use: |
|
19 // a) Construct an instance with the URI containing the fragment |
|
20 // b) Check for the validity of the values you are interested in |
|
21 // using e.g. HasStartTime(). |
|
22 // c) If the values are valid, obtain them using e.g. GetStartTime(). |
|
23 |
|
24 namespace mozilla { namespace net { |
|
25 |
|
26 enum ClipUnit |
|
27 { |
|
28 eClipUnit_Pixel, |
|
29 eClipUnit_Percent, |
|
30 }; |
|
31 |
|
32 class nsMediaFragmentURIParser |
|
33 { |
|
34 public: |
|
35 // Create a parser with the provided URI. |
|
36 nsMediaFragmentURIParser(nsIURI* aURI); |
|
37 |
|
38 // Create a parser with the provided URI reference portion. |
|
39 nsMediaFragmentURIParser(nsCString& aRef); |
|
40 |
|
41 // True if a valid temporal media fragment indicated a start time. |
|
42 bool HasStartTime() const { return !mStart.empty(); } |
|
43 |
|
44 // If a valid temporal media fragment indicated a start time, returns |
|
45 // it in units of seconds. If not, defaults to 0. |
|
46 double GetStartTime() const { return mStart.ref(); } |
|
47 |
|
48 // True if a valid temporal media fragment indicated an end time. |
|
49 bool HasEndTime() const { return !mEnd.empty(); } |
|
50 |
|
51 // If a valid temporal media fragment indicated an end time, returns |
|
52 // it in units of seconds. If not, defaults to -1. |
|
53 double GetEndTime() const { return mEnd.ref(); } |
|
54 |
|
55 // True if a valid spatial media fragment indicated a clipping region. |
|
56 bool HasClip() const { return !mClip.empty(); } |
|
57 |
|
58 // True if a valid spatial media fragment indicated a resolution. |
|
59 bool HasResolution() const { return !mResolution.empty(); } |
|
60 |
|
61 // True if a valid spatial media fragment indicated a resolution. |
|
62 nsIntSize GetResolution() const { return mResolution.ref(); } |
|
63 |
|
64 // If a valid spatial media fragment indicated a clipping region, |
|
65 // returns the region. If not, returns an empty region. The unit |
|
66 // used depends on the value returned by GetClipUnit(). |
|
67 nsIntRect GetClip() const { return mClip.ref(); } |
|
68 |
|
69 // If a valid spatial media fragment indicated a clipping region, |
|
70 // returns the unit used. |
|
71 ClipUnit GetClipUnit() const { return mClipUnit; } |
|
72 |
|
73 bool HasSampleSize() const { return !mSampleSize.empty(); } |
|
74 |
|
75 int GetSampleSize() const { return mSampleSize.ref(); } |
|
76 |
|
77 private: |
|
78 // Parse the URI ref provided, looking for media fragments. This is |
|
79 // the top-level parser the invokes the others below. |
|
80 void Parse(nsACString& aRef); |
|
81 |
|
82 // The following methods parse the fragment as per the media |
|
83 // fragments specification. 'aString' contains the remaining |
|
84 // fragment data to be parsed. The method returns true |
|
85 // if the parse was successful and leaves the remaining unparsed |
|
86 // data in 'aString'. If the parse fails then false is returned |
|
87 // and 'aString' is left as it was when called. |
|
88 bool ParseNPT(nsDependentSubstring aString); |
|
89 bool ParseNPTTime(nsDependentSubstring& aString, double& aTime); |
|
90 bool ParseNPTSec(nsDependentSubstring& aString, double& aSec); |
|
91 bool ParseNPTFraction(nsDependentSubstring& aString, double& aFraction); |
|
92 bool ParseNPTMMSS(nsDependentSubstring& aString, double& aTime); |
|
93 bool ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime); |
|
94 bool ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour); |
|
95 bool ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute); |
|
96 bool ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond); |
|
97 bool ParseXYWH(nsDependentSubstring aString); |
|
98 bool ParseMozResolution(nsDependentSubstring aString); |
|
99 bool ParseMozSampleSize(nsDependentSubstring aString); |
|
100 |
|
101 // Media fragment information. |
|
102 Maybe<double> mStart; |
|
103 Maybe<double> mEnd; |
|
104 Maybe<nsIntRect> mClip; |
|
105 ClipUnit mClipUnit; |
|
106 Maybe<nsIntSize> mResolution; |
|
107 Maybe<int> mSampleSize; |
|
108 }; |
|
109 |
|
110 }} // namespace mozilla::net |
|
111 |
|
112 #endif |