/*
    This file is part of the WebKit open source project.
    This file has been generated by generate-bindings.pl. DO NOT MODIFY!

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"

#if ENABLE(MEDIA_SOURCE)

#include "JSSourceBuffer.h"

#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include "JSTimeRanges.h"
#include "SourceBuffer.h"
#include "TimeRanges.h"
#include <runtime/Error.h>
#include <wtf/GetPtr.h>

#if ENABLE(VIDEO_TRACK)
#include "AudioTrackList.h"
#include "JSAudioTrackList.h"
#include "JSTextTrackList.h"
#include "JSVideoTrackList.h"
#include "TextTrackList.h"
#include "VideoTrackList.h"
#endif

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSSourceBufferTableValues[] =
{
    { "updating", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferUpdating), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "buffered", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferBuffered), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "timestampOffset", DontDelete, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferTimestampOffset), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(setJSSourceBufferTimestampOffset) },
#if ENABLE(VIDEO_TRACK)
    { "audioTracks", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferAudioTracks), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
#endif
#if ENABLE(VIDEO_TRACK)
    { "videoTracks", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferVideoTracks), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
#endif
#if ENABLE(VIDEO_TRACK)
    { "textTracks", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSourceBufferTextTracks), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
#endif
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSSourceBufferTable = { 17, 15, true, JSSourceBufferTableValues, 0 };
/* Hash table for prototype */

static const HashTableValue JSSourceBufferPrototypeTableValues[] =
{
    { "appendBuffer", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsSourceBufferPrototypeFunctionAppendBuffer), (intptr_t) (1) },
    { "abort", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsSourceBufferPrototypeFunctionAbort), (intptr_t) (0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSSourceBufferPrototypeTable = { 8, 7, false, JSSourceBufferPrototypeTableValues, 0 };
const ClassInfo JSSourceBufferPrototype::s_info = { "SourceBufferPrototype", &Base::s_info, &JSSourceBufferPrototypeTable, 0, CREATE_METHOD_TABLE(JSSourceBufferPrototype) };

JSObject* JSSourceBufferPrototype::self(VM& vm, JSGlobalObject* globalObject)
{
    return getDOMPrototype<JSSourceBuffer>(vm, globalObject);
}

bool JSSourceBufferPrototype::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    JSSourceBufferPrototype* thisObject = jsCast<JSSourceBufferPrototype*>(object);
    return getStaticFunctionSlot<JSObject>(exec, JSSourceBufferPrototypeTable, thisObject, propertyName, slot);
}

const ClassInfo JSSourceBuffer::s_info = { "SourceBuffer", &Base::s_info, &JSSourceBufferTable, 0 , CREATE_METHOD_TABLE(JSSourceBuffer) };

JSSourceBuffer::JSSourceBuffer(Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<SourceBuffer> impl)
    : JSEventTarget(structure, globalObject, impl)
{
}

void JSSourceBuffer::finishCreation(VM& vm)
{
    Base::finishCreation(vm);
    ASSERT(inherits(info()));
}

JSObject* JSSourceBuffer::createPrototype(VM& vm, JSGlobalObject* globalObject)
{
    return JSSourceBufferPrototype::create(vm, globalObject, JSSourceBufferPrototype::createStructure(vm, globalObject, JSEventTargetPrototype::self(vm, globalObject)));
}

bool JSSourceBuffer::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    JSSourceBuffer* thisObject = jsCast<JSSourceBuffer*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    return getStaticValueSlot<JSSourceBuffer, Base>(exec, JSSourceBufferTable, thisObject, propertyName, slot);
}

EncodedJSValue jsSourceBufferUpdating(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SourceBuffer& impl = castedThis->impl();
    JSValue result = jsBoolean(impl.updating());
    return JSValue::encode(result);
}


EncodedJSValue jsSourceBufferBuffered(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    ExceptionCode ec = 0;
    SourceBuffer& impl = castedThis->impl();
    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.buffered(ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}


EncodedJSValue jsSourceBufferTimestampOffset(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SourceBuffer& impl = castedThis->impl();
    JSValue result = jsNumber(impl.timestampOffset());
    return JSValue::encode(result);
}


#if ENABLE(VIDEO_TRACK)
EncodedJSValue jsSourceBufferAudioTracks(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SourceBuffer& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.audioTracks()));
    return JSValue::encode(result);
}

#endif

#if ENABLE(VIDEO_TRACK)
EncodedJSValue jsSourceBufferVideoTracks(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SourceBuffer& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.videoTracks()));
    return JSValue::encode(result);
}

#endif

#if ENABLE(VIDEO_TRACK)
EncodedJSValue jsSourceBufferTextTracks(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SourceBuffer& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.textTracks()));
    return JSValue::encode(result);
}

#endif

void setJSSourceBufferTimestampOffset(ExecState* exec, JSObject* /* baseObject */, EncodedJSValue thisValue, EncodedJSValue encodedValue)
{
    JSValue value = JSValue::decode(encodedValue);
    UNUSED_PARAM(exec);
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(JSValue::decode(thisValue));
    if (!castedThis) {
        throwVMTypeError(exec);
        return;
    }
    SourceBuffer& impl = castedThis->impl();
    ExceptionCode ec = 0;
    double nativeValue(value.toNumber(exec));
    if (exec->hadException())
        return;
    impl.setTimestampOffset(nativeValue, ec);
    setDOMException(exec, ec);
}


static EncodedJSValue JSC_HOST_CALL jsSourceBufferPrototypeFunctionAppendBuffer1(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSSourceBuffer::info());
    SourceBuffer& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    ArrayBuffer* data(toArrayBuffer(exec->argument(0)));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.appendBuffer(data, ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

static EncodedJSValue JSC_HOST_CALL jsSourceBufferPrototypeFunctionAppendBuffer2(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSSourceBuffer::info());
    SourceBuffer& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    RefPtr<ArrayBufferView> data(toArrayBufferView(exec->argument(0)));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.appendBuffer(data.get(), ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsSourceBufferPrototypeFunctionAppendBuffer(ExecState* exec)
{
    size_t argsCount = exec->argumentCount();
    JSValue arg0(exec->argument(0));
    if ((argsCount == 1 && (arg0.isObject() && asObject(arg0)->inherits(JSArrayBuffer::info()))))
        return jsSourceBufferPrototypeFunctionAppendBuffer1(exec);
    if ((argsCount == 1 && (arg0.isObject() && asObject(arg0)->inherits(JSArrayBufferView::info()))))
        return jsSourceBufferPrototypeFunctionAppendBuffer2(exec);
    if (argsCount < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    return throwVMTypeError(exec);
}

EncodedJSValue JSC_HOST_CALL jsSourceBufferPrototypeFunctionAbort(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSSourceBuffer* castedThis = jsDynamicCast<JSSourceBuffer*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSSourceBuffer::info());
    SourceBuffer& impl = castedThis->impl();
    ExceptionCode ec = 0;
    impl.abort(ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

void JSSourceBuffer::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
    JSSourceBuffer* thisObject = jsCast<JSSourceBuffer*>(cell);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
    Base::visitChildren(thisObject, visitor);
    thisObject->impl().visitJSEventListeners(visitor);
}

bool JSSourceBufferOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    JSSourceBuffer* jsSourceBuffer = jsCast<JSSourceBuffer*>(handle.get().asCell());
    if (jsSourceBuffer->impl().hasPendingActivity())
        return true;
    if (jsSourceBuffer->impl().isFiringEventListeners())
        return true;
    UNUSED_PARAM(visitor);
    return false;
}

void JSSourceBufferOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
{
    JSSourceBuffer* jsSourceBuffer = jsCast<JSSourceBuffer*>(handle.get().asCell());
    DOMWrapperWorld& world = *static_cast<DOMWrapperWorld*>(context);
    uncacheWrapper(world, &jsSourceBuffer->impl(), jsSourceBuffer);
    jsSourceBuffer->releaseImpl();
}

#if ENABLE(BINDING_INTEGRITY)
#if PLATFORM(WIN)
#pragma warning(disable: 4483)
extern "C" { extern void (*const __identifier("??_7SourceBuffer@WebCore@@6B@")[])(); }
#else
extern "C" { extern void* _ZTVN7WebCore12SourceBufferE[]; }
#endif
#endif
JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, SourceBuffer* impl)
{
    if (!impl)
        return jsNull();
    if (JSValue result = getExistingWrapper<JSSourceBuffer>(exec, impl))
        return result;

#if ENABLE(BINDING_INTEGRITY)
    void* actualVTablePointer = *(reinterpret_cast<void**>(impl));
#if PLATFORM(WIN)
    void* expectedVTablePointer = reinterpret_cast<void*>(__identifier("??_7SourceBuffer@WebCore@@6B@"));
#else
    void* expectedVTablePointer = &_ZTVN7WebCore12SourceBufferE[2];
#if COMPILER(CLANG)
    // If this fails SourceBuffer does not have a vtable, so you need to add the
    // ImplementationLacksVTable attribute to the interface definition
    COMPILE_ASSERT(__is_polymorphic(SourceBuffer), SourceBuffer_is_not_polymorphic);
#endif
#endif
    // If you hit this assertion you either have a use after free bug, or
    // SourceBuffer has subclasses. If SourceBuffer has subclasses that get passed
    // to toJS() we currently require SourceBuffer you to opt out of binding hardening
    // by adding the SkipVTableValidation attribute to the interface IDL definition
    RELEASE_ASSERT(actualVTablePointer == expectedVTablePointer);
#endif
    ReportMemoryCost<SourceBuffer>::reportMemoryCost(exec, impl);
    return createNewWrapper<JSSourceBuffer>(exec, globalObject, impl);
}

SourceBuffer* toSourceBuffer(JSC::JSValue value)
{
    return value.inherits(JSSourceBuffer::info()) ? &jsCast<JSSourceBuffer*>(value)->impl() : 0;
}

}

#endif // ENABLE(MEDIA_SOURCE)
