-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 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,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 |
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,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_ |
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,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; | ||
}; |
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,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 |