Line data Source code
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 "nsCOMPtr.h"
11 : #include "nsIDOMTimeRanges.h"
12 : #include "nsISupports.h"
13 : #include "nsTArray.h"
14 : #include "nsWrapperCache.h"
15 : #include "mozilla/ErrorResult.h"
16 :
17 : namespace mozilla {
18 : namespace dom {
19 :
20 : class TimeRanges;
21 :
22 : } // namespace dom
23 :
24 : namespace dom {
25 :
26 : // Implements media TimeRanges:
27 : // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#timeranges
28 : class TimeRanges final : public nsIDOMTimeRanges,
29 : public nsWrapperCache
30 : {
31 : public:
32 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
33 2 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TimeRanges)
34 : NS_DECL_NSIDOMTIMERANGES
35 :
36 : TimeRanges();
37 : explicit TimeRanges(nsISupports* aParent);
38 :
39 : void Add(double aStart, double aEnd);
40 :
41 : // Returns the start time of the first range, or -1 if no ranges exist.
42 : double GetStartTime();
43 :
44 : // Returns the end time of the last range, or -1 if no ranges exist.
45 : double GetEndTime();
46 :
47 : // See http://www.whatwg.org/html/#normalized-timeranges-object
48 : void Normalize(double aTolerance = 0.0);
49 :
50 : // Mutate this TimeRange to be the union of this and aOtherRanges.
51 : void Union(const TimeRanges* aOtherRanges, double aTolerance);
52 :
53 : // Mutate this TimeRange to be the intersection of this and aOtherRanges.
54 : void Intersection(const TimeRanges* aOtherRanges);
55 :
56 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
57 :
58 : nsISupports* GetParentObject() const;
59 :
60 0 : uint32_t Length() const
61 : {
62 0 : return mRanges.Length();
63 : }
64 :
65 : virtual double Start(uint32_t aIndex, ErrorResult& aRv);
66 :
67 : virtual double End(uint32_t aIndex, ErrorResult& aRv);
68 :
69 : // Shift all values by aOffset seconds.
70 : void Shift(double aOffset);
71 :
72 : private:
73 : ~TimeRanges();
74 :
75 : // Comparator which orders TimeRanges by start time. Used by Normalize().
76 : struct TimeRange
77 : {
78 0 : TimeRange(double aStart, double aEnd)
79 0 : : mStart(aStart),
80 0 : mEnd(aEnd) {}
81 : double mStart;
82 : double mEnd;
83 : };
84 :
85 : struct CompareTimeRanges
86 : {
87 0 : bool Equals(const TimeRange& aTr1, const TimeRange& aTr2) const {
88 0 : return aTr1.mStart == aTr2.mStart && aTr1.mEnd == aTr2.mEnd;
89 : }
90 :
91 0 : bool LessThan(const TimeRange& aTr1, const TimeRange& aTr2) const {
92 0 : 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_
|