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

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: rebase and slightly improved tests 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 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>);
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 onError();
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 m_binding.set_connection_error_handler(createBaseCallback(bind(&BroadcastCha nnelConnection::onError, this)));
Marijn Kruisselbrink 2016/05/31 23:57:08 Oh, one more mojo specific question: since my erro
94 auto result = getConnectionMap()->set(ConnectionKey(m_origin, m_name), this) ;
95 DCHECK(result.isNewEntry) << "There should only be one connection per channe l per thread.";
96 ALLOW_UNUSED_LOCAL(result);
97 }
98
99 void BroadcastChannelConnection::OnMessage(const String& message)
100 {
101 for (Client* client : m_clients)
102 client->onMessage(message);
103 }
104
105 void BroadcastChannelConnection::onError()
106 {
107 // Destructor will call onError of clients and unregister the connection.
108 delete this;
109 }
110
111 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698