diff --git a/cobalt/browser/idl_files.gni b/cobalt/browser/idl_files.gni index ca2b572ee9c7..5728b21e0a4f 100644 --- a/cobalt/browser/idl_files.gni +++ b/cobalt/browser/idl_files.gni @@ -212,6 +212,7 @@ source_idl_files = [ "//cobalt/web/event.idl", "//cobalt/web/event_listener.idl", "//cobalt/web/event_target.idl", + "//cobalt/web/message_channel.idl", "//cobalt/web/message_event.idl", "//cobalt/web/message_port.idl", "//cobalt/web/navigator_ua_data.idl", diff --git a/cobalt/web/BUILD.gn b/cobalt/web/BUILD.gn index bbdac0de617f..a2babba2135b 100644 --- a/cobalt/web/BUILD.gn +++ b/cobalt/web/BUILD.gn @@ -80,6 +80,8 @@ static_library("web") { "csp_violation_reporter.h", "environment_settings_helper.cc", "location_base.h", + "message_channel.cc", + "message_channel.h", "message_event.cc", "message_event.h", "message_port.cc", @@ -184,6 +186,7 @@ target(gtest_target_type, "web_test") { "error_event_test.cc", "event_target_test.cc", "event_test.cc", + "message_channel_test.cc", "message_event_test.cc", "message_port_test.cc", "url_test.cc", diff --git a/cobalt/web/message_channel.cc b/cobalt/web/message_channel.cc new file mode 100644 index 000000000000..06b4717d7e90 --- /dev/null +++ b/cobalt/web/message_channel.cc @@ -0,0 +1,42 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cobalt/web/message_channel.h" + +#include +#include +#include + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/logging.h" +#include "base/memory/ptr_util.h" +#include "base/memory/ref_counted.h" +#include "base/task_runner.h" +#include "cobalt/script/environment_settings.h" +#include "cobalt/web/context.h" +#include "cobalt/web/event.h" +#include "cobalt/web/event_target.h" +#include "cobalt/web/message_event.h" + +namespace cobalt { +namespace web { + +MessageChannel::MessageChannel() { + port1_ = new MessagePort; + port2_ = new MessagePort; +} + +} // namespace web +} // namespace cobalt diff --git a/cobalt/web/message_channel.h b/cobalt/web/message_channel.h new file mode 100644 index 000000000000..de024adde98f --- /dev/null +++ b/cobalt/web/message_channel.h @@ -0,0 +1,45 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef COBALT_WEB_MESSAGE_CHANNEL_H_ +#define COBALT_WEB_MESSAGE_CHANNEL_H_ + +#include "cobalt/script/wrappable.h" +#include "cobalt/web/message_port.h" + +namespace cobalt { +namespace web { + +class MessageChannel : public script::Wrappable { + public: + MessageChannel(); + ~MessageChannel() {} + + MessageChannel(const MessageChannel&) = delete; + MessageChannel& operator=(const MessageChannel&) = delete; + + const scoped_refptr& port1() { return port1_; } + const scoped_refptr& port2() { return port2_; } + + DEFINE_WRAPPABLE_TYPE(MessageChannel); + + private: + scoped_refptr port1_; + scoped_refptr port2_; +}; + +} // namespace web +} // namespace cobalt + +#endif // COBALT_WEB_MESSAGE_CHANNEL_H_ diff --git a/cobalt/web/message_channel.idl b/cobalt/web/message_channel.idl new file mode 100644 index 000000000000..e76829a33313 --- /dev/null +++ b/cobalt/web/message_channel.idl @@ -0,0 +1,20 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// https://html.spec.whatwg.org/multipage/web-messaging.html#messagechannel +// https://html.spec.whatwg.org/dev/web-messaging.html#message-ports +[Constructor, Exposed = (Window,Worker)] interface MessageChannel { + readonly attribute MessagePort port1; + readonly attribute MessagePort port2; +}; diff --git a/cobalt/web/message_channel_test.cc b/cobalt/web/message_channel_test.cc new file mode 100644 index 000000000000..d84ae946aedd --- /dev/null +++ b/cobalt/web/message_channel_test.cc @@ -0,0 +1,65 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cobalt/web/message_channel.h" + +#include + +#include "base/strings/string_util.h" +#include "cobalt/web/testing/test_with_javascript.h" + + +#define EXPECT_SUBSTRING(needle, haystack) \ + EXPECT_PRED_FORMAT2(::testing::IsSubstring, (needle), (haystack)) + +namespace cobalt { +namespace web { + +namespace { +class MessageChannelTestWithJavaScript : public testing::TestWebWithJavaScript { +}; +} // namespace + +TEST_P(MessageChannelTestWithJavaScript, MessageChannelIsConstructible) { + std::string result; + EXPECT_TRUE(EvaluateScript("var channel = new MessageChannel();", &result)) + << "Failed to evaluate script."; + EXPECT_EQ("undefined", result) << result; +} + +TEST_P(MessageChannelTestWithJavaScript, MessageChannelPort1IsMessagePort) { + std::string result; + EXPECT_TRUE(EvaluateScript( + "var channel = new MessageChannel(); channel.port1", &result)) + << "Failed to evaluate script."; + EXPECT_NE("null", result) << result; + EXPECT_EQ("[object MessagePort]", result) << result; +} + +TEST_P(MessageChannelTestWithJavaScript, MessageChannelPort2IsMessagePort) { + std::string result; + EXPECT_TRUE(EvaluateScript( + "var channel = new MessageChannel(); channel.port2", &result)) + << "Failed to evaluate script."; + EXPECT_NE("null", result) << result; + EXPECT_EQ("[object MessagePort]", result) << result; +} + +INSTANTIATE_TEST_CASE_P( + MessageChannelTestsWithJavaScript, MessageChannelTestWithJavaScript, + ::testing::ValuesIn(testing::TestWebWithJavaScript::GetWebTypes()), + testing::TestWebWithJavaScript::GetTypeName); + +} // namespace web +} // namespace cobalt