2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2010 The CefSharp Authors. All rights reserved.
|
2015-08-03 10:41:29 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
2016-04-04 21:44:22 -07:00
|
|
|
using System.Linq;
|
2015-03-12 11:48:42 +10:00
|
|
|
using System.Threading.Tasks;
|
2012-02-24 17:03:55 -08:00
|
|
|
|
2018-10-29 16:21:42 +10:00
|
|
|
namespace CefSharp.Example.JavascriptBinding
|
2012-02-24 17:03:55 -08:00
|
|
|
{
|
2014-08-27 14:59:27 +10:00
|
|
|
public class BoundObject
|
2012-02-24 17:03:55 -08:00
|
|
|
{
|
2014-06-12 02:41:59 +03:00
|
|
|
public int MyProperty { get; set; }
|
2014-06-07 09:52:45 +02:00
|
|
|
|
2013-05-11 09:44:19 +03:00
|
|
|
public string MyReadOnlyProperty { get; internal set; }
|
2013-05-11 09:29:41 +03:00
|
|
|
public Type MyUnconvertibleProperty { get; set; }
|
2014-08-29 16:50:16 +10:00
|
|
|
public SubBoundObject SubObject { get; set; }
|
2015-09-08 11:20:31 +09:00
|
|
|
public ExceptionTestBoundObject ExceptionTestObject { get; set; }
|
2013-05-07 08:20:41 +03:00
|
|
|
|
2018-01-22 10:24:25 +10:00
|
|
|
public int this[int i]
|
|
|
|
|
{
|
|
|
|
|
get { return i; }
|
|
|
|
|
set { }
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 11:57:37 -07:00
|
|
|
public uint[] MyUintArray
|
|
|
|
|
{
|
|
|
|
|
get { return new uint[] { 7, 8 }; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int[] MyIntArray
|
|
|
|
|
{
|
2018-09-10 02:41:13 +02:00
|
|
|
get { return new[] { 1, 2, 3, 4, 5, 6, 7, 8 }; }
|
2015-04-24 11:57:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Array MyArray
|
|
|
|
|
{
|
|
|
|
|
get { return new short[] { 1, 2, 3 }; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] MyBytes
|
|
|
|
|
{
|
|
|
|
|
get { return new byte[] { 3, 4, 5 }; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 08:20:41 +03:00
|
|
|
public BoundObject()
|
|
|
|
|
{
|
|
|
|
|
MyProperty = 42;
|
|
|
|
|
MyReadOnlyProperty = "I'm immutable!";
|
2014-10-29 16:52:55 +10:00
|
|
|
IgnoredProperty = "I am an Ignored Property";
|
2013-05-11 09:29:41 +03:00
|
|
|
MyUnconvertibleProperty = GetType();
|
2014-08-29 16:50:16 +10:00
|
|
|
SubObject = new SubBoundObject();
|
2015-09-08 11:20:31 +09:00
|
|
|
ExceptionTestObject = new ExceptionTestBoundObject();
|
2013-05-07 08:20:41 +03:00
|
|
|
}
|
|
|
|
|
|
2019-02-28 20:25:23 +10:00
|
|
|
public void TestCallbackWithDateTime(IJavascriptCallback javascriptCallback)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = new DateTime(2019, 01, 01, 12, 00, 00);
|
|
|
|
|
await javascriptCallback.ExecuteAsync(dateTime, new[] { dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second });
|
|
|
|
|
}
|
2019-02-28 20:25:23 +10:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 19:15:50 +08:00
|
|
|
public void TestCallbackWithDateTime1900(IJavascriptCallback javascriptCallback)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = new DateTime(1900, 01, 01, 12, 00, 00);
|
|
|
|
|
await javascriptCallback.ExecuteAsync(dateTime, new[] { dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second });
|
|
|
|
|
}
|
2019-03-20 19:15:50 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestCallbackWithDateTime1970(IJavascriptCallback javascriptCallback)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = new DateTime(1970, 01, 01, 12, 00, 00);
|
|
|
|
|
await javascriptCallback.ExecuteAsync(dateTime, new[] { dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second });
|
|
|
|
|
}
|
2019-03-20 19:15:50 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestCallbackWithDateTime1985(IJavascriptCallback javascriptCallback)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = new DateTime(1985, 01, 01, 12, 00, 00);
|
|
|
|
|
await javascriptCallback.ExecuteAsync(dateTime, new[] { dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second });
|
|
|
|
|
}
|
2019-03-20 19:15:50 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-04-26 08:31:33 +10:00
|
|
|
|
2015-03-12 11:48:42 +10:00
|
|
|
public void TestCallback(IJavascriptCallback javascriptCallback)
|
2015-03-08 16:23:49 +01:00
|
|
|
{
|
2015-03-12 11:48:42 +10:00
|
|
|
const int taskDelay = 1500;
|
|
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
2015-03-08 16:23:49 +01:00
|
|
|
{
|
2015-03-12 11:48:42 +10:00
|
|
|
await Task.Delay(taskDelay);
|
|
|
|
|
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
//NOTE: Classes are not supported, simple structs are
|
|
|
|
|
var response = new CallbackResponseStruct("This callback from C# was delayed " + taskDelay + "ms");
|
|
|
|
|
await javascriptCallback.ExecuteAsync(response);
|
|
|
|
|
}
|
2015-03-12 11:48:42 +10:00
|
|
|
}
|
|
|
|
|
});
|
2015-03-08 16:23:49 +01:00
|
|
|
}
|
|
|
|
|
|
2016-07-20 13:38:24 +10:00
|
|
|
public string TestCallbackFromObject(SimpleClass simpleClass)
|
2016-06-30 18:17:39 -04:00
|
|
|
{
|
2016-07-20 13:38:24 +10:00
|
|
|
if (simpleClass == null)
|
2016-06-30 18:17:39 -04:00
|
|
|
{
|
2016-07-06 11:06:00 +10:00
|
|
|
return "TestCallbackFromObject dictionary param is null";
|
|
|
|
|
}
|
2016-06-30 18:17:39 -04:00
|
|
|
|
2016-07-20 13:38:24 +10:00
|
|
|
IJavascriptCallback javascriptCallback = simpleClass.Callback;
|
2016-07-06 11:06:00 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (javascriptCallback == null)
|
2016-07-06 11:06:00 +10:00
|
|
|
{
|
|
|
|
|
return "callback property not found or property is not a function";
|
2016-06-30 18:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int taskDelay = 1500;
|
|
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(taskDelay);
|
|
|
|
|
|
|
|
|
|
if (javascriptCallback != null)
|
|
|
|
|
{
|
|
|
|
|
using (javascriptCallback)
|
|
|
|
|
{
|
2019-04-26 08:31:33 +10:00
|
|
|
if (javascriptCallback.CanExecute)
|
|
|
|
|
{
|
|
|
|
|
await javascriptCallback.ExecuteAsync("message from C# " + simpleClass.TestString + " - " + simpleClass.SubClasses[0].PropertyOne);
|
|
|
|
|
}
|
2016-06-30 18:17:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return "waiting for callback execution...";
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-11 17:00:17 +10:00
|
|
|
public int EchoMyProperty()
|
|
|
|
|
{
|
|
|
|
|
return MyProperty;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-24 17:03:55 -08:00
|
|
|
public string Repeat(string str, int n)
|
|
|
|
|
{
|
|
|
|
|
string result = String.Empty;
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
|
{
|
|
|
|
|
result += str;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 12:54:38 +10:00
|
|
|
public string EchoParamOrDefault(string param = "This is the default value")
|
|
|
|
|
{
|
|
|
|
|
return param;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-24 17:03:55 -08:00
|
|
|
public void EchoVoid()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean EchoBoolean(Boolean arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean? EchoNullableBoolean(Boolean? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SByte EchoSByte(SByte arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SByte? EchoNullableSByte(SByte? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int16 EchoInt16(Int16 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int16? EchoNullableInt16(Int16? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int32 EchoInt32(Int32 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int32? EchoNullableInt32(Int32? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int64 EchoInt64(Int64 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Int64? EchoNullableInt64(Int64? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Byte EchoByte(Byte arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Byte? EchoNullableByte(Byte? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt16 EchoUInt16(UInt16 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt16? EchoNullableUInt16(UInt16? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt32 EchoUInt32(UInt32 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt32? EchoNullableUInt32(UInt32? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt64 EchoUInt64(UInt64 arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UInt64? EchoNullableUInt64(UInt64? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Single EchoSingle(Single arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Single? EchoNullableSingle(Single? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Double EchoDouble(Double arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Double? EchoNullableDouble(Double? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Char EchoChar(Char arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Char? EchoNullableChar(Char? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime EchoDateTime(DateTime arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime? EchoNullableDateTime(DateTime? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Decimal EchoDecimal(Decimal arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Decimal? EchoNullableDecimal(Decimal? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String EchoString(String arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
2013-06-05 14:15:37 +03:00
|
|
|
|
2014-06-12 02:41:59 +03:00
|
|
|
// TODO: This will currently not work, as it causes a collision w/ the EchoString() method. We need to find a way around that I guess.
|
2013-06-05 14:15:37 +03:00
|
|
|
//public String echoString(String arg)
|
|
|
|
|
//{
|
|
|
|
|
// return "Lowercase echo: " + arg;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public String lowercaseMethod()
|
|
|
|
|
{
|
|
|
|
|
return "lowercase";
|
|
|
|
|
}
|
2014-10-29 16:52:55 +10:00
|
|
|
|
2014-12-16 21:42:09 +10:00
|
|
|
public string ReturnJsonEmployeeList()
|
|
|
|
|
{
|
|
|
|
|
return "{\"employees\":[{\"firstName\":\"John\", \"lastName\":\"Doe\"},{\"firstName\":\"Anna\", \"lastName\":\"Smith\"},{\"firstName\":\"Peter\", \"lastName\":\"Jones\"}]}";
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 16:52:55 +10:00
|
|
|
[JavascriptIgnore]
|
|
|
|
|
public string IgnoredProperty { get; set; }
|
|
|
|
|
|
|
|
|
|
[JavascriptIgnore]
|
|
|
|
|
public string IgnoredMethod()
|
|
|
|
|
{
|
|
|
|
|
return "I am an Ignored Method";
|
|
|
|
|
}
|
2015-01-06 20:47:30 +10:00
|
|
|
|
|
|
|
|
public string ComplexParamObject(object param)
|
|
|
|
|
{
|
|
|
|
|
if (param == null)
|
|
|
|
|
{
|
|
|
|
|
return "param is null";
|
|
|
|
|
}
|
|
|
|
|
return "The param type is:" + param.GetType();
|
|
|
|
|
}
|
2015-01-08 15:03:24 +10:00
|
|
|
|
|
|
|
|
public SubBoundObject GetSubObject()
|
|
|
|
|
{
|
|
|
|
|
return SubObject;
|
|
|
|
|
}
|
2016-04-04 21:44:22 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Demonstrates the use of params as an argument in a bound object
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Dummy Argument</param>
|
|
|
|
|
/// <param name="args">Params Argument</param>
|
|
|
|
|
public string MethodWithParams(string name, params object[] args)
|
|
|
|
|
{
|
2016-04-05 16:19:55 +10:00
|
|
|
return "Name:" + name + ";Args:" + string.Join(", ", args.ToArray());
|
2016-04-04 21:44:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string MethodWithoutParams(string name, string arg2)
|
|
|
|
|
{
|
2016-04-05 16:19:55 +10:00
|
|
|
return string.Format("{0}, {1}", name, arg2);
|
2016-04-04 21:44:22 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-05 16:19:55 +10:00
|
|
|
public string MethodWithoutAnything()
|
2016-04-04 21:44:22 -07:00
|
|
|
{
|
|
|
|
|
return "Method without anything called and returned successfully.";
|
|
|
|
|
}
|
2016-04-29 18:10:43 +10:00
|
|
|
|
|
|
|
|
public string MethodWithThreeParamsOneOptionalOneArray(string name, string optionalParam = null, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
return "MethodWithThreeParamsOneOptionalOneArray:" + (name ?? "No Name Specified") + " - " + (optionalParam ?? "No Optional Param Specified") + ";Args:" + string.Join(", ", args.ToArray());
|
|
|
|
|
}
|
2012-02-24 17:03:55 -08:00
|
|
|
}
|
|
|
|
|
}
|