forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BroadcastChannel as specified in https://html.spec.whatwg.o…
…rg/multipage/comms.html#broadcastchannel BUG=161070 Review-Url: https://codereview.chromium.org/2004643002 Cr-Commit-Position: refs/heads/master@{#403331}
- Loading branch information
1 parent
9348eec
commit e69cdae
Showing
41 changed files
with
1,028 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2016 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
{ | ||
'variables': { | ||
'chromium_code': 1, | ||
'mojom_files': [ | ||
'webmessaging/public/interfaces/broadcast_channel.mojom', | ||
], | ||
}, | ||
'targets': [ | ||
{ | ||
# GN version: //components/webmessaging/webmessaging | ||
'target_name': 'webmessaging', | ||
'type': 'static_library', | ||
'dependencies': [ | ||
'webmessaging_mojo_bindings', | ||
'../base/base.gyp:base', | ||
'../url/url.gyp:url_lib', | ||
], | ||
'sources': [ | ||
'webmessaging/broadcast_channel_provider.cc', | ||
'webmessaging/broadcast_channel_provider.h', | ||
], | ||
}, | ||
{ | ||
'target_name': 'webmessaging_mojo_bindings', | ||
'type': 'static_library', | ||
'sources': [ '<@(mojom_files)' ], | ||
'dependencies': [ | ||
'../url/url.gyp:url_mojom', | ||
], | ||
'export_dependent_settings': [ | ||
'../url/url.gyp:url_mojom', | ||
], | ||
'variables': { | ||
'mojom_typemaps': [ | ||
'../url/mojo/gurl.typemap', | ||
'../url/mojo/origin.typemap', | ||
], | ||
}, | ||
'includes': [ | ||
'../mojo/mojom_bindings_generator.gypi', | ||
], | ||
}, | ||
{ | ||
'target_name': 'webmessaging_mojo_bindings_for_blink', | ||
'type': 'static_library', | ||
'sources': [ '<@(mojom_files)' ], | ||
'dependencies': [ | ||
'../url/url.gyp:url_mojom_for_blink', | ||
], | ||
'export_dependent_settings': [ | ||
'../url/url.gyp:url_mojom_for_blink', | ||
], | ||
'variables': { | ||
'for_blink': 'true', | ||
'mojom_typemaps': [ | ||
'../third_party/WebKit/Source/platform/mojo/KURL.typemap', | ||
'../third_party/WebKit/Source/platform/mojo/SecurityOrigin.typemap', | ||
], | ||
}, | ||
'includes': [ | ||
'../mojo/mojom_bindings_generator.gypi', | ||
], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2016 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
static_library("webmessaging") { | ||
sources = [ | ||
"broadcast_channel_provider.cc", | ||
"broadcast_channel_provider.h", | ||
] | ||
|
||
deps = [ | ||
"//base", | ||
"//components/webmessaging/public/interfaces", | ||
"//mojo/common", | ||
"//url", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include_rules = [ | ||
"+mojo/public", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mek@chromium.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/webmessaging/broadcast_channel_provider.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/stl_util.h" | ||
#include "mojo/public/cpp/bindings/associated_binding.h" | ||
#include "mojo/public/cpp/bindings/interface_ptr_set.h" | ||
#include "mojo/public/cpp/bindings/strong_binding.h" | ||
|
||
namespace webmessaging { | ||
|
||
// There is a one-to-one mapping of BroadcastChannel instances in the renderer | ||
// and Connection instances in the browser. The Connection is owned by a | ||
// BroadcastChannelProvider. | ||
class BroadcastChannelProvider::Connection | ||
: public mojom::BroadcastChannelClient { | ||
public: | ||
Connection(const url::Origin& origin, | ||
const mojo::String& name, | ||
mojom::BroadcastChannelClientAssociatedPtrInfo client, | ||
mojom::BroadcastChannelClientAssociatedRequest connection, | ||
webmessaging::BroadcastChannelProvider* service); | ||
|
||
void OnMessage(const mojo::String& message) override; | ||
void MessageToClient(const mojo::String& message) const { | ||
client_->OnMessage(message); | ||
} | ||
const url::Origin& origin() const { return origin_; } | ||
const std::string& name() const { return name_; } | ||
|
||
void set_connection_error_handler(const base::Closure& error_handler) { | ||
binding_.set_connection_error_handler(error_handler); | ||
client_.set_connection_error_handler(error_handler); | ||
} | ||
|
||
private: | ||
mojo::AssociatedBinding<mojom::BroadcastChannelClient> binding_; | ||
mojom::BroadcastChannelClientAssociatedPtr client_; | ||
|
||
webmessaging::BroadcastChannelProvider* service_; | ||
url::Origin origin_; | ||
std::string name_; | ||
}; | ||
|
||
BroadcastChannelProvider::Connection::Connection( | ||
const url::Origin& origin, | ||
const mojo::String& name, | ||
mojom::BroadcastChannelClientAssociatedPtrInfo client, | ||
mojom::BroadcastChannelClientAssociatedRequest connection, | ||
webmessaging::BroadcastChannelProvider* service) | ||
: binding_(this, std::move(connection)), | ||
service_(service), | ||
origin_(origin), | ||
name_(name) { | ||
client_.Bind(std::move(client)); | ||
} | ||
|
||
void BroadcastChannelProvider::Connection::OnMessage( | ||
const mojo::String& message) { | ||
service_->ReceivedMessageOnConnection(this, message); | ||
} | ||
|
||
BroadcastChannelProvider::BroadcastChannelProvider() {} | ||
|
||
void BroadcastChannelProvider::Connect( | ||
mojo::InterfaceRequest<mojom::BroadcastChannelProvider> request) { | ||
bindings_.AddBinding(this, std::move(request)); | ||
} | ||
|
||
void BroadcastChannelProvider::ConnectToChannel( | ||
const url::Origin& origin, | ||
const mojo::String& name, | ||
mojom::BroadcastChannelClientAssociatedPtrInfo client, | ||
mojom::BroadcastChannelClientAssociatedRequest connection) { | ||
std::unique_ptr<Connection> c(new Connection(origin, name, std::move(client), | ||
std::move(connection), this)); | ||
c->set_connection_error_handler( | ||
base::Bind(&BroadcastChannelProvider::UnregisterConnection, | ||
base::Unretained(this), c.get())); | ||
connections_[origin].insert(std::make_pair(name, std::move(c))); | ||
} | ||
|
||
BroadcastChannelProvider::~BroadcastChannelProvider() {} | ||
|
||
void BroadcastChannelProvider::UnregisterConnection(Connection* c) { | ||
url::Origin origin = c->origin(); | ||
auto& connections = connections_[origin]; | ||
for (auto it = connections.lower_bound(c->name()), | ||
end = connections.upper_bound(c->name()); | ||
it != end; ++it) { | ||
if (it->second.get() == c) { | ||
connections.erase(it); | ||
break; | ||
} | ||
} | ||
if (connections.empty()) | ||
connections_.erase(origin); | ||
} | ||
|
||
void BroadcastChannelProvider::ReceivedMessageOnConnection( | ||
Connection* c, | ||
const mojo::String& message) { | ||
auto& connections = connections_[c->origin()]; | ||
for (auto it = connections.lower_bound(c->name()), | ||
end = connections.upper_bound(c->name()); | ||
it != end; ++it) { | ||
if (it->second.get() != c) | ||
it->second->MessageToClient(message); | ||
} | ||
} | ||
|
||
} // namespace webmessaging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_WEBMESSAGING_BROADCAST_CHANNEL_PROVIDER_H_ | ||
#define COMPONENTS_WEBMESSAGING_BROADCAST_CHANNEL_PROVIDER_H_ | ||
|
||
#include <map> | ||
|
||
#include "base/memory/ref_counted.h" | ||
#include "components/webmessaging/public/interfaces/broadcast_channel.mojom.h" | ||
#include "mojo/public/cpp/bindings/binding_set.h" | ||
#include "url/origin.h" | ||
|
||
namespace webmessaging { | ||
|
||
class BroadcastChannelProvider | ||
: public base::RefCountedThreadSafe<BroadcastChannelProvider>, | ||
public mojom::BroadcastChannelProvider { | ||
public: | ||
BroadcastChannelProvider(); | ||
void Connect(mojo::InterfaceRequest<mojom::BroadcastChannelProvider> request); | ||
|
||
void ConnectToChannel( | ||
const url::Origin& origin, | ||
const mojo::String& name, | ||
mojom::BroadcastChannelClientAssociatedPtrInfo client, | ||
mojom::BroadcastChannelClientAssociatedRequest connection) override; | ||
|
||
private: | ||
friend class base::RefCountedThreadSafe<BroadcastChannelProvider>; | ||
class Connection; | ||
|
||
~BroadcastChannelProvider() override; | ||
|
||
void UnregisterConnection(Connection*); | ||
void ReceivedMessageOnConnection(Connection*, const mojo::String& message); | ||
|
||
mojo::BindingSet<mojom::BroadcastChannelProvider> bindings_; | ||
std::map<url::Origin, std::multimap<std::string, std::unique_ptr<Connection>>> | ||
connections_; | ||
}; | ||
|
||
} // namespace webmessaging | ||
|
||
#endif // COMPONENTS_WEBMESSAGING_BROADCAST_CHANNEL_PROVIDER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright 2016 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import("//mojo/public/tools/bindings/mojom.gni") | ||
|
||
mojom("interfaces") { | ||
sources = [ | ||
"broadcast_channel.mojom", | ||
] | ||
deps = [ | ||
"//url/mojo:url_mojom_origin", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
per-file *.mojom=set noparent | ||
per-file *.mojom=file://ipc/SECURITY_OWNERS |
34 changes: 34 additions & 0 deletions
34
components/webmessaging/public/interfaces/broadcast_channel.mojom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
module webmessaging.mojom; | ||
|
||
import "url/mojo/origin.mojom"; | ||
|
||
// A pair of BroadcastChannelClient interfaces is used to represent a connection | ||
// to a particular channel. One client is implemented in the browser, for | ||
// messages sent from the renderer to the browser, and one client is implemented | ||
// in the renderer for messages from the browser to the renderer. | ||
interface BroadcastChannelClient { | ||
// Messages are passed as SerializedScriptValue. | ||
OnMessage(string message); | ||
}; | ||
|
||
// This interface is used to set up connections to broadcast channels. All | ||
// connections to channels made from the same event loop should be made | ||
// through the same BroadcastChannelProvider connection to ensure correct | ||
// ordering of messages. | ||
// Typically the browser will have one instance of a BroadcastChannelProvider | ||
// per storage partition, to which all connections from renderers in that | ||
// partition are bound. This instance will then forward messages received on a | ||
// particular connection to all other connections in the same storage partition | ||
// with the same origin and name. | ||
interface BroadcastChannelProvider { | ||
// Connect to the channel identified by the |origin| and |name|. Messages can | ||
// be sent to the channel using |sender|, and messages to the channel will be | ||
// received by |receiver|. | ||
ConnectToChannel(url.mojom.Origin origin, string name, | ||
associated BroadcastChannelClient receiver, | ||
associated BroadcastChannelClient& sender); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.