| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 32 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
| 33 #include "core/dom/CrossThreadTask.h" | 33 #include "core/dom/CrossThreadTask.h" |
| 34 #include "core/dom/ExceptionCode.h" | 34 #include "core/dom/ExceptionCode.h" |
| 35 #include "core/dom/ExecutionContext.h" | 35 #include "core/dom/ExecutionContext.h" |
| 36 #include "core/events/MessageEvent.h" | 36 #include "core/events/MessageEvent.h" |
| 37 #include "core/frame/LocalDOMWindow.h" | 37 #include "core/frame/LocalDOMWindow.h" |
| 38 #include "core/inspector/ConsoleMessage.h" | 38 #include "core/inspector/ConsoleMessage.h" |
| 39 #include "core/workers/WorkerGlobalScope.h" | 39 #include "core/workers/WorkerGlobalScope.h" |
| 40 #include "public/platform/WebString.h" | 40 #include "public/platform/WebString.h" |
| 41 #include "wtf/Functional.h" | 41 #include "wtf/Functional.h" |
| 42 #include "wtf/PtrUtil.h" | |
| 43 #include "wtf/text/AtomicString.h" | 42 #include "wtf/text/AtomicString.h" |
| 44 #include <memory> | |
| 45 | 43 |
| 46 namespace blink { | 44 namespace blink { |
| 47 | 45 |
| 48 MessagePort* MessagePort::create(ExecutionContext& executionContext) | 46 MessagePort* MessagePort::create(ExecutionContext& executionContext) |
| 49 { | 47 { |
| 50 MessagePort* port = new MessagePort(executionContext); | 48 MessagePort* port = new MessagePort(executionContext); |
| 51 port->suspendIfNeeded(); | 49 port->suspendIfNeeded(); |
| 52 return port; | 50 return port; |
| 53 } | 51 } |
| 54 | 52 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 DCHECK(getExecutionContext()); | 72 DCHECK(getExecutionContext()); |
| 75 DCHECK(m_entangledChannel); | 73 DCHECK(m_entangledChannel); |
| 76 | 74 |
| 77 // Make sure we aren't connected to any of the passed-in ports. | 75 // Make sure we aren't connected to any of the passed-in ports. |
| 78 for (unsigned i = 0; i < ports.size(); ++i) { | 76 for (unsigned i = 0; i < ports.size(); ++i) { |
| 79 if (ports[i] == this) { | 77 if (ports[i] == this) { |
| 80 exceptionState.throwDOMException(DataCloneError, "Port at index " +
String::number(i) + " contains the source port."); | 78 exceptionState.throwDOMException(DataCloneError, "Port at index " +
String::number(i) + " contains the source port."); |
| 81 return; | 79 return; |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 std::unique_ptr<MessagePortChannelArray> channels = MessagePort::disentangle
Ports(context, ports, exceptionState); | 82 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(con
text, ports, exceptionState); |
| 85 if (exceptionState.hadException()) | 83 if (exceptionState.hadException()) |
| 86 return; | 84 return; |
| 87 | 85 |
| 88 if (message->containsTransferableArrayBuffer()) | 86 if (message->containsTransferableArrayBuffer()) |
| 89 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessag
eSource, WarningMessageLevel, "MessagePort cannot send an ArrayBuffer as a trans
ferable object yet. See http://crbug.com/334408")); | 87 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessag
eSource, WarningMessageLevel, "MessagePort cannot send an ArrayBuffer as a trans
ferable object yet. See http://crbug.com/334408")); |
| 90 | 88 |
| 91 WebString messageString = message->toWireString(); | 89 WebString messageString = message->toWireString(); |
| 92 std::unique_ptr<WebMessagePortChannelArray> webChannels = toWebMessagePortCh
annelArray(std::move(channels)); | 90 OwnPtr<WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArra
y(std::move(channels)); |
| 93 m_entangledChannel->postMessage(messageString, webChannels.release()); | 91 m_entangledChannel->postMessage(messageString, webChannels.leakPtr()); |
| 94 } | 92 } |
| 95 | 93 |
| 96 // static | 94 // static |
| 97 std::unique_ptr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannel
Array(std::unique_ptr<MessagePortChannelArray> channels) | 95 PassOwnPtr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray
(PassOwnPtr<MessagePortChannelArray> channels) |
| 98 { | 96 { |
| 99 std::unique_ptr<WebMessagePortChannelArray> webChannels; | 97 OwnPtr<WebMessagePortChannelArray> webChannels; |
| 100 if (channels && channels->size()) { | 98 if (channels && channels->size()) { |
| 101 webChannels = wrapUnique(new WebMessagePortChannelArray(channels->size()
)); | 99 webChannels = adoptPtr(new WebMessagePortChannelArray(channels->size()))
; |
| 102 for (size_t i = 0; i < channels->size(); ++i) | 100 for (size_t i = 0; i < channels->size(); ++i) |
| 103 (*webChannels)[i] = (*channels)[i].release(); | 101 (*webChannels)[i] = (*channels)[i].release(); |
| 104 } | 102 } |
| 105 return webChannels; | 103 return webChannels; |
| 106 } | 104 } |
| 107 | 105 |
| 108 // static | 106 // static |
| 109 MessagePortArray* MessagePort::toMessagePortArray(ExecutionContext* context, con
st WebMessagePortChannelArray& webChannels) | 107 MessagePortArray* MessagePort::toMessagePortArray(ExecutionContext* context, con
st WebMessagePortChannelArray& webChannels) |
| 110 { | 108 { |
| 111 std::unique_ptr<MessagePortChannelArray> channels = wrapUnique(new MessagePo
rtChannelArray(webChannels.size())); | 109 OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChannelAr
ray(webChannels.size())); |
| 112 for (size_t i = 0; i < webChannels.size(); ++i) | 110 for (size_t i = 0; i < webChannels.size(); ++i) |
| 113 (*channels)[i] = WebMessagePortChannelUniquePtr(webChannels[i]); | 111 (*channels)[i] = WebMessagePortChannelUniquePtr(webChannels[i]); |
| 114 return MessagePort::entanglePorts(*context, std::move(channels)); | 112 return MessagePort::entanglePorts(*context, std::move(channels)); |
| 115 } | 113 } |
| 116 | 114 |
| 117 WebMessagePortChannelUniquePtr MessagePort::disentangle() | 115 WebMessagePortChannelUniquePtr MessagePort::disentangle() |
| 118 { | 116 { |
| 119 DCHECK(m_entangledChannel); | 117 DCHECK(m_entangledChannel); |
| 120 m_entangledChannel->setClient(0); | 118 m_entangledChannel->setClient(0); |
| 121 return std::move(m_entangledChannel); | 119 return std::move(m_entangledChannel); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 156 |
| 159 m_entangledChannel = std::move(remote); | 157 m_entangledChannel = std::move(remote); |
| 160 m_entangledChannel->setClient(this); | 158 m_entangledChannel->setClient(this); |
| 161 } | 159 } |
| 162 | 160 |
| 163 const AtomicString& MessagePort::interfaceName() const | 161 const AtomicString& MessagePort::interfaceName() const |
| 164 { | 162 { |
| 165 return EventTargetNames::MessagePort; | 163 return EventTargetNames::MessagePort; |
| 166 } | 164 } |
| 167 | 165 |
| 168 static bool tryGetMessageFrom(WebMessagePortChannel& webChannel, RefPtr<Serializ
edScriptValue>& message, std::unique_ptr<MessagePortChannelArray>& channels) | 166 static bool tryGetMessageFrom(WebMessagePortChannel& webChannel, RefPtr<Serializ
edScriptValue>& message, OwnPtr<MessagePortChannelArray>& channels) |
| 169 { | 167 { |
| 170 WebString messageString; | 168 WebString messageString; |
| 171 WebMessagePortChannelArray webChannels; | 169 WebMessagePortChannelArray webChannels; |
| 172 if (!webChannel.tryGetMessage(&messageString, webChannels)) | 170 if (!webChannel.tryGetMessage(&messageString, webChannels)) |
| 173 return false; | 171 return false; |
| 174 | 172 |
| 175 if (webChannels.size()) { | 173 if (webChannels.size()) { |
| 176 channels = wrapUnique(new MessagePortChannelArray(webChannels.size())); | 174 channels = adoptPtr(new MessagePortChannelArray(webChannels.size())); |
| 177 for (size_t i = 0; i < webChannels.size(); ++i) | 175 for (size_t i = 0; i < webChannels.size(); ++i) |
| 178 (*channels)[i] = WebMessagePortChannelUniquePtr(webChannels[i]); | 176 (*channels)[i] = WebMessagePortChannelUniquePtr(webChannels[i]); |
| 179 } | 177 } |
| 180 message = SerializedScriptValue::create(messageString); | 178 message = SerializedScriptValue::create(messageString); |
| 181 return true; | 179 return true; |
| 182 } | 180 } |
| 183 | 181 |
| 184 bool MessagePort::tryGetMessage(RefPtr<SerializedScriptValue>& message, std::uni
que_ptr<MessagePortChannelArray>& channels) | 182 bool MessagePort::tryGetMessage(RefPtr<SerializedScriptValue>& message, OwnPtr<M
essagePortChannelArray>& channels) |
| 185 { | 183 { |
| 186 if (!m_entangledChannel) | 184 if (!m_entangledChannel) |
| 187 return false; | 185 return false; |
| 188 return tryGetMessageFrom(*m_entangledChannel, message, channels); | 186 return tryGetMessageFrom(*m_entangledChannel, message, channels); |
| 189 } | 187 } |
| 190 | 188 |
| 191 void MessagePort::dispatchMessages() | 189 void MessagePort::dispatchMessages() |
| 192 { | 190 { |
| 193 // Because close() doesn't cancel any in flight calls to dispatchMessages()
we need to check if the port is still open before dispatch. | 191 // Because close() doesn't cancel any in flight calls to dispatchMessages()
we need to check if the port is still open before dispatch. |
| 194 if (m_closed) | 192 if (m_closed) |
| 195 return; | 193 return; |
| 196 | 194 |
| 197 // Messages for contexts that are not fully active get dispatched too, but J
SAbstractEventListener::handleEvent() doesn't call handlers for these. | 195 // Messages for contexts that are not fully active get dispatched too, but J
SAbstractEventListener::handleEvent() doesn't call handlers for these. |
| 198 // The HTML5 spec specifies that any messages sent to a document that is not
fully active should be dropped, so this behavior is OK. | 196 // The HTML5 spec specifies that any messages sent to a document that is not
fully active should be dropped, so this behavior is OK. |
| 199 if (!started()) | 197 if (!started()) |
| 200 return; | 198 return; |
| 201 | 199 |
| 202 RefPtr<SerializedScriptValue> message; | 200 RefPtr<SerializedScriptValue> message; |
| 203 std::unique_ptr<MessagePortChannelArray> channels; | 201 OwnPtr<MessagePortChannelArray> channels; |
| 204 while (tryGetMessage(message, channels)) { | 202 while (tryGetMessage(message, channels)) { |
| 205 // close() in Worker onmessage handler should prevent next message from
dispatching. | 203 // close() in Worker onmessage handler should prevent next message from
dispatching. |
| 206 if (getExecutionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(
getExecutionContext())->isClosing()) | 204 if (getExecutionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(
getExecutionContext())->isClosing()) |
| 207 return; | 205 return; |
| 208 | 206 |
| 209 MessagePortArray* ports = MessagePort::entanglePorts(*getExecutionContex
t(), std::move(channels)); | 207 MessagePortArray* ports = MessagePort::entanglePorts(*getExecutionContex
t(), std::move(channels)); |
| 210 Event* evt = MessageEvent::create(ports, message.release()); | 208 Event* evt = MessageEvent::create(ports, message.release()); |
| 211 | 209 |
| 212 dispatchEvent(evt); | 210 dispatchEvent(evt); |
| 213 } | 211 } |
| 214 } | 212 } |
| 215 | 213 |
| 216 bool MessagePort::hasPendingActivity() const | 214 bool MessagePort::hasPendingActivity() const |
| 217 { | 215 { |
| 218 // The spec says that entangled message ports should always be treated as if
they have a strong reference. | 216 // The spec says that entangled message ports should always be treated as if
they have a strong reference. |
| 219 // 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). | 217 // 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). |
| 220 return m_started && isEntangled(); | 218 return m_started && isEntangled(); |
| 221 } | 219 } |
| 222 | 220 |
| 223 std::unique_ptr<MessagePortChannelArray> MessagePort::disentanglePorts(Execution
Context* context, const MessagePortArray& ports, ExceptionState& exceptionState) | 221 PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(ExecutionConte
xt* context, const MessagePortArray& ports, ExceptionState& exceptionState) |
| 224 { | 222 { |
| 225 if (!ports.size()) | 223 if (!ports.size()) |
| 226 return nullptr; | 224 return nullptr; |
| 227 | 225 |
| 228 HeapHashSet<Member<MessagePort>> visited; | 226 HeapHashSet<Member<MessagePort>> visited; |
| 229 | 227 |
| 230 // 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). | 228 // 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). |
| 231 for (unsigned i = 0; i < ports.size(); ++i) { | 229 for (unsigned i = 0; i < ports.size(); ++i) { |
| 232 MessagePort* port = ports[i]; | 230 MessagePort* port = ports[i]; |
| 233 if (!port || port->isNeutered() || visited.contains(port)) { | 231 if (!port || port->isNeutered() || visited.contains(port)) { |
| 234 String type; | 232 String type; |
| 235 if (!port) | 233 if (!port) |
| 236 type = "null"; | 234 type = "null"; |
| 237 else if (port->isNeutered()) | 235 else if (port->isNeutered()) |
| 238 type = "already neutered"; | 236 type = "already neutered"; |
| 239 else | 237 else |
| 240 type = "a duplicate"; | 238 type = "a duplicate"; |
| 241 exceptionState.throwDOMException(DataCloneError, "Port at index " +
String::number(i) + " is " + type + "."); | 239 exceptionState.throwDOMException(DataCloneError, "Port at index " +
String::number(i) + " is " + type + "."); |
| 242 return nullptr; | 240 return nullptr; |
| 243 } | 241 } |
| 244 visited.add(port); | 242 visited.add(port); |
| 245 } | 243 } |
| 246 | 244 |
| 247 UseCounter::count(context, UseCounter::MessagePortsTransferred); | 245 UseCounter::count(context, UseCounter::MessagePortsTransferred); |
| 248 | 246 |
| 249 // Passed-in ports passed validity checks, so we can disentangle them. | 247 // Passed-in ports passed validity checks, so we can disentangle them. |
| 250 std::unique_ptr<MessagePortChannelArray> portArray = wrapUnique(new MessageP
ortChannelArray(ports.size())); | 248 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelA
rray(ports.size())); |
| 251 for (unsigned i = 0; i < ports.size(); ++i) | 249 for (unsigned i = 0; i < ports.size(); ++i) |
| 252 (*portArray)[i] = ports[i]->disentangle(); | 250 (*portArray)[i] = ports[i]->disentangle(); |
| 253 return portArray; | 251 return portArray; |
| 254 } | 252 } |
| 255 | 253 |
| 256 MessagePortArray* MessagePort::entanglePorts(ExecutionContext& context, std::uni
que_ptr<MessagePortChannelArray> channels) | 254 MessagePortArray* MessagePort::entanglePorts(ExecutionContext& context, PassOwnP
tr<MessagePortChannelArray> channels) |
| 257 { | 255 { |
| 258 // https://html.spec.whatwg.org/multipage/comms.html#message-ports | 256 // https://html.spec.whatwg.org/multipage/comms.html#message-ports |
| 259 // |ports| should be an empty array, not null even when there is no ports. | 257 // |ports| should be an empty array, not null even when there is no ports. |
| 260 if (!channels || !channels->size()) | 258 if (!channels || !channels->size()) |
| 261 return new MessagePortArray; | 259 return new MessagePortArray; |
| 262 | 260 |
| 263 MessagePortArray* portArray = new MessagePortArray(channels->size()); | 261 MessagePortArray* portArray = new MessagePortArray(channels->size()); |
| 264 for (unsigned i = 0; i < channels->size(); ++i) { | 262 for (unsigned i = 0; i < channels->size(); ++i) { |
| 265 MessagePort* port = MessagePort::create(context); | 263 MessagePort* port = MessagePort::create(context); |
| 266 port->entangle(std::move((*channels)[i])); | 264 port->entangle(std::move((*channels)[i])); |
| 267 (*portArray)[i] = port; | 265 (*portArray)[i] = port; |
| 268 } | 266 } |
| 269 return portArray; | 267 return portArray; |
| 270 } | 268 } |
| 271 | 269 |
| 272 DEFINE_TRACE(MessagePort) | 270 DEFINE_TRACE(MessagePort) |
| 273 { | 271 { |
| 274 ActiveDOMObject::trace(visitor); | 272 ActiveDOMObject::trace(visitor); |
| 275 EventTargetWithInlineData::trace(visitor); | 273 EventTargetWithInlineData::trace(visitor); |
| 276 } | 274 } |
| 277 | 275 |
| 278 } // namespace blink | 276 } // namespace blink |
| OLD | NEW |