Chromium Code Reviews| 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..ec1c835a210133b0951d31910de5cd52dd92c279 |
| --- /dev/null |
| +++ b/components/webmessaging/broadcast_channel_service.cc |
| @@ -0,0 +1,114 @@ |
| +// 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 { |
| + |
| +// There is a one-to-one mapping of BroadcastChannel instances in the renderer |
| +// and Connection instances in the browser. The Connection is owned by a |
| +// BroadcastChannelService. |
| +class BroadcastChannelService::Connection |
| + : public mojom::BroadcastChannelClient { |
| + public: |
| + Connection(const url::Origin& origin, |
| + const mojo::String& name, |
| + mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| + mojom::BroadcastChannelClientAssociatedRequest connection, |
| + webmessaging::BroadcastChannelService* service); |
| + |
| + void OnMessage(const mojo::String& message) override; |
| + mojom::BroadcastChannelClient* client() const { return client_.get(); } |
|
kinuko
2016/06/09 09:14:58
nit: could we avoid having const method returning
Marijn Kruisselbrink
2016/06/22 18:46:56
Done
|
| + const url::Origin& origin() const { return origin_; } |
| + const std::string& name() const { return name_; } |
| + |
| + void set_connection_error_handler(const mojo::Closure& error_handler) { |
| + binding_.set_connection_error_handler(error_handler); |
| + client_.set_connection_error_handler(error_handler); |
| + } |
| + |
| + private: |
| + mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_; |
| + mojom::BroadcastChannelClientAssociatedPtr client_; |
| + |
| + webmessaging::BroadcastChannelService* service_; |
| + url::Origin origin_; |
| + std::string name_; |
| +}; |
| + |
| +BroadcastChannelService::Connection::Connection( |
| + const url::Origin& origin, |
| + const mojo::String& name, |
| + mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| + mojom::BroadcastChannelClientAssociatedRequest connection, |
| + webmessaging::BroadcastChannelService* service) |
| + : binding_(this, std::move(connection)), |
| + service_(service), |
| + origin_(origin), |
| + name_(name) { |
| + client_.Bind(std::move(client)); |
| +} |
| + |
| +void BroadcastChannelService::Connection::OnMessage( |
| + const mojo::String& message) { |
| + service_->ReceivedMessageOnConnection(this, message); |
| +} |
| + |
| +BroadcastChannelService::BroadcastChannelService() {} |
| + |
| +void BroadcastChannelService::Connect( |
| + mojo::InterfaceRequest<mojom::BroadcastChannelService> request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +void BroadcastChannelService::ConnectToChannel( |
| + const url::Origin& origin, |
| + const mojo::String& name, |
| + mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| + mojom::BroadcastChannelClientAssociatedRequest connection) { |
| + std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client), |
| + std::move(connection), this)); |
| + c->set_connection_error_handler( |
| + base::Bind(&BroadcastChannelService::UnregisterConnection, |
| + base::Unretained(this), c.get())); |
| + connections_[c->origin()].insert(std::make_pair(c->name(), std::move(c))); |
|
kinuko
2016/06/09 09:14:58
is it safe to access c on the same param list of s
Marijn Kruisselbrink
2016/06/22 18:46:56
I think it is. std::move doesn't actually modify c
|
| +} |
| + |
| +BroadcastChannelService::~BroadcastChannelService() {} |
| + |
| +void BroadcastChannelService::UnregisterConnection(Connection* c) { |
| + url::Origin origin = c->origin(); |
| + auto& connections = connections_[origin]; |
| + for (auto it = connections.lower_bound(c->name()), |
| + end = connections.upper_bound(c->name()); |
| + it != end; ++it) { |
| + if (it->second.get() == c) { |
| + connections.erase(it); |
| + break; |
| + } |
| + } |
| + if (connections.empty()) |
| + connections_.erase(origin); |
| +} |
| + |
| +void BroadcastChannelService::ReceivedMessageOnConnection( |
| + Connection* c, |
| + const mojo::String& message) { |
| + auto& connections = connections_[c->origin()]; |
| + for (auto it = connections.lower_bound(c->name()), |
| + end = connections.upper_bound(c->name()); |
| + it != end; ++it) { |
| + if (it->second.get() != c) { |
| + it->second->client()->OnMessage(message); |
| + } |
| + } |
| +} |
| + |
| +} // namespace webmessaging |