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 "V8MessageEvent.h" 33 34 #include "V8Blob.h" 35 #include "V8MessagePort.h" 36 #include "V8Window.h" 37 #include "bindings/v8/SerializedScriptValue.h" 38 #include "bindings/v8/V8Binding.h" 39 #include "bindings/v8/V8HiddenPropertyName.h" 40 #include "bindings/v8/custom/V8ArrayBufferCustom.h" 41 #include "core/events/MessageEvent.h" 42 43 namespace WebCore { 44 45 void V8MessageEvent::dataAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info) 46 { 47 MessageEvent* event = V8MessageEvent::toNative(info.Holder()); 48 49 v8::Handle<v8::Value> result; 50 switch (event->dataType()) { 51 case MessageEvent::DataTypeScriptValue: { 52 result = info.Holder()->GetHiddenValue(V8HiddenPropertyName::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 = getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenPropertyName::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 String stringValue = event->dataAsString(); 80 result = v8String(info.GetIsolate(), stringValue); 81 break; 82 } 83 84 case MessageEvent::DataTypeBlob: 85 result = toV8(event->dataAsBlob(), info.Holder(), info.GetIsolate()); 86 break; 87 88 case MessageEvent::DataTypeArrayBuffer: 89 result = toV8(event->dataAsArrayBuffer(), info.Holder(), info.GetIsolate()); 90 break; 91 } 92 93 // Overwrite the data attribute so it returns the cached result in future invocations. 94 // This custom handler (dataAccessGetter) will not be called again. 95 v8::PropertyAttribute dataAttr = static_cast<v8::PropertyAttribute>(v8::DontDelete | v8::ReadOnly); 96 info.Holder()->ForceSet(v8AtomicString(info.GetIsolate(), "data"), result, dataAttr); 97 v8SetReturnValue(info, result); 98 } 99 100 void V8MessageEvent::initMessageEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 101 { 102 MessageEvent* event = V8MessageEvent::toNative(info.Holder()); 103 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, typeArg, info[0]); 104 V8TRYCATCH_VOID(bool, canBubbleArg, info[1]->BooleanValue()); 105 V8TRYCATCH_VOID(bool, cancelableArg, info[2]->BooleanValue()); 106 v8::Handle<v8::Value> dataArg = info[3]; 107 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, originArg, info[4]); 108 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, lastEventIdArg, info[5]); 109 110 DOMWindow* sourceArg = 0; 111 if (info[6]->IsObject()) { 112 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(info[6]); 113 v8::Handle<v8::Object> window = wrapper->FindInstanceInPrototypeChain(V8Window::domTemplate(info.GetIsolate(), worldTypeInMainThread(info.GetIsolate()))); 114 if (!window.IsEmpty()) 115 sourceArg = V8Window::toNative(window); 116 } 117 OwnPtr<MessagePortArray> portArray; 118 119 const int portArrayIndex = 7; 120 if (!isUndefinedOrNull(info[portArrayIndex])) { 121 portArray = adoptPtr(new MessagePortArray); 122 if (!getMessagePortArray(info[portArrayIndex], portArrayIndex + 1, *portArray, info.GetIsolate())) 123 return; 124 } 125 event->initMessageEvent(typeArg, canBubbleArg, cancelableArg, originArg, lastEventIdArg, sourceArg, portArray.release()); 126 127 if (!dataArg.IsEmpty()) { 128 info.Holder()->SetHiddenValue(V8HiddenPropertyName::data(info.GetIsolate()), dataArg); 129 if (isolatedWorldForIsolate(info.GetIsolate())) 130 event->setSerializedData(SerializedScriptValue::createAndSwallowExceptions(dataArg, info.GetIsolate())); 131 } 132 } 133 134 void V8MessageEvent::webkitInitMessageEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 135 { 136 initMessageEventMethodCustom(info); 137 } 138 139 140 } // namespace WebCore 141