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