Line data Source code
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 0 : class nsMediaFragmentURIParser
33 : {
34 : public:
35 : // Create a parser with the provided URI.
36 : explicit nsMediaFragmentURIParser(nsIURI* aURI);
37 :
38 : // Create a parser with the provided URI reference portion.
39 : explicit nsMediaFragmentURIParser(nsCString& aRef);
40 :
41 : // True if a valid temporal media fragment indicated a start time.
42 0 : bool HasStartTime() const { return mStart.isSome(); }
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 0 : double GetStartTime() const { return *mStart; }
47 :
48 : // True if a valid temporal media fragment indicated an end time.
49 0 : bool HasEndTime() const { return mEnd.isSome(); }
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 0 : double GetEndTime() const { return *mEnd; }
54 :
55 : // True if a valid spatial media fragment indicated a clipping region.
56 : bool HasClip() const { return mClip.isSome(); }
57 :
58 : // If a valid spatial media fragment indicated a clipping region,
59 : // returns the region. If not, returns an empty region. The unit
60 : // used depends on the value returned by GetClipUnit().
61 : nsIntRect GetClip() const { return *mClip; }
62 :
63 : // If a valid spatial media fragment indicated a clipping region,
64 : // returns the unit used.
65 : ClipUnit GetClipUnit() const { return mClipUnit; }
66 :
67 : private:
68 : // Parse the URI ref provided, looking for media fragments. This is
69 : // the top-level parser the invokes the others below.
70 : void Parse(nsACString& aRef);
71 :
72 : // The following methods parse the fragment as per the media
73 : // fragments specification. 'aString' contains the remaining
74 : // fragment data to be parsed. The method returns true
75 : // if the parse was successful and leaves the remaining unparsed
76 : // data in 'aString'. If the parse fails then false is returned
77 : // and 'aString' is left as it was when called.
78 : bool ParseNPT(nsDependentSubstring aString);
79 : bool ParseNPTTime(nsDependentSubstring& aString, double& aTime);
80 : bool ParseNPTSec(nsDependentSubstring& aString, double& aSec);
81 : bool ParseNPTFraction(nsDependentSubstring& aString, double& aFraction);
82 : bool ParseNPTMMSS(nsDependentSubstring& aString, double& aTime);
83 : bool ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime);
84 : bool ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour);
85 : bool ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute);
86 : bool ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond);
87 : bool ParseXYWH(nsDependentSubstring aString);
88 : bool ParseMozResolution(nsDependentSubstring aString);
89 :
90 : // Media fragment information.
91 : Maybe<double> mStart;
92 : Maybe<double> mEnd;
93 : Maybe<nsIntRect> mClip;
94 : ClipUnit mClipUnit;
95 : };
96 :
97 : } // namespace net
98 : } // namespace mozilla
99 :
100 : #endif
|