Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannelConnection.cpp

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address some comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // 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/07 00:53:26 How about creating a map from ExecutionContext* to
Marijn Kruisselbrink 2016/06/07 01:18:39 Maybe I should just go back to not having any kind
haraken 2016/06/07 01:29:56 Yeah, let's exclude the optimization from this CL.
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))));
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);
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698