• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MessagePort.h"
29 
30 #include "AtomicString.h"
31 #include "DOMWindow.h"
32 #include "Document.h"
33 #include "EventException.h"
34 #include "EventNames.h"
35 #include "MessageEvent.h"
36 #include "SecurityOrigin.h"
37 #include "Timer.h"
38 
39 namespace WebCore {
40 
MessagePort(ScriptExecutionContext & scriptExecutionContext)41 MessagePort::MessagePort(ScriptExecutionContext& scriptExecutionContext)
42     : m_entangledChannel(0)
43     , m_started(false)
44     , m_closed(false)
45     , m_scriptExecutionContext(&scriptExecutionContext)
46 {
47     m_scriptExecutionContext->createdMessagePort(this);
48 
49     // Don't need to call processMessagePortMessagesSoon() here, because the port will not be opened until start() is invoked.
50 }
51 
~MessagePort()52 MessagePort::~MessagePort()
53 {
54     close();
55     if (m_scriptExecutionContext)
56         m_scriptExecutionContext->destroyedMessagePort(this);
57 }
58 
59 // FIXME: remove this when we update the ObjC bindings (bug #28774).
postMessage(PassRefPtr<SerializedScriptValue> message,MessagePort * port,ExceptionCode & ec)60 void MessagePort::postMessage(PassRefPtr<SerializedScriptValue> message, MessagePort* port, ExceptionCode& ec)
61 {
62     MessagePortArray ports;
63     if (port)
64         ports.append(port);
65     postMessage(message, &ports, ec);
66 }
67 
postMessage(PassRefPtr<SerializedScriptValue> message,ExceptionCode & ec)68 void MessagePort::postMessage(PassRefPtr<SerializedScriptValue> message, ExceptionCode& ec)
69 {
70     postMessage(message, static_cast<MessagePortArray*>(0), ec);
71 }
72 
postMessage(PassRefPtr<SerializedScriptValue> message,const MessagePortArray * ports,ExceptionCode & ec)73 void MessagePort::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionCode& ec)
74 {
75     if (!m_entangledChannel)
76         return;
77     ASSERT(m_scriptExecutionContext);
78 
79     OwnPtr<MessagePortChannelArray> channels;
80     // Make sure we aren't connected to any of the passed-in ports.
81     if (ports) {
82         for (unsigned int i = 0; i < ports->size(); ++i) {
83             MessagePort* dataPort = (*ports)[i].get();
84             if (dataPort == this || m_entangledChannel->isConnectedTo(dataPort)) {
85                 ec = INVALID_STATE_ERR;
86                 return;
87             }
88         }
89         channels = MessagePort::disentanglePorts(ports, ec);
90         if (ec)
91             return;
92     }
93     m_entangledChannel->postMessageToRemote(MessagePortChannel::EventData::create(message, channels.release()));
94 }
95 
disentangle(ExceptionCode & ec)96 PassOwnPtr<MessagePortChannel> MessagePort::disentangle(ExceptionCode& ec)
97 {
98     if (!m_entangledChannel)
99         ec = INVALID_STATE_ERR;
100     else {
101         m_entangledChannel->disentangle();
102 
103         // We can't receive any messages or generate any events, so remove ourselves from the list of active ports.
104         ASSERT(m_scriptExecutionContext);
105         m_scriptExecutionContext->destroyedMessagePort(this);
106         m_scriptExecutionContext = 0;
107     }
108     return m_entangledChannel.release();
109 }
110 
111 // Invoked to notify us that there are messages available for this port.
112 // 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).
messageAvailable()113 void MessagePort::messageAvailable()
114 {
115     ASSERT(m_scriptExecutionContext);
116     m_scriptExecutionContext->processMessagePortMessagesSoon();
117 }
118 
start()119 void MessagePort::start()
120 {
121     // Do nothing if we've been cloned
122     if (!m_entangledChannel)
123         return;
124 
125     ASSERT(m_scriptExecutionContext);
126     if (m_started)
127         return;
128 
129     m_started = true;
130     m_scriptExecutionContext->processMessagePortMessagesSoon();
131 }
132 
close()133 void MessagePort::close()
134 {
135     m_closed = true;
136     if (!m_entangledChannel)
137         return;
138     m_entangledChannel->close();
139 }
140 
entangle(PassOwnPtr<MessagePortChannel> remote)141 void MessagePort::entangle(PassOwnPtr<MessagePortChannel> remote)
142 {
143     // Only invoked to set our initial entanglement.
144     ASSERT(!m_entangledChannel);
145     ASSERT(m_scriptExecutionContext);
146 
147     // Don't entangle the ports if the channel is closed.
148     if (remote->entangleIfOpen(this))
149         m_entangledChannel = remote;
150 }
151 
contextDestroyed()152 void MessagePort::contextDestroyed()
153 {
154     ASSERT(m_scriptExecutionContext);
155     // Must close port before blowing away the cached context, to ensure that we get no more calls to messageAvailable().
156     close();
157     m_scriptExecutionContext = 0;
158 }
159 
scriptExecutionContext() const160 ScriptExecutionContext* MessagePort::scriptExecutionContext() const
161 {
162     return m_scriptExecutionContext;
163 }
164 
dispatchMessages()165 void MessagePort::dispatchMessages()
166 {
167     // Messages for contexts that are not fully active get dispatched too, but JSAbstractEventListener::handleEvent() doesn't call handlers for these.
168     // The HTML5 spec specifies that any messages sent to a document that is not fully active should be dropped, so this behavior is OK.
169     ASSERT(started());
170 
171     OwnPtr<MessagePortChannel::EventData> eventData;
172     while (m_entangledChannel && m_entangledChannel->tryGetMessageFromRemote(eventData)) {
173         OwnPtr<MessagePortArray> ports = MessagePort::entanglePorts(*m_scriptExecutionContext, eventData->channels());
174         RefPtr<Event> evt = MessageEvent::create(ports.release(), eventData->message());
175 
176         ExceptionCode ec = 0;
177         dispatchEvent(evt.release(), ec);
178         ASSERT(!ec);
179     }
180 }
181 
hasPendingActivity()182 bool MessagePort::hasPendingActivity()
183 {
184     // The spec says that entangled message ports should always be treated as if they have a strong reference.
185     // 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).
186     return m_started && m_entangledChannel && m_entangledChannel->hasPendingActivity();
187 }
188 
locallyEntangledPort()189 MessagePort* MessagePort::locallyEntangledPort()
190 {
191     return m_entangledChannel ? m_entangledChannel->locallyEntangledPort(m_scriptExecutionContext) : 0;
192 }
193 
disentanglePorts(const MessagePortArray * ports,ExceptionCode & ec)194 PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(const MessagePortArray* ports, ExceptionCode& ec)
195 {
196     if (!ports || !ports->size())
197         return 0;
198 
199     // HashSet used to efficiently check for duplicates in the passed-in array.
200     HashSet<MessagePort*> portSet;
201 
202     // 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).
203     for (unsigned int i = 0; i < ports->size(); ++i) {
204         MessagePort* port = (*ports)[i].get();
205         if (!port || port->isCloned() || portSet.contains(port)) {
206             ec = INVALID_STATE_ERR;
207             return 0;
208         }
209         portSet.add(port);
210     }
211 
212     // Passed-in ports passed validity checks, so we can disentangle them.
213     MessagePortChannelArray* portArray = new MessagePortChannelArray(ports->size());
214     for (unsigned int i = 0 ; i < ports->size() ; ++i) {
215         OwnPtr<MessagePortChannel> channel = (*ports)[i]->disentangle(ec);
216         ASSERT(!ec); // Can't generate exception here if passed above checks.
217         (*portArray)[i] = channel.release();
218     }
219     return portArray;
220 }
221 
entanglePorts(ScriptExecutionContext & context,PassOwnPtr<MessagePortChannelArray> channels)222 PassOwnPtr<MessagePortArray> MessagePort::entanglePorts(ScriptExecutionContext& context, PassOwnPtr<MessagePortChannelArray> channels)
223 {
224     if (!channels || !channels->size())
225         return 0;
226 
227     MessagePortArray* portArray = new MessagePortArray(channels->size());
228     for (unsigned int i = 0; i < channels->size(); ++i) {
229         RefPtr<MessagePort> port = MessagePort::create(context);
230         port->entangle((*channels)[i].release());
231         (*portArray)[i] = port.release();
232     }
233     return portArray;
234 }
235 
eventTargetData()236 EventTargetData* MessagePort::eventTargetData()
237 {
238     return &m_eventTargetData;
239 }
240 
ensureEventTargetData()241 EventTargetData* MessagePort::ensureEventTargetData()
242 {
243     return &m_eventTargetData;
244 }
245 
246 } // namespace WebCore
247