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/SerializedScriptValue.h"
35 #include "bindings/core/v8/V8Binding.h"
36 #include "bindings/core/v8/V8Blob.h"
37 #include "bindings/core/v8/V8HiddenValue.h"
38 #include "bindings/core/v8/V8MessagePort.h"
39 #include "bindings/core/v8/V8Window.h"
40 #include "bindings/core/v8/custom/V8ArrayBufferCustom.h"
41 #include "core/events/MessageEvent.h"
42
43 namespace blink {
44
dataAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value> & info)45 void V8MessageEvent::dataAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
46 {
47 MessageEvent* event = V8MessageEvent::toImpl(info.Holder());
48
49 v8::Handle<v8::Value> result;
50 switch (event->dataType()) {
51 case MessageEvent::DataTypeScriptValue: {
52 result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::data(info.GetIsolate()));
53 if (result.IsEmpty()) {
54 if (!event->dataAsSerializedScriptValue()) {
55 // If we're in an isolated world and the event was created in the main world,
56 // we need to find the 'data' property on the main world wrapper and clone it.
57 v8::Local<v8::Value> mainWorldData = V8HiddenValue::getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenValue::data(info.GetIsolate()));
58 if (!mainWorldData.IsEmpty())
59 event->setSerializedData(SerializedScriptValue::createAndSwallowExceptions(mainWorldData, info.GetIsolate()));
60 }
61 if (event->dataAsSerializedScriptValue())
62 result = event->dataAsSerializedScriptValue()->deserialize(info.GetIsolate());
63 else
64 result = v8::Null(info.GetIsolate());
65 }
66 break;
67 }
68
69 case MessageEvent::DataTypeSerializedScriptValue:
70 if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue()) {
71 MessagePortArray ports = event->ports();
72 result = serializedValue->deserialize(info.GetIsolate(), &ports);
73 } else {
74 result = v8::Null(info.GetIsolate());
75 }
76 break;
77
78 case MessageEvent::DataTypeString: {
79 result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::stringData(info.GetIsolate()));
80 if (result.IsEmpty()) {
81 String stringValue = event->dataAsString();
82 result = v8String(info.GetIsolate(), stringValue);
83 }
84 break;
85 }
86
87 case MessageEvent::DataTypeBlob:
88 result = toV8(event->dataAsBlob(), info.Holder(), info.GetIsolate());
89 break;
90
91 case MessageEvent::DataTypeArrayBuffer:
92 result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::arrayBufferData(info.GetIsolate()));
93 if (result.IsEmpty())
94 result = toV8(event->dataAsArrayBuffer(), info.Holder(), info.GetIsolate());
95 break;
96 }
97
98 // Overwrite the data attribute so it returns the cached result in future invocations.
99 // This custom getter handler will not be called again.
100 v8::PropertyAttribute dataAttr = static_cast<v8::PropertyAttribute>(v8::DontDelete | v8::ReadOnly);
101 info.Holder()->ForceSet(v8AtomicString(info.GetIsolate(), "data"), result, dataAttr);
102 v8SetReturnValue(info, result);
103 }
104
initMessageEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value> & info)105 void V8MessageEvent::initMessageEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
106 {
107 ExceptionState exceptionState(ExceptionState::ExecutionContext, "initMessageEvent", "MessageEvent", info.Holder(), info.GetIsolate());
108 MessageEvent* event = V8MessageEvent::toImpl(info.Holder());
109 TOSTRING_VOID(V8StringResource<>, typeArg, info[0]);
110 TONATIVE_VOID(bool, canBubbleArg, info[1]->BooleanValue());
111 TONATIVE_VOID(bool, cancelableArg, info[2]->BooleanValue());
112 v8::Handle<v8::Value> dataArg = info[3];
113 TOSTRING_VOID(V8StringResource<>, originArg, info[4]);
114 TOSTRING_VOID(V8StringResource<>, lastEventIdArg, info[5]);
115 LocalDOMWindow* sourceArg = toDOMWindow(info[6], info.GetIsolate());
116 OwnPtrWillBeRawPtr<MessagePortArray> portArray = nullptr;
117 const int portArrayIndex = 7;
118 if (!isUndefinedOrNull(info[portArrayIndex])) {
119 portArray = adoptPtrWillBeNoop(new MessagePortArray);
120 *portArray = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(info[portArrayIndex], portArrayIndex + 1, info.GetIsolate(), exceptionState);
121 if (exceptionState.throwIfNeeded())
122 return;
123 }
124 event->initMessageEvent(typeArg, canBubbleArg, cancelableArg, originArg, lastEventIdArg, sourceArg, portArray.release());
125
126 if (!dataArg.IsEmpty()) {
127 V8HiddenValue::setHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::data(info.GetIsolate()), dataArg);
128 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld())
129 event->setSerializedData(SerializedScriptValue::createAndSwallowExceptions(dataArg, info.GetIsolate()));
130 }
131 }
132
133 } // namespace blink
134