1/*
2 * Copyright (C) 2012 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/V8CustomEvent.h"
33
34#include "bindings/core/v8/Dictionary.h"
35#include "bindings/core/v8/SerializedScriptValue.h"
36#include "bindings/core/v8/V8Binding.h"
37#include "bindings/core/v8/V8DOMWrapper.h"
38#include "bindings/core/v8/V8Event.h"
39#include "bindings/core/v8/V8HiddenValue.h"
40#include "core/dom/ContextFeatures.h"
41#include "platform/RuntimeEnabledFeatures.h"
42
43namespace blink {
44
45static v8::Handle<v8::Value> cacheState(v8::Handle<v8::Object> customEvent, v8::Handle<v8::Value> detail, v8::Isolate* isolate)
46{
47    V8HiddenValue::setHiddenValue(isolate, customEvent, V8HiddenValue::detail(isolate), detail);
48    return detail;
49}
50
51
52void V8CustomEvent::detailAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
53{
54    CustomEvent* event = V8CustomEvent::toImpl(info.Holder());
55
56    v8::Handle<v8::Value> result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::detail(info.GetIsolate()));
57
58    if (!result.IsEmpty()) {
59        v8SetReturnValue(info, result);
60        return;
61    }
62
63    if (!event->serializedDetail()) {
64        // If we're in an isolated world and the event was created in the main world,
65        // we need to find the 'detail' property on the main world wrapper and clone it.
66        v8::Local<v8::Value> mainWorldDetail = V8HiddenValue::getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenValue::detail(info.GetIsolate()));
67        if (!mainWorldDetail.IsEmpty())
68            event->setSerializedDetail(SerializedScriptValue::createAndSwallowExceptions(mainWorldDetail, info.GetIsolate()));
69    }
70
71    if (event->serializedDetail()) {
72        result = event->serializedDetail()->deserialize();
73        v8SetReturnValue(info, cacheState(info.Holder(), result, info.GetIsolate()));
74        return;
75    }
76
77    v8SetReturnValue(info, cacheState(info.Holder(), v8::Null(info.GetIsolate()), info.GetIsolate()));
78}
79
80void V8CustomEvent::initCustomEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
81{
82    CustomEvent* event = V8CustomEvent::toImpl(info.Holder());
83    ASSERT(!event->serializedDetail());
84
85    TOSTRING_VOID(V8StringResource<>, typeArg, info[0]);
86    TONATIVE_VOID(bool, canBubbleArg, info[1]->BooleanValue());
87    TONATIVE_VOID(bool, cancelableArg, info[2]->BooleanValue());
88    v8::Handle<v8::Value> detailsArg = info[3];
89
90    event->initEvent(typeArg, canBubbleArg, cancelableArg);
91
92    if (!detailsArg.IsEmpty()) {
93        V8HiddenValue::setHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::detail(info.GetIsolate()), detailsArg);
94        if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld())
95            event->setSerializedDetail(SerializedScriptValue::createAndSwallowExceptions(detailsArg, info.GetIsolate()));
96    }
97}
98
99} // namespace blink
100