2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2015 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;
|
2018-03-22 09:00:52 +08:00
|
|
|
using System.Collections;
|
2017-12-21 13:00:36 +10:00
|
|
|
using System.Collections.Generic;
|
2015-08-03 11:37:49 +10:00
|
|
|
using System.Diagnostics;
|
2019-06-08 17:21:49 +10:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2017-12-21 13:00:36 +10:00
|
|
|
using System.Text;
|
2015-07-29 13:39:07 +02:00
|
|
|
using System.Threading;
|
2019-06-08 17:21:49 +10:00
|
|
|
using System.Threading.Tasks;
|
2019-07-20 12:11:43 +10:00
|
|
|
using CefSharp.Web;
|
2015-07-29 13:39:07 +02:00
|
|
|
|
2018-10-29 16:21:42 +10:00
|
|
|
namespace CefSharp.Example.JavascriptBinding
|
2015-07-29 13:39:07 +02:00
|
|
|
{
|
|
|
|
|
public class AsyncBoundObject
|
|
|
|
|
{
|
2015-08-03 11:37:49 +10:00
|
|
|
//We expect an exception here, so tell VS to ignore
|
|
|
|
|
[DebuggerHidden]
|
2015-07-29 13:39:07 +02:00
|
|
|
public void Error()
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("This is an exception coming from C#");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 11:37:49 +10:00
|
|
|
//We expect an exception here, so tell VS to ignore
|
|
|
|
|
[DebuggerHidden]
|
2015-07-29 13:39:07 +02:00
|
|
|
public int Div(int divident, int divisor)
|
|
|
|
|
{
|
|
|
|
|
return divident / divisor;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 15:16:57 +10:00
|
|
|
public string DivWithBlockingTaskCall(int dividend, int divisor)
|
|
|
|
|
{
|
|
|
|
|
var taskToWaitOn = ExecuteTaskBeforeDivision(dividend, divisor);
|
|
|
|
|
taskToWaitOn.Wait();
|
|
|
|
|
return taskToWaitOn.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> ExecuteTaskBeforeDivision(int dividend, int divisor)
|
|
|
|
|
{
|
|
|
|
|
await RunAsync();
|
|
|
|
|
return (dividend / divisor).ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task RunAsync()
|
|
|
|
|
{
|
|
|
|
|
return Task.Run(() => Run());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Run()
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("AsyncBoundObject Run execution.");
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 13:39:07 +02:00
|
|
|
public string Hello(string name)
|
|
|
|
|
{
|
|
|
|
|
return "Hello " + name;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 13:00:36 +10:00
|
|
|
public string DoSomething()
|
2015-07-29 13:39:07 +02:00
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
2017-12-21 13:00:36 +10:00
|
|
|
|
|
|
|
|
return "Waited for 1000ms before returning";
|
2015-07-29 13:39:07 +02:00
|
|
|
}
|
2017-03-13 11:37:55 +00:00
|
|
|
|
2018-01-08 15:12:52 +10:00
|
|
|
public JsSerializableStruct ReturnObject(string name)
|
2017-10-23 14:24:04 +05:00
|
|
|
{
|
2018-01-08 15:12:52 +10:00
|
|
|
return new JsSerializableStruct
|
2017-10-23 14:24:04 +05:00
|
|
|
{
|
|
|
|
|
Value = name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 12:20:02 +10:00
|
|
|
public JsSerializableClass ReturnClass(string name)
|
|
|
|
|
{
|
|
|
|
|
return new JsSerializableClass
|
|
|
|
|
{
|
|
|
|
|
Value = name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 12:11:43 +10:00
|
|
|
public JsonString ReturnClassAsJsonString(string name)
|
|
|
|
|
{
|
|
|
|
|
return JsonString.FromObject(new JsSerializableClass { Value = name });
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-08 15:12:52 +10:00
|
|
|
public JsSerializableStruct[] ReturnStructArray(string name)
|
2017-03-13 11:37:55 +00:00
|
|
|
{
|
2018-09-10 02:41:13 +02:00
|
|
|
return new[]
|
2017-03-13 11:37:55 +00:00
|
|
|
{
|
2018-01-08 15:12:52 +10:00
|
|
|
new JsSerializableStruct { Value = name + "Item1" },
|
|
|
|
|
new JsSerializableStruct { Value = name + "Item2" }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JsSerializableClass[] ReturnClassesArray(string name)
|
|
|
|
|
{
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
new JsSerializableClass { Value = name + "Item1" },
|
|
|
|
|
new JsSerializableClass { Value = name + "Item2" }
|
2017-03-13 11:37:55 +00:00
|
|
|
};
|
|
|
|
|
}
|
2017-12-21 13:00:36 +10:00
|
|
|
|
2022-10-04 13:31:47 +10:00
|
|
|
public DateTime EchoDateTime(DateTime arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime? EchoNullableDateTime(DateTime? arg0)
|
|
|
|
|
{
|
|
|
|
|
return arg0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
public string[] EchoArray(string[] arg)
|
2018-01-18 01:46:58 +00:00
|
|
|
{
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
public int[] EchoValueTypeArray(int[] arg)
|
2018-01-18 01:46:58 +00:00
|
|
|
{
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
public int[][] EchoMultidimensionalArray(int[][] arg)
|
2018-01-18 01:46:58 +00:00
|
|
|
{
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 13:00:36 +10:00
|
|
|
public string DynamiObjectList(IList<dynamic> objects)
|
|
|
|
|
{
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
foreach (var browser in objects)
|
2017-12-21 13:00:36 +10:00
|
|
|
{
|
|
|
|
|
builder.Append("Browser(Name:" + browser.Name + ";Engine:" + browser.Engine.Name + ");");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
2018-03-22 09:00:52 +08:00
|
|
|
|
2018-06-24 18:20:42 -07:00
|
|
|
public List<string> MethodReturnsList()
|
|
|
|
|
{
|
|
|
|
|
return new List<string>()
|
|
|
|
|
{
|
|
|
|
|
"Element 0 - First",
|
|
|
|
|
"Element 1",
|
|
|
|
|
"Element 2 - Last",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<List<string>> MethodReturnsListOfLists()
|
|
|
|
|
{
|
|
|
|
|
return new List<List<string>>()
|
|
|
|
|
{
|
|
|
|
|
new List<string>() {"Element 0, 0", "Element 0, 1" },
|
|
|
|
|
new List<string>() {"Element 1, 0", "Element 1, 1" },
|
|
|
|
|
new List<string>() {"Element 2, 0", "Element 2, 1" },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 09:00:52 +08:00
|
|
|
public Dictionary<string, int> MethodReturnsDictionary1()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, int>()
|
|
|
|
|
{
|
|
|
|
|
{"five", 5},
|
|
|
|
|
{"ten", 10}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, object> MethodReturnsDictionary2()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{"onepointfive", 1.5},
|
|
|
|
|
{"five", 5},
|
|
|
|
|
{"ten", "ten"},
|
|
|
|
|
{"twotwo", new Int32[]{2, 2} }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, IDictionary> MethodReturnsDictionary3()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, IDictionary>()
|
|
|
|
|
{
|
|
|
|
|
{"data", MethodReturnsDictionary2()}
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-06-08 17:21:49 +10:00
|
|
|
|
2021-02-26 11:35:26 +10:00
|
|
|
public Tuple<bool, string> PassSimpleClassAsArgument(SimpleClass simpleClass)
|
|
|
|
|
{
|
|
|
|
|
if (simpleClass == null)
|
|
|
|
|
{
|
|
|
|
|
return Tuple.Create(false, "PassSimpleClassAsArgument dictionary param is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Tuple.Create(true, "TestString:" + simpleClass.TestString + ";SubClasses[0].PropertyOne:" + simpleClass.SubClasses[0].PropertyOne);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 17:21:49 +10:00
|
|
|
//The Following Test methods can only be used when
|
|
|
|
|
//CefSharpSettings.ConcurrentTaskExecution = true;
|
|
|
|
|
//There is a seperate set of QUnit tests for these
|
|
|
|
|
|
|
|
|
|
public Task<string> ReturnTaskStringAsync()
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(nameof(ReturnTaskStringAsync));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void VoidReturnAsync()
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("Delayed 1 second.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 13:43:51 +10:00
|
|
|
public async Task<string> JavascriptCallbackEvalPromise(string msg, IJavascriptCallback callback)
|
|
|
|
|
{
|
2023-01-30 11:35:43 +10:00
|
|
|
var response = await callback.ExecuteAsync(msg);
|
2021-02-15 13:43:51 +10:00
|
|
|
|
|
|
|
|
//Echo the response
|
|
|
|
|
return (string)response.Result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 14:17:50 +11:00
|
|
|
public async Task WaitBeforeReturnAsync(int milliseconds)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(milliseconds);
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("Delayed in ms:" + milliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 17:21:49 +10:00
|
|
|
public async Task<string> AsyncWaitTwoSeconds(string str)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(2000);
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string[]> AsyncDownloadFileAndSplitOnNewLines(string url)
|
|
|
|
|
{
|
|
|
|
|
var webClient = new WebClient();
|
|
|
|
|
var download = await webClient.DownloadStringTaskAsync(new Uri(url));
|
|
|
|
|
|
|
|
|
|
var lines = download.Split('\n').Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//We expect an exception here, so tell VS to ignore
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
public async Task<string> AsyncThrowException()
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(2000);
|
|
|
|
|
|
|
|
|
|
throw new Exception("Expected Exception");
|
|
|
|
|
}
|
2019-09-27 06:46:48 +10:00
|
|
|
|
|
|
|
|
public uint UIntAddModel(UIntAddModel model)
|
|
|
|
|
{
|
|
|
|
|
return model.ParamA + model.ParamB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint UIntAdd(uint paramA, uint paramB)
|
|
|
|
|
{
|
|
|
|
|
return paramA + paramB;
|
|
|
|
|
}
|
2022-01-27 15:23:21 +10:00
|
|
|
|
|
|
|
|
public async Task<string> JavascriptOptionalCallbackEvalPromise(bool invokeCallback, string msg, IJavascriptCallback callback)
|
|
|
|
|
{
|
|
|
|
|
using (callback)
|
|
|
|
|
{
|
|
|
|
|
if (invokeCallback)
|
|
|
|
|
{
|
2023-01-30 11:35:43 +10:00
|
|
|
var response = await callback.ExecuteAsync(msg).ConfigureAwait(false);
|
2022-01-27 15:23:21 +10:00
|
|
|
//Echo the response
|
|
|
|
|
return (string)response.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-29 13:39:07 +02:00
|
|
|
}
|
|
|
|
|
}
|