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 "JSMessageEvent.h"
33
34#include "JSBlob.h"
35#include "JSDOMBinding.h"
36#include "JSDOMWindow.h"
37#include "JSEventTarget.h"
38#include "JSMessagePortCustom.h"
39#include "MessageEvent.h"
40#include <runtime/JSArray.h>
41#include <runtime/JSArrayBuffer.h>
42
43using namespace JSC;
44
45namespace WebCore {
46
47JSValue JSMessageEvent::data(ExecState* exec) const
48{
49    if (JSValue cachedValue = m_data.get())
50        return cachedValue;
51
52    MessageEvent& event = impl();
53    JSValue result;
54    switch (event.dataType()) {
55    case MessageEvent::DataTypeScriptValue: {
56        Deprecated::ScriptValue scriptValue = event.dataAsScriptValue();
57        if (scriptValue.hasNoValue())
58            result = jsNull();
59        else
60            result = scriptValue.jsValue();
61        break;
62    }
63
64    case MessageEvent::DataTypeSerializedScriptValue:
65        if (RefPtr<SerializedScriptValue> serializedValue = event.dataAsSerializedScriptValue()) {
66            MessagePortArray ports = impl().ports();
67            // FIXME: Why does this suppress exceptions?
68            result = serializedValue->deserialize(exec, globalObject(), &ports, NonThrowing);
69        } else
70            result = jsNull();
71        break;
72
73    case MessageEvent::DataTypeString:
74        result = jsStringWithCache(exec, event.dataAsString());
75        break;
76
77    case MessageEvent::DataTypeBlob:
78        result = toJS(exec, globalObject(), event.dataAsBlob());
79        break;
80
81    case MessageEvent::DataTypeArrayBuffer:
82        result = toJS(exec, globalObject(), event.dataAsArrayBuffer());
83        break;
84    }
85
86    // Save the result so we don't have to deserialize the value again.
87    const_cast<JSMessageEvent*>(this)->m_data.set(exec->vm(), this, result);
88    return result;
89}
90
91static JSC::JSValue handleInitMessageEvent(JSMessageEvent* jsEvent, JSC::ExecState* exec)
92{
93    const String& typeArg = exec->argument(0).toString(exec)->value(exec);
94    bool canBubbleArg = exec->argument(1).toBoolean(exec);
95    bool cancelableArg = exec->argument(2).toBoolean(exec);
96    const String originArg = exec->argument(4).toString(exec)->value(exec);
97    const String lastEventIdArg = exec->argument(5).toString(exec)->value(exec);
98    DOMWindow* sourceArg = toDOMWindow(exec->argument(6));
99    std::unique_ptr<MessagePortArray> messagePorts;
100    OwnPtr<ArrayBufferArray> arrayBuffers;
101    if (!exec->argument(7).isUndefinedOrNull()) {
102        messagePorts = std::make_unique<MessagePortArray>();
103        arrayBuffers = adoptPtr(new ArrayBufferArray);
104        fillMessagePortArray(exec, exec->argument(7), *messagePorts, *arrayBuffers);
105        if (exec->hadException())
106            return jsUndefined();
107    }
108    Deprecated::ScriptValue dataArg = Deprecated::ScriptValue(exec->vm(), exec->argument(3));
109    if (exec->hadException())
110        return jsUndefined();
111
112    MessageEvent& event = jsEvent->impl();
113    event.initMessageEvent(typeArg, canBubbleArg, cancelableArg, dataArg, originArg, lastEventIdArg, sourceArg, WTF::move(messagePorts));
114    jsEvent->m_data.set(exec->vm(), jsEvent, dataArg.jsValue());
115    return jsUndefined();
116}
117
118JSC::JSValue JSMessageEvent::initMessageEvent(JSC::ExecState* exec)
119{
120    return handleInitMessageEvent(this, exec);
121}
122
123JSC::JSValue JSMessageEvent::webkitInitMessageEvent(JSC::ExecState* exec)
124{
125    return handleInitMessageEvent(this, exec);
126}
127
128} // namespace WebCore
129