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/. */
7 #ifndef mozilla_dom_TimeRanges_h_
8 #define mozilla_dom_TimeRanges_h_
11 #include "nsISupports.h"
13 #include "nsWrapperCache.h"
14 #include "TimeUnits.h"
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
{
29 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
30 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(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
);
39 media::TimeIntervals
ToTimeIntervals() const;
41 void Add(double aStart
, double aEnd
);
43 // Returns the start time of the first range, or -1 if no ranges exist.
44 double GetStartTime();
46 // Returns the end time of the last range, or -1 if no ranges exist.
49 // See http://www.whatwg.org/html/#normalized-timeranges-object
50 void Normalize(double aTolerance
= 0.0);
52 // Mutate this TimeRange to be the union of this and aOtherRanges.
53 void Union(const TimeRanges
* aOtherRanges
, double aTolerance
);
55 // Mutate this TimeRange to be the intersection of this and aOtherRanges.
56 void Intersection(const TimeRanges
* aOtherRanges
);
58 JSObject
* WrapObject(JSContext
* aCx
,
59 JS::Handle
<JSObject
*> aGivenProto
) override
;
61 nsISupports
* GetParentObject() const;
63 uint32_t Length() const { return mRanges
.Length(); }
65 double Start(uint32_t aIndex
, ErrorResult
& aRv
) const;
67 double End(uint32_t aIndex
, ErrorResult
& aRv
) const;
69 double Start(uint32_t aIndex
) const { return mRanges
[aIndex
].mStart
; }
71 double End(uint32_t aIndex
) const { return mRanges
[aIndex
].mEnd
; }
73 // Shift all values by aOffset seconds.
74 void Shift(double aOffset
);
79 // Comparator which orders TimeRanges by start time. Used by Normalize().
81 TimeRange(double aStart
, double aEnd
) : mStart(aStart
), mEnd(aEnd
) {}
86 struct CompareTimeRanges
{
87 bool Equals(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
88 return aTr1
.mStart
== aTr2
.mStart
&& aTr1
.mEnd
== aTr2
.mEnd
;
91 bool LessThan(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
92 return aTr1
.mStart
< aTr2
.mStart
;
96 AutoTArray
<TimeRange
, 4> mRanges
;
98 nsCOMPtr
<nsISupports
> mParent
;
101 typedef nsTArray
<TimeRange
>::index_type index_type
;
102 static const index_type NoIndex
= index_type(-1);
104 index_type
Find(double aTime
, double aTolerance
= 0);
106 bool Contains(double aStart
, double aEnd
) {
107 index_type target
= Find(aStart
);
108 if (target
== NoIndex
) {
112 return mRanges
[target
].mEnd
>= aEnd
;
117 } // namespace mozilla
119 #endif // mozilla_dom_TimeRanges_h_