Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / html / TimeRanges.h
blob1a113d26d5189d0a3bdcab0138529f214c414fc1
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_
10 #include "nsCOMPtr.h"
11 #include "nsISupports.h"
12 #include "nsTArray.h"
13 #include "nsWrapperCache.h"
14 #include "mozilla/ErrorResult.h"
15 #include "TimeUnits.h"
17 namespace mozilla {
19 namespace dom {
20 class TimeRanges;
21 } // namespace dom
23 namespace dom {
25 // Implements media TimeRanges:
26 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#timeranges
27 class TimeRanges final : public nsISupports,
28 public nsWrapperCache
30 public:
31 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
32 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TimeRanges)
34 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.
47 double GetEndTime();
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);
85 private:
86 ~TimeRanges();
88 // Comparator which orders TimeRanges by start time. Used by Normalize().
89 struct TimeRange
91 TimeRange(double aStart, double aEnd)
92 : mStart(aStart),
93 mEnd(aEnd) {}
94 double mStart;
95 double mEnd;
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;
113 public:
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) {
122 return false;
125 return mRanges[target].mEnd >= aEnd;
129 } // namespace dom
130 } // namespace mozilla
132 #endif // mozilla_dom_TimeRanges_h_