Commit 3bebc73e authored by Song Fangzhen's avatar Song Fangzhen Committed by Chromium LUCI CQ

Direct Sockets: Support to resolve hostname.

Implements class DirectSocketsServiceImpl::ResolveHostAndOpenSocket to
finish the resolution of remote hostname.

This job is performed asynchronously.

Below documents are from Eric Willigers <ericwilligers@chromium.org>.
Explainer: https://github.com/WICG/raw-sockets/blob/master/docs/explainer.md

Design doc:
https://docs.google.com/document/d/1Xa5nFkIWxkL3hZHvDYWPhT8sZvNeFpCUKNuqIwZHxnE/edit?usp=sharing

Bug: 905818
Change-Id: Ie040b0b1dc05025030df74b3e81663c94377c2ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2603775
Commit-Queue: Ke He <kehe@chromium.org>
Reviewed-by: default avatarEric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#839845}
parent 70b00289
...@@ -35,10 +35,8 @@ namespace content { ...@@ -35,10 +35,8 @@ namespace content {
namespace { namespace {
enum class ProtocolType { kTcp, kUdp };
struct RecordedCall { struct RecordedCall {
ProtocolType protocol_type; DirectSocketsServiceImpl::ProtocolType protocol_type;
std::string remote_address; std::string remote_address;
uint16_t remote_port; uint16_t remote_port;
...@@ -65,8 +63,8 @@ class MockNetworkContext : public network::TestNetworkContext { ...@@ -65,8 +63,8 @@ class MockNetworkContext : public network::TestNetworkContext {
mojo::PendingRemote<network::mojom::SocketObserver> observer, mojo::PendingRemote<network::mojom::SocketObserver> observer,
CreateTCPConnectedSocketCallback callback) override { CreateTCPConnectedSocketCallback callback) override {
history_.push_back(RecordedCall{ history_.push_back(RecordedCall{
ProtocolType::kTcp, remote_addr_list[0].address().ToString(), DirectSocketsServiceImpl::ProtocolType::kTcp,
remote_addr_list[0].port(), remote_addr_list[0].address().ToString(), remote_addr_list[0].port(),
tcp_connected_socket_options->send_buffer_size, tcp_connected_socket_options->send_buffer_size,
tcp_connected_socket_options->receive_buffer_size, tcp_connected_socket_options->receive_buffer_size,
tcp_connected_socket_options->no_delay}); tcp_connected_socket_options->no_delay});
...@@ -82,11 +80,8 @@ class MockNetworkContext : public network::TestNetworkContext { ...@@ -82,11 +80,8 @@ class MockNetworkContext : public network::TestNetworkContext {
}; };
net::Error UnconditionallyPermitConnection( net::Error UnconditionallyPermitConnection(
const blink::mojom::DirectSocketOptions& options, const blink::mojom::DirectSocketOptions& options) {
net::IPAddress& remote_address) {
DCHECK(options.remote_hostname.has_value()); DCHECK(options.remote_hostname.has_value());
DCHECK(remote_address.AssignFromIPLiteral(*options.remote_hostname));
EXPECT_EQ(remote_address.ToString(), *options.remote_hostname);
return net::OK; return net::OK;
} }
...@@ -162,15 +157,14 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_Success) { ...@@ -162,15 +157,14 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_Success) {
EXPECT_EQ("openTcp succeeded", EvalJs(shell(), script)); EXPECT_EQ("openTcp succeeded", EvalJs(shell(), script));
} }
IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, OpenTcp_NotAllowedError) { IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, OpenTcp_Success_Global) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestPageURL())); EXPECT_TRUE(NavigateToURL(shell(), GetTestPageURL()));
const uint16_t listening_port = StartTcpServer(); const uint16_t listening_port = StartTcpServer();
const std::string script = base::StringPrintf( const std::string script = base::StringPrintf(
"openTcp({remoteAddress: '127.0.0.1', remotePort: %d})", listening_port); "openTcp({remoteAddress: '127.0.0.1', remotePort: %d})", listening_port);
EXPECT_EQ("openTcp failed: NotAllowedError: Permission denied", EXPECT_EQ("openTcp succeeded", EvalJs(shell(), script));
EvalJs(shell(), script));
} }
IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, OpenTcp_CannotEvadeCors) { IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, OpenTcp_CannotEvadeCors) {
...@@ -210,7 +204,7 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_OptionsOne) { ...@@ -210,7 +204,7 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_OptionsOne) {
DCHECK_EQ(1U, mock_network_context.history().size()); DCHECK_EQ(1U, mock_network_context.history().size());
const RecordedCall& call = mock_network_context.history()[0]; const RecordedCall& call = mock_network_context.history()[0];
EXPECT_EQ(ProtocolType::kTcp, call.protocol_type); EXPECT_EQ(DirectSocketsServiceImpl::ProtocolType::kTcp, call.protocol_type);
EXPECT_EQ("12.34.56.78", call.remote_address); EXPECT_EQ("12.34.56.78", call.remote_address);
EXPECT_EQ(9012, call.remote_port); EXPECT_EQ(9012, call.remote_port);
EXPECT_EQ(3456, call.send_buffer_size); EXPECT_EQ(3456, call.send_buffer_size);
...@@ -243,7 +237,7 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_OptionsTwo) { ...@@ -243,7 +237,7 @@ IN_PROC_BROWSER_TEST_F(DirectSocketsBrowserTest, DISABLED_OpenTcp_OptionsTwo) {
DCHECK_EQ(1U, mock_network_context.history().size()); DCHECK_EQ(1U, mock_network_context.history().size());
const RecordedCall& call = mock_network_context.history()[0]; const RecordedCall& call = mock_network_context.history()[0];
EXPECT_EQ(ProtocolType::kTcp, call.protocol_type); EXPECT_EQ(DirectSocketsServiceImpl::ProtocolType::kTcp, call.protocol_type);
EXPECT_EQ("fedc:ba98:7654:3210:fedc:ba98:7654:3210", call.remote_address); EXPECT_EQ("fedc:ba98:7654:3210:fedc:ba98:7654:3210", call.remote_address);
EXPECT_EQ(789, call.remote_port); EXPECT_EQ(789, call.remote_port);
EXPECT_EQ(0, call.send_buffer_size); EXPECT_EQ(0, call.send_buffer_size);
......
...@@ -29,8 +29,10 @@ class CONTENT_EXPORT DirectSocketsServiceImpl ...@@ -29,8 +29,10 @@ class CONTENT_EXPORT DirectSocketsServiceImpl
: public blink::mojom::DirectSocketsService, : public blink::mojom::DirectSocketsService,
public WebContentsObserver { public WebContentsObserver {
public: public:
using PermissionCallback = base::RepeatingCallback< enum class ProtocolType { kTcp, kUdp };
net::Error(const blink::mojom::DirectSocketOptions&, net::IPAddress&)>;
using PermissionCallback = base::RepeatingCallback<net::Error(
const blink::mojom::DirectSocketOptions&)>;
explicit DirectSocketsServiceImpl(RenderFrameHost& frame_host); explicit DirectSocketsServiceImpl(RenderFrameHost& frame_host);
~DirectSocketsServiceImpl() override = default; ~DirectSocketsServiceImpl() override = default;
...@@ -63,17 +65,17 @@ class CONTENT_EXPORT DirectSocketsServiceImpl ...@@ -63,17 +65,17 @@ class CONTENT_EXPORT DirectSocketsServiceImpl
static void SetNetworkContextForTesting(network::mojom::NetworkContext*); static void SetNetworkContextForTesting(network::mojom::NetworkContext*);
static void PopulateLocalAddrForTesting(
const blink::mojom::DirectSocketOptions& options,
base::Optional<net::IPEndPoint>& local_addr);
private: private:
friend class DirectSocketsUnitTest; friend class DirectSocketsUnitTest;
// Returns net::OK and populates |remote_address| if the options are valid and class ResolveHostAndOpenSocket;
// the connection is permitted.
net::Error ValidateOptions(const blink::mojom::DirectSocketOptions& options,
net::IPAddress& remote_address);
// Populate |local_addr| from options. // Returns net::OK if the options are valid and the connection is permitted.
void PopulateLocalAddr(const blink::mojom::DirectSocketOptions& options, net::Error ValidateOptions(const blink::mojom::DirectSocketOptions& options);
base::Optional<net::IPEndPoint>& local_addr);
network::mojom::NetworkContext* GetNetworkContext(); network::mojom::NetworkContext* GetNetworkContext();
......
...@@ -32,13 +32,12 @@ class DirectSocketsUnitTest : public RenderViewHostTestHarness { ...@@ -32,13 +32,12 @@ class DirectSocketsUnitTest : public RenderViewHostTestHarness {
} }
net::Error ValidateOptions(const blink::mojom::DirectSocketOptions& options) { net::Error ValidateOptions(const blink::mojom::DirectSocketOptions& options) {
net::IPAddress remote_address; return direct_sockets_service().ValidateOptions(options);
return direct_sockets_service().ValidateOptions(options, remote_address);
} }
void PopulateLocalAddr(const blink::mojom::DirectSocketOptions& options, void PopulateLocalAddr(const blink::mojom::DirectSocketOptions& options,
base::Optional<net::IPEndPoint>& local_addr) { base::Optional<net::IPEndPoint>& local_addr) {
return direct_sockets_service().PopulateLocalAddr(options, local_addr); DirectSocketsServiceImpl::PopulateLocalAddrForTesting(options, local_addr);
} }
private: private:
......
...@@ -86,6 +86,7 @@ TCPReadableStreamWrapper::TCPReadableStreamWrapper( ...@@ -86,6 +86,7 @@ TCPReadableStreamWrapper::TCPReadableStreamWrapper(
WTF::BindRepeating(&TCPReadableStreamWrapper::OnPeerClosed, WTF::BindRepeating(&TCPReadableStreamWrapper::OnPeerClosed,
WrapWeakPersistent(this))); WrapWeakPersistent(this)));
ScriptState::Scope scope(script_state_);
// Set queuing strategy of default behavior with a high water mark of 1. // Set queuing strategy of default behavior with a high water mark of 1.
readable_ = ReadableStream::CreateWithCountQueueingStrategy( readable_ = ReadableStream::CreateWithCountQueueingStrategy(
script_state_, script_state_,
......
...@@ -122,6 +122,7 @@ TCPWritableStreamWrapper::TCPWritableStreamWrapper( ...@@ -122,6 +122,7 @@ TCPWritableStreamWrapper::TCPWritableStreamWrapper(
WTF::BindRepeating(&TCPWritableStreamWrapper::OnPeerClosed, WTF::BindRepeating(&TCPWritableStreamWrapper::OnPeerClosed,
WrapWeakPersistent(this))); WrapWeakPersistent(this)));
ScriptState::Scope scope(script_state_);
// Set the CountQueueingStrategy's high water mark as 1 to make the logic of // Set the CountQueueingStrategy's high water mark as 1 to make the logic of
// |WriteOrCacheData| much simpler // |WriteOrCacheData| much simpler
writable_ = WritableStream::CreateWithCountQueueingStrategy( writable_ = WritableStream::CreateWithCountQueueingStrategy(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment