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 "components/webmessaging/broadcast_channel_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 10 #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 |
| 13 namespace webmessaging { |
| 14 |
| 15 // Connections are owned by the BroadcastChannelService, but can also destroy |
| 16 // and unregister themselves if the connection is disconnected. |
| 17 class BroadcastChannelService::Connection |
| 18 : public mojom::BroadcastChannelService { |
| 19 public: |
| 20 Connection(mojom::BroadcastChannelServiceRequest request, |
| 21 webmessaging::BroadcastChannelService* service) |
| 22 : binding_(this, std::move(request)), service_(service) {} |
| 23 |
| 24 ~Connection() override { service_->UnregisterConnection(this); } |
| 25 |
| 26 void Subscribe( |
| 27 const url::Origin& origin, |
| 28 const mojo::String& name, |
| 29 mojom::BroadcastChannelClientAssociatedPtrInfo clientInfo) override { |
| 30 mojom::BroadcastChannelClientAssociatedPtr client; |
| 31 client.Bind(std::move(clientInfo)); |
| 32 subscriptions_[ChannelKey(origin, name)].AddPtr(std::move(client)); |
| 33 } |
| 34 |
| 35 void Broadcast(const url::Origin& origin, |
| 36 const mojo::String& name, |
| 37 const mojo::String& message) override { |
| 38 service_->ReceivedMessageOnConnection(this, origin, name, message); |
| 39 } |
| 40 |
| 41 void DispatchMessage(const url::Origin& origin, |
| 42 const mojo::String& name, |
| 43 const mojo::String& message) { |
| 44 auto it = subscriptions_.find(ChannelKey(origin, name)); |
| 45 if (it == subscriptions_.end()) |
| 46 return; |
| 47 it->second.ForAllPtrs([message](mojom::BroadcastChannelClient* client) { |
| 48 client->OnMessage(message); |
| 49 }); |
| 50 } |
| 51 |
| 52 private: |
| 53 using ChannelKey = std::pair<url::Origin, std::string>; |
| 54 std::map<ChannelKey, |
| 55 mojo::AssociatedInterfacePtrSet<mojom::BroadcastChannelClient>> |
| 56 subscriptions_; |
| 57 mojo::StrongBinding<mojom::BroadcastChannelService> binding_; |
| 58 webmessaging::BroadcastChannelService* service_; |
| 59 }; |
| 60 |
| 61 BroadcastChannelService::BroadcastChannelService() {} |
| 62 |
| 63 void BroadcastChannelService::Connect( |
| 64 mojom::BroadcastChannelServiceRequest request) { |
| 65 connections_.insert(new Connection(std::move(request), this)); |
| 66 } |
| 67 |
| 68 BroadcastChannelService::~BroadcastChannelService() { |
| 69 STLDeleteElements(&connections_); |
| 70 } |
| 71 |
| 72 void BroadcastChannelService::UnregisterConnection(Connection* c) { |
| 73 connections_.erase(c); |
| 74 } |
| 75 |
| 76 void BroadcastChannelService::ReceivedMessageOnConnection( |
| 77 Connection* from, |
| 78 const url::Origin& origin, |
| 79 const mojo::String& name, |
| 80 const mojo::String& message) { |
| 81 for (Connection* c : connections_) { |
| 82 if (c != from) |
| 83 c->DispatchMessage(origin, name, message); |
| 84 } |
| 85 } |
| 86 |
| 87 } // namespace webmessaging |
OLD | NEW |