8be81beccbd4eec4b966563d0b8f2234d372a19e
[vuplus_webkit] / Source / WebCore / bindings / js / JSMessageEventCustom.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 "JSMessageEvent.h"
33
34 #include "JSArrayBuffer.h"
35 #include "JSBlob.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>
42
43 using namespace JSC;
44
45 namespace WebCore {
46
47 JSValue JSMessageEvent::data(ExecState* exec) const
48 {
49     if (JSValue cachedValue = m_data.get())
50         return cachedValue;
51
52     MessageEvent* event = static_cast<MessageEvent*>(impl());
53     JSValue result;
54     switch (event->dataType()) {
55     case MessageEvent::DataTypeSerializedScriptValue:
56         if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue())
57             result = serializedValue->deserialize(exec, globalObject(), NonThrowing);
58         else
59             result = jsNull();
60         break;
61
62     case MessageEvent::DataTypeString:
63         result = jsString(exec, event->dataAsString());
64         break;
65
66     case MessageEvent::DataTypeBlob:
67         result = toJS(exec, globalObject(), event->dataAsBlob());
68         break;
69
70     case MessageEvent::DataTypeArrayBuffer:
71         result = toJS(exec, globalObject(), event->dataAsArrayBuffer());
72         break;
73     }
74
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);
77     return result;
78 }
79
80 JSValue JSMessageEvent::ports(ExecState* exec) const
81 {
82     MessagePortArray* ports = static_cast<MessageEvent*>(impl())->ports();
83     if (!ports)
84         return constructEmptyArray(exec, globalObject());
85
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);
90 }
91
92 JSC::JSValue JSMessageEvent::initMessageEvent(JSC::ExecState* exec)
93 {
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())
99         return jsUndefined();
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();
109     }
110
111     MessageEvent* event = static_cast<MessageEvent*>(this->impl());
112     event->initMessageEvent(ustringToAtomicString(typeArg), canBubbleArg, cancelableArg, dataArg.release(), ustringToString(originArg), ustringToString(lastEventIdArg), sourceArg, messagePorts.release());
113     JSValue result;
114     if (SerializedScriptValue* serializedValue = event->dataAsSerializedScriptValue())
115         result = serializedValue->deserialize(exec, globalObject(), NonThrowing);
116     else
117         result = jsNull();
118     m_data.set(exec->globalData(), this, result);
119     return jsUndefined();
120 }
121
122 } // namespace WebCore