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 "modules/broadcastchannel/BroadcastChannelConnection.h" | |
6 | |
7 #include "platform/ThreadSafeFunctional.h" | |
8 #include "platform/mojo/MojoHelper.h" | |
9 #include "public/platform/Platform.h" | |
10 #include "public/platform/ServiceRegistry.h" | |
11 #include "public/platform/WebTaskRunner.h" | |
12 #include "public/platform/WebTraceLocation.h" | |
13 | |
14 namespace blink { | |
15 | |
16 namespace { | |
17 | |
18 void connectToService(webmessaging::mojom::blink::BroadcastChannelServiceRequest request) | |
19 { | |
20 DCHECK(Platform::current()->mainThread()->isCurrentThread()); | |
21 Platform::current()->serviceRegistry()->connectToRemoteService(std::move(req uest)); | |
22 } | |
23 | |
24 webmessaging::mojom::blink::BroadcastChannelServicePtr& getThreadSpecificService () | |
25 { | |
26 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<webmessaging::mojom::blink::B roadcastChannelServicePtr>, service, new ThreadSpecific<webmessaging::mojom::bli nk::BroadcastChannelServicePtr>); | |
esprehn
2016/06/06 02:29:42
Hmm why do we need statics for this? I don't think
Marijn Kruisselbrink
2016/06/06 20:05:29
Because in mojo every interface is its own pipe wi
haraken
2016/06/07 00:53:26
OK. Another option would be to use Page for the ma
Marijn Kruisselbrink
2016/06/07 01:18:39
Yeah, anything more contained than a service conne
| |
27 if (!service.isSet()) { | |
28 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FR OM_HERE, | |
29 threadSafeBind(&connectToService, passed(mojo::GetProxy(&*service))) ); | |
30 } | |
31 return *service; | |
32 } | |
33 | |
34 // BroadcastChannelConnection instances are jointly owned by the clients using | |
35 // the connection. This map is merely a way for new clients to lookup the | |
36 // connection to use, hence it stores weak references to connections. | |
37 using ConnectionKey = std::pair<RefPtr<SecurityOrigin>, String>; | |
38 using ConnectionMap = HeapHashMap<ConnectionKey, WeakMember<BroadcastChannelConn ection>>; | |
39 | |
40 ConnectionMap* getConnectionMap() | |
41 { | |
42 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<Persistent<ConnectionMap>>, c onnectionMap, new ThreadSpecific<Persistent<ConnectionMap>>); | |
haraken
2016/06/06 02:05:47
+sigbjornf@: Please check if this usage is correct
sof
2016/06/06 06:35:45
Usage is exemplary; we don't try to hide very many
| |
43 if (!connectionMap.isSet()) { | |
44 *connectionMap = new ConnectionMap; | |
45 connectionMap->registerAsStaticReference(); | |
46 } | |
47 return *connectionMap; | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 // static | |
53 BroadcastChannelConnection* BroadcastChannelConnection::getForChannel(const RefP tr<SecurityOrigin>& origin, const String& name) | |
54 { | |
55 ConnectionMap* connectionMap = getConnectionMap(); | |
56 auto it = connectionMap->find(ConnectionKey(origin, name)); | |
57 if (it != connectionMap->end()) | |
58 return it->value; | |
59 | |
60 webmessaging::mojom::blink::BroadcastChannelServicePtr& service = getThreadS pecificService(); | |
61 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localCli entInfo; | |
62 BroadcastChannelConnection* connection = new BroadcastChannelConnection(name , origin, &localClientInfo, service.associated_group()); | |
63 service->Subscribe(origin, name, std::move(localClientInfo)); | |
64 return connection; | |
65 } | |
66 | |
67 BroadcastChannelConnection::~BroadcastChannelConnection() | |
68 { | |
69 } | |
70 | |
71 void BroadcastChannelConnection::registerClient(Client* client) | |
72 { | |
73 m_clients.add(client); | |
74 } | |
75 | |
76 void BroadcastChannelConnection::unregisterClient(Client* client) | |
77 { | |
78 m_clients.remove(client); | |
79 } | |
80 | |
81 void BroadcastChannelConnection::broadcast(Client* from, const String& message) | |
82 { | |
83 for (Client* client : m_clients) { | |
84 if (client != from) | |
85 client->onMessage(message); | |
86 } | |
87 getThreadSpecificService()->Broadcast(from->origin(), from->name(), message) ; | |
88 } | |
89 | |
90 DEFINE_TRACE(BroadcastChannelConnection) | |
91 { | |
92 visitor->trace(m_clients); | |
93 } | |
94 | |
95 BroadcastChannelConnection::BroadcastChannelConnection(const String& name, const RefPtr<SecurityOrigin>& origin, webmessaging::mojom::blink::BroadcastChannelCli entAssociatedPtrInfo* clientInfo, mojo::AssociatedGroup* associatedGroup) | |
96 : m_name(name) | |
97 , m_origin(origin) | |
98 , m_binding(this, clientInfo, associatedGroup) | |
99 { | |
100 m_binding.set_connection_error_handler(createBaseCallback(bind(&BroadcastCha nnelConnection::onError, WeakPersistentThisPointer<BroadcastChannelConnection>(t his)))); | |
haraken
2016/06/06 02:05:47
+hiroshige@: Is this a correct usage to bind a Bli
Marijn Kruisselbrink
2016/06/06 20:05:29
fwiw this is how at least PaymentRequest does it (
| |
101 auto result = getConnectionMap()->set(ConnectionKey(m_origin, m_name), this) ; | |
102 DCHECK(result.isNewEntry) << "There should only be one connection per channe l per thread."; | |
103 ALLOW_UNUSED_LOCAL(result); | |
haraken
2016/06/06 02:05:47
Nit: Is this needed?
Marijn Kruisselbrink
2016/06/06 20:05:29
Not sure. Assertions.h seems to suggest so though
| |
104 } | |
105 | |
106 void BroadcastChannelConnection::OnMessage(const String& message) | |
107 { | |
108 for (Client* client : m_clients) | |
109 client->onMessage(message); | |
110 } | |
111 | |
112 void BroadcastChannelConnection::onError() | |
113 { | |
114 for (Client* client : m_clients) | |
115 client->onError(); | |
116 m_clients.clear(); | |
117 getConnectionMap()->remove(ConnectionKey(m_origin, m_name)); | |
118 } | |
119 | |
120 } // namespace blink | |
OLD | NEW |