| 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..c3f56251fd1f4783cfb1f5a24b906f3065a18c67
|
| --- /dev/null
|
| +++ b/components/webmessaging/broadcast_channel_service.cc
|
| @@ -0,0 +1,106 @@
|
| +// 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 "mojo/public/cpp/bindings/associated_binding.h"
|
| +#include "mojo/public/cpp/bindings/strong_binding.h"
|
| +#include "url/gurl.h"
|
| +
|
| +namespace webmessaging {
|
| +
|
| +class BroadcastChannelService::Connection
|
| + : public mojom::BroadcastChannelClient {
|
| + public:
|
| + Connection(const mojo::String& origin,
|
| + const mojo::String& name,
|
| + mojom::BroadcastChannelClientAssociatedPtrInfo client,
|
| + mojom::BroadcastChannelClientAssociatedRequest connection,
|
| + base::WeakPtr<webmessaging::BroadcastChannelService> service);
|
| + ~Connection() override;
|
| +
|
| + void OnMessage(const mojo::String& message) override;
|
| +
|
| + mojom::BroadcastChannelClient* client() const { return client_.get(); }
|
| + const url::Origin& origin() const { return origin_; }
|
| + const std::string& name() const { return name_; }
|
| +
|
| + private:
|
| + mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_;
|
| + mojom::BroadcastChannelClientAssociatedPtr client_;
|
| + base::WeakPtr<webmessaging::BroadcastChannelService> service_;
|
| + url::Origin origin_;
|
| + std::string name_;
|
| +};
|
| +
|
| +BroadcastChannelService::Connection::Connection(
|
| + const mojo::String& origin,
|
| + const mojo::String& name,
|
| + mojom::BroadcastChannelClientAssociatedPtrInfo client,
|
| + mojom::BroadcastChannelClientAssociatedRequest connection,
|
| + base::WeakPtr<webmessaging::BroadcastChannelService> service)
|
| + : binding_(this, std::move(connection)),
|
| + service_(service),
|
| + origin_(url::Origin(GURL(std::string(origin)))),
|
| + name_(name) {
|
| + client_.Bind(std::move(client));
|
| + service_->RegisterConnection(this);
|
| +}
|
| +
|
| +BroadcastChannelService::Connection::~Connection() {
|
| + if (service_)
|
| + service_->UnregisterConnection(this);
|
| +}
|
| +
|
| +void BroadcastChannelService::Connection::OnMessage(
|
| + const mojo::String& message) {
|
| + if (service_)
|
| + service_->ReceivedMessageOnConnection(this, message);
|
| +}
|
| +
|
| +BroadcastChannelService::BroadcastChannelService() : weak_factory_(this) {}
|
| +
|
| +void BroadcastChannelService::ConnectProcess(
|
| + mojo::InterfaceRequest<mojom::BroadcastChannelService> request) {
|
| + bindings_.AddBinding(this, std::move(request));
|
| +}
|
| +
|
| +void BroadcastChannelService::ConnectToChannel(
|
| + const mojo::String& origin,
|
| + const mojo::String& name,
|
| + mojom::BroadcastChannelClientAssociatedPtrInfo client,
|
| + mojom::BroadcastChannelClientAssociatedRequest connection) {
|
| + new Connection(origin, name, std::move(client), std::move(connection),
|
| + weak_factory_.GetWeakPtr());
|
| +}
|
| +
|
| +BroadcastChannelService::~BroadcastChannelService() {}
|
| +
|
| +void BroadcastChannelService::RegisterConnection(Connection* c) {
|
| + connections_.insert(std::make_pair(c->origin(), c));
|
| +}
|
| +
|
| +void BroadcastChannelService::UnregisterConnection(Connection* c) {
|
| + auto begin = connections_.lower_bound(c->origin());
|
| + auto end = connections_.upper_bound(c->origin());
|
| + for (auto it = begin; it != end; ++it) {
|
| + if (it->second == c) {
|
| + connections_.erase(it);
|
| + return;
|
| + }
|
| + }
|
| +}
|
| +
|
| +void BroadcastChannelService::ReceivedMessageOnConnection(
|
| + Connection* c,
|
| + const mojo::String& message) {
|
| + auto begin = connections_.lower_bound(c->origin());
|
| + auto end = connections_.upper_bound(c->origin());
|
| + for (auto it = begin; it != end; ++it) {
|
| + if (it->second != c && it->second->name() == c->name())
|
| + it->second->client()->OnMessage(message);
|
| + }
|
| +}
|
| +
|
| +} // namespace webmessaging
|
|
|