diff --git a/dotnet/xwalk_dotnet_bridge.cc b/dotnet/xwalk_dotnet_bridge.cc index c30e6ad554..e98a8a7777 100644 --- a/dotnet/xwalk_dotnet_bridge.cc +++ b/dotnet/xwalk_dotnet_bridge.cc @@ -22,6 +22,7 @@ xwalk::extensions::XWalkDotNetBridge* g_bridge = nullptr; XW_Extension g_xw_extension = 0; const XW_CoreInterface* g_core = nullptr; const XW_MessagingInterface* g_messaging = nullptr; +const XW_MessagingInterface2* g_messaging2 = nullptr; const XW_Internal_SyncMessagingInterface* g_sync_messaging = nullptr; const XW_Internal_EntryPointsInterface* g_entry_points = nullptr; const XW_Internal_RuntimeInterface* g_runtime = nullptr; @@ -53,6 +54,14 @@ bool InitializeInterfaces(XW_GetInterface get_interface) { return false; } + g_messaging2 = reinterpret_cast( + get_interface(XW_MESSAGING_INTERFACE_2)); + if (!g_messaging2) { + std::cerr << + "Can't initialize extension: error getting binary Messaging interface.\n"; + return false; + } + g_sync_messaging = reinterpret_cast( get_interface(XW_INTERNAL_SYNC_MESSAGING_INTERFACE)); @@ -136,6 +145,13 @@ public ref class Bridge : public Object { void setNativeInstance(XW_Instance instance) { instance_ = instance; } + void PostBinaryMessageToJS(array^ message, size_t size) { + pin_ptr message_pin = &message[0]; + const unsigned char* message_ptr = message_pin; + const char* message_to_js = (const char*)(message_ptr); + + bridge_->PostBinaryMessageToInstance(instance_, message_to_js, size); + } void PostMessageToJS(String^ message) { std::string message_to_js; MarshalString(message, &message_to_js); @@ -409,6 +425,12 @@ void XWalkDotNetBridge::HandleSyncMessage(XW_Instance instance, } #undef PostMessage +void XWalkDotNetBridge::PostBinaryMessageToInstance(XW_Instance instance, + const char* message, + const size_t size) { + g_messaging2->PostBinaryMessage(instance, message, size); +} + void XWalkDotNetBridge::PostMessageToInstance(XW_Instance instance, const std::string& message) { g_messaging->PostMessage(instance, message.c_str()); diff --git a/dotnet/xwalk_dotnet_bridge.h b/dotnet/xwalk_dotnet_bridge.h index c77d3d25ab..9165031d6b 100644 --- a/dotnet/xwalk_dotnet_bridge.h +++ b/dotnet/xwalk_dotnet_bridge.h @@ -8,6 +8,7 @@ #include "xwalk/extensions/public/XW_Extension.h" #include "xwalk/extensions/public/XW_Extension_EntryPoints.h" +#include "xwalk/extensions/public/XW_Extension_Message_2.h" #include "xwalk/extensions/public/XW_Extension_Permissions.h" #include "xwalk/extensions/public/XW_Extension_Runtime.h" #include "xwalk/extensions/public/XW_Extension_SyncMessage.h" @@ -32,6 +33,7 @@ class XWalkDotNetBridge { bool Initialize(); XWalkExtensionDotNetInstance CreateInstance(XW_Instance native_instance); + void PostBinaryMessageToInstance(XW_Instance instance, const char* message, const size_t size); void PostMessageToInstance(XW_Instance instance, const std::string& message); void SetSyncReply(XW_Instance instance, const std::string& message); diff --git a/extensions/test/data/binaryTest.html b/extensions/test/data/binaryTest.html new file mode 100644 index 0000000000..67ae59f238 --- /dev/null +++ b/extensions/test/data/binaryTest.html @@ -0,0 +1,18 @@ + + + + + + + + diff --git a/extensions/test/win/BUILD.gn b/extensions/test/win/BUILD.gn index aa8cd5c38a..1fbe4f165e 100644 --- a/extensions/test/win/BUILD.gn +++ b/extensions/test/win/BUILD.gn @@ -174,10 +174,23 @@ msbuild("dotnet_echo_extension2") { ] } +msbuild("dotnet_binary_extension") { + testonly = true + visibility = [ ":*" ] + output_dir = "$_out_dir_prefix/binary_extension" + output_name = "binary_extension" + project_path = "binary_extension/binary_extension.csproj" + sources = [ + "binary_extension/XWalkExtension.cs", + "binary_extension/XWalkExtensionInstance.cs", + ] +} + group("dotnet_extensions") { visibility = [ ":*" ] testonly = true public_deps = [ + ":dotnet_binary_extension", ":dotnet_echo_extension", ":dotnet_echo_extension1", ":dotnet_echo_extension2", diff --git a/extensions/test/win/binary_extension/XWalkExtension.cs b/extensions/test/win/binary_extension/XWalkExtension.cs new file mode 100644 index 0000000000..23d8bad71c --- /dev/null +++ b/extensions/test/win/binary_extension/XWalkExtension.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2013 Intel Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +using System; + +namespace xwalk +{ +public class XWalkExtension +{ + public XWalkExtension() { + } + + public String ExtensionName() { + return "binaryTest"; + } + + public String ExtensionAPI() { + return + @"var binaryTestListener = null; + extension.setMessageListener(function(msg) { + if (binaryTestListener instanceof Function) { + binaryTestListener(msg); + }; + }); + exports.binaryEcho = function(msg, callback) { + binaryTestListener = callback; + extension.postMessage(msg); + }; + } +} +} diff --git a/extensions/test/win/binary_extension/XWalkExtensionInstance.cs b/extensions/test/win/binary_extension/XWalkExtensionInstance.cs new file mode 100644 index 0000000000..ed7cb411f2 --- /dev/null +++ b/extensions/test/win/binary_extension/XWalkExtensionInstance.cs @@ -0,0 +1,24 @@ +// Copyright (c) 2013 Intel Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +using System; + +namespace xwalk +{ +public class XWalkExtensionInstance +{ + public XWalkExtensionInstance(dynamic native) { + native_ = native; + } + + public void HandleMessage(String message) { + byte[] bytes = System.Text.Encoding.Unicode.GetBytes(message); + native_.PostBinaryMessageToJS(bytes, (ulong)bytes.Length); + } + public void HandleSyncMessage(String message) { + native_.SendSyncReply(message); + } + private dynamic native_; +} +} diff --git a/extensions/test/win/binary_extension/binary_extension.csproj b/extensions/test/win/binary_extension/binary_extension.csproj new file mode 100644 index 0000000000..e7d7abf001 --- /dev/null +++ b/extensions/test/win/binary_extension/binary_extension.csproj @@ -0,0 +1,52 @@ + + + + + Debug + AnyCPU + {D70B6453-8366-40F2-9504-EF9BD45C5843} + Library + Properties + binary_extension + binary_extension + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/extensions/test/win/xwalk_dotnet_extension_unittest.cc b/extensions/test/win/xwalk_dotnet_extension_unittest.cc index e774307588..5604ac44e1 100644 --- a/extensions/test/win/xwalk_dotnet_extension_unittest.cc +++ b/extensions/test/win/xwalk_dotnet_extension_unittest.cc @@ -132,4 +132,12 @@ TEST(XWalkDotNetExtensionTest, InvalidExtensions) { EXPECT_TRUE(invalid_extension_12->Initialize()); instance = invalid_extension_12->CreateExternalInstance(); EXPECT_FALSE(instance->GetInstanceData()); + + test_path = GetDotNetExtensionTestPath( + FILE_PATH_LITERAL("binary_extension/binary_extension_bridge.dll")); + TestExtension* valid_extension = new TestExtension(test_path); + EXPECT_TRUE(base::PathExists(test_path)); + EXPECT_TRUE(valid_extension->Initialize()); + XWalkExternalInstance* instance = valid_extension->CreateExternalInstance(); + EXPECT_TRUE(instance->GetInstanceData()); } diff --git a/extensions/test/win/xwalk_dotnet_extensions_browsertest.cc b/extensions/test/win/xwalk_dotnet_extensions_browsertest.cc index ac74127c2f..6901c11160 100644 --- a/extensions/test/win/xwalk_dotnet_extensions_browsertest.cc +++ b/extensions/test/win/xwalk_dotnet_extensions_browsertest.cc @@ -34,6 +34,15 @@ class DotNetMultipleExtensionTest : public XWalkExtensionsTestBase { } }; +class DotNetBinaryTest : public XWalkExtensionsTestBase { + public: + void SetUp() override { + XWalkExtensionService::SetExternalExtensionsPathForTesting( + GetDotNetExtensionTestPath(FILE_PATH_LITERAL("binary_extension"))); + XWalkExtensionsTestBase::SetUp(); + } +}; + IN_PROC_BROWSER_TEST_F(DotNetEchoTest, DotNetExtension) { Runtime* runtime = CreateRuntime(); GURL url = GetExtensionsTestURL(base::FilePath(), @@ -65,3 +74,13 @@ IN_PROC_BROWSER_TEST_F(DotNetMultipleExtensionTest, DotnetExtensionMultiple) { xwalk_test_utils::NavigateToURL(runtime, url); EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle()); } + +IN_PROC_BROWSER_TEST_F(DotNetBinaryTest, DotNetExtension) { + Runtime* runtime = CreateRuntime(); + GURL url = GetExtensionsTestURL(base::FilePath(), + base::FilePath().AppendASCII("binaryTest.html")); + content::TitleWatcher title_watcher(runtime->web_contents(), kPassString); + title_watcher.AlsoWaitForTitle(kFailString); + xwalk_test_utils::NavigateToURL(runtime, url); + EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle()); +}