Chromium Code Reviews| 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/SerializedScriptValue.h" | 32 #include "bindings/core/v8/SerializedScriptValue.h" |
| 33 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 33 #include "bindings/core/v8/SerializedScriptValueFactory.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/dom/ExecutionContextTask.h" | 36 #include "core/dom/ExecutionContextTask.h" |
| 37 #include "core/dom/TaskRunnerHelper.h" | 37 #include "core/dom/TaskRunnerHelper.h" |
| 38 #include "core/events/MessageEvent.h" | 38 #include "core/events/MessageEvent.h" |
| 39 #include "core/frame/LocalDOMWindow.h" | 39 #include "core/frame/LocalDOMWindow.h" |
| 40 #include "core/workers/WorkerGlobalScope.h" | 40 #include "core/workers/WorkerGlobalScope.h" |
| 41 #include "public/platform/WebString.h" | 41 #include "public/platform/WebString.h" |
| 42 #include "wtf/Functional.h" | 42 #include "wtf/Atomics.h" |
| 43 #include "wtf/PtrUtil.h" | 43 #include "wtf/PtrUtil.h" |
| 44 #include "wtf/text/AtomicString.h" | 44 #include "wtf/text/AtomicString.h" |
| 45 | 45 |
| 46 namespace blink { | 46 namespace blink { |
| 47 | 47 |
| 48 MessagePort* MessagePort::create(ExecutionContext& executionContext) { | 48 MessagePort* MessagePort::create(ExecutionContext& executionContext) { |
| 49 return new MessagePort(executionContext); | 49 return new MessagePort(executionContext); |
| 50 } | 50 } |
| 51 | 51 |
| 52 MessagePort::MessagePort(ExecutionContext& executionContext) | 52 MessagePort::MessagePort(ExecutionContext& executionContext) |
| 53 : ContextLifecycleObserver(&executionContext), | 53 : ContextLifecycleObserver(&executionContext), |
| 54 m_pendingDispatchTask(0), | |
| 54 m_started(false), | 55 m_started(false), |
| 55 m_closed(false) {} | 56 m_closed(false) {} |
| 56 | 57 |
| 57 MessagePort::~MessagePort() { | 58 MessagePort::~MessagePort() { |
| 58 DCHECK(!m_started || !isEntangled()); | 59 DCHECK(!m_started || !isEntangled()); |
| 59 } | 60 } |
| 60 | 61 |
| 61 void MessagePort::postMessage(ScriptState* scriptState, | 62 void MessagePort::postMessage(ScriptState* scriptState, |
| 62 PassRefPtr<SerializedScriptValue> message, | 63 PassRefPtr<SerializedScriptValue> message, |
| 63 const MessagePortArray& ports, | 64 const MessagePortArray& ports, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 DCHECK(m_entangledChannel); | 112 DCHECK(m_entangledChannel); |
| 112 m_entangledChannel->setClient(nullptr); | 113 m_entangledChannel->setClient(nullptr); |
| 113 return std::move(m_entangledChannel); | 114 return std::move(m_entangledChannel); |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Invoked to notify us that there are messages available for this port. | 117 // Invoked to notify us that there are messages available for this port. |
| 117 // This code may be called from another thread, and so should not call any | 118 // This code may be called from another thread, and so should not call any |
| 118 // non-threadsafe APIs (i.e. should not call into the entangled channel or | 119 // non-threadsafe APIs (i.e. should not call into the entangled channel or |
| 119 // access mutable variables). | 120 // access mutable variables). |
| 120 void MessagePort::messageAvailable() { | 121 void MessagePort::messageAvailable() { |
| 122 // Don't post another task if there's an identical one pending. | |
| 123 if (atomicTestAndSetToOne(&m_pendingDispatchTask)) | |
|
dcheng
2017/02/27 08:06:37
It really feels like we should have a better way o
sof
2017/02/27 08:21:37
"this" == cross-thread prevention of task re-posts
| |
| 124 return; | |
| 125 | |
| 121 DCHECK(getExecutionContext()); | 126 DCHECK(getExecutionContext()); |
| 122 // TODO(tzik): Use ParentThreadTaskRunners instead of ExecutionContext here to | 127 // TODO(tzik): Use ParentThreadTaskRunners instead of ExecutionContext here to |
| 123 // avoid touching foreign thread GCed object. | 128 // avoid touching foreign thread GCed object. |
| 124 getExecutionContext()->postTask( | 129 getExecutionContext()->postTask( |
| 125 TaskType::PostedMessage, BLINK_FROM_HERE, | 130 TaskType::PostedMessage, BLINK_FROM_HERE, |
| 126 createCrossThreadTask(&MessagePort::dispatchMessages, | 131 createCrossThreadTask(&MessagePort::dispatchMessages, |
| 127 wrapCrossThreadWeakPersistent(this))); | 132 wrapCrossThreadWeakPersistent(this))); |
| 128 } | 133 } |
| 129 | 134 |
| 130 void MessagePort::start() { | 135 void MessagePort::start() { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 } | 182 } |
| 178 | 183 |
| 179 bool MessagePort::tryGetMessage(RefPtr<SerializedScriptValue>& message, | 184 bool MessagePort::tryGetMessage(RefPtr<SerializedScriptValue>& message, |
| 180 MessagePortChannelArray& channels) { | 185 MessagePortChannelArray& channels) { |
| 181 if (!m_entangledChannel) | 186 if (!m_entangledChannel) |
| 182 return false; | 187 return false; |
| 183 return tryGetMessageFrom(*m_entangledChannel, message, channels); | 188 return tryGetMessageFrom(*m_entangledChannel, message, channels); |
| 184 } | 189 } |
| 185 | 190 |
| 186 void MessagePort::dispatchMessages() { | 191 void MessagePort::dispatchMessages() { |
| 192 // Signal to |messageAvailable()| that there are no ongoing | |
| 193 // dispatches of messages. This can cause redundantly posted | |
| 194 // tasks, but safely avoids messages languishing. | |
| 195 releaseStore(&m_pendingDispatchTask, 0); | |
| 196 | |
| 187 // Messages for contexts that are not fully active get dispatched too, but | 197 // Messages for contexts that are not fully active get dispatched too, but |
| 188 // JSAbstractEventListener::handleEvent() doesn't call handlers for these. | 198 // JSAbstractEventListener::handleEvent() doesn't call handlers for these. |
| 189 // The HTML5 spec specifies that any messages sent to a document that is not | 199 // The HTML5 spec specifies that any messages sent to a document that is not |
| 190 // fully active should be dropped, so this behavior is OK. | 200 // fully active should be dropped, so this behavior is OK. |
| 191 if (!started()) | 201 if (!started()) |
| 192 return; | 202 return; |
| 193 | 203 |
| 194 while (true) { | 204 while (true) { |
| 195 // Because close() doesn't cancel any in flight calls to dispatchMessages(), | 205 // Because close() doesn't cancel any in flight calls to dispatchMessages(), |
| 196 // and can be triggered by the onmessage event handler, we need to check if | 206 // and can be triggered by the onmessage event handler, we need to check if |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 } | 287 } |
| 278 return portArray; | 288 return portArray; |
| 279 } | 289 } |
| 280 | 290 |
| 281 DEFINE_TRACE(MessagePort) { | 291 DEFINE_TRACE(MessagePort) { |
| 282 ContextLifecycleObserver::trace(visitor); | 292 ContextLifecycleObserver::trace(visitor); |
| 283 EventTargetWithInlineData::trace(visitor); | 293 EventTargetWithInlineData::trace(visitor); |
| 284 } | 294 } |
| 285 | 295 |
| 286 } // namespace blink | 296 } // namespace blink |
| OLD | NEW |