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