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/MessageEvent.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 // static |
| 14 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c
onst String& name, ExceptionState& exceptionState) |
| 15 { |
| 16 if (executionContext->getSecurityOrigin()->isUnique()) { |
| 17 // TODO(mek): Decide what to do here depending on https://github.com/wha
twg/html/issues/1319 |
| 18 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc
astChannel in an opaque origin"); |
| 19 return nullptr; |
| 20 } |
| 21 BroadcastChannel* channel = new BroadcastChannel(executionContext, name); |
| 22 channel->suspendIfNeeded(); |
| 23 return channel; |
| 24 } |
| 25 |
| 26 BroadcastChannel::~BroadcastChannel() |
| 27 { |
| 28 close(); |
| 29 } |
| 30 |
| 31 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e
xceptionState) |
| 32 { |
| 33 if (!m_connection) { |
| 34 exceptionState.throwDOMException(InvalidStateError, "Channel is closed")
; |
| 35 return; |
| 36 } |
| 37 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa
ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); |
| 38 if (exceptionState.hadException()) |
| 39 return; |
| 40 |
| 41 String data = value->toWireString(); |
| 42 m_connection->broadcast(this, data); |
| 43 } |
| 44 |
| 45 void BroadcastChannel::close() |
| 46 { |
| 47 if (m_connection) |
| 48 m_connection->unregisterClient(this); |
| 49 m_connection = nullptr; |
| 50 } |
| 51 |
| 52 void BroadcastChannel::onMessage(const String& message) |
| 53 { |
| 54 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message)
; |
| 55 dispatchEvent(MessageEvent::create(nullptr, value.release(), getExecutionCon
text()->getSecurityOrigin()->toString())); |
| 56 } |
| 57 |
| 58 void BroadcastChannel::onError() |
| 59 { |
| 60 m_connection = nullptr; |
| 61 close(); |
| 62 } |
| 63 |
| 64 const AtomicString& BroadcastChannel::interfaceName() const |
| 65 { |
| 66 return EventTargetNames::BroadcastChannel; |
| 67 } |
| 68 |
| 69 bool BroadcastChannel::hasPendingActivity() const |
| 70 { |
| 71 return m_connection && hasEventListeners(EventTypeNames::message); |
| 72 } |
| 73 |
| 74 DEFINE_TRACE(BroadcastChannel) |
| 75 { |
| 76 ActiveDOMObject::trace(visitor); |
| 77 EventTargetWithInlineData::trace(visitor); |
| 78 } |
| 79 |
| 80 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str
ing& name) |
| 81 : ActiveScriptWrappable(this) |
| 82 , ActiveDOMObject(executionContext) |
| 83 , m_origin(executionContext->getSecurityOrigin()) |
| 84 , m_name(name) |
| 85 , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name)) |
| 86 { |
| 87 m_connection->registerClient(this); |
| 88 } |
| 89 |
| 90 } // namespace blink |
OLD | NEW |