2010-08-05 00:40:35 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-05 00:40:35 -07:00
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2013-03-02 11:16:43 -08:00
|
|
|
#include "mozilla/dom/TimeRangesBinding.h"
|
2013-03-19 05:23:54 -07:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2010-08-05 00:40:35 -07:00
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-08-05 00:40:35 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(TimeRanges, nsIDOMTimeRanges)
|
2010-08-05 00:40:35 -07:00
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::TimeRanges()
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
2013-03-02 11:14:44 -08:00
|
|
|
MOZ_COUNT_CTOR(TimeRanges);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::~TimeRanges()
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
2013-03-02 11:14:44 -08:00
|
|
|
MOZ_COUNT_DTOR(TimeRanges);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_IMETHODIMP
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::GetLength(uint32_t* aLength)
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
2013-03-02 11:16:43 -08:00
|
|
|
*aLength = Length();
|
2010-08-05 00:40:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-02 11:16:43 -08:00
|
|
|
double
|
|
|
|
TimeRanges::Start(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRanges[aIndex].mStart;
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_IMETHODIMP
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::Start(uint32_t aIndex, double* aTime)
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
2013-03-02 11:16:43 -08:00
|
|
|
ErrorResult rv;
|
|
|
|
*aTime = Start(aIndex, rv);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TimeRanges::End(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRanges[aIndex].mEnd;
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::End(uint32_t aIndex, double* aTime)
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
2013-03-02 11:16:43 -08:00
|
|
|
ErrorResult rv;
|
|
|
|
*aTime = End(aIndex, rv);
|
|
|
|
return rv.ErrorCode();
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-02 11:14:44 -08:00
|
|
|
TimeRanges::Add(double aStart, double aEnd)
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
|
|
|
if (aStart > aEnd) {
|
|
|
|
NS_WARNING("Can't add a range if the end is older that the start.");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-05 00:40:35 -07:00
|
|
|
mRanges.AppendElement(TimeRange(aStart,aEnd));
|
|
|
|
}
|
2012-04-30 17:29:24 -07:00
|
|
|
|
2013-01-28 18:34:27 -08:00
|
|
|
double
|
2014-04-14 04:23:00 -07:00
|
|
|
TimeRanges::GetStartTime()
|
2013-01-28 18:34:27 -08:00
|
|
|
{
|
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
2014-04-14 04:23:00 -07:00
|
|
|
return mRanges[0].mStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TimeRanges::GetEndTime()
|
|
|
|
{
|
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
return mRanges[mRanges.Length() - 1].mEnd;
|
2013-01-28 18:34:27 -08:00
|
|
|
}
|
|
|
|
|
2012-04-30 17:29:24 -07:00
|
|
|
void
|
2014-12-10 15:52:47 -08:00
|
|
|
TimeRanges::Normalize()
|
2012-04-30 17:29:24 -07:00
|
|
|
{
|
|
|
|
if (mRanges.Length() >= 2) {
|
|
|
|
nsAutoTArray<TimeRange,4> normalized;
|
|
|
|
|
|
|
|
mRanges.Sort(CompareTimeRanges());
|
|
|
|
|
|
|
|
// This merges the intervals.
|
|
|
|
TimeRange current(mRanges[0]);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 1; i < mRanges.Length(); i++) {
|
2012-06-06 16:43:13 -07:00
|
|
|
if (current.mStart <= mRanges[i].mStart &&
|
|
|
|
current.mEnd >= mRanges[i].mEnd) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 15:52:47 -08:00
|
|
|
if (current.mEnd >= mRanges[i].mStart) {
|
2012-04-30 17:29:24 -07:00
|
|
|
current.mEnd = mRanges[i].mEnd;
|
|
|
|
} else {
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
current = mRanges[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
|
|
|
|
mRanges = normalized;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 11:14:44 -08:00
|
|
|
|
2014-08-10 21:32:21 -07:00
|
|
|
void
|
2014-12-10 15:52:47 -08:00
|
|
|
TimeRanges::Union(const TimeRanges* aOtherRanges)
|
2014-08-10 21:32:21 -07:00
|
|
|
{
|
|
|
|
mRanges.AppendElements(aOtherRanges->mRanges);
|
2014-12-10 15:52:47 -08:00
|
|
|
Normalize();
|
2014-08-10 21:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimeRanges::Intersection(const TimeRanges* aOtherRanges)
|
|
|
|
{
|
|
|
|
nsAutoTArray<TimeRange,4> intersection;
|
|
|
|
|
|
|
|
const nsTArray<TimeRange>& otherRanges = aOtherRanges->mRanges;
|
|
|
|
for (index_type i = 0, j = 0; i < mRanges.Length() && j < otherRanges.Length();) {
|
|
|
|
double start = std::max(mRanges[i].mStart, otherRanges[j].mStart);
|
|
|
|
double end = std::min(mRanges[i].mEnd, otherRanges[j].mEnd);
|
|
|
|
if (start < end) {
|
|
|
|
intersection.AppendElement(TimeRange(start, end));
|
|
|
|
}
|
|
|
|
if (mRanges[i].mEnd < otherRanges[j].mEnd) {
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRanges = intersection;
|
|
|
|
}
|
|
|
|
|
2014-08-10 19:05:09 -07:00
|
|
|
TimeRanges::index_type
|
|
|
|
TimeRanges::Find(double aTime)
|
|
|
|
{
|
|
|
|
for (index_type i = 0; i < mRanges.Length(); ++i) {
|
|
|
|
if (aTime >= mRanges[i].mStart && aTime < mRanges[i].mEnd) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
2013-03-02 11:16:43 -08:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
TimeRanges::WrapObject(JSContext* aCx)
|
2013-03-02 11:16:43 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return TimeRangesBinding::Wrap(aCx, this);
|
2013-03-02 11:16:43 -08:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|