OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/broadcastchannel/BroadcastChannel.h" |
| 6 |
| 7 #include "bindings/core/v8/SerializedScriptValue.h" |
| 8 #include "core/dom/ExceptionCode.h" |
| 9 #include "core/events/EventQueue.h" |
| 10 #include "core/events/MessageEvent.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 // static |
| 15 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c
onst String& name, ExceptionState& exceptionState) |
| 16 { |
| 17 if (executionContext->getSecurityOrigin()->isUnique()) { |
| 18 // TODO(mek): Decide what to do here depending on https://github.com/wha
twg/html/issues/1319 |
| 19 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc
astChannel in an opaque origin"); |
| 20 return nullptr; |
| 21 } |
| 22 return new BroadcastChannel(executionContext, name); |
| 23 } |
| 24 |
| 25 BroadcastChannel::~BroadcastChannel() |
| 26 { |
| 27 } |
| 28 |
| 29 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e
xceptionState) |
| 30 { |
| 31 if (!m_connection) { |
| 32 exceptionState.throwDOMException(InvalidStateError, "Channel is closed")
; |
| 33 return; |
| 34 } |
| 35 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa
ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); |
| 36 if (exceptionState.hadException()) |
| 37 return; |
| 38 |
| 39 String data = value->toWireString(); |
| 40 m_connection->broadcast(this, data); |
| 41 } |
| 42 |
| 43 void BroadcastChannel::close() |
| 44 { |
| 45 if (m_connection) |
| 46 m_connection->unregisterClient(this); |
| 47 m_connection = nullptr; |
| 48 } |
| 49 |
| 50 void BroadcastChannel::onMessage(const String& message) |
| 51 { |
| 52 // Queue a task to dispatch the event. |
| 53 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message)
; |
| 54 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec
utionContext()->getSecurityOrigin()->toString()); |
| 55 event->setTarget(this); |
| 56 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); |
| 57 DCHECK(success); |
| 58 ALLOW_UNUSED_LOCAL(success); |
| 59 } |
| 60 |
| 61 void BroadcastChannel::onError() |
| 62 { |
| 63 m_connection = nullptr; |
| 64 } |
| 65 |
| 66 const AtomicString& BroadcastChannel::interfaceName() const |
| 67 { |
| 68 return EventTargetNames::BroadcastChannel; |
| 69 } |
| 70 |
| 71 bool BroadcastChannel::hasPendingActivity() const |
| 72 { |
| 73 return m_connection && hasEventListeners(EventTypeNames::message); |
| 74 } |
| 75 |
| 76 DEFINE_TRACE(BroadcastChannel) |
| 77 { |
| 78 ContextLifecycleObserver::trace(visitor); |
| 79 EventTargetWithInlineData::trace(visitor); |
| 80 visitor->trace(m_connection); |
| 81 } |
| 82 |
| 83 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str
ing& name) |
| 84 : ActiveScriptWrappable(this) |
| 85 , ContextLifecycleObserver(executionContext) |
| 86 , m_origin(executionContext->getSecurityOrigin()) |
| 87 , m_name(name) |
| 88 , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name)) |
| 89 { |
| 90 m_connection->registerClient(this); |
| 91 } |
| 92 |
| 93 } // namespace blink |
OLD | NEW |