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>); | |
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 using ConnectionKey = std::pair<RefPtr<SecurityOrigin>, String>; | |
35 using ConnectionMap = HashMap<ConnectionKey, BroadcastChannelConnection*>; | |
36 | |
37 ConnectionMap* getConnectionMap() | |
38 { | |
39 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<ConnectionMap>, connectionMap , new ThreadSpecific<ConnectionMap>); | |
haraken
2016/06/02 04:23:41
It's a bit nasty to use thread-local storage for t
Marijn Kruisselbrink
2016/06/02 18:33:29
All BroadcastChannel instances that share thread,
| |
40 return connectionMap; | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 // static | |
46 BroadcastChannelConnection* BroadcastChannelConnection::getForChannel(const RefP tr<SecurityOrigin>& origin, const String& name) | |
47 { | |
48 ConnectionMap* connectionMap = getConnectionMap(); | |
49 auto it = connectionMap->find(ConnectionKey(origin, name)); | |
50 if (it != connectionMap->end()) | |
51 return it->value; | |
52 | |
53 webmessaging::mojom::blink::BroadcastChannelServicePtr& service = getThreadS pecificService(); | |
54 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localCli entInfo; | |
55 BroadcastChannelConnection* connection = new BroadcastChannelConnection(name , origin, &localClientInfo, service.associated_group()); | |
56 service->Subscribe(origin, name, std::move(localClientInfo)); | |
57 return connection; | |
58 } | |
59 | |
60 BroadcastChannelConnection::~BroadcastChannelConnection() | |
61 { | |
62 getConnectionMap()->remove(ConnectionKey(m_origin, m_name)); | |
63 for (Client* client : m_clients) | |
64 client->onError(); | |
65 } | |
66 | |
67 void BroadcastChannelConnection::registerClient(Client* client) | |
68 { | |
69 m_clients.add(client); | |
70 } | |
71 | |
72 void BroadcastChannelConnection::unregisterClient(Client* client) | |
73 { | |
74 m_clients.remove(client); | |
75 if (m_clients.isEmpty()) | |
76 delete this; | |
haraken
2016/06/02 04:23:41
Can we use a unique_ptr to avoid calling manual de
Marijn Kruisselbrink
2016/06/02 18:33:29
The lifetime of this class is mostly managed by th
haraken
2016/06/03 05:33:03
We normally use a dispose() method to promptly dis
| |
77 } | |
78 | |
79 void BroadcastChannelConnection::broadcast(Client* from, const String& message) | |
80 { | |
81 for (Client* client : m_clients) { | |
82 if (client != from) | |
83 client->onMessage(message); | |
84 } | |
85 getThreadSpecificService()->Broadcast(from->origin(), from->name(), message) ; | |
86 } | |
87 | |
88 BroadcastChannelConnection::BroadcastChannelConnection(const String& name, const RefPtr<SecurityOrigin>& origin, webmessaging::mojom::blink::BroadcastChannelCli entAssociatedPtrInfo* clientInfo, mojo::AssociatedGroup* associatedGroup) | |
89 : m_name(name) | |
90 , m_origin(origin) | |
91 , m_binding(this, clientInfo, associatedGroup) | |
92 { | |
93 auto result = getConnectionMap()->set(ConnectionKey(m_origin, m_name), this) ; | |
94 DCHECK(result.isNewEntry) << "There should only be one connection per channe l per thread."; | |
95 ALLOW_UNUSED_LOCAL(result); | |
96 } | |
97 | |
98 void BroadcastChannelConnection::OnMessage(const String& message) | |
99 { | |
100 for (Client* client : m_clients) | |
101 client->onMessage(message); | |
102 } | |
103 | |
104 } // namespace blink | |
OLD | NEW |