diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp index a412b4d58..19cfa3578 100644 --- a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp @@ -69,11 +69,7 @@ namespace CefSharp } else if (obj->IsDate()) { - //TODO: Issue #4234 - auto baseTime = obj->GetDateValue(); - CefTime time; - cef_time_from_basetime(baseTime, &time); - SetCefTime(list, index, time); + SetCefTime(list, index, obj->GetDateValue().val); } else if (obj->IsArray()) { @@ -160,11 +156,8 @@ namespace CefSharp if (IsCefTime(list, index)) { - //TODO: Issue #4234 auto time = GetCefTime(list, index); - CefBaseTime baseTime; - cef_time_to_basetime(&time, &baseTime); - return CefV8Value::CreateDate(baseTime); + return CefV8Value::CreateDate(time); } if (type == VTYPE_LIST) diff --git a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp index 77a6c4fb6..1d0afe469 100644 --- a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp +++ b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp @@ -92,7 +92,9 @@ namespace CefSharp } if (type == DateTime::typeid) { - return CefV8Value::CreateDate(TypeUtils::ConvertDateTimeToCefTime(safe_cast(obj))); + CefBaseTime baseTime; + baseTime.val = CefTimeUtils::FromDateTimeToBaseTime(safe_cast(obj)); + return CefV8Value::CreateDate(baseTime); } if (type->IsArray) { @@ -175,7 +177,7 @@ namespace CefSharp } if (obj->IsDate()) { - return TypeUtils::ConvertCefTimeToDateTime(obj->GetDateValue()); + return CefTimeUtils::FromBaseTimeToDateTime(obj->GetDateValue().val); } if (obj->IsArray()) @@ -254,30 +256,5 @@ namespace CefSharp //TODO: What exception type? throw gcnew Exception("Cannot convert object from Cef to CLR."); } - - DateTime TypeUtils::ConvertCefTimeToDateTime(CefBaseTime baseTime) - { - //TODO: Issue #4234 - CefTime time; - cef_time_from_basetime(baseTime, &time); - - return CefTimeUtils::FromCefTime(time.year, - time.month, - time.day_of_month, - time.hour, - time.minute, - time.second, - time.millisecond); - } - - CefBaseTime TypeUtils::ConvertDateTimeToCefTime(DateTime dateTime) - { - //TODO: Issue #4234 - auto time = CefTime(CefTimeUtils::ToCefTime(dateTime)); - CefBaseTime baseTime; - cef_time_to_basetime(&time, &baseTime); - - return baseTime; - } } } diff --git a/CefSharp.Core.Runtime/CookieManager.cpp b/CefSharp.Core.Runtime/CookieManager.cpp index 6c783a3f1..0766bde51 100644 --- a/CefSharp.Core.Runtime/CookieManager.cpp +++ b/CefSharp.Core.Runtime/CookieManager.cpp @@ -48,11 +48,11 @@ namespace CefSharp if (cookie->Expires.HasValue) { - c.expires.val = CefTimeUtils::FromDateTimeBaseTime(cookie->Expires.Value); + c.expires.val = CefTimeUtils::FromDateTimeToBaseTime(cookie->Expires.Value); } - c.creation.val = CefTimeUtils::FromDateTimeBaseTime(cookie->Creation); - c.last_access.val = CefTimeUtils::FromDateTimeBaseTime(cookie->LastAccess); + c.creation.val = CefTimeUtils::FromDateTimeToBaseTime(cookie->Creation); + c.last_access.val = CefTimeUtils::FromDateTimeToBaseTime(cookie->LastAccess); c.same_site = (cef_cookie_same_site_t)cookie->SameSite; c.priority = (cef_cookie_priority_t)cookie->Priority; diff --git a/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp b/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp index 5a6b5a298..0c911e99f 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp +++ b/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp @@ -36,7 +36,7 @@ namespace CefSharp else if (IsCefTime(list, index)) { auto cefTime = GetCefTime(list, index); - result = ConvertCefTimeToDateTime(cefTime); + result = CefTimeUtils::FromBaseTimeToDateTime(cefTime.val); } else if (IsJsCallback(list, index) && javascriptCallbackFactory != nullptr) { @@ -81,16 +81,6 @@ namespace CefSharp return result; } - - DateTime ConvertCefTimeToDateTime(CefTime time) - { - auto epoch = time.GetDoubleT(); - if (epoch == 0) - { - return DateTime::MinValue; - } - return DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime(); - } } } } diff --git a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp index 46f5cd802..420aec9b3 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp +++ b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp @@ -65,26 +65,25 @@ namespace CefSharp } template - void SetCefTime(const CefRefPtr& list, TIndex index, const CefTime &value) + void SetCefTime(const CefRefPtr& list, TIndex index, const int64 &value) { - auto doubleT = value.GetDoubleT(); - unsigned char mem[1 + sizeof(double)]; + unsigned char mem[1 + sizeof(int64)]; mem[0] = static_cast(PrimitiveType::CEFTIME); - memcpy(reinterpret_cast(mem + 1), &doubleT, sizeof(double)); + memcpy(reinterpret_cast(mem + 1), &value, sizeof(int64)); auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); list->SetBinary(index, binaryValue); } template - CefTime GetCefTime(const CefRefPtr& list, TIndex index) + CefBaseTime GetCefTime(const CefRefPtr& list, TIndex index) { - double doubleT; + CefBaseTime baseTime; auto binaryValue = list->GetBinary(index); - binaryValue->GetData(&doubleT, sizeof(double), 1); + binaryValue->GetData(&baseTime.val, sizeof(int64), 1); - return CefTime(doubleT); + return baseTime; } template @@ -136,4 +135,4 @@ namespace CefSharp } } } -} \ No newline at end of file +} diff --git a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h index 6d4a3bbc0..7853a47c2 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h +++ b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h @@ -20,9 +20,9 @@ namespace CefSharp bool IsInt64(const CefRefPtr& list, TIndex index); template - void SetCefTime(const CefRefPtr& list, TIndex index, const CefTime &value); + void SetCefTime(const CefRefPtr& list, TIndex index, const int64 &value); template - CefTime GetCefTime(const CefRefPtr& list, TIndex index); + CefBaseTime GetCefTime(const CefRefPtr& list, TIndex index); template bool IsCefTime(const CefRefPtr& list, TIndex index); @@ -43,12 +43,12 @@ namespace CefSharp template bool IsInt64(const CefRefPtr& list, size_t index); template bool IsInt64(const CefRefPtr& list, CefString index); - template void SetCefTime(const CefRefPtr& list, int index, const CefTime &value); - template void SetCefTime(const CefRefPtr& list, size_t index, const CefTime &value); - template void SetCefTime(const CefRefPtr& list, CefString index, const CefTime &value); - template CefTime GetCefTime(const CefRefPtr& list, int index); - template CefTime GetCefTime(const CefRefPtr& list, size_t index); - template CefTime GetCefTime(const CefRefPtr& list, CefString index); + template void SetCefTime(const CefRefPtr& list, int index, const int64 &value); + template void SetCefTime(const CefRefPtr& list, size_t index, const int64 &value); + template void SetCefTime(const CefRefPtr& list, CefString index, const int64 &value); + template CefBaseTime GetCefTime(const CefRefPtr& list, int index); + template CefBaseTime GetCefTime(const CefRefPtr& list, size_t index); + template CefBaseTime GetCefTime(const CefRefPtr& list, CefString index); template bool IsCefTime(const CefRefPtr& list, size_t index); template bool IsCefTime(const CefRefPtr& list, int index); template bool IsCefTime(const CefRefPtr& list, CefString index); @@ -64,4 +64,4 @@ namespace CefSharp template bool IsJsCallback(const CefRefPtr& list, CefString index); } } -} \ No newline at end of file +} diff --git a/CefSharp.Core.Runtime/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core.Runtime/Internals/Serialization/V8Serialization.cpp index 598f979ad..09d641082 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/V8Serialization.cpp +++ b/CefSharp.Core.Runtime/Internals/Serialization/V8Serialization.cpp @@ -114,7 +114,7 @@ namespace CefSharp } else if (type == DateTime::typeid) { - SetCefTime(list, index, ConvertDateTimeToCefTime(safe_cast(obj))); + SetCefTime(list, index, CefTimeUtils::FromDateTimeToBaseTime(safe_cast(obj))); } // Serialize enum to sbyte, short, int, long, byte, ushort, uint, ulong (check type of enum) else if (type->IsEnum) @@ -214,13 +214,6 @@ namespace CefSharp ancestors->Remove(obj); } - - CefTime ConvertDateTimeToCefTime(DateTime dateTime) - { - auto timeSpan = dateTime.ToUniversalTime() - DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc); - - return CefTime(timeSpan.TotalSeconds); - } } } } diff --git a/CefSharp.Test/Javascript/EvaluateScriptAsyncFacts.cs b/CefSharp.Test/Javascript/EvaluateScriptAsyncFacts.cs index 51f8700bf..f60090180 100644 --- a/CefSharp.Test/Javascript/EvaluateScriptAsyncFacts.cs +++ b/CefSharp.Test/Javascript/EvaluateScriptAsyncFacts.cs @@ -102,6 +102,7 @@ namespace CefSharp.Test.Javascript } [Theory] + [InlineData("1970-01-01", "1970-01-01")] [InlineData("1980-01-01", "1980-01-01")] //https://github.com/cefsharp/CefSharp/issues/4234 public async Task CanEvaluateDateValues(DateTime expected, string actual) diff --git a/CefSharp.Test/Javascript/JavascriptCallbackFacts.cs b/CefSharp.Test/Javascript/JavascriptCallbackFacts.cs new file mode 100644 index 000000000..fcbc2da3a --- /dev/null +++ b/CefSharp.Test/Javascript/JavascriptCallbackFacts.cs @@ -0,0 +1,185 @@ +// Copyright © 2021 The CefSharp Authors. 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; +using System.Globalization; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace CefSharp.Test.Javascript +{ + [Collection(CefSharpFixtureCollection.Key)] + public class JavascriptCallbackFacts : IClassFixture + { + private readonly ITestOutputHelper output; + private readonly CefSharpFixture collectionFixture; + private readonly ChromiumWebBrowserOffScreenFixture classFixture; + + public JavascriptCallbackFacts(ITestOutputHelper output, CefSharpFixture collectionFixture, ChromiumWebBrowserOffScreenFixture classFixture) + { + this.output = output; + this.collectionFixture = collectionFixture; + this.classFixture = classFixture; + } + + [Theory] + [InlineData(double.MaxValue, "Number.MAX_VALUE")] + [InlineData(double.MaxValue / 2, "Number.MAX_VALUE / 2")] + //https://github.com/cefsharp/CefSharp/issues/3858 + public async Task CanEvaluateDoubleComputation(double expectedValue, string script) + { + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function() { return " + script + "})"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(); + + Assert.True(callbackResponse.Success); + + Assert.Equal(expectedValue, (double)callbackResponse.Result, 5); + + output.WriteLine("Script {0} : Result {1}", script, callbackResponse.Result); + } + + [Theory] + [InlineData(0.5d)] + [InlineData(1.5d)] + [InlineData(-0.5d)] + [InlineData(-1.5d)] + [InlineData(100000.24500d)] + [InlineData(-100000.24500d)] + [InlineData((double)uint.MaxValue)] + [InlineData((double)int.MaxValue + 1)] + [InlineData((double)int.MaxValue + 10)] + [InlineData((double)int.MinValue - 1)] + [InlineData((double)int.MinValue - 10)] + [InlineData(((double)uint.MaxValue * 2))] + [InlineData(((double)uint.MaxValue * 2) + 0.1)] + //https://github.com/cefsharp/CefSharp/issues/3858 + public async Task CanEvaluateDoubleValues(double num) + { + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function() { return " + num + "})"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(); + + Assert.Equal(num, (double)callbackResponse.Result, 5); + + output.WriteLine("Expected {0} : Actual {1}", num, callbackResponse.Result); + } + + [Theory] + [InlineData(0)] + [InlineData(-1)] + [InlineData(100)] + [InlineData(int.MaxValue)] + [InlineData(int.MinValue)] + //https://github.com/cefsharp/CefSharp/issues/3858 + public async Task CanEvaluateIntValues(object num) + { + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function() { return " + num.ToString() + "})"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(); + + Assert.Equal(num, callbackResponse.Result); + + output.WriteLine("Expected {0} : Actual {1}", num, callbackResponse.Result); + } + + [Theory] + [InlineData("1970-01-01", "1970-01-01")] + [InlineData("1980-01-01", "1980-01-01")] + //https://github.com/cefsharp/CefSharp/issues/4234 + public async Task CanEvaluateDateValues(DateTime expected, string actual) + { + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function() { return new Date('" + actual + "');})"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(); + + var actualDateTime = (DateTime)callbackResponse.Result; + + Assert.Equal(expected.ToLocalTime(), actualDateTime); + + output.WriteLine("Expected {0} : Actual {1}", expected.ToLocalTime(), actualDateTime); + } + + [Theory] + [InlineData("1970-01-01", DateTimeStyles.AssumeLocal)] + [InlineData("1970-01-01", DateTimeStyles.AssumeUniversal)] + [InlineData("1980-01-01", DateTimeStyles.AssumeLocal)] + [InlineData("1980-01-01", DateTimeStyles.AssumeUniversal)] + //https://github.com/cefsharp/CefSharp/issues/4234 + public async Task CanEchoDateTime(string expected, DateTimeStyles dateTimeStyle) + { + var expectedDateTime = DateTime.Parse(expected, CultureInfo.InvariantCulture, dateTimeStyle); + + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function(p) { return p; })"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(expectedDateTime); + + var actualDateTime = (DateTime)callbackResponse.Result; + + Assert.Equal(expectedDateTime, actualDateTime); + + output.WriteLine("Expected {0} : Actual {1}", expectedDateTime, actualDateTime); + } + + [Fact] + //https://github.com/cefsharp/CefSharp/issues/4234 + public async Task CanEchoDateTimeNow() + { + var expectedDateTime = DateTime.Now; + + var browser = classFixture.Browser; + + Assert.False(browser.IsLoading); + + var javascriptResponse = await browser.EvaluateScriptAsync("(function(p) { return p; })"); + Assert.True(javascriptResponse.Success); + + var callback = (IJavascriptCallback)javascriptResponse.Result; + + var callbackResponse = await callback.ExecuteAsync(expectedDateTime); + + var actualDateTime = (DateTime)callbackResponse.Result; + + Assert.Equal(expectedDateTime, actualDateTime, TimeSpan.FromMilliseconds(1)); + + output.WriteLine("Expected {0} : Actual {1}", expectedDateTime, actualDateTime); + } + } +} diff --git a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj index f48cc9a4e..01f79f9db 100644 --- a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj +++ b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj @@ -17,9 +17,7 @@ - - UserControl - + diff --git a/CefSharp/Internals/CefTimeUtils.cs b/CefSharp/Internals/CefTimeUtils.cs index 02384cb72..393895519 100644 --- a/CefSharp/Internals/CefTimeUtils.cs +++ b/CefSharp/Internals/CefTimeUtils.cs @@ -7,12 +7,10 @@ using System; namespace CefSharp.Internals { /// - /// Mapping to/from CefTime/CefBaseTime + /// Mapping to/from CefBaseTime /// public static class CefTimeUtils { - private static DateTime FirstOfTheFirstNineteenSeventy = new DateTime(1970, 1, 1, 0, 0, 0); - /// /// Converts from CefBaseTime to DateTime? /// @@ -53,55 +51,9 @@ namespace CefSharp.Internals /// /// Represents a wall clock time in UTC. Time as microseconds since the Windows epoch (1601). /// - public static long FromDateTimeBaseTime(DateTime dateTime) + public static long FromDateTimeToBaseTime(DateTime dateTime) { - return dateTime.ToFileTimeUtc() / 10; - } - - //dateTime.ToFileTimeUtc() / 10 - - public static DateTime ConvertCefTimeToDateTime(double epoch) - { - if (epoch == 0) - { - return DateTime.MinValue; - } - return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime(); - } - - /// - /// Converts a cef - /// - /// year - /// month - /// day - /// hour - /// minute - /// second - /// millisecond - /// DateTime - public static DateTime FromCefTime(int year, int month, int day, int hour, int minute, int second, int millisecond) - { - try - { - return new DateTime(year, month, day, hour, minute, second, millisecond); - } - catch (Exception) - { - return DateTime.MinValue; - } - } - - /// - /// Returns epoch (different from 01/01/1970) - /// - /// datetime - /// epoch - public static double ToCefTime(DateTime dateTime) - { - var timeSpan = dateTime - FirstOfTheFirstNineteenSeventy; - - return timeSpan.TotalSeconds; + return dateTime.ToUniversalTime().ToFileTime() / 10; } } }