2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/html/TimeRanges.h"
29 #include "bindings/core/v8/ExceptionMessages.h"
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
32 #include "core/dom/ExceptionCode.h"
35 using namespace blink;
37 TimeRanges::TimeRanges(double start, double end)
39 ScriptWrappable::init(this);
43 PassRefPtrWillBeRawPtr<TimeRanges> TimeRanges::create(const blink::WebTimeRanges& webRanges)
45 RefPtrWillBeRawPtr<TimeRanges> ranges = TimeRanges::create();
47 unsigned size = webRanges.size();
48 for (unsigned i = 0; i < size; ++i)
49 ranges->add(webRanges[i].start, webRanges[i].end);
51 return ranges.release();
54 PassRefPtrWillBeRawPtr<TimeRanges> TimeRanges::copy() const
56 RefPtrWillBeRawPtr<TimeRanges> newSession = TimeRanges::create();
58 unsigned size = m_ranges.size();
59 for (unsigned i = 0; i < size; i++)
60 newSession->add(m_ranges[i].m_start, m_ranges[i].m_end);
62 return newSession.release();
65 void TimeRanges::invert()
67 RefPtrWillBeRawPtr<TimeRanges> inverted = TimeRanges::create();
68 double posInf = std::numeric_limits<double>::infinity();
69 double negInf = -std::numeric_limits<double>::infinity();
72 inverted->add(negInf, posInf);
74 double start = m_ranges.first().m_start;
76 inverted->add(negInf, start);
78 for (size_t index = 0; index + 1 < m_ranges.size(); ++index)
79 inverted->add(m_ranges[index].m_end, m_ranges[index + 1].m_start);
81 double end = m_ranges.last().m_end;
83 inverted->add(end, posInf);
86 m_ranges.swap(inverted->m_ranges);
89 void TimeRanges::intersectWith(const TimeRanges* other)
96 RefPtrWillBeRawPtr<TimeRanges> invertedOther = other->copy();
97 invertedOther->invert();
100 unionWith(invertedOther.get());
104 void TimeRanges::unionWith(const TimeRanges* other)
107 RefPtrWillBeRawPtr<TimeRanges> unioned = copy();
108 for (size_t index = 0; index < other->m_ranges.size(); ++index) {
109 const Range& range = other->m_ranges[index];
110 unioned->add(range.m_start, range.m_end);
113 m_ranges.swap(unioned->m_ranges);
116 double TimeRanges::start(unsigned index, ExceptionState& exceptionState) const
118 if (index >= length()) {
119 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("index", index, length()));
122 return m_ranges[index].m_start;
125 double TimeRanges::end(unsigned index, ExceptionState& exceptionState) const
127 if (index >= length()) {
128 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("index", index, length()));
131 return m_ranges[index].m_end;
134 void TimeRanges::add(double start, double end)
136 ASSERT(start <= end);
137 unsigned overlappingArcIndex;
138 Range addedRange(start, end);
140 // For each present range check if we need to:
141 // - merge with the added range, in case we are overlapping or contiguous
142 // - Need to insert in place, we we are completely, not overlapping and not contiguous
143 // in between two ranges.
145 // TODO: Given that we assume that ranges are correctly ordered, this could be optimized.
147 for (overlappingArcIndex = 0; overlappingArcIndex < m_ranges.size(); overlappingArcIndex++) {
148 if (addedRange.isOverlappingRange(m_ranges[overlappingArcIndex])
149 || addedRange.isContiguousWithRange(m_ranges[overlappingArcIndex])) {
150 // We need to merge the addedRange and that range.
151 addedRange = addedRange.unionWithOverlappingOrContiguousRange(m_ranges[overlappingArcIndex]);
152 m_ranges.remove(overlappingArcIndex);
153 overlappingArcIndex--;
155 // Check the case for which there is no more to do
156 if (!overlappingArcIndex) {
157 if (addedRange.isBeforeRange(m_ranges[0])) {
158 // First index, and we are completely before that range (and not contiguous, nor overlapping).
159 // We just need to be inserted here.
163 if (m_ranges[overlappingArcIndex - 1].isBeforeRange(addedRange)
164 && addedRange.isBeforeRange(m_ranges[overlappingArcIndex])) {
165 // We are exactly after the current previous range, and before the current range, while
166 // not overlapping with none of them. Insert here.
173 // Now that we are sure we don't overlap with any range, just add it.
174 m_ranges.insert(overlappingArcIndex, addedRange);
177 bool TimeRanges::contain(double time) const
179 for (unsigned n = 0; n < length(); n++) {
180 if (time >= start(n, IGNORE_EXCEPTION) && time <= end(n, IGNORE_EXCEPTION))
186 double TimeRanges::nearest(double time) const
189 unsigned count = length();
190 for (unsigned ndx = 0; ndx < count; ndx++) {
191 double startTime = start(ndx, IGNORE_EXCEPTION);
192 double endTime = end(ndx, IGNORE_EXCEPTION);
193 if (time >= startTime && time <= endTime)
195 if (fabs(startTime - time) < closest)
196 closest = fabs(startTime - time);
197 else if (fabs(endTime - time) < closest)
198 closest = fabs(endTime - time);
203 void TimeRanges::trace(Visitor* visitor)
205 visitor->trace(m_ranges);