/*
    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 "JSMediaSource.h"

#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include "JSSourceBuffer.h"
#include "JSSourceBufferList.h"
#include "MediaSource.h"
#include "SourceBuffer.h"
#include "SourceBufferList.h"
#include "URL.h"
#include <runtime/Error.h>
#include <runtime/JSString.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSMediaSourceTableValues[] =
{
    { "sourceBuffers", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsMediaSourceSourceBuffers), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "activeSourceBuffers", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsMediaSourceActiveSourceBuffers), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "duration", DontDelete, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsMediaSourceDuration), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(setJSMediaSourceDuration) },
    { "readyState", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsMediaSourceReadyState), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "constructor", DontEnum | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsMediaSourceConstructor), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSMediaSourceTable = { 16, 15, true, JSMediaSourceTableValues, 0 };
/* Hash table for constructor */

static const HashTableValue JSMediaSourceConstructorTableValues[] =
{
    { "isTypeSupported", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMediaSourceConstructorFunctionIsTypeSupported), (intptr_t) (1) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSMediaSourceConstructorTable = { 1, 0, false, JSMediaSourceConstructorTableValues, 0 };
EncodedJSValue JSC_HOST_CALL JSMediaSourceConstructor::constructJSMediaSource(ExecState* exec)
{
    JSMediaSourceConstructor* castedThis = jsCast<JSMediaSourceConstructor*>(exec->callee());
    ScriptExecutionContext* context = castedThis->scriptExecutionContext();
    if (!context)
        return throwVMError(exec, createReferenceError(exec, "MediaSource constructor associated document is unavailable"));
    RefPtr<MediaSource> object = MediaSource::create(*context);
    return JSValue::encode(asObject(toJS(exec, castedThis->globalObject(), object.get())));
}

const ClassInfo JSMediaSourceConstructor::s_info = { "MediaSourceConstructor", &Base::s_info, &JSMediaSourceConstructorTable, 0, CREATE_METHOD_TABLE(JSMediaSourceConstructor) };

JSMediaSourceConstructor::JSMediaSourceConstructor(Structure* structure, JSDOMGlobalObject* globalObject)
    : DOMConstructorObject(structure, globalObject)
{
}

void JSMediaSourceConstructor::finishCreation(VM& vm, JSDOMGlobalObject* globalObject)
{
    Base::finishCreation(vm);
    ASSERT(inherits(info()));
    putDirect(vm, vm.propertyNames->prototype, JSMediaSourcePrototype::self(vm, globalObject), DontDelete | ReadOnly);
    putDirect(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontDelete | DontEnum);
}

bool JSMediaSourceConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    return getStaticPropertySlot<JSMediaSourceConstructor, JSDOMWrapper>(exec, JSMediaSourceConstructorTable, jsCast<JSMediaSourceConstructor*>(object), propertyName, slot);
}

ConstructType JSMediaSourceConstructor::getConstructData(JSCell*, ConstructData& constructData)
{
    constructData.native.function = constructJSMediaSource;
    return ConstructTypeHost;
}

/* Hash table for prototype */

static const HashTableValue JSMediaSourcePrototypeTableValues[] =
{
    { "addSourceBuffer", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMediaSourcePrototypeFunctionAddSourceBuffer), (intptr_t) (1) },
    { "removeSourceBuffer", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMediaSourcePrototypeFunctionRemoveSourceBuffer), (intptr_t) (1) },
    { "endOfStream", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMediaSourcePrototypeFunctionEndOfStream), (intptr_t) (0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSMediaSourcePrototypeTable = { 8, 7, false, JSMediaSourcePrototypeTableValues, 0 };
const ClassInfo JSMediaSourcePrototype::s_info = { "MediaSourcePrototype", &Base::s_info, &JSMediaSourcePrototypeTable, 0, CREATE_METHOD_TABLE(JSMediaSourcePrototype) };

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

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

const ClassInfo JSMediaSource::s_info = { "MediaSource", &Base::s_info, &JSMediaSourceTable, 0 , CREATE_METHOD_TABLE(JSMediaSource) };

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

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

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

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

EncodedJSValue jsMediaSourceSourceBuffers(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    MediaSource& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.sourceBuffers()));
    return JSValue::encode(result);
}


EncodedJSValue jsMediaSourceActiveSourceBuffers(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    MediaSource& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.activeSourceBuffers()));
    return JSValue::encode(result);
}


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


EncodedJSValue jsMediaSourceReadyState(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    MediaSource& impl = castedThis->impl();
    JSValue result = jsStringWithCache(exec, impl.readyState());
    return JSValue::encode(result);
}


EncodedJSValue jsMediaSourceConstructor(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue, PropertyName)
{
    JSMediaSource* domObject = jsDynamicCast<JSMediaSource*>(JSValue::decode(thisValue));
    if (!domObject)
        return throwVMTypeError(exec);
    if (!domObject)
        return throwVMTypeError(exec);
    return JSValue::encode(JSMediaSource::getConstructor(exec->vm(), domObject->globalObject()));
}

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


JSValue JSMediaSource::getConstructor(VM& vm, JSGlobalObject* globalObject)
{
    return getDOMConstructor<JSMediaSourceConstructor>(vm, jsCast<JSDOMGlobalObject*>(globalObject));
}

EncodedJSValue JSC_HOST_CALL jsMediaSourcePrototypeFunctionAddSourceBuffer(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMediaSource::info());
    MediaSource& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    const String& type(exec->argument(0).isEmpty() ? String() : exec->argument(0).toString(exec)->value(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.addSourceBuffer(type, ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}

EncodedJSValue JSC_HOST_CALL jsMediaSourcePrototypeFunctionRemoveSourceBuffer(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMediaSource::info());
    MediaSource& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    SourceBuffer* buffer(toSourceBuffer(exec->argument(0)));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.removeSourceBuffer(buffer, ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsMediaSourcePrototypeFunctionEndOfStream(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMediaSource* castedThis = jsDynamicCast<JSMediaSource*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMediaSource::info());
    MediaSource& impl = castedThis->impl();
    ExceptionCode ec = 0;
    const String& error(argumentOrNull(exec, 0).isEmpty() ? String() : argumentOrNull(exec, 0).toString(exec)->value(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.endOfStream(error, ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsMediaSourceConstructorFunctionIsTypeSupported(ExecState* exec)
{
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    const String& type(exec->argument(0).isEmpty() ? String() : exec->argument(0).toString(exec)->value(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = jsBoolean(MediaSource::isTypeSupported(type));
    return JSValue::encode(result);
}

void JSMediaSource::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
    JSMediaSource* thisObject = jsCast<JSMediaSource*>(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 JSMediaSourceOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    JSMediaSource* jsMediaSource = jsCast<JSMediaSource*>(handle.get().asCell());
    if (jsMediaSource->impl().hasPendingActivity())
        return true;
    if (jsMediaSource->impl().isFiringEventListeners())
        return true;
    UNUSED_PARAM(visitor);
    return false;
}

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

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

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

MediaSource* toMediaSource(JSC::JSValue value)
{
    return value.inherits(JSMediaSource::info()) ? &jsCast<JSMediaSource*>(value)->impl() : 0;
}

}

#endif // ENABLE(MEDIA_SOURCE)
