Index: components/webmessaging/broadcast_channel_service.cc |
diff --git a/components/webmessaging/broadcast_channel_service.cc b/components/webmessaging/broadcast_channel_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..693e95bd5c0e4e58b7634d06ab47a804875c5a0c |
--- /dev/null |
+++ b/components/webmessaging/broadcast_channel_service.cc |
@@ -0,0 +1,87 @@ |
+// 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 "components/webmessaging/broadcast_channel_service.h" |
+ |
+#include "base/bind.h" |
+#include "base/stl_util.h" |
+#include "mojo/public/cpp/bindings/associated_binding.h" |
+#include "mojo/public/cpp/bindings/interface_ptr_set.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+ |
+namespace webmessaging { |
+ |
+// Connections are owned by the BroadcastChannelService, but can also destroy |
+// and unregister themselves if the connection is disconnected. |
+class BroadcastChannelService::Connection |
+ : public mojom::BroadcastChannelService { |
+ public: |
+ Connection(mojom::BroadcastChannelServiceRequest request, |
+ webmessaging::BroadcastChannelService* service) |
+ : binding_(this, std::move(request)), service_(service) {} |
+ |
+ ~Connection() override { service_->UnregisterConnection(this); } |
+ |
+ void Subscribe( |
+ const url::Origin& origin, |
+ const mojo::String& name, |
+ mojom::BroadcastChannelClientAssociatedPtrInfo clientInfo) override { |
+ mojom::BroadcastChannelClientAssociatedPtr client; |
+ client.Bind(std::move(clientInfo)); |
+ subscriptions_[ChannelKey(origin, name)].AddPtr(std::move(client)); |
+ } |
+ |
+ void Broadcast(const url::Origin& origin, |
+ const mojo::String& name, |
+ const mojo::String& message) override { |
+ service_->ReceivedMessageOnConnection(this, origin, name, message); |
+ } |
+ |
+ void DispatchMessage(const url::Origin& origin, |
+ const mojo::String& name, |
+ const mojo::String& message) { |
+ auto it = subscriptions_.find(ChannelKey(origin, name)); |
+ if (it == subscriptions_.end()) |
+ return; |
+ it->second.ForAllPtrs([message](mojom::BroadcastChannelClient* client) { |
+ client->OnMessage(message); |
+ }); |
+ } |
+ |
+ private: |
+ using ChannelKey = std::pair<url::Origin, std::string>; |
+ std::map<ChannelKey, |
+ mojo::AssociatedInterfacePtrSet<mojom::BroadcastChannelClient>> |
+ subscriptions_; |
+ mojo::StrongBinding<mojom::BroadcastChannelService> binding_; |
+ webmessaging::BroadcastChannelService* service_; |
+}; |
+ |
+BroadcastChannelService::BroadcastChannelService() {} |
+ |
+void BroadcastChannelService::Connect( |
+ mojom::BroadcastChannelServiceRequest request) { |
+ connections_.insert(new Connection(std::move(request), this)); |
+} |
+ |
+BroadcastChannelService::~BroadcastChannelService() { |
+ STLDeleteElements(&connections_); |
+} |
+ |
+void BroadcastChannelService::UnregisterConnection(Connection* c) { |
+ connections_.erase(c); |
+} |
+ |
+void BroadcastChannelService::ReceivedMessageOnConnection( |
+ Connection* from, |
+ const url::Origin& origin, |
+ const mojo::String& name, |
+ const mojo::String& message) { |
+ for (Connection* c : connections_) { |
+ if (c != from) |
+ c->DispatchMessage(origin, name, message); |
+ } |
+} |
+ |
+} // namespace webmessaging |