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

Side by Side Diff: components/webmessaging/broadcast_channel_service.cc

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: revert to mojo interface of PS4 to have less logic in renderer 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 "components/webmessaging/broadcast_channel_service.h"
6
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "mojo/public/cpp/bindings/associated_binding.h"
10 #include "mojo/public/cpp/bindings/interface_ptr_set.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h"
12
13 namespace webmessaging {
14
15 // There is a one-to-one mapping of BroadcastChannel instances in the renderer
16 // and Connection instances in the browser. The Connection is owned by a
17 // BroadcastChannelService.
18 class BroadcastChannelService::Connection
19 : public mojom::BroadcastChannelClient {
20 public:
21 Connection(const url::Origin& origin,
22 const mojo::String& name,
23 mojom::BroadcastChannelClientAssociatedPtrInfo client,
24 mojom::BroadcastChannelClientAssociatedRequest connection,
25 webmessaging::BroadcastChannelService* service);
26
27 void OnMessage(const mojo::String& message) override;
28 mojom::BroadcastChannelClient* client() const { return client_.get(); }
kinuko 2016/06/09 09:14:58 nit: could we avoid having const method returning
Marijn Kruisselbrink 2016/06/22 18:46:56 Done
29 const url::Origin& origin() const { return origin_; }
30 const std::string& name() const { return name_; }
31
32 void set_connection_error_handler(const mojo::Closure& error_handler) {
33 binding_.set_connection_error_handler(error_handler);
34 client_.set_connection_error_handler(error_handler);
35 }
36
37 private:
38 mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_;
39 mojom::BroadcastChannelClientAssociatedPtr client_;
40
41 webmessaging::BroadcastChannelService* service_;
42 url::Origin origin_;
43 std::string name_;
44 };
45
46 BroadcastChannelService::Connection::Connection(
47 const url::Origin& origin,
48 const mojo::String& name,
49 mojom::BroadcastChannelClientAssociatedPtrInfo client,
50 mojom::BroadcastChannelClientAssociatedRequest connection,
51 webmessaging::BroadcastChannelService* service)
52 : binding_(this, std::move(connection)),
53 service_(service),
54 origin_(origin),
55 name_(name) {
56 client_.Bind(std::move(client));
57 }
58
59 void BroadcastChannelService::Connection::OnMessage(
60 const mojo::String& message) {
61 service_->ReceivedMessageOnConnection(this, message);
62 }
63
64 BroadcastChannelService::BroadcastChannelService() {}
65
66 void BroadcastChannelService::Connect(
67 mojo::InterfaceRequest<mojom::BroadcastChannelService> request) {
68 bindings_.AddBinding(this, std::move(request));
69 }
70
71 void BroadcastChannelService::ConnectToChannel(
72 const url::Origin& origin,
73 const mojo::String& name,
74 mojom::BroadcastChannelClientAssociatedPtrInfo client,
75 mojom::BroadcastChannelClientAssociatedRequest connection) {
76 std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client),
77 std::move(connection), this));
78 c->set_connection_error_handler(
79 base::Bind(&BroadcastChannelService::UnregisterConnection,
80 base::Unretained(this), c.get()));
81 connections_[c->origin()].insert(std::make_pair(c->name(), std::move(c)));
kinuko 2016/06/09 09:14:58 is it safe to access c on the same param list of s
Marijn Kruisselbrink 2016/06/22 18:46:56 I think it is. std::move doesn't actually modify c
82 }
83
84 BroadcastChannelService::~BroadcastChannelService() {}
85
86 void BroadcastChannelService::UnregisterConnection(Connection* c) {
87 url::Origin origin = c->origin();
88 auto& connections = connections_[origin];
89 for (auto it = connections.lower_bound(c->name()),
90 end = connections.upper_bound(c->name());
91 it != end; ++it) {
92 if (it->second.get() == c) {
93 connections.erase(it);
94 break;
95 }
96 }
97 if (connections.empty())
98 connections_.erase(origin);
99 }
100
101 void BroadcastChannelService::ReceivedMessageOnConnection(
102 Connection* c,
103 const mojo::String& message) {
104 auto& connections = connections_[c->origin()];
105 for (auto it = connections.lower_bound(c->name()),
106 end = connections.upper_bound(c->name());
107 it != end; ++it) {
108 if (it->second.get() != c) {
109 it->second->client()->OnMessage(message);
110 }
111 }
112 }
113
114 } // namespace webmessaging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698