initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSMessagePortCustom.cpp
1 /*
2  * Copyright (C) 2008, 2009 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "JSMessagePort.h"
28
29 #include "Event.h"
30 #include "ExceptionCode.h"
31 #include "Frame.h"
32 #include "JSDOMGlobalObject.h"
33 #include "JSEvent.h"
34 #include "JSEventListener.h"
35 #include "JSMessagePortCustom.h"
36 #include "MessagePort.h"
37 #include <runtime/Error.h>
38 #include <wtf/text/AtomicString.h>
39
40 using namespace JSC;
41
42 namespace WebCore {
43
44 void JSMessagePort::visitChildren(SlotVisitor& visitor)
45 {
46     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
47     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
48     ASSERT(structure()->typeInfo().overridesVisitChildren());
49     Base::visitChildren(visitor);
50
51     // If we have a locally entangled port, we can directly mark it as reachable. Ports that are remotely entangled are marked in-use by markActiveObjectsForContext().
52     if (MessagePort* port = m_impl->locallyEntangledPort())
53         visitor.addOpaqueRoot(port);
54
55     m_impl->visitJSEventListeners(visitor);
56 }
57
58 JSC::JSValue JSMessagePort::postMessage(JSC::ExecState* exec)
59 {
60     return handlePostMessage(exec, impl());
61 }
62
63 void fillMessagePortArray(JSC::ExecState* exec, JSC::JSValue value, MessagePortArray& portArray)
64 {
65     // Convert from the passed-in JS array-like object to a MessagePortArray.
66     // Also validates the elements per sections 4.1.13 and 4.1.15 of the WebIDL spec and section 8.3.3 of the HTML5 spec.
67     if (value.isUndefinedOrNull()) {
68         portArray.resize(0);
69         return;
70     }
71
72     // Validation of sequence types, per WebIDL spec 4.1.13.
73     unsigned length;
74     JSObject* object = toJSSequence(exec, value, length);
75     if (exec->hadException())
76         return;
77
78     for (unsigned i = 0 ; i < length; ++i) {
79         JSValue value = object->get(exec, i);
80         if (exec->hadException())
81             return;
82         // Validation of non-null objects, per HTML5 spec 10.3.3.
83         if (value.isUndefinedOrNull()) {
84             setDOMException(exec, DATA_CLONE_ERR);
85             return;
86         }
87
88         // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
89         RefPtr<MessagePort> port = toMessagePort(value);
90         if (!port) {
91             throwTypeError(exec);
92             return;
93         }
94         portArray.append(port.release());
95     }
96 }
97
98 } // namespace WebCore