tor-browser

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

TimeRanges.h (3513B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 
      7 #ifndef mozilla_dom_TimeRanges_h_
      8 #define mozilla_dom_TimeRanges_h_
      9 
     10 #include "TimeUnits.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsISupports.h"
     13 #include "nsTArray.h"
     14 #include "nsWrapperCache.h"
     15 
     16 namespace mozilla {
     17 class ErrorResult;
     18 
     19 namespace dom {
     20 class TimeRanges;
     21 }  // namespace dom
     22 
     23 namespace dom {
     24 
     25 // Implements media TimeRanges:
     26 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#timeranges
     27 class TimeRanges final : public nsISupports, public nsWrapperCache {
     28 public:
     29  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     30  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(TimeRanges)
     31 
     32  TimeRanges();
     33  explicit TimeRanges(nsISupports* aParent);
     34  explicit TimeRanges(const media::TimeIntervals& aTimeIntervals);
     35  explicit TimeRanges(const media::TimeRanges& aTimeRanges);
     36  TimeRanges(nsISupports* aParent, const media::TimeIntervals& aTimeIntervals);
     37  TimeRanges(nsISupports* aParent, const media::TimeRanges& aTimeRanges);
     38 
     39  media::TimeIntervals ToTimeIntervals() const;
     40 
     41  void Add(double aStart, double aEnd);
     42 
     43  // Returns the start time of the first range, or -1 if no ranges exist.
     44  double GetStartTime();
     45 
     46  // Returns the end time of the last range, or -1 if no ranges exist.
     47  double GetEndTime();
     48 
     49  // See http://www.whatwg.org/html/#normalized-timeranges-object
     50  void Normalize(double aTolerance = 0.0);
     51 
     52  // Mutate this TimeRange to be the union of this and aOtherRanges.
     53  void Union(const TimeRanges* aOtherRanges, double aTolerance);
     54 
     55  // Mutate this TimeRange to be the intersection of this and aOtherRanges.
     56  void Intersection(const TimeRanges* aOtherRanges);
     57 
     58  JSObject* WrapObject(JSContext* aCx,
     59                       JS::Handle<JSObject*> aGivenProto) override;
     60 
     61  nsISupports* GetParentObject() const;
     62 
     63  uint32_t Length() const { return mRanges.Length(); }
     64 
     65  double Start(uint32_t aIndex, ErrorResult& aRv) const;
     66 
     67  double End(uint32_t aIndex, ErrorResult& aRv) const;
     68 
     69  double Start(uint32_t aIndex) const { return mRanges[aIndex].mStart; }
     70 
     71  double End(uint32_t aIndex) const { return mRanges[aIndex].mEnd; }
     72 
     73  // Shift all values by aOffset seconds.
     74  void Shift(double aOffset);
     75 
     76 private:
     77  ~TimeRanges();
     78 
     79  // Comparator which orders TimeRanges by start time. Used by Normalize().
     80  struct TimeRange {
     81    TimeRange(double aStart, double aEnd) : mStart(aStart), mEnd(aEnd) {}
     82    double mStart;
     83    double mEnd;
     84  };
     85 
     86  struct CompareTimeRanges {
     87    bool Equals(const TimeRange& aTr1, const TimeRange& aTr2) const {
     88      return aTr1.mStart == aTr2.mStart && aTr1.mEnd == aTr2.mEnd;
     89    }
     90 
     91    bool LessThan(const TimeRange& aTr1, const TimeRange& aTr2) const {
     92      return aTr1.mStart < aTr2.mStart;
     93    }
     94  };
     95 
     96  AutoTArray<TimeRange, 4> mRanges;
     97 
     98  nsCOMPtr<nsISupports> mParent;
     99 
    100 public:
    101  typedef nsTArray<TimeRange>::index_type index_type;
    102  static const index_type NoIndex = index_type(-1);
    103 
    104  index_type Find(double aTime, double aTolerance = 0);
    105 
    106  bool Contains(double aStart, double aEnd) {
    107    index_type target = Find(aStart);
    108    if (target == NoIndex) {
    109      return false;
    110    }
    111 
    112    return mRanges[target].mEnd >= aEnd;
    113  }
    114 };
    115 
    116 }  // namespace dom
    117 }  // namespace mozilla
    118 
    119 #endif  // mozilla_dom_TimeRanges_h_