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 "mozilla/ErrorResult.h"
15 #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
,
31 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
32 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TimeRanges
)
35 explicit TimeRanges(nsISupports
* aParent
);
36 explicit TimeRanges(const media::TimeIntervals
& aTimeIntervals
);
37 TimeRanges(nsISupports
* aParent
, const media::TimeIntervals
& aTimeIntervals
);
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
65 return mRanges
.Length();
68 double Start(uint32_t aIndex
, ErrorResult
& aRv
) const;
70 double End(uint32_t aIndex
, ErrorResult
& aRv
) const;
72 double Start(uint32_t aIndex
) const
74 return mRanges
[aIndex
].mStart
;
77 double End(uint32_t aIndex
) const
79 return mRanges
[aIndex
].mEnd
;
82 // Shift all values by aOffset seconds.
83 void Shift(double aOffset
);
88 // Comparator which orders TimeRanges by start time. Used by Normalize().
91 TimeRange(double aStart
, double aEnd
)
98 struct CompareTimeRanges
100 bool Equals(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
101 return aTr1
.mStart
== aTr2
.mStart
&& aTr1
.mEnd
== aTr2
.mEnd
;
104 bool LessThan(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
105 return aTr1
.mStart
< aTr2
.mStart
;
109 AutoTArray
<TimeRange
,4> mRanges
;
111 nsCOMPtr
<nsISupports
> mParent
;
114 typedef nsTArray
<TimeRange
>::index_type index_type
;
115 static const index_type NoIndex
= index_type(-1);
117 index_type
Find(double aTime
, double aTolerance
= 0);
119 bool Contains(double aStart
, double aEnd
) {
120 index_type target
= Find(aStart
);
121 if (target
== NoIndex
) {
125 return mRanges
[target
].mEnd
>= aEnd
;
130 } // namespace mozilla
132 #endif // mozilla_dom_TimeRanges_h_