tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MediaFragmentURIParser.h (3753B)


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