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/. */
7 #ifndef mozilla_dom_TimeRanges_h_
8 #define mozilla_dom_TimeRanges_h_
10 #include "nsIDOMTimeRanges.h"
11 #include "nsISupports.h"
13 #include "nsWrapperCache.h"
14 #include "mozilla/ErrorResult.h"
15 #include "nsAutoPtr.h"
26 // Implements media TimeRanges:
27 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#timeranges
28 class TimeRanges MOZ_FINAL
: public nsIDOMTimeRanges
32 NS_DECL_NSIDOMTIMERANGES
36 void Add(double aStart
, double aEnd
);
38 // Returns the start time of the first range, or -1 if no ranges exist.
39 double GetStartTime();
41 // Returns the end time of the last range, or -1 if no ranges exist.
44 // See http://www.whatwg.org/html/#normalized-timeranges-object
45 void Normalize(double aTolerance
= 0.0);
47 // Mutate this TimeRange to be the union of this and aOtherRanges.
48 void Union(const TimeRanges
* aOtherRanges
, double aTolerance
);
50 // Mutate this TimeRange to be the intersection of this and aOtherRanges.
51 void Intersection(const TimeRanges
* aOtherRanges
);
53 JSObject
* WrapObject(JSContext
* aCx
);
55 uint32_t Length() const
57 return mRanges
.Length();
60 virtual double Start(uint32_t aIndex
, ErrorResult
& aRv
);
62 virtual double End(uint32_t aIndex
, ErrorResult
& aRv
);
64 // Shift all values by aOffset seconds.
65 void Shift(double aOffset
);
70 // Comparator which orders TimeRanges by start time. Used by Normalize().
73 TimeRange(double aStart
, double aEnd
)
80 struct CompareTimeRanges
82 bool Equals(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
83 return aTr1
.mStart
== aTr2
.mStart
&& aTr1
.mEnd
== aTr2
.mEnd
;
86 bool LessThan(const TimeRange
& aTr1
, const TimeRange
& aTr2
) const {
87 return aTr1
.mStart
< aTr2
.mStart
;
91 nsAutoTArray
<TimeRange
,4> mRanges
;
94 typedef nsTArray
<TimeRange
>::index_type index_type
;
95 static const index_type NoIndex
= index_type(-1);
97 index_type
Find(double aTime
, double aTolerance
= 0);
99 bool Contains(double aStart
, double aEnd
) {
100 index_type target
= Find(aStart
);
101 if (target
== NoIndex
) {
105 return mRanges
[target
].mEnd
>= aEnd
;
110 } // namespace mozilla
112 #endif // mozilla_dom_TimeRanges_h_