Skip to content

Commit

Permalink
Add MessageChannel and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jellefoks committed Nov 16, 2023
1 parent 3b9dd00 commit a57e62e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
1 change: 1 addition & 0 deletions cobalt/browser/idl_files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions cobalt/web/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
42 changes: 42 additions & 0 deletions cobalt/web/message_channel.cc
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>
#include <utility>

#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
45 changes: 45 additions & 0 deletions cobalt/web/message_channel.h
Original file line number Diff line number Diff line change
@@ -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<MessagePort>& port1() { return port1_; }
const scoped_refptr<MessagePort>& port2() { return port2_; }

DEFINE_WRAPPABLE_TYPE(MessageChannel);

private:
scoped_refptr<MessagePort> port1_;
scoped_refptr<MessagePort> port2_;
};

} // namespace web
} // namespace cobalt

#endif // COBALT_WEB_MESSAGE_CHANNEL_H_
20 changes: 20 additions & 0 deletions cobalt/web/message_channel.idl
Original file line number Diff line number Diff line change
@@ -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;
};
65 changes: 65 additions & 0 deletions cobalt/web/message_channel_test.cc
Original file line number Diff line number Diff line change
@@ -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 <string>

#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

0 comments on commit a57e62e

Please sign in to comment.