| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/broadcast_channel/broadcast_channel_provider.h" | 5 #include "content/browser/broadcast_channel/broadcast_channel_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "mojo/public/cpp/bindings/associated_binding.h" | 9 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 10 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | 10 #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 // There is a one-to-one mapping of BroadcastChannel instances in the renderer | 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 | 16 // and Connection instances in the browser. The Connection is owned by a |
| 17 // BroadcastChannelProvider. | 17 // BroadcastChannelProvider. |
| 18 class BroadcastChannelProvider::Connection | 18 class BroadcastChannelProvider::Connection |
| 19 : public blink::mojom::BroadcastChannelClient { | 19 : public blink::mojom::BroadcastChannelClient { |
| 20 public: | 20 public: |
| 21 Connection(const url::Origin& origin, | 21 Connection(const url::Origin& origin, |
| 22 const mojo::String& name, | 22 const std::string& name, |
| 23 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, | 23 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 24 blink::mojom::BroadcastChannelClientAssociatedRequest connection, | 24 blink::mojom::BroadcastChannelClientAssociatedRequest connection, |
| 25 BroadcastChannelProvider* service); | 25 BroadcastChannelProvider* service); |
| 26 | 26 |
| 27 void OnMessage(mojo::Array<uint8_t> message) override; | 27 void OnMessage(const std::vector<uint8_t>& message) override; |
| 28 void MessageToClient(mojo::Array<uint8_t> message) const { | 28 void MessageToClient(const std::vector<uint8_t>& message) const { |
| 29 client_->OnMessage(std::move(message)); | 29 client_->OnMessage(message); |
| 30 } | 30 } |
| 31 const url::Origin& origin() const { return origin_; } | 31 const url::Origin& origin() const { return origin_; } |
| 32 const std::string& name() const { return name_; } | 32 const std::string& name() const { return name_; } |
| 33 | 33 |
| 34 void set_connection_error_handler(const base::Closure& error_handler) { | 34 void set_connection_error_handler(const base::Closure& error_handler) { |
| 35 binding_.set_connection_error_handler(error_handler); | 35 binding_.set_connection_error_handler(error_handler); |
| 36 client_.set_connection_error_handler(error_handler); | 36 client_.set_connection_error_handler(error_handler); |
| 37 } | 37 } |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 mojo::AssociatedBinding<blink::mojom::BroadcastChannelClient> binding_; | 40 mojo::AssociatedBinding<blink::mojom::BroadcastChannelClient> binding_; |
| 41 blink::mojom::BroadcastChannelClientAssociatedPtr client_; | 41 blink::mojom::BroadcastChannelClientAssociatedPtr client_; |
| 42 | 42 |
| 43 BroadcastChannelProvider* service_; | 43 BroadcastChannelProvider* service_; |
| 44 url::Origin origin_; | 44 url::Origin origin_; |
| 45 std::string name_; | 45 std::string name_; |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 BroadcastChannelProvider::Connection::Connection( | 48 BroadcastChannelProvider::Connection::Connection( |
| 49 const url::Origin& origin, | 49 const url::Origin& origin, |
| 50 const mojo::String& name, | 50 const std::string& name, |
| 51 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, | 51 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 52 blink::mojom::BroadcastChannelClientAssociatedRequest connection, | 52 blink::mojom::BroadcastChannelClientAssociatedRequest connection, |
| 53 BroadcastChannelProvider* service) | 53 BroadcastChannelProvider* service) |
| 54 : binding_(this, std::move(connection)), | 54 : binding_(this, std::move(connection)), |
| 55 service_(service), | 55 service_(service), |
| 56 origin_(origin), | 56 origin_(origin), |
| 57 name_(name) { | 57 name_(name) { |
| 58 client_.Bind(std::move(client)); | 58 client_.Bind(std::move(client)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void BroadcastChannelProvider::Connection::OnMessage( | 61 void BroadcastChannelProvider::Connection::OnMessage( |
| 62 mojo::Array<uint8_t> message) { | 62 const std::vector<uint8_t>& message) { |
| 63 service_->ReceivedMessageOnConnection(this, std::move(message)); | 63 service_->ReceivedMessageOnConnection(this, message); |
| 64 } | 64 } |
| 65 | 65 |
| 66 BroadcastChannelProvider::BroadcastChannelProvider() {} | 66 BroadcastChannelProvider::BroadcastChannelProvider() {} |
| 67 | 67 |
| 68 void BroadcastChannelProvider::Connect( | 68 void BroadcastChannelProvider::Connect( |
| 69 mojo::InterfaceRequest<blink::mojom::BroadcastChannelProvider> request) { | 69 mojo::InterfaceRequest<blink::mojom::BroadcastChannelProvider> request) { |
| 70 bindings_.AddBinding(this, std::move(request)); | 70 bindings_.AddBinding(this, std::move(request)); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void BroadcastChannelProvider::ConnectToChannel( | 73 void BroadcastChannelProvider::ConnectToChannel( |
| 74 const url::Origin& origin, | 74 const url::Origin& origin, |
| 75 const mojo::String& name, | 75 const std::string& name, |
| 76 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, | 76 blink::mojom::BroadcastChannelClientAssociatedPtrInfo client, |
| 77 blink::mojom::BroadcastChannelClientAssociatedRequest connection) { | 77 blink::mojom::BroadcastChannelClientAssociatedRequest connection) { |
| 78 std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client), | 78 std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client), |
| 79 std::move(connection), this)); | 79 std::move(connection), this)); |
| 80 c->set_connection_error_handler( | 80 c->set_connection_error_handler( |
| 81 base::Bind(&BroadcastChannelProvider::UnregisterConnection, | 81 base::Bind(&BroadcastChannelProvider::UnregisterConnection, |
| 82 base::Unretained(this), c.get())); | 82 base::Unretained(this), c.get())); |
| 83 connections_[origin].insert(std::make_pair(name, std::move(c))); | 83 connections_[origin].insert(std::make_pair(name, std::move(c))); |
| 84 } | 84 } |
| 85 | 85 |
| 86 BroadcastChannelProvider::~BroadcastChannelProvider() {} | 86 BroadcastChannelProvider::~BroadcastChannelProvider() {} |
| 87 | 87 |
| 88 void BroadcastChannelProvider::UnregisterConnection(Connection* c) { | 88 void BroadcastChannelProvider::UnregisterConnection(Connection* c) { |
| 89 url::Origin origin = c->origin(); | 89 url::Origin origin = c->origin(); |
| 90 auto& connections = connections_[origin]; | 90 auto& connections = connections_[origin]; |
| 91 for (auto it = connections.lower_bound(c->name()), | 91 for (auto it = connections.lower_bound(c->name()), |
| 92 end = connections.upper_bound(c->name()); | 92 end = connections.upper_bound(c->name()); |
| 93 it != end; ++it) { | 93 it != end; ++it) { |
| 94 if (it->second.get() == c) { | 94 if (it->second.get() == c) { |
| 95 connections.erase(it); | 95 connections.erase(it); |
| 96 break; | 96 break; |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 if (connections.empty()) | 99 if (connections.empty()) |
| 100 connections_.erase(origin); | 100 connections_.erase(origin); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void BroadcastChannelProvider::ReceivedMessageOnConnection( | 103 void BroadcastChannelProvider::ReceivedMessageOnConnection( |
| 104 Connection* c, | 104 Connection* c, |
| 105 mojo::Array<uint8_t> message) { | 105 const std::vector<uint8_t>& message) { |
| 106 auto& connections = connections_[c->origin()]; | 106 auto& connections = connections_[c->origin()]; |
| 107 for (auto it = connections.lower_bound(c->name()), | 107 for (auto it = connections.lower_bound(c->name()), |
| 108 end = connections.upper_bound(c->name()); | 108 end = connections.upper_bound(c->name()); |
| 109 it != end; ++it) { | 109 it != end; ++it) { |
| 110 if (it->second.get() != c) | 110 if (it->second.get() != c) |
| 111 it->second->MessageToClient(message.Clone()); | 111 it->second->MessageToClient(message); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace content | 115 } // namespace content |
| OLD | NEW |