Add project

This commit is contained in:
Diamond Creeper 2023-02-20 23:24:10 +13:00
commit 7fcb279842
961 changed files with 370491 additions and 0 deletions

View file

@ -0,0 +1,80 @@
// Copyright © 2020 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.Linq;
using System.Reflection;
namespace CefSharp.JavascriptBinding
{
/// <summary>
/// CamelCaseJavascriptNameConverter converts .Net property/method names
/// to camcel case.
/// </summary>
public class CamelCaseJavascriptNameConverter : IJavascriptNameConverter
{
string IJavascriptNameConverter.ConvertToJavascript(MemberInfo memberInfo)
{
return ConvertToJavascript(memberInfo);
}
/// <summary>
/// Get the javascript name for the property/field/method.
/// Typically this would be based on <see cref="MemberInfo.Name"/>
/// </summary>
/// <param name="memberInfo">property/field/method</param>
/// <returns>javascript name</returns>
protected virtual string ConvertToJavascript(MemberInfo memberInfo)
{
return ConvertMemberInfoNameToCamelCase(memberInfo);
}
string IJavascriptNameConverter.ConvertReturnedObjectPropertyAndFieldToNameJavascript(MemberInfo memberInfo)
{
return ConvertReturnedObjectPropertyAndFieldToNameJavascript(memberInfo);
}
/// <summary>
/// This method exists for backwards compatability reasons, historically
/// only the bound methods/fields/properties were converted. Objects returned
/// from a method call were not translated. To preserve this functionality
/// for upgrading users we split this into two methods. Typically thie method
/// would return the same result as <see cref="ConvertToJavascript(string)"/>
/// Issue #2442
/// </summary>
/// <param name="memberInfo">property/field/method</param>
/// <returns>javascript name</returns>
protected virtual string ConvertReturnedObjectPropertyAndFieldToNameJavascript(MemberInfo memberInfo)
{
return ConvertMemberInfoNameToCamelCase(memberInfo);
}
/// <summary>
/// Converts the <see cref="MemberInfo.Name"/> to CamelCase
/// </summary>
/// <param name="memberInfo">memberInfo</param>
/// <returns>camelcased name</returns>
protected static string ConvertMemberInfoNameToCamelCase(MemberInfo memberInfo)
{
var name = memberInfo.Name;
// camelCase says that if the string is only one character that it is preserved.
if (name.Length == 1)
{
return name;
}
// camelCase says that if the entire string is uppercase to preserve it.
//TODO: We need to cache these values to avoid the cost of validating this
if (name.All(char.IsUpper))
{
return name;
}
var firstHalf = name.Substring(0, 1);
var remainingHalf = name.Substring(1);
return firstHalf.ToLowerInvariant() + remainingHalf;
}
}
}

View file

@ -0,0 +1,35 @@
// Copyright © 2020 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.Reflection;
namespace CefSharp.JavascriptBinding
{
/// <summary>
/// Implement this interface to have control of how the names
/// are converted when binding/executing javascript.
/// </summary>
public interface IJavascriptNameConverter
{
/// <summary>
/// Get the javascript name for the property/field/method.
/// Typically this would be based on <see cref="MemberInfo.Name"/>
/// </summary>
/// <param name="memberInfo">property/field/method</param>
/// <returns>javascript name</returns>
string ConvertToJavascript(MemberInfo memberInfo);
/// <summary>
/// This method exists for backwards compatability reasons, historically
/// only the bound methods/fields/properties were converted. Objects returned
/// from a method call were not translated. To preserve this functionality
/// for upgrading users we split this into two methods. Typically thie method
/// would return the same result as <see cref="ConvertToJavascript(string)"/>
/// Issue #2442
/// </summary>
/// <param name="memberInfo">property/field/method</param>
/// <returns>javascript name</returns>
string ConvertReturnedObjectPropertyAndFieldToNameJavascript(MemberInfo memberInfo);
}
}

View file

@ -0,0 +1,54 @@
// Copyright © 2018 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.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CefSharp.Event;
namespace CefSharp
{
/// <summary>
/// Javascript binding extension methods
/// </summary>
public static class JavascriptBindingExtensions
{
/// <summary>
/// Make sure an object is bound in javascript. Executes against the main frame
/// </summary>
/// <param name="browser">browser</param>
/// <param name="names">object names</param>
/// <returns>List of objects that were bound</returns>
public static Task<IList<string>> EnsureObjectBoundAsync(this IWebBrowser browser, params string[] names)
{
var objBoundTasks = new TaskCompletionSource<IList<string>>();
EventHandler<JavascriptBindingMultipleCompleteEventArgs> handler = null;
handler = (sender, args) =>
{
//Remove handler
browser.JavascriptObjectRepository.ObjectsBoundInJavascript -= handler;
var allObjectsBound = names.ToList().SequenceEqual(args.ObjectNames);
if (allObjectsBound)
{
objBoundTasks.SetResult(args.ObjectNames);
}
else
{
objBoundTasks.SetException(new Exception("Not all objects were bound successfully, bound objects were " + string.Join(",", args.ObjectNames)));
}
};
browser.JavascriptObjectRepository.ObjectsBoundInJavascript += handler;
var bindCommand = "(function() { CefSharp.BindObjectAsync({ NotifyIfAlreadyBound: true, IgnoreCache: false }, '" + string.Join("', '", names) + "'); })();";
browser.ExecuteScriptAsync(bindCommand);
return objBoundTasks.Task;
}
}
}

View file

@ -0,0 +1,91 @@
// Copyright © 2020 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 CefSharp.Internals;
namespace CefSharp.JavascriptBinding
{
/// <summary>
/// Javascript Binding Settings
/// </summary>
public class JavascriptBindingSettings : FreezableBase
{
private bool alwaysInterceptAsynchronously;
private bool legacyBindingEnabled;
private string jsBindingGlobalObjectName;
/// <summary>
/// The Javascript methods that CefSharp provides in relation to JavaScript Binding are
/// created using a Global (window) Object. Settings this property allows you to customise
/// the name of this object. If not specified then both cefSharp and CefSharp objects will
/// be created e.g. cefSharp.bindObjectAsync, CefSharp.BindObjectAsync.
/// If specified then your custom name will be used, if the name starts with a lowercase letter
/// then all the functions will be lowercase, e.g. myObjName.bindObjectAsync otherwise
/// the functions will start with a uppercase letter e.g. MyObjName.BindObjectAsync
/// </summary>
/// <remarks>
/// This object is also accessible through the window property. e.g. window.cefSharp.bindObjectAsync
/// </remarks>
public string JavascriptBindingApiGlobalObjectName
{
get { return jsBindingGlobalObjectName; }
set
{
ThrowIfFrozen();
if (!StringCheck.IsLettersAndNumbers(value))
{
//TODO: See if there's a better suited Exception class for this.
throw new System.Exception("invalid or illegal characters used for binding property names. Alphanumeric and underscores characters only.");
}
jsBindingGlobalObjectName = value;
}
}
/// <summary>
/// Objects registered using <see cref="IJavascriptObjectRepository.Register"/>
/// will be automatically bound when a V8Context is created. (Soon as the Javascript
/// context is created for a browser). This behaviour is like that seen with Javascript
/// Binding in version 57 and earlier.
/// </summary>
public bool LegacyBindingEnabled
{
get { return legacyBindingEnabled; }
set
{
ThrowIfFrozen();
legacyBindingEnabled = value;
}
}
/// <summary>
/// When using an <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor"/>
/// the <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor.InterceptAsync(System.Func{object[], object}, object[], string)"/>
/// method is call for all methods (the default is to call InterceptAsync only for methods that return a Task).
/// This only applies when <see cref="BindingOptions.MethodInterceptor"/> is of type <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor"/>
/// </summary>
public bool AlwaysInterceptAsynchronously
{
get { return alwaysInterceptAsynchronously; }
set
{
ThrowIfFrozen();
alwaysInterceptAsynchronously = value;
}
}
/// <summary>
/// Default Constructor
/// </summary>
public JavascriptBindingSettings()
{
//Default to CefSharpSettings.LegacyJavascriptBindingEnabled
//until it's eventually removed
LegacyBindingEnabled = CefSharpSettings.LegacyJavascriptBindingEnabled;
}
}
}

View file

@ -0,0 +1,28 @@
// Copyright © 2020 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.Reflection;
namespace CefSharp.JavascriptBinding
{
/// <summary>
/// Legacy Naming converter.
/// Used by default for backwards compatability
/// Issue #2442
/// </summary>
public class LegacyCamelCaseJavascriptNameConverter : IJavascriptNameConverter
{
string IJavascriptNameConverter.ConvertReturnedObjectPropertyAndFieldToNameJavascript(MemberInfo memberInfo)
{
return memberInfo.Name;
}
string IJavascriptNameConverter.ConvertToJavascript(MemberInfo memberInfo)
{
var name = memberInfo.Name;
return char.ToLowerInvariant(name[0]) + name.Substring(1);
}
}
}