| 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 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 #include "core/dom/MessagePort.h" | 27 #include "core/dom/MessagePort.h" |
| 28 | 28 |
| 29 #include <memory> | 29 #include <memory> |
| 30 #include "bindings/core/v8/ExceptionState.h" | 30 #include "bindings/core/v8/ExceptionState.h" |
| 31 #include "bindings/core/v8/ScriptState.h" | 31 #include "bindings/core/v8/ScriptState.h" |
| 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" | |
| 37 #include "core/dom/TaskRunnerHelper.h" | 36 #include "core/dom/TaskRunnerHelper.h" |
| 38 #include "core/events/MessageEvent.h" | 37 #include "core/events/MessageEvent.h" |
| 39 #include "core/frame/LocalDOMWindow.h" | 38 #include "core/frame/LocalDOMWindow.h" |
| 40 #include "core/workers/WorkerGlobalScope.h" | 39 #include "core/workers/WorkerGlobalScope.h" |
| 40 #include "platform/CrossThreadFunctional.h" |
| 41 #include "platform/wtf/Atomics.h" | 41 #include "platform/wtf/Atomics.h" |
| 42 #include "platform/wtf/PtrUtil.h" | 42 #include "platform/wtf/PtrUtil.h" |
| 43 #include "platform/wtf/text/AtomicString.h" | 43 #include "platform/wtf/text/AtomicString.h" |
| 44 #include "public/platform/WebString.h" | 44 #include "public/platform/WebString.h" |
| 45 | 45 |
| 46 namespace blink { | 46 namespace blink { |
| 47 | 47 |
| 48 MessagePort* MessagePort::Create(ExecutionContext& execution_context) { | 48 MessagePort* MessagePort::Create(ExecutionContext& execution_context) { |
| 49 return new MessagePort(execution_context); | 49 return new MessagePort(execution_context); |
| 50 } | 50 } |
| 51 | 51 |
| 52 MessagePort::MessagePort(ExecutionContext& execution_context) | 52 MessagePort::MessagePort(ExecutionContext& execution_context) |
| 53 : ContextLifecycleObserver(&execution_context), | 53 : ContextLifecycleObserver(&execution_context), |
| 54 pending_dispatch_task_(0), | 54 task_runner_(TaskRunnerHelper::Get(TaskType::kPostedMessage, |
| 55 started_(false), | 55 &execution_context)) {} |
| 56 closed_(false) {} | |
| 57 | 56 |
| 58 MessagePort::~MessagePort() { | 57 MessagePort::~MessagePort() { |
| 59 DCHECK(!started_ || !IsEntangled()); | 58 DCHECK(!started_ || !IsEntangled()); |
| 60 } | 59 } |
| 61 | 60 |
| 62 void MessagePort::postMessage(ScriptState* script_state, | 61 void MessagePort::postMessage(ScriptState* script_state, |
| 63 PassRefPtr<SerializedScriptValue> message, | 62 PassRefPtr<SerializedScriptValue> message, |
| 64 const MessagePortArray& ports, | 63 const MessagePortArray& ports, |
| 65 ExceptionState& exception_state) { | 64 ExceptionState& exception_state) { |
| 66 if (!IsEntangled()) | 65 if (!IsEntangled()) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 114 |
| 116 // Invoked to notify us that there are messages available for this port. | 115 // 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 | 116 // 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 | 117 // non-threadsafe APIs (i.e. should not call into the entangled channel or |
| 119 // access mutable variables). | 118 // access mutable variables). |
| 120 void MessagePort::MessageAvailable() { | 119 void MessagePort::MessageAvailable() { |
| 121 // Don't post another task if there's an identical one pending. | 120 // Don't post another task if there's an identical one pending. |
| 122 if (AtomicTestAndSetToOne(&pending_dispatch_task_)) | 121 if (AtomicTestAndSetToOne(&pending_dispatch_task_)) |
| 123 return; | 122 return; |
| 124 | 123 |
| 125 DCHECK(GetExecutionContext()); | 124 task_runner_->PostTask(BLINK_FROM_HERE, |
| 126 // TODO(tzik): Use ParentThreadTaskRunners instead of ExecutionContext here to | 125 CrossThreadBind(&MessagePort::DispatchMessages, |
| 127 // avoid touching foreign thread GCed object. | 126 WrapCrossThreadWeakPersistent(this))); |
| 128 GetExecutionContext()->PostTask( | |
| 129 TaskType::kPostedMessage, BLINK_FROM_HERE, | |
| 130 CreateCrossThreadTask(&MessagePort::DispatchMessages, | |
| 131 WrapCrossThreadWeakPersistent(this))); | |
| 132 } | 127 } |
| 133 | 128 |
| 134 void MessagePort::start() { | 129 void MessagePort::start() { |
| 135 // Do nothing if we've been cloned or closed. | 130 // Do nothing if we've been cloned or closed. |
| 136 if (!IsEntangled()) | 131 if (!IsEntangled()) |
| 137 return; | 132 return; |
| 138 | 133 |
| 139 DCHECK(GetExecutionContext()); | 134 DCHECK(GetExecutionContext()); |
| 140 if (started_) | 135 if (started_) |
| 141 return; | 136 return; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 } | 281 } |
| 287 return port_array; | 282 return port_array; |
| 288 } | 283 } |
| 289 | 284 |
| 290 DEFINE_TRACE(MessagePort) { | 285 DEFINE_TRACE(MessagePort) { |
| 291 ContextLifecycleObserver::Trace(visitor); | 286 ContextLifecycleObserver::Trace(visitor); |
| 292 EventTargetWithInlineData::Trace(visitor); | 287 EventTargetWithInlineData::Trace(visitor); |
| 293 } | 288 } |
| 294 | 289 |
| 295 } // namespace blink | 290 } // namespace blink |
| OLD | NEW |