Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: sky/engine/core/dom/MessagePort.cpp

Issue 676723003: Remove postMessage, MessageChannel and MessagePort. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/engine/core/dom/MessagePort.h ('k') | sky/engine/core/dom/MessagePort.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/core/v8/ExceptionState.h"
31 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
32 #include "bindings/core/v8/SerializedScriptValue.h"
33 #include "core/dom/ExceptionCode.h"
34 #include "core/dom/ExecutionContext.h"
35 #include "core/events/MessageEvent.h"
36 #include "core/frame/LocalDOMWindow.h"
37 #include "public/platform/WebString.h"
38 #include "wtf/Functional.h"
39 #include "wtf/text/AtomicString.h"
40
41 namespace blink {
42
43 PassRefPtrWillBeRawPtr<MessagePort> MessagePort::create(ExecutionContext& execut ionContext)
44 {
45 RefPtrWillBeRawPtr<MessagePort> port = adoptRefWillBeNoop(new MessagePort(ex ecutionContext));
46 port->suspendIfNeeded();
47 return port.release();
48 }
49
50 MessagePort::MessagePort(ExecutionContext& executionContext)
51 : ActiveDOMObject(&executionContext)
52 , m_started(false)
53 , m_closed(false)
54 , m_weakFactory(this)
55 {
56 ScriptWrappable::init(this);
57 }
58
59 MessagePort::~MessagePort()
60 {
61 close();
62 }
63
64 void MessagePort::postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValu e> message, const MessagePortArray* ports, ExceptionState& exceptionState)
65 {
66 if (!isEntangled())
67 return;
68 ASSERT(executionContext());
69 ASSERT(m_entangledChannel);
70
71 OwnPtr<MessagePortChannelArray> channels;
72 // Make sure we aren't connected to any of the passed-in ports.
73 if (ports) {
74 for (unsigned i = 0; i < ports->size(); ++i) {
75 MessagePort* dataPort = (*ports)[i].get();
76 if (dataPort == this) {
77 exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " contains the source port.");
78 return;
79 }
80 }
81 channels = MessagePort::disentanglePorts(ports, exceptionState);
82 if (exceptionState.hadException())
83 return;
84 }
85
86 WebString messageString = message->toWireString();
87 OwnPtr<WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArra y(channels.release());
88 m_entangledChannel->postMessage(messageString, webChannels.leakPtr());
89 }
90
91 // static
92 PassOwnPtr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray (PassOwnPtr<MessagePortChannelArray> channels)
93 {
94 OwnPtr<WebMessagePortChannelArray> webChannels;
95 if (channels && channels->size()) {
96 webChannels = adoptPtr(new WebMessagePortChannelArray(channels->size())) ;
97 for (size_t i = 0; i < channels->size(); ++i)
98 (*webChannels)[i] = (*channels)[i].leakPtr();
99 }
100 return webChannels.release();
101 }
102
103 // static
104 PassOwnPtrWillBeRawPtr<MessagePortArray> MessagePort::toMessagePortArray(Executi onContext* context, const WebMessagePortChannelArray& webChannels)
105 {
106 OwnPtrWillBeRawPtr<MessagePortArray> ports = nullptr;
107 if (!webChannels.isEmpty()) {
108 OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChann elArray(webChannels.size()));
109 for (size_t i = 0; i < webChannels.size(); ++i)
110 (*channels)[i] = adoptPtr(webChannels[i]);
111 ports = MessagePort::entanglePorts(*context, channels.release());
112 }
113 return ports.release();
114 }
115
116 PassOwnPtr<WebMessagePortChannel> MessagePort::disentangle()
117 {
118 ASSERT(m_entangledChannel);
119 m_entangledChannel->setClient(0);
120 return m_entangledChannel.release();
121 }
122
123 // Invoked to notify us that there are messages available for this port.
124 // This code may be called from another thread, and so should not call any non-t hreadsafe APIs (i.e. should not call into the entangled channel or access mutabl e variables).
125 void MessagePort::messageAvailable()
126 {
127 callOnMainThread(bind(&MessagePort::dispatchMessages, m_weakFactory.createWe akPtr()));
128 }
129
130 void MessagePort::start()
131 {
132 // Do nothing if we've been cloned or closed.
133 if (!isEntangled())
134 return;
135
136 ASSERT(executionContext());
137 if (m_started)
138 return;
139
140 m_started = true;
141 messageAvailable();
142 }
143
144 void MessagePort::close()
145 {
146 if (isEntangled())
147 m_entangledChannel->setClient(0);
148 m_closed = true;
149 }
150
151 void MessagePort::entangle(PassOwnPtr<WebMessagePortChannel> remote)
152 {
153 // Only invoked to set our initial entanglement.
154 ASSERT(!m_entangledChannel);
155 ASSERT(executionContext());
156
157 m_entangledChannel = remote;
158 m_entangledChannel->setClient(this);
159 }
160
161 const AtomicString& MessagePort::interfaceName() const
162 {
163 return EventTargetNames::MessagePort;
164 }
165
166 static bool tryGetMessageFrom(WebMessagePortChannel& webChannel, RefPtr<Serializ edScriptValue>& message, OwnPtr<MessagePortChannelArray>& channels)
167 {
168 WebString messageString;
169 WebMessagePortChannelArray webChannels;
170 if (!webChannel.tryGetMessage(&messageString, webChannels))
171 return false;
172
173 if (webChannels.size()) {
174 channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
175 for (size_t i = 0; i < webChannels.size(); ++i)
176 (*channels)[i] = adoptPtr(webChannels[i]);
177 }
178 message = SerializedScriptValue::createFromWire(messageString);
179 return true;
180 }
181
182 void MessagePort::dispatchMessages()
183 {
184 // Messages for contexts that are not fully active get dispatched too, but J SAbstractEventListener::handleEvent() doesn't call handlers for these.
185 // The HTML5 spec specifies that any messages sent to a document that is not fully active should be dropped, so this behavior is OK.
186 if (!started())
187 return;
188
189 RefPtr<SerializedScriptValue> message;
190 OwnPtr<MessagePortChannelArray> channels;
191 while (m_entangledChannel && tryGetMessageFrom(*m_entangledChannel, message, channels)) {
192 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts( *executionContext(), channels.release());
193 RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), me ssage.release());
194
195 dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION);
196 }
197 }
198
199 bool MessagePort::hasPendingActivity() const
200 {
201 // The spec says that entangled message ports should always be treated as if they have a strong reference.
202 // We'll also stipulate that the queue needs to be open (if the app drops it s reference to the port before start()-ing it, then it's not really entangled as it's unreachable).
203 return m_started && isEntangled();
204 }
205
206 PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(const MessageP ortArray* ports, ExceptionState& exceptionState)
207 {
208 if (!ports || !ports->size())
209 return nullptr;
210
211 // HashSet used to efficiently check for duplicates in the passed-in array.
212 HashSet<MessagePort*> portSet;
213
214 // 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).
215 for (unsigned i = 0; i < ports->size(); ++i) {
216 MessagePort* port = (*ports)[i].get();
217 if (!port || port->isNeutered() || portSet.contains(port)) {
218 String type;
219 if (!port)
220 type = "null";
221 else if (port->isNeutered())
222 type = "already neutered";
223 else
224 type = "a duplicate";
225 exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " is " + type + ".");
226 return nullptr;
227 }
228 portSet.add(port);
229 }
230
231 // Passed-in ports passed validity checks, so we can disentangle them.
232 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelA rray(ports->size()));
233 for (unsigned i = 0; i < ports->size(); ++i)
234 (*portArray)[i] = (*ports)[i]->disentangle();
235 return portArray.release();
236 }
237
238 PassOwnPtrWillBeRawPtr<MessagePortArray> MessagePort::entanglePorts(ExecutionCon text& context, PassOwnPtr<MessagePortChannelArray> channels)
239 {
240 if (!channels || !channels->size())
241 return nullptr;
242
243 OwnPtrWillBeRawPtr<MessagePortArray> portArray = adoptPtrWillBeNoop(new Mess agePortArray(channels->size()));
244 for (unsigned i = 0; i < channels->size(); ++i) {
245 RefPtrWillBeRawPtr<MessagePort> port = MessagePort::create(context);
246 port->entangle((*channels)[i].release());
247 (*portArray)[i] = port.release();
248 }
249 return portArray.release();
250 }
251
252 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/core/dom/MessagePort.h ('k') | sky/engine/core/dom/MessagePort.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698