| Index: third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp
|
| diff --git a/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp b/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..67229199caaeaef8653e638e7dc0b683fbf6c02d
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp
|
| @@ -0,0 +1,90 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "modules/broadcastchannel/BroadcastChannel.h"
|
| +
|
| +#include "bindings/core/v8/SerializedScriptValue.h"
|
| +#include "core/dom/ExceptionCode.h"
|
| +#include "core/events/MessageEvent.h"
|
| +
|
| +namespace blink {
|
| +
|
| +// static
|
| +BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, const String& name, ExceptionState& exceptionState)
|
| +{
|
| + if (executionContext->getSecurityOrigin()->isUnique()) {
|
| + // TODO(mek): Decide what to do here depending on https://github.com/whatwg/html/issues/1319
|
| + exceptionState.throwDOMException(NotSupportedError, "Can't create BroadcastChannel in an opaque origin");
|
| + return nullptr;
|
| + }
|
| + BroadcastChannel* channel = new BroadcastChannel(executionContext, name);
|
| + channel->suspendIfNeeded();
|
| + return channel;
|
| +}
|
| +
|
| +BroadcastChannel::~BroadcastChannel()
|
| +{
|
| + close();
|
| +}
|
| +
|
| +void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& exceptionState)
|
| +{
|
| + if (!m_connection) {
|
| + exceptionState.throwDOMException(InvalidStateError, "Channel is closed");
|
| + return;
|
| + }
|
| + RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(message.isolate(), message.v8Value(), nullptr, nullptr, exceptionState);
|
| + if (exceptionState.hadException())
|
| + return;
|
| +
|
| + String data = value->toWireString();
|
| + m_connection->broadcast(this, data);
|
| +}
|
| +
|
| +void BroadcastChannel::close()
|
| +{
|
| + if (m_connection)
|
| + m_connection->unregisterClient(this);
|
| + m_connection = nullptr;
|
| +}
|
| +
|
| +void BroadcastChannel::onMessage(const String& message)
|
| +{
|
| + RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message);
|
| + dispatchEvent(MessageEvent::create(nullptr, value.release(), getExecutionContext()->getSecurityOrigin()->toString()));
|
| +}
|
| +
|
| +void BroadcastChannel::onError()
|
| +{
|
| + m_connection = nullptr;
|
| + close();
|
| +}
|
| +
|
| +const AtomicString& BroadcastChannel::interfaceName() const
|
| +{
|
| + return EventTargetNames::BroadcastChannel;
|
| +}
|
| +
|
| +bool BroadcastChannel::hasPendingActivity() const
|
| +{
|
| + return m_connection && hasEventListeners(EventTypeNames::message);
|
| +}
|
| +
|
| +DEFINE_TRACE(BroadcastChannel)
|
| +{
|
| + ActiveDOMObject::trace(visitor);
|
| + EventTargetWithInlineData::trace(visitor);
|
| +}
|
| +
|
| +BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const String& name)
|
| + : ActiveScriptWrappable(this)
|
| + , ActiveDOMObject(executionContext)
|
| + , m_origin(executionContext->getSecurityOrigin())
|
| + , m_name(name)
|
| + , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name))
|
| +{
|
| + m_connection->registerClient(this);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|