| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(con
text, ports, exceptionState); | 82 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(con
text, ports, exceptionState); |
| 83 if (exceptionState.hadException()) | 83 if (exceptionState.hadException()) |
| 84 return; | 84 return; |
| 85 | 85 |
| 86 if (message->containsTransferableArrayBuffer()) | 86 if (message->containsTransferableArrayBuffer()) |
| 87 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")); |
| 88 | 88 |
| 89 WebString messageString = message->toWireString(); | 89 WebString messageString = message->toWireString(); |
| 90 OwnPtr<WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArra
y(channels.release()); | 90 OwnPtr<WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArra
y(std::move(channels)); |
| 91 m_entangledChannel->postMessage(messageString, webChannels.leakPtr()); | 91 m_entangledChannel->postMessage(messageString, webChannels.leakPtr()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // static | 94 // static |
| 95 PassOwnPtr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray
(PassOwnPtr<MessagePortChannelArray> channels) | 95 PassOwnPtr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray
(PassOwnPtr<MessagePortChannelArray> channels) |
| 96 { | 96 { |
| 97 OwnPtr<WebMessagePortChannelArray> webChannels; | 97 OwnPtr<WebMessagePortChannelArray> webChannels; |
| 98 if (channels && channels->size()) { | 98 if (channels && channels->size()) { |
| 99 webChannels = adoptPtr(new WebMessagePortChannelArray(channels->size()))
; | 99 webChannels = adoptPtr(new WebMessagePortChannelArray(channels->size()))
; |
| 100 for (size_t i = 0; i < channels->size(); ++i) | 100 for (size_t i = 0; i < channels->size(); ++i) |
| 101 (*webChannels)[i] = (*channels)[i].leakPtr(); | 101 (*webChannels)[i] = (*channels)[i].leakPtr(); |
| 102 } | 102 } |
| 103 return webChannels.release(); | 103 return webChannels; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // static | 106 // static |
| 107 MessagePortArray* MessagePort::toMessagePortArray(ExecutionContext* context, con
st WebMessagePortChannelArray& webChannels) | 107 MessagePortArray* MessagePort::toMessagePortArray(ExecutionContext* context, con
st WebMessagePortChannelArray& webChannels) |
| 108 { | 108 { |
| 109 OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChannelAr
ray(webChannels.size())); | 109 OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChannelAr
ray(webChannels.size())); |
| 110 for (size_t i = 0; i < webChannels.size(); ++i) | 110 for (size_t i = 0; i < webChannels.size(); ++i) |
| 111 (*channels)[i] = adoptPtr(webChannels[i]); | 111 (*channels)[i] = adoptPtr(webChannels[i]); |
| 112 return MessagePort::entanglePorts(*context, channels.release()); | 112 return MessagePort::entanglePorts(*context, std::move(channels)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 PassOwnPtr<WebMessagePortChannel> MessagePort::disentangle() | 115 PassOwnPtr<WebMessagePortChannel> MessagePort::disentangle() |
| 116 { | 116 { |
| 117 DCHECK(m_entangledChannel); | 117 DCHECK(m_entangledChannel); |
| 118 m_entangledChannel->setClient(0); | 118 m_entangledChannel->setClient(0); |
| 119 return m_entangledChannel.release(); | 119 return std::move(m_entangledChannel); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Invoked to notify us that there are messages available for this port. | 122 // Invoked to notify us that there are messages available for this port. |
| 123 // 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). | 123 // 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). |
| 124 void MessagePort::messageAvailable() | 124 void MessagePort::messageAvailable() |
| 125 { | 125 { |
| 126 DCHECK(getExecutionContext()); | 126 DCHECK(getExecutionContext()); |
| 127 getExecutionContext()->postTask(BLINK_FROM_HERE, createCrossThreadTask(&Mess
agePort::dispatchMessages, CrossThreadWeakPersistentThisPointer<MessagePort>(thi
s))); | 127 getExecutionContext()->postTask(BLINK_FROM_HERE, createCrossThreadTask(&Mess
agePort::dispatchMessages, CrossThreadWeakPersistentThisPointer<MessagePort>(thi
s))); |
| 128 } | 128 } |
| 129 | 129 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (!started()) | 197 if (!started()) |
| 198 return; | 198 return; |
| 199 | 199 |
| 200 RefPtr<SerializedScriptValue> message; | 200 RefPtr<SerializedScriptValue> message; |
| 201 OwnPtr<MessagePortChannelArray> channels; | 201 OwnPtr<MessagePortChannelArray> channels; |
| 202 while (tryGetMessage(message, channels)) { | 202 while (tryGetMessage(message, channels)) { |
| 203 // close() in Worker onmessage handler should prevent next message from
dispatching. | 203 // close() in Worker onmessage handler should prevent next message from
dispatching. |
| 204 if (getExecutionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(
getExecutionContext())->isClosing()) | 204 if (getExecutionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(
getExecutionContext())->isClosing()) |
| 205 return; | 205 return; |
| 206 | 206 |
| 207 MessagePortArray* ports = MessagePort::entanglePorts(*getExecutionContex
t(), channels.release()); | 207 MessagePortArray* ports = MessagePort::entanglePorts(*getExecutionContex
t(), std::move(channels)); |
| 208 Event* evt = MessageEvent::create(ports, message.release()); | 208 Event* evt = MessageEvent::create(ports, message.release()); |
| 209 | 209 |
| 210 dispatchEvent(evt); | 210 dispatchEvent(evt); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 bool MessagePort::hasPendingActivity() const | 214 bool MessagePort::hasPendingActivity() const |
| 215 { | 215 { |
| 216 // 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. |
| 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). | 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). |
| (...skipping 23 matching lines...) Expand all Loading... |
| 241 } | 241 } |
| 242 visited.add(port); | 242 visited.add(port); |
| 243 } | 243 } |
| 244 | 244 |
| 245 UseCounter::count(context, UseCounter::MessagePortsTransferred); | 245 UseCounter::count(context, UseCounter::MessagePortsTransferred); |
| 246 | 246 |
| 247 // Passed-in ports passed validity checks, so we can disentangle them. | 247 // Passed-in ports passed validity checks, so we can disentangle them. |
| 248 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelA
rray(ports.size())); | 248 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelA
rray(ports.size())); |
| 249 for (unsigned i = 0; i < ports.size(); ++i) | 249 for (unsigned i = 0; i < ports.size(); ++i) |
| 250 (*portArray)[i] = ports[i]->disentangle(); | 250 (*portArray)[i] = ports[i]->disentangle(); |
| 251 return portArray.release(); | 251 return portArray; |
| 252 } | 252 } |
| 253 | 253 |
| 254 MessagePortArray* MessagePort::entanglePorts(ExecutionContext& context, PassOwnP
tr<MessagePortChannelArray> channels) | 254 MessagePortArray* MessagePort::entanglePorts(ExecutionContext& context, PassOwnP
tr<MessagePortChannelArray> channels) |
| 255 { | 255 { |
| 256 // https://html.spec.whatwg.org/multipage/comms.html#message-ports | 256 // https://html.spec.whatwg.org/multipage/comms.html#message-ports |
| 257 // |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. |
| 258 if (!channels || !channels->size()) | 258 if (!channels || !channels->size()) |
| 259 return new MessagePortArray; | 259 return new MessagePortArray; |
| 260 | 260 |
| 261 MessagePortArray* portArray = new MessagePortArray(channels->size()); | 261 MessagePortArray* portArray = new MessagePortArray(channels->size()); |
| 262 for (unsigned i = 0; i < channels->size(); ++i) { | 262 for (unsigned i = 0; i < channels->size(); ++i) { |
| 263 MessagePort* port = MessagePort::create(context); | 263 MessagePort* port = MessagePort::create(context); |
| 264 port->entangle((*channels)[i].release()); | 264 port->entangle(std::move((*channels)[i])); |
| 265 (*portArray)[i] = port; | 265 (*portArray)[i] = port; |
| 266 } | 266 } |
| 267 return portArray; | 267 return portArray; |
| 268 } | 268 } |
| 269 | 269 |
| 270 DEFINE_TRACE(MessagePort) | 270 DEFINE_TRACE(MessagePort) |
| 271 { | 271 { |
| 272 ActiveDOMObject::trace(visitor); | 272 ActiveDOMObject::trace(visitor); |
| 273 EventTargetWithInlineData::trace(visitor); | 273 EventTargetWithInlineData::trace(visitor); |
| 274 } | 274 } |
| 275 | 275 |
| 276 } // namespace blink | 276 } // namespace blink |
| OLD | NEW |