Home | History | Annotate | Download | only in js
      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::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 JSC::JSValue JSMessagePort::postMessage(JSC::ExecState* exec)
     56 {
     57     return handlePostMessage(exec, impl());
     58 }
     59 
     60 void fillMessagePortArray(JSC::ExecState* exec, JSC::JSValue value, MessagePortArray& portArray)
     61 {
     62     // Convert from the passed-in JS array-like object to a MessagePortArray.
     63     // 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.
     64     if (value.isUndefinedOrNull()) {
     65         portArray.resize(0);
     66         return;
     67     }
     68 
     69     // Validation of sequence types, per WebIDL spec 4.1.13.
     70     unsigned length;
     71     JSObject* object = toJSSequence(exec, value, length);
     72     if (exec->hadException())
     73         return;
     74 
     75     portArray.resize(length);
     76     for (unsigned i = 0 ; i < length; ++i) {
     77         JSValue value = object->get(exec, i);
     78         if (exec->hadException())
     79             return;
     80         // Validation of non-null objects, per HTML5 spec 8.3.3.
     81         if (value.isUndefinedOrNull()) {
     82             setDOMException(exec, INVALID_STATE_ERR);
     83             return;
     84         }
     85 
     86         // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
     87         RefPtr<MessagePort> port = toMessagePort(value);
     88         if (!port) {
     89             throwTypeError(exec);
     90             return;
     91         }
     92         portArray[i] = port.release();
     93     }
     94 }
     95 
     96 } // namespace WebCore
     97