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

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

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase and slightly improved tests Created 4 years, 7 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/BroadcastChannelConnection.cpp
diff --git a/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannelConnection.cpp b/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannelConnection.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..88cae18a46d932ad20b082fea8c78b60ec393ddd
--- /dev/null
+++ b/third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannelConnection.cpp
@@ -0,0 +1,111 @@
+// 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/BroadcastChannelConnection.h"
+
+#include "platform/ThreadSafeFunctional.h"
+#include "platform/mojo/MojoHelper.h"
+#include "public/platform/Platform.h"
+#include "public/platform/ServiceRegistry.h"
+#include "public/platform/WebTaskRunner.h"
+#include "public/platform/WebTraceLocation.h"
+
+namespace blink {
+
+namespace {
+
+void connectToService(webmessaging::mojom::blink::BroadcastChannelServiceRequest request)
+{
+ DCHECK(Platform::current()->mainThread()->isCurrentThread());
+ Platform::current()->serviceRegistry()->connectToRemoteService(std::move(request));
+}
+
+webmessaging::mojom::blink::BroadcastChannelServicePtr& getThreadSpecificService()
+{
+ DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<webmessaging::mojom::blink::BroadcastChannelServicePtr>, service, new ThreadSpecific<webmessaging::mojom::blink::BroadcastChannelServicePtr>);
+ if (!service.isSet()) {
+ Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FROM_HERE,
+ threadSafeBind(&connectToService, passed(mojo::GetProxy(&*service))));
+ }
+ return *service;
+}
+
+using ConnectionKey = std::pair<RefPtr<SecurityOrigin>, String>;
+using ConnectionMap = HashMap<ConnectionKey, BroadcastChannelConnection*>;
+
+ConnectionMap* getConnectionMap()
+{
+ DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<ConnectionMap>, connectionMap, new ThreadSpecific<ConnectionMap>);
+ return connectionMap;
+}
+
+} // namespace
+
+// static
+BroadcastChannelConnection* BroadcastChannelConnection::getForChannel(const RefPtr<SecurityOrigin>& origin, const String& name)
+{
+ ConnectionMap* connectionMap = getConnectionMap();
+ auto it = connectionMap->find(ConnectionKey(origin, name));
+ if (it != connectionMap->end())
+ return it->value;
+
+ webmessaging::mojom::blink::BroadcastChannelServicePtr& service = getThreadSpecificService();
+ webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localClientInfo;
+ BroadcastChannelConnection* connection = new BroadcastChannelConnection(name, origin, &localClientInfo, service.associated_group());
+ service->Subscribe(origin, name, std::move(localClientInfo));
+ return connection;
+}
+
+BroadcastChannelConnection::~BroadcastChannelConnection()
+{
+ getConnectionMap()->remove(ConnectionKey(m_origin, m_name));
+ for (Client* client : m_clients)
+ client->onError();
+}
+
+void BroadcastChannelConnection::registerClient(Client* client)
+{
+ m_clients.add(client);
+}
+
+void BroadcastChannelConnection::unregisterClient(Client* client)
+{
+ m_clients.remove(client);
+ if (m_clients.isEmpty())
+ onError();
+}
+
+void BroadcastChannelConnection::broadcast(Client* from, const String& message)
+{
+ for (Client* client : m_clients) {
+ if (client != from)
+ client->onMessage(message);
+ }
+ getThreadSpecificService()->Broadcast(from->origin(), from->name(), message);
+}
+
+BroadcastChannelConnection::BroadcastChannelConnection(const String& name, const RefPtr<SecurityOrigin>& origin, webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo* clientInfo, mojo::AssociatedGroup* associatedGroup)
+ : m_name(name)
+ , m_origin(origin)
+ , m_binding(this, clientInfo, associatedGroup)
+{
+ m_binding.set_connection_error_handler(createBaseCallback(bind(&BroadcastChannelConnection::onError, this)));
Marijn Kruisselbrink 2016/05/31 23:57:08 Oh, one more mojo specific question: since my erro
+ auto result = getConnectionMap()->set(ConnectionKey(m_origin, m_name), this);
+ DCHECK(result.isNewEntry) << "There should only be one connection per channel per thread.";
+ ALLOW_UNUSED_LOCAL(result);
+}
+
+void BroadcastChannelConnection::OnMessage(const String& message)
+{
+ for (Client* client : m_clients)
+ client->onMessage(message);
+}
+
+void BroadcastChannelConnection::onError()
+{
+ // Destructor will call onError of clients and unregister the connection.
+ delete this;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698