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 "AtomicString.h" 30 #include "Event.h" 31 #include "ExceptionCode.h" 32 #include "Frame.h" 33 #include "JSDOMGlobalObject.h" 34 #include "JSEvent.h" 35 #include "JSEventListener.h" 36 #include "JSMessagePortCustom.h" 37 #include "MessagePort.h" 38 #include <runtime/Error.h> 39 40 using namespace JSC; 41 42 namespace WebCore { 43 44 void JSMessagePort::markChildren(MarkStack& markStack) 45 { 46 Base::markChildren(markStack); 47 48 // 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(). 49 if (MessagePort* entangledPort = m_impl->locallyEntangledPort()) 50 markDOMObjectWrapper(markStack, *Heap::heap(this)->globalData(), entangledPort); 51 52 m_impl->markJSEventListeners(markStack); 53 } 54 55 JSValue JSMessagePort::addEventListener(ExecState* exec, const ArgList& args) 56 { 57 JSValue listener = args.at(1); 58 if (!listener.isObject()) 59 return jsUndefined(); 60 61 impl()->addEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)).get(), args.at(2).toBoolean(exec)); 62 return jsUndefined(); 63 } 64 65 JSValue JSMessagePort::removeEventListener(ExecState* exec, const ArgList& args) 66 { 67 JSValue listener = args.at(1); 68 if (!listener.isObject()) 69 return jsUndefined(); 70 71 impl()->removeEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)).get(), args.at(2).toBoolean(exec)); 72 return jsUndefined(); 73 } 74 75 JSC::JSValue JSMessagePort::postMessage(JSC::ExecState* exec, const JSC::ArgList& args) 76 { 77 return handlePostMessage(exec, args, impl()); 78 } 79 80 void fillMessagePortArray(JSC::ExecState* exec, JSC::JSValue value, MessagePortArray& portArray) 81 { 82 // Convert from the passed-in JS array-like object to a MessagePortArray. 83 // 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. 84 if (value.isUndefinedOrNull()) { 85 portArray.resize(0); 86 return; 87 } 88 89 // Validation of sequence types, per WebIDL spec 4.1.13. 90 unsigned length; 91 JSObject* object = toJSSequence(exec, value, length); 92 if (exec->hadException()) 93 return; 94 95 portArray.resize(length); 96 for (unsigned i = 0 ; i < length; ++i) { 97 JSValue value = object->get(exec, i); 98 if (exec->hadException()) 99 return; 100 // Validation of non-null objects, per HTML5 spec 8.3.3. 101 if (value.isUndefinedOrNull()) { 102 setDOMException(exec, INVALID_STATE_ERR); 103 return; 104 } 105 106 // Validation of Objects implementing an interface, per WebIDL spec 4.1.15. 107 RefPtr<MessagePort> port = toMessagePort(value); 108 if (!port) { 109 throwError(exec, TypeError); 110 return; 111 } 112 portArray[i] = port.release(); 113 } 114 } 115 116 } // namespace WebCore 117