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 #ifndef BroadcastChannelConnection_h |
| 6 #define BroadcastChannelConnection_h |
| 7 |
| 8 #include "components/webmessaging/public/interfaces/broadcast_channel.mojom-blin
k.h" |
| 9 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 10 #include "platform/heap/GarbageCollected.h" |
| 11 #include "platform/heap/HeapAllocator.h" |
| 12 #include "platform/weborigin/SecurityOriginHash.h" |
| 13 #include "wtf/text/StringHash.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 // To ensure proper message ordering, all BroadcastChannel instances on the same |
| 18 // thread share a single mojo connection. This class represents such a |
| 19 // connection. BroadcastChannel instances register themselves with the correct |
| 20 // connection, and use the connection to broadcast messages. |
| 21 class BroadcastChannelConnection final |
| 22 : public GarbageCollectedFinalized<BroadcastChannelConnection> |
| 23 , public webmessaging::mojom::blink::BroadcastChannelClient { |
| 24 WTF_MAKE_NONCOPYABLE(BroadcastChannelConnection); |
| 25 public: |
| 26 class Client : public GarbageCollectedMixin { |
| 27 public: |
| 28 virtual String name() const = 0; |
| 29 virtual SecurityOrigin* origin() const = 0; |
| 30 |
| 31 // Called when a message is received for this channel. Not called for |
| 32 // messages sent by this client to the channel. |
| 33 virtual void onMessage(const String& message) = 0; |
| 34 |
| 35 // Called when the connection this client is registered with closes. |
| 36 // After onError is called a client should not try to do anything with |
| 37 // the connection anymore. In particular the client should not call |
| 38 // unregisterClient in its onError implementation. |
| 39 virtual void onError() = 0; |
| 40 }; |
| 41 |
| 42 // Returns the BroadcastChannelConnection instance to use for the channel |
| 43 // identified by the particular origin and name. When called with the same |
| 44 // parameters on the same thread this will always return the same instance, |
| 45 // except if for some reason the mojo connection got severed. |
| 46 // |
| 47 // If all clients using this particular connection get closed, the |
| 48 // connection itself might also be closed, in which case the next time the |
| 49 // method is called a new connection is created. |
| 50 static BroadcastChannelConnection* getForChannel(const RefPtr<SecurityOrigin
>&, const String& name); |
| 51 ~BroadcastChannelConnection(); |
| 52 |
| 53 // Register a new client that uses this connection. |
| 54 void registerClient(Client*); |
| 55 void unregisterClient(Client*); |
| 56 |
| 57 // Broadcasts a message to the channel this connection is for. The message |
| 58 // will be delivered to all clients except the client that send the message. |
| 59 void broadcast(Client*, const String& message); |
| 60 |
| 61 DECLARE_TRACE(); |
| 62 |
| 63 private: |
| 64 BroadcastChannelConnection(const String& name, const RefPtr<SecurityOrigin>&
, webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo*, mojo::As
sociatedGroup*); |
| 65 |
| 66 // webmessaging::mojom::blink::BroadcastChannelClient: |
| 67 void OnMessage(const String& message) override; |
| 68 |
| 69 // Called when the mojo binding disconnects. |
| 70 void onError(); |
| 71 |
| 72 String m_name; |
| 73 RefPtr<SecurityOrigin> m_origin; |
| 74 HeapLinkedHashSet<WeakMember<Client>> m_clients; |
| 75 mojo::AssociatedBinding<webmessaging::mojom::blink::BroadcastChannelClient>
m_binding; |
| 76 }; |
| 77 |
| 78 } // namespace blink |
| 79 |
| 80 #endif // BroadcastChannelConnection_h |
OLD | NEW |