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 "mojo/public/cpp/bindings/associated_binding.h" |
| 8 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 9 #include "url/gurl.h" |
| 10 |
| 11 namespace webmessaging { |
| 12 |
| 13 class BroadcastChannelService::Connection |
| 14 : public mojom::BroadcastChannelClient { |
| 15 public: |
| 16 Connection(const mojo::String& origin, |
| 17 const mojo::String& name, |
| 18 mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 19 mojom::BroadcastChannelClientAssociatedRequest connection, |
| 20 base::WeakPtr<webmessaging::BroadcastChannelService> service); |
| 21 ~Connection() override; |
| 22 |
| 23 void OnMessage(const mojo::String& message) override; |
| 24 |
| 25 mojom::BroadcastChannelClient* client() const { return client_.get(); } |
| 26 const url::Origin& origin() const { return origin_; } |
| 27 const std::string& name() const { return name_; } |
| 28 |
| 29 private: |
| 30 mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_; |
| 31 mojom::BroadcastChannelClientAssociatedPtr client_; |
| 32 base::WeakPtr<webmessaging::BroadcastChannelService> service_; |
| 33 url::Origin origin_; |
| 34 std::string name_; |
| 35 }; |
| 36 |
| 37 BroadcastChannelService::Connection::Connection( |
| 38 const mojo::String& origin, |
| 39 const mojo::String& name, |
| 40 mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 41 mojom::BroadcastChannelClientAssociatedRequest connection, |
| 42 base::WeakPtr<webmessaging::BroadcastChannelService> service) |
| 43 : binding_(this, std::move(connection)), |
| 44 service_(service), |
| 45 origin_(url::Origin(GURL(std::string(origin)))), |
| 46 name_(name) { |
| 47 client_.Bind(std::move(client)); |
| 48 service_->RegisterConnection(this); |
| 49 } |
| 50 |
| 51 BroadcastChannelService::Connection::~Connection() { |
| 52 if (service_) |
| 53 service_->UnregisterConnection(this); |
| 54 } |
| 55 |
| 56 void BroadcastChannelService::Connection::OnMessage( |
| 57 const mojo::String& message) { |
| 58 if (service_) |
| 59 service_->ReceivedMessageOnConnection(this, message); |
| 60 } |
| 61 |
| 62 BroadcastChannelService::BroadcastChannelService() : weak_factory_(this) {} |
| 63 |
| 64 void BroadcastChannelService::ConnectProcess( |
| 65 mojo::InterfaceRequest<mojom::BroadcastChannelService> request) { |
| 66 bindings_.AddBinding(this, std::move(request)); |
| 67 } |
| 68 |
| 69 void BroadcastChannelService::ConnectToChannel( |
| 70 const mojo::String& origin, |
| 71 const mojo::String& name, |
| 72 mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 73 mojom::BroadcastChannelClientAssociatedRequest connection) { |
| 74 new Connection(origin, name, std::move(client), std::move(connection), |
| 75 weak_factory_.GetWeakPtr()); |
| 76 } |
| 77 |
| 78 BroadcastChannelService::~BroadcastChannelService() {} |
| 79 |
| 80 void BroadcastChannelService::RegisterConnection(Connection* c) { |
| 81 connections_.insert(std::make_pair(c->origin(), c)); |
| 82 } |
| 83 |
| 84 void BroadcastChannelService::UnregisterConnection(Connection* c) { |
| 85 auto begin = connections_.lower_bound(c->origin()); |
| 86 auto end = connections_.upper_bound(c->origin()); |
| 87 for (auto it = begin; it != end; ++it) { |
| 88 if (it->second == c) { |
| 89 connections_.erase(it); |
| 90 return; |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 void BroadcastChannelService::ReceivedMessageOnConnection( |
| 96 Connection* c, |
| 97 const mojo::String& message) { |
| 98 auto begin = connections_.lower_bound(c->origin()); |
| 99 auto end = connections_.upper_bound(c->origin()); |
| 100 for (auto it = begin; it != end; ++it) { |
| 101 if (it->second != c && it->second->name() == c->name()) |
| 102 it->second->client()->OnMessage(message); |
| 103 } |
| 104 } |
| 105 |
| 106 } // namespace webmessaging |
OLD | NEW |