1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
24 */
25
26#include "config.h"
27
28#if ENABLE(VIDEO_TRACK)
29
30#include "JSDataCue.h"
31
32#include "JSDOMBinding.h"
33
34using namespace JSC;
35
36namespace WebCore {
37
38#if ENABLE(DATACUE_VALUE)
39JSValue JSDataCue::value(ExecState* exec) const
40{
41    return impl().value(exec);
42}
43
44void JSDataCue::setValue(ExecState* exec, JSValue value)
45{
46    impl().setValue(exec, value);
47}
48#endif
49
50EncodedJSValue JSC_HOST_CALL constructJSDataCue(ExecState* exec)
51{
52    DOMConstructorObject* castedThis = jsCast<DOMConstructorObject*>(exec->callee());
53    if (exec->argumentCount() < 3)
54        return throwVMError(exec, createNotEnoughArgumentsError(exec));
55
56    ExceptionCode ec = 0;
57    double startTime(exec->argument(0).toNumber(exec));
58    if (UNLIKELY(exec->hadException()))
59        return JSValue::encode(jsUndefined());
60
61    double endTime(exec->argument(1).toNumber(exec));
62    if (UNLIKELY(exec->hadException()))
63        return JSValue::encode(jsUndefined());
64
65    ScriptExecutionContext* context = castedThis->scriptExecutionContext();
66    if (!context)
67        return throwConstructorDocumentUnavailableError(*exec, "DataCue");
68
69    String type;
70#if ENABLE(DATACUE_VALUE)
71    if (exec->argumentCount() > 3) {
72        if (!exec->argument(3).isString())
73            return throwVMError(exec, createTypeError(exec, "Second argument of the constructor is not of type String"));
74        type = exec->argument(3).getString(exec);
75    }
76#endif
77
78    JSValue valueArgument = exec->argument(2);
79    if (valueArgument.isUndefinedOrNull()) {
80        setDOMException(exec, TypeError);
81        return JSValue::encode(JSValue());
82    }
83
84    RefPtr<DataCue> object;
85    if (valueArgument.isCell() && valueArgument.asCell()->inherits(std::remove_pointer<JSArrayBuffer*>::type::info())) {
86
87        ArrayBuffer* data(toArrayBuffer(valueArgument));
88        if (UNLIKELY(exec->hadException()))
89            return JSValue::encode(jsUndefined());
90
91        object = DataCue::create(*context, startTime, endTime, data, type, ec);
92        if (ec) {
93            setDOMException(exec, ec);
94            return JSValue::encode(JSValue());
95        }
96
97        return JSValue::encode(asObject(toJS(exec, castedThis->globalObject(), object.get())));
98    }
99
100#if !ENABLE(DATACUE_VALUE)
101    return JSValue::encode(jsUndefined());
102#else
103    object = DataCue::create(*context, startTime, endTime, valueArgument, type);
104    return JSValue::encode(asObject(toJS(exec, castedThis->globalObject(), object.get())));
105#endif
106}
107
108} // namespace WebCore
109
110#endif
111