Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Unified Diff: third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase on hashtraits change Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..71bd788faccdf5cf25a4bad21f286fc917012e17
--- /dev/null
+++ b/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp
@@ -0,0 +1,94 @@
+// 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/EventQueue.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;
+ }
+ return new BroadcastChannel(executionContext, name);
+}
+
+BroadcastChannel::~BroadcastChannel()
+{
+}
+
+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)
+{
+ // Queue a task to dispatch the event.
+ RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message);
+ MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExecutionContext()->getSecurityOrigin()->toString());
+ event->setTarget(this);
+ bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event);
+ DCHECK(success);
+ ALLOW_UNUSED_LOCAL(success);
haraken 2016/06/06 02:05:47 Nit: Do we need this?
Marijn Kruisselbrink 2016/06/06 20:05:29 Not sure. Assertions.h seems to suggest so though
+}
+
+void BroadcastChannel::onError()
+{
+ m_connection = nullptr;
haraken 2016/06/06 02:05:47 Do you really want to clear m_connection before ca
Marijn Kruisselbrink 2016/06/06 20:05:29 Good question. That pretty much turns close() into
+ close();
+}
+
+const AtomicString& BroadcastChannel::interfaceName() const
+{
+ return EventTargetNames::BroadcastChannel;
+}
+
+bool BroadcastChannel::hasPendingActivity() const
+{
+ return m_connection && hasEventListeners(EventTypeNames::message);
+}
+
+DEFINE_TRACE(BroadcastChannel)
+{
+ ContextLifecycleObserver::trace(visitor);
+ EventTargetWithInlineData::trace(visitor);
+ visitor->trace(m_connection);
+}
+
+BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const String& name)
+ : ActiveScriptWrappable(this)
+ , ContextLifecycleObserver(executionContext)
haraken 2016/06/06 02:05:47 The fact that you're observing ContextLifecycleObs
haraken 2016/06/07 00:53:26 Any idea on this?
Marijn Kruisselbrink 2016/06/07 01:18:39 Sorry about missing this one. Yes, contextDestroye
haraken 2016/06/07 01:29:55 Don't know. But I think MessagePort should inherit
+ , m_origin(executionContext->getSecurityOrigin())
+ , m_name(name)
+ , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name))
+{
+ m_connection->registerClient(this);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698