| 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_provider.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 // There is a one-to-one mapping of BroadcastChannel instances in the renderer | |
| 16 // and Connection instances in the browser. The Connection is owned by a | |
| 17 // BroadcastChannelProvider. | |
| 18 class BroadcastChannelProvider::Connection | |
| 19 : public mojom::BroadcastChannelClient { | |
| 20 public: | |
| 21 Connection(const url::Origin& origin, | |
| 22 const mojo::String& name, | |
| 23 mojom::BroadcastChannelClientAssociatedPtrInfo client, | |
| 24 mojom::BroadcastChannelClientAssociatedRequest connection, | |
| 25 webmessaging::BroadcastChannelProvider* service); | |
| 26 | |
| 27 void OnMessage(const mojo::String& message) override; | |
| 28 void MessageToClient(const mojo::String& message) const { | |
| 29 client_->OnMessage(message); | |
| 30 } | |
| 31 const url::Origin& origin() const { return origin_; } | |
| 32 const std::string& name() const { return name_; } | |
| 33 | |
| 34 void set_connection_error_handler(const base::Closure& error_handler) { | |
| 35 binding_.set_connection_error_handler(error_handler); | |
| 36 client_.set_connection_error_handler(error_handler); | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_; | |
| 41 mojom::BroadcastChannelClientAssociatedPtr client_; | |
| 42 | |
| 43 webmessaging::BroadcastChannelProvider* service_; | |
| 44 url::Origin origin_; | |
| 45 std::string name_; | |
| 46 }; | |
| 47 | |
| 48 BroadcastChannelProvider::Connection::Connection( | |
| 49 const url::Origin& origin, | |
| 50 const mojo::String& name, | |
| 51 mojom::BroadcastChannelClientAssociatedPtrInfo client, | |
| 52 mojom::BroadcastChannelClientAssociatedRequest connection, | |
| 53 webmessaging::BroadcastChannelProvider* service) | |
| 54 : binding_(this, std::move(connection)), | |
| 55 service_(service), | |
| 56 origin_(origin), | |
| 57 name_(name) { | |
| 58 client_.Bind(std::move(client)); | |
| 59 } | |
| 60 | |
| 61 void BroadcastChannelProvider::Connection::OnMessage( | |
| 62 const mojo::String& message) { | |
| 63 service_->ReceivedMessageOnConnection(this, message); | |
| 64 } | |
| 65 | |
| 66 BroadcastChannelProvider::BroadcastChannelProvider() {} | |
| 67 | |
| 68 void BroadcastChannelProvider::Connect( | |
| 69 mojo::InterfaceRequest<mojom::BroadcastChannelProvider> request) { | |
| 70 bindings_.AddBinding(this, std::move(request)); | |
| 71 } | |
| 72 | |
| 73 void BroadcastChannelProvider::ConnectToChannel( | |
| 74 const url::Origin& origin, | |
| 75 const mojo::String& name, | |
| 76 mojom::BroadcastChannelClientAssociatedPtrInfo client, | |
| 77 mojom::BroadcastChannelClientAssociatedRequest connection) { | |
| 78 std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client), | |
| 79 std::move(connection), this)); | |
| 80 c->set_connection_error_handler( | |
| 81 base::Bind(&BroadcastChannelProvider::UnregisterConnection, | |
| 82 base::Unretained(this), c.get())); | |
| 83 connections_[origin].insert(std::make_pair(name, std::move(c))); | |
| 84 } | |
| 85 | |
| 86 BroadcastChannelProvider::~BroadcastChannelProvider() {} | |
| 87 | |
| 88 void BroadcastChannelProvider::UnregisterConnection(Connection* c) { | |
| 89 url::Origin origin = c->origin(); | |
| 90 auto& connections = connections_[origin]; | |
| 91 for (auto it = connections.lower_bound(c->name()), | |
| 92 end = connections.upper_bound(c->name()); | |
| 93 it != end; ++it) { | |
| 94 if (it->second.get() == c) { | |
| 95 connections.erase(it); | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 if (connections.empty()) | |
| 100 connections_.erase(origin); | |
| 101 } | |
| 102 | |
| 103 void BroadcastChannelProvider::ReceivedMessageOnConnection( | |
| 104 Connection* c, | |
| 105 const mojo::String& message) { | |
| 106 auto& connections = connections_[c->origin()]; | |
| 107 for (auto it = connections.lower_bound(c->name()), | |
| 108 end = connections.upper_bound(c->name()); | |
| 109 it != end; ++it) { | |
| 110 if (it->second.get() != c) | |
| 111 it->second->MessageToClient(message); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 } // namespace webmessaging | |
| OLD | NEW |