1 /* 2 * Copyright (C) 2008 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 COMPUTER, 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 COMPUTER, 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 27 #include "config.h" 28 #include "core/dom/MessagePort.h" 29 30 #include "bindings/v8/ExceptionState.h" 31 #include "bindings/v8/ExceptionStatePlaceholder.h" 32 #include "core/dom/Document.h" 33 #include "core/dom/EventNames.h" 34 #include "core/dom/ExceptionCode.h" 35 #include "core/dom/MessageEvent.h" 36 #include "core/page/DOMWindow.h" 37 #include "core/workers/WorkerGlobalScope.h" 38 #include "wtf/text/AtomicString.h" 39 40 namespace WebCore { 41 42 MessagePort::MessagePort(ScriptExecutionContext& scriptExecutionContext) 43 : m_started(false) 44 , m_closed(false) 45 , m_scriptExecutionContext(&scriptExecutionContext) 46 { 47 ScriptWrappable::init(this); 48 m_scriptExecutionContext->createdMessagePort(this); 49 50 // Don't need to call processMessagePortMessagesSoon() here, because the port will not be opened until start() is invoked. 51 } 52 53 MessagePort::~MessagePort() 54 { 55 close(); 56 if (m_scriptExecutionContext) 57 m_scriptExecutionContext->destroyedMessagePort(this); 58 } 59 60 void MessagePort::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& es) 61 { 62 if (!isEntangled()) 63 return; 64 ASSERT(m_scriptExecutionContext); 65 66 OwnPtr<MessagePortChannelArray> channels; 67 // Make sure we aren't connected to any of the passed-in ports. 68 if (ports) { 69 for (unsigned int i = 0; i < ports->size(); ++i) { 70 MessagePort* dataPort = (*ports)[i].get(); 71 if (dataPort == this || m_entangledChannel->isConnectedTo(dataPort)) { 72 es.throwDOMException(DataCloneError); 73 return; 74 } 75 } 76 channels = MessagePort::disentanglePorts(ports, es); 77 if (es.hadException()) 78 return; 79 } 80 m_entangledChannel->postMessageToRemote(message, channels.release()); 81 } 82 83 PassOwnPtr<MessagePortChannel> MessagePort::disentangle() 84 { 85 ASSERT(m_entangledChannel); 86 87 m_entangledChannel->disentangle(); 88 89 // We can't receive any messages or generate any events, so remove ourselves from the list of active ports. 90 ASSERT(m_scriptExecutionContext); 91 m_scriptExecutionContext->destroyedMessagePort(this); 92 m_scriptExecutionContext = 0; 93 94 return m_entangledChannel.release(); 95 } 96 97 // Invoked to notify us that there are messages available for this port. 98 // This code may be called from another thread, and so should not call any non-threadsafe APIs (i.e. should not call into the entangled channel or access mutable variables). 99 void MessagePort::messageAvailable() 100 { 101 ASSERT(m_scriptExecutionContext); 102 m_scriptExecutionContext->processMessagePortMessagesSoon(); 103 } 104 105 void MessagePort::start() 106 { 107 // Do nothing if we've been cloned or closed. 108 if (!isEntangled()) 109 return; 110 111 ASSERT(m_scriptExecutionContext); 112 if (m_started) 113 return; 114 115 m_started = true; 116 m_scriptExecutionContext->processMessagePortMessagesSoon(); 117 } 118 119 void MessagePort::close() 120 { 121 if (isEntangled()) 122 m_entangledChannel->close(); 123 m_closed = true; 124 } 125 126 void MessagePort::entangle(PassOwnPtr<MessagePortChannel> remote) 127 { 128 // Only invoked to set our initial entanglement. 129 ASSERT(!m_entangledChannel); 130 ASSERT(m_scriptExecutionContext); 131 132 // Don't entangle the ports if the channel is closed. 133 if (remote->entangleIfOpen(this)) 134 m_entangledChannel = remote; 135 } 136 137 void MessagePort::contextDestroyed() 138 { 139 ASSERT(m_scriptExecutionContext); 140 // Must be closed before blowing away the cached context, to ensure that we get no more calls to messageAvailable(). 141 // ScriptExecutionContext::closeMessagePorts() takes care of that. 142 ASSERT(m_closed); 143 m_scriptExecutionContext = 0; 144 } 145 146 const AtomicString& MessagePort::interfaceName() const 147 { 148 return eventNames().interfaceForMessagePort; 149 } 150 151 ScriptExecutionContext* MessagePort::scriptExecutionContext() const 152 { 153 return m_scriptExecutionContext; 154 } 155 156 void MessagePort::dispatchMessages() 157 { 158 // Messages for contexts that are not fully active get dispatched too, but JSAbstractEventListener::handleEvent() doesn't call handlers for these. 159 // The HTML5 spec specifies that any messages sent to a document that is not fully active should be dropped, so this behavior is OK. 160 ASSERT(started()); 161 162 RefPtr<SerializedScriptValue> message; 163 OwnPtr<MessagePortChannelArray> channels; 164 while (m_entangledChannel && m_entangledChannel->tryGetMessageFromRemote(message, channels)) { 165 166 // close() in Worker onmessage handler should prevent next message from dispatching. 167 if (m_scriptExecutionContext->isWorkerGlobalScope() && toWorkerGlobalScope(m_scriptExecutionContext)->isClosing()) 168 return; 169 170 OwnPtr<MessagePortArray> ports = MessagePort::entanglePorts(*m_scriptExecutionContext, channels.release()); 171 RefPtr<Event> evt = MessageEvent::create(ports.release(), message.release()); 172 173 dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION); 174 } 175 } 176 177 bool MessagePort::hasPendingActivity() 178 { 179 // The spec says that entangled message ports should always be treated as if they have a strong reference. 180 // We'll also stipulate that the queue needs to be open (if the app drops its reference to the port before start()-ing it, then it's not really entangled as it's unreachable). 181 if (m_started && m_entangledChannel && m_entangledChannel->hasPendingActivity()) 182 return true; 183 if (isEntangled() && !locallyEntangledPort()) 184 return true; 185 return false; 186 } 187 188 MessagePort* MessagePort::locallyEntangledPort() 189 { 190 return m_entangledChannel ? m_entangledChannel->locallyEntangledPort(m_scriptExecutionContext) : 0; 191 } 192 193 PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(const MessagePortArray* ports, ExceptionState& es) 194 { 195 if (!ports || !ports->size()) 196 return nullptr; 197 198 // HashSet used to efficiently check for duplicates in the passed-in array. 199 HashSet<MessagePort*> portSet; 200 201 // Walk the incoming array - if there are any duplicate ports, or null ports or cloned ports, throw an error (per section 8.3.3 of the HTML5 spec). 202 for (unsigned int i = 0; i < ports->size(); ++i) { 203 MessagePort* port = (*ports)[i].get(); 204 if (!port || port->isNeutered() || portSet.contains(port)) { 205 es.throwDOMException(DataCloneError); 206 return nullptr; 207 } 208 portSet.add(port); 209 } 210 211 // Passed-in ports passed validity checks, so we can disentangle them. 212 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelArray(ports->size())); 213 for (unsigned int i = 0 ; i < ports->size() ; ++i) { 214 OwnPtr<MessagePortChannel> channel = (*ports)[i]->disentangle(); 215 (*portArray)[i] = channel.release(); 216 } 217 return portArray.release(); 218 } 219 220 PassOwnPtr<MessagePortArray> MessagePort::entanglePorts(ScriptExecutionContext& context, PassOwnPtr<MessagePortChannelArray> channels) 221 { 222 if (!channels || !channels->size()) 223 return nullptr; 224 225 OwnPtr<MessagePortArray> portArray = adoptPtr(new MessagePortArray(channels->size())); 226 for (unsigned int i = 0; i < channels->size(); ++i) { 227 RefPtr<MessagePort> port = MessagePort::create(context); 228 port->entangle((*channels)[i].release()); 229 (*portArray)[i] = port.release(); 230 } 231 return portArray.release(); 232 } 233 234 EventTargetData* MessagePort::eventTargetData() 235 { 236 return &m_eventTargetData; 237 } 238 239 EventTargetData* MessagePort::ensureEventTargetData() 240 { 241 return &m_eventTargetData; 242 } 243 244 } // namespace WebCore 245