2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "JSMessageEvent.h"
34 #include "JSArrayBuffer.h"
36 #include "JSDOMBinding.h"
37 #include "JSDOMWindow.h"
38 #include "JSEventTarget.h"
39 #include "JSMessagePortCustom.h"
40 #include "MessageEvent.h"
41 #include <runtime/JSArray.h>
47 JSValue JSMessageEvent::data(ExecState* exec) const
49 if (JSValue cachedValue = m_data.get())
52 MessageEvent* event = static_cast<MessageEvent*>(impl());
54 switch (event->dataType()) {
55 case MessageEvent::DataTypeSerializedScriptValue:
56 if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue())
57 result = serializedValue->deserialize(exec, globalObject(), NonThrowing);
62 case MessageEvent::DataTypeString:
63 result = jsString(exec, event->dataAsString());
66 case MessageEvent::DataTypeBlob:
67 result = toJS(exec, globalObject(), event->dataAsBlob());
70 case MessageEvent::DataTypeArrayBuffer:
71 result = toJS(exec, globalObject(), event->dataAsArrayBuffer());
75 // Save the result so we don't have to deserialize the value again.
76 const_cast<JSMessageEvent*>(this)->m_data.set(exec->globalData(), this, result);
80 JSValue JSMessageEvent::ports(ExecState* exec) const
82 MessagePortArray* ports = static_cast<MessageEvent*>(impl())->ports();
84 return constructEmptyArray(exec, globalObject());
86 MarkedArgumentBuffer list;
87 for (size_t i = 0; i < ports->size(); i++)
88 list.append(toJS(exec, globalObject(), (*ports)[i].get()));
89 return constructArray(exec, globalObject(), list);
92 JSC::JSValue JSMessageEvent::initMessageEvent(JSC::ExecState* exec)
94 const UString& typeArg = exec->argument(0).toString(exec);
95 bool canBubbleArg = exec->argument(1).toBoolean(exec);
96 bool cancelableArg = exec->argument(2).toBoolean(exec);
97 RefPtr<SerializedScriptValue> dataArg = SerializedScriptValue::create(exec, exec->argument(3));
98 if (exec->hadException())
100 const UString& originArg = exec->argument(4).toString(exec);
101 const UString& lastEventIdArg = exec->argument(5).toString(exec);
102 DOMWindow* sourceArg = toDOMWindow(exec->argument(6));
103 OwnPtr<MessagePortArray> messagePorts;
104 if (!exec->argument(7).isUndefinedOrNull()) {
105 messagePorts = adoptPtr(new MessagePortArray);
106 fillMessagePortArray(exec, exec->argument(7), *messagePorts);
107 if (exec->hadException())
108 return jsUndefined();
111 MessageEvent* event = static_cast<MessageEvent*>(this->impl());
112 event->initMessageEvent(ustringToAtomicString(typeArg), canBubbleArg, cancelableArg, dataArg.release(), ustringToString(originArg), ustringToString(lastEventIdArg), sourceArg, messagePorts.release());
114 if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue())
115 result = serializedValue->deserialize(exec, globalObject(), NonThrowing);
118 m_data.set(exec->globalData(), this, result);
119 return jsUndefined();
122 } // namespace WebCore