Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8MessageEventCustom.cpp
1 /*
2  * Copyright (C) 2009 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/V8MessageEvent.h"
33
34 #include "bindings/core/v8/V8Blob.h"
35 #include "bindings/core/v8/V8MessagePort.h"
36 #include "bindings/core/v8/V8Window.h"
37 #include "bindings/v8/SerializedScriptValue.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8HiddenValue.h"
40 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
41 #include "core/events/MessageEvent.h"
42
43 namespace WebCore {
44
45 // Ensures a wrapper is created for the data to return now so that V8 knows how
46 // much memory is used via the wrapper. To keep the wrapper alive, it's set to
47 // the wrapper of the MessageEvent as a hidden value.
48 static void ensureWrapperCreatedAndAssociated(MessageEvent* eventImpl, v8::Handle<v8::Object> eventWrapper, v8::Isolate* isolate)
49 {
50     switch (eventImpl->dataType()) {
51     case MessageEvent::DataTypeScriptValue:
52     case MessageEvent::DataTypeSerializedScriptValue:
53         break;
54     case MessageEvent::DataTypeString: {
55         String stringValue = eventImpl->dataAsString();
56         V8HiddenValue::setHiddenValue(isolate, eventWrapper, V8HiddenValue::stringData(isolate), v8String(isolate, stringValue));
57         break;
58     }
59     case MessageEvent::DataTypeBlob:
60         break;
61     case MessageEvent::DataTypeArrayBuffer:
62         V8HiddenValue::setHiddenValue(isolate, eventWrapper, V8HiddenValue::arrayBufferData(isolate), toV8(eventImpl->dataAsArrayBuffer(), eventWrapper, isolate));
63         break;
64     }
65 }
66
67 v8::Handle<v8::Object> wrap(MessageEvent* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
68 {
69     ASSERT(impl);
70     ASSERT(!DOMDataStore::containsWrapper<V8MessageEvent>(impl, isolate));
71     v8::Handle<v8::Object> wrapper = V8MessageEvent::createWrapper(impl, creationContext, isolate);
72     ensureWrapperCreatedAndAssociated(impl, wrapper, isolate);
73     return wrapper;
74 }
75
76 void V8MessageEvent::dataAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
77 {
78     MessageEvent* event = V8MessageEvent::toNative(info.Holder());
79
80     v8::Handle<v8::Value> result;
81     switch (event->dataType()) {
82     case MessageEvent::DataTypeScriptValue: {
83         result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::data(info.GetIsolate()));
84         if (result.IsEmpty()) {
85             if (!event->dataAsSerializedScriptValue()) {
86                 // If we're in an isolated world and the event was created in the main world,
87                 // we need to find the 'data' property on the main world wrapper and clone it.
88                 v8::Local<v8::Value> mainWorldData = V8HiddenValue::getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenValue::data(info.GetIsolate()));
89                 if (!mainWorldData.IsEmpty())
90                     event->setSerializedData(SerializedScriptValue::createAndSwallowExceptions(mainWorldData, info.GetIsolate()));
91             }
92             if (event->dataAsSerializedScriptValue())
93                 result = event->dataAsSerializedScriptValue()->deserialize(info.GetIsolate());
94             else
95                 result = v8::Null(info.GetIsolate());
96         }
97         break;
98     }
99
100     case MessageEvent::DataTypeSerializedScriptValue:
101         if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue()) {
102             MessagePortArray ports = event->ports();
103             result = serializedValue->deserialize(info.GetIsolate(), &ports);
104         } else {
105             result = v8::Null(info.GetIsolate());
106         }
107         break;
108
109     case MessageEvent::DataTypeString: {
110         result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::stringData(info.GetIsolate()));
111         if (result.IsEmpty()) {
112             String stringValue = event->dataAsString();
113             result = v8String(info.GetIsolate(), stringValue);
114         }
115         break;
116     }
117
118     case MessageEvent::DataTypeBlob:
119         result = toV8(event->dataAsBlob(), info.Holder(), info.GetIsolate());
120         break;
121
122     case MessageEvent::DataTypeArrayBuffer:
123         result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::arrayBufferData(info.GetIsolate()));
124         if (result.IsEmpty())
125             result = toV8(event->dataAsArrayBuffer(), info.Holder(), info.GetIsolate());
126         break;
127     }
128
129     // Overwrite the data attribute so it returns the cached result in future invocations.
130     // This custom getter handler will not be called again.
131     v8::PropertyAttribute dataAttr = static_cast<v8::PropertyAttribute>(v8::DontDelete | v8::ReadOnly);
132     info.Holder()->ForceSet(v8AtomicString(info.GetIsolate(), "data"), result, dataAttr);
133     v8SetReturnValue(info, result);
134 }
135
136 void V8MessageEvent::initMessageEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
137 {
138     MessageEvent* event = V8MessageEvent::toNative(info.Holder());
139     TOSTRING_VOID(V8StringResource<>, typeArg, info[0]);
140     TONATIVE_VOID(bool, canBubbleArg, info[1]->BooleanValue());
141     TONATIVE_VOID(bool, cancelableArg, info[2]->BooleanValue());
142     v8::Handle<v8::Value> dataArg = info[3];
143     TOSTRING_VOID(V8StringResource<>, originArg, info[4]);
144     TOSTRING_VOID(V8StringResource<>, lastEventIdArg, info[5]);
145     LocalDOMWindow* sourceArg = toDOMWindow(info[6], info.GetIsolate());
146     OwnPtr<MessagePortArray> portArray;
147     const int portArrayIndex = 7;
148     if (!isUndefinedOrNull(info[portArrayIndex])) {
149         portArray = adoptPtr(new MessagePortArray);
150         bool success = false;
151         *portArray = toRefPtrNativeArray<MessagePort, V8MessagePort>(info[portArrayIndex], portArrayIndex + 1, info.GetIsolate(), &success);
152         if (!success)
153             return;
154     }
155     event->initMessageEvent(typeArg, canBubbleArg, cancelableArg, originArg, lastEventIdArg, sourceArg, portArray.release());
156
157     if (!dataArg.IsEmpty()) {
158         V8HiddenValue::setHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::data(info.GetIsolate()), dataArg);
159         if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld())
160             event->setSerializedData(SerializedScriptValue::createAndSwallowExceptions(dataArg, info.GetIsolate()));
161     }
162 }
163
164 } // namespace WebCore