1/*
2 * Copyright (C) 2014 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "bindings/core/v8/V8Element.h"
33
34#include "bindings/core/v8/Dictionary.h"
35#include "bindings/core/v8/ExceptionState.h"
36#include "bindings/core/v8/V8AnimationEffect.h"
37#include "bindings/core/v8/V8AnimationPlayer.h"
38#include "bindings/core/v8/V8Binding.h"
39#include "bindings/core/v8/V8BindingMacros.h"
40#include "core/animation/ElementAnimation.h"
41#include "core/dom/Element.h"
42#include "core/frame/UseCounter.h"
43#include "platform/RuntimeEnabledFeatures.h"
44#include "wtf/GetPtr.h"
45
46namespace blink {
47
48void V8Element::scrollLeftAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
49{
50    ExceptionState exceptionState(ExceptionState::SetterContext, "scrollLeft", "Element", info.Holder(), info.GetIsolate());
51    Element* impl = V8Element::toImpl(info.Holder());
52
53    if (RuntimeEnabledFeatures::cssomSmoothScrollEnabled() && value->IsObject()) {
54        TONATIVE_VOID(Dictionary, scrollOptionsHorizontal, Dictionary(value, info.GetIsolate()));
55        impl->setScrollLeft(scrollOptionsHorizontal, exceptionState);
56        exceptionState.throwIfNeeded();
57        return;
58    }
59
60    TONATIVE_VOID_EXCEPTIONSTATE(float, position, toFloat(value, exceptionState), exceptionState);
61    impl->setScrollLeft(position);
62}
63
64void V8Element::scrollTopAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
65{
66    ExceptionState exceptionState(ExceptionState::SetterContext, "scrollTop", "Element", info.Holder(), info.GetIsolate());
67    Element* impl = V8Element::toImpl(info.Holder());
68
69    if (RuntimeEnabledFeatures::cssomSmoothScrollEnabled() && value->IsObject()) {
70        TONATIVE_VOID(Dictionary, scrollOptionsVertical, Dictionary(value, info.GetIsolate()));
71        impl->setScrollTop(scrollOptionsVertical, exceptionState);
72        exceptionState.throwIfNeeded();
73        return;
74    }
75
76    TONATIVE_VOID_EXCEPTIONSTATE(float, position, toFloat(value, exceptionState), exceptionState);
77    impl->setScrollTop(position);
78}
79
80////////////////////////////////////////////////////////////////////////////////
81// Overload resolution for animate()
82// FIXME: needs support for union types http://crbug.com/240176
83////////////////////////////////////////////////////////////////////////////////
84
85// AnimationPlayer animate(AnimationEffect? effect);
86void animate1Method(const v8::FunctionCallbackInfo<v8::Value>& info)
87{
88    Element* impl = V8Element::toImpl(info.Holder());
89    TONATIVE_VOID(AnimationEffect*, effect, V8AnimationEffect::toImplWithTypeCheck(info.GetIsolate(), info[0]));
90    v8SetReturnValueFast(info, WTF::getPtr(ElementAnimation::animate(*impl, effect)), impl);
91}
92
93// [RaisesException] AnimationPlayer animate(sequence<Dictionary> effect);
94void animate2Method(const v8::FunctionCallbackInfo<v8::Value>& info)
95{
96    ExceptionState exceptionState(ExceptionState::ExecutionContext, "animate", "Element", info.Holder(), info.GetIsolate());
97    Element* impl = V8Element::toImpl(info.Holder());
98    TONATIVE_VOID_EXCEPTIONSTATE(Vector<Dictionary>, keyframes, toImplArray<Dictionary>(info[0], 1, info.GetIsolate(), exceptionState), exceptionState);
99    RefPtrWillBeRawPtr<AnimationPlayer> result = ElementAnimation::animate(*impl, keyframes, exceptionState);
100    if (exceptionState.throwIfNeeded())
101        return;
102    v8SetReturnValueFast(info, WTF::getPtr(result.release()), impl);
103}
104
105// AnimationPlayer animate(AnimationEffect? effect, double timing);
106void animate3Method(const v8::FunctionCallbackInfo<v8::Value>& info)
107{
108    Element* impl = V8Element::toImpl(info.Holder());
109    TONATIVE_VOID(AnimationEffect*, effect, V8AnimationEffect::toImplWithTypeCheck(info.GetIsolate(), info[0]));
110    TONATIVE_VOID(double, duration, static_cast<double>(info[1]->NumberValue()));
111    v8SetReturnValueFast(info, WTF::getPtr(ElementAnimation::animate(*impl, effect, duration)), impl);
112}
113
114// AnimationPlayer animate(AnimationEffect? effect, Dictionary timing);
115void animate4Method(const v8::FunctionCallbackInfo<v8::Value>& info)
116{
117    Element* impl = V8Element::toImpl(info.Holder());
118    TONATIVE_VOID(AnimationEffect*, effect, V8AnimationEffect::toImplWithTypeCheck(info.GetIsolate(), info[0]));
119    TONATIVE_VOID(Dictionary, timingInput, Dictionary(info[1], info.GetIsolate()));
120    if (!timingInput.isUndefinedOrNull() && !timingInput.isObject()) {
121        V8ThrowException::throwTypeError(ExceptionMessages::failedToExecute("animate", "Element", "parameter 2 ('timingInput') is not an object."), info.GetIsolate());
122        return;
123    }
124    v8SetReturnValueFast(info, WTF::getPtr(ElementAnimation::animate(*impl, effect, timingInput)), impl);
125}
126
127// [RaisesException] AnimationPlayer animate(sequence<Dictionary> effect, double timing);
128void animate5Method(const v8::FunctionCallbackInfo<v8::Value>& info)
129{
130    ExceptionState exceptionState(ExceptionState::ExecutionContext, "animate", "Element", info.Holder(), info.GetIsolate());
131    Element* impl = V8Element::toImpl(info.Holder());
132    TONATIVE_VOID_EXCEPTIONSTATE(Vector<Dictionary>, keyframes, toImplArray<Dictionary>(info[0], 1, info.GetIsolate(), exceptionState), exceptionState);
133    TONATIVE_VOID(double, duration, static_cast<double>(info[1]->NumberValue()));
134    RefPtrWillBeRawPtr<AnimationPlayer> result = ElementAnimation::animate(*impl, keyframes, duration, exceptionState);
135    if (exceptionState.throwIfNeeded())
136        return;
137    v8SetReturnValueFast(info, WTF::getPtr(result.release()), impl);
138}
139
140// [RaisesException] AnimationPlayer animate(sequence<Dictionary> effect, Dictionary timing);
141void animate6Method(const v8::FunctionCallbackInfo<v8::Value>& info)
142{
143    ExceptionState exceptionState(ExceptionState::ExecutionContext, "animate", "Element", info.Holder(), info.GetIsolate());
144    Element* impl = V8Element::toImpl(info.Holder());
145    TONATIVE_VOID_EXCEPTIONSTATE(Vector<Dictionary>, keyframes, toImplArray<Dictionary>(info[0], 1, info.GetIsolate(), exceptionState), exceptionState);
146    TONATIVE_VOID(Dictionary, timingInput, Dictionary(info[1], info.GetIsolate()));
147    if (!timingInput.isUndefinedOrNull() && !timingInput.isObject()) {
148        exceptionState.throwTypeError("parameter 2 ('timingInput') is not an object.");
149        exceptionState.throwIfNeeded();
150        return;
151    }
152    RefPtrWillBeRawPtr<AnimationPlayer> result = ElementAnimation::animate(*impl, keyframes, timingInput, exceptionState);
153    if (exceptionState.throwIfNeeded())
154        return;
155    v8SetReturnValueFast(info, WTF::getPtr(result.release()), impl);
156}
157
158void V8Element::animateMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
159{
160    v8::Isolate* isolate = info.GetIsolate();
161    ExceptionState exceptionState(ExceptionState::ExecutionContext, "animate", "Element", info.Holder(), isolate);
162    // AnimationPlayer animate(
163    //     (AnimationEffect or sequence<Dictionary>)? effect,
164    //     optional (double or Dictionary) timing);
165    switch (info.Length()) {
166    case 1:
167        // null resolved as to AnimationEffect, as if the member were nullable:
168        // (AnimationEffect? or sequence<Dictionary>)
169        // instead of the *union* being nullable:
170        // (AnimationEffect or sequence<Dictionary>)?
171        // AnimationPlayer animate(AnimationEffect? effect);
172        if (info[0]->IsNull()) {
173            animate1Method(info);
174            return;
175        }
176        // AnimationPlayer animate(AnimationEffect effect);
177        if (V8AnimationEffect::hasInstance(info[0], isolate)) {
178            animate1Method(info);
179            return;
180        }
181        // [MeasureAs=ElementAnimateKeyframeListEffectNoTiming]
182        // AnimationPlayer animate(sequence<Dictionary> effect);
183        if (info[0]->IsArray()) {
184            UseCounter::count(callingExecutionContext(isolate), UseCounter::ElementAnimateKeyframeListEffectNoTiming);
185            animate2Method(info);
186            return;
187        }
188        break;
189    case 2:
190        // As above, null resolved to AnimationEffect
191        // AnimationPlayer animate(AnimationEffect? effect, Dictionary timing);
192        if (info[0]->IsNull() && info[1]->IsObject()) {
193            animate4Method(info);
194            return;
195        }
196        // AnimationPlayer animate(AnimationEffect? effect, double timing);
197        if (info[0]->IsNull()) {
198            animate3Method(info);
199            return;
200        }
201        // AnimationPlayer animate(AnimationEffect effect, Dictionary timing);
202        if (V8AnimationEffect::hasInstance(info[0], isolate)
203            && info[1]->IsObject()) {
204            animate4Method(info);
205            return;
206        }
207        // AnimationPlayer animate(AnimationEffect effect, double timing);
208        if (V8AnimationEffect::hasInstance(info[0], isolate)) {
209            animate3Method(info);
210            return;
211        }
212        // [MeasureAs=ElementAnimateKeyframeListEffectObjectTiming]
213        // AnimationPlayer animate(sequence<Dictionary> effect, Dictionary timing);
214        if (info[0]->IsArray() && info[1]->IsObject()) {
215            UseCounter::count(callingExecutionContext(isolate), UseCounter::ElementAnimateKeyframeListEffectObjectTiming);
216            animate6Method(info);
217            return;
218        }
219        // [MeasureAs=ElementAnimateKeyframeListEffectDoubleTiming]
220        // AnimationPlayer animate(sequence<Dictionary> effect, double timing);
221        if (info[0]->IsArray()) {
222            UseCounter::count(callingExecutionContext(isolate), UseCounter::ElementAnimateKeyframeListEffectDoubleTiming);
223            animate5Method(info);
224            return;
225        }
226        break;
227    default:
228        setArityTypeError(exceptionState, "[1]", info.Length());
229        exceptionState.throwIfNeeded();
230        return;
231        break;
232    }
233    exceptionState.throwTypeError("No function was found that matched the signature provided.");
234    exceptionState.throwIfNeeded();
235}
236
237} // namespace blink
238