mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
9b01b69d2f
* JSB Rewrite - Add CefSharp.RegisterBoundObject javascript method To get around the problem of IPC timing, I've reversed the communication, now it's the render process requesting the bound objects, the implementation is incomplete at this stage, more just a basic proof of concept Rewrite BindingTest.html using QUnit One of the tests doesn't map correctly as it requires def user interaction TODO: - Get objects by name - Rename Messages.h values that relate to this change so they are more descriptive - Better error handling - Caching of objects within a render process - Investigate global context options, don't think it's possible to call an async method directly in the global context # Conflicts: # CefSharp.Example/Resources/BindingTest.html * JSB Improve Request/Response message names * BindingTest.html - Output stress test results * Remove JavascriptRootObject was leftover from time we used WCF to transmit objects * JavascriptObjectRepository - Objects are now sent in the same list with an IsAsync flag Separation when sending over IPC with two unique lists isn't necessary * JavascriptObjectRepository - Return objects by name or return all by default Untabify CefAppUnmanagedWrapper.cpp Add minor comment to CefBrowserWrapper for a reminder that we use Frame Identifier as dictionary key * Add //TODO: JSB comments for work that's left to be done Ideally within a render process we cache bound objects once they've been communicated, It would also be nice to make multiple binding requests so objects can later be added dynamically * JSB Allow multiple calls to CefSharp.RegisterBoundObject Update BindingTest.html to name two distinct calls to different bound objects * JSB - Add some notes about caching, no working implementation yet * JSB - Dynamically Register object on Request * JSB Add ability to unbind an object and rename RegisterBoundObject to BindObjectAsync # Conflicts: # CefSharp.Example/Resources/BindingTest.html * JSB BindObjectAsync - if objects already bound then return false * JSB - Ignore Indexer properties Were previously throwing an exception * JSB - Add LegacyJavascriptBindingEnabled option so preserve existing behaviour This is only useful for SPA application and those that only navigate to pages hosting within a single domain. Any sort of cross-site navigation and a new render process will be spawned and objects won't be bound automatically (You can use the new methods to request they're bound yourself, at least in theory, more testing required on this) * Add LegacyBindingTest.html Current disabled by default. To enable uncomment CefSharpSettings.LegacyJavascriptBindingEnabled = true; in CefExample.cs * RegisterJsObject and RegisterAsyncJsObject can now only be used when CefSharpSettings.LegacyJavascriptBindingEnabled = true See https://github.com/cefsharp/CefSharp/issues/2246 for details
469 lines
19 KiB
C#
469 lines
19 KiB
C#
// Copyright © 2010-2017 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.Reflection;
|
|
using System.Threading;
|
|
using CefSharp.ModelBinding;
|
|
using CefSharp.Event;
|
|
|
|
namespace CefSharp.Internals
|
|
{
|
|
/// <summary>
|
|
/// This class manages the registration of objects in the browser
|
|
/// process to be exposed to JavaScript in the renderer process.
|
|
/// Registration performs method, parameter, property type analysis
|
|
/// of the registered objects into meta-data tied to reflection data
|
|
/// for later use.
|
|
///
|
|
/// This class also is the adaptation layer between the BrowserProcessService
|
|
/// and the registered objects. This means when the renderer wants to call an
|
|
/// exposed method, get a property of an object, or
|
|
/// set a property of an object in the browser process, that this
|
|
/// class does deals with the previously created meta-data and invokes the correct
|
|
/// behavior via reflection APIs.
|
|
///
|
|
/// All of the registered objects are tracked via meta-data for the objects
|
|
/// expressed starting with the JavaScriptObject type.
|
|
/// </summary>
|
|
public class JavascriptObjectRepository : IJavascriptObjectRepository
|
|
{
|
|
private static long lastId;
|
|
|
|
public event EventHandler<JavascriptBindingEventArgs> ResolveObject;
|
|
|
|
/// <summary>
|
|
/// A hash from assigned object ids to the objects,
|
|
/// this is done to speed up finding the object in O(1) time
|
|
/// instead of traversing the JavaScriptRootObject tree.
|
|
/// </summary>
|
|
private readonly Dictionary<long, JavascriptObject> objects = new Dictionary<long, JavascriptObject>();
|
|
|
|
public void Dispose()
|
|
{
|
|
ResolveObject = null;
|
|
}
|
|
|
|
public bool HasBoundObjects
|
|
{
|
|
get { return objects.Count > 0; }
|
|
}
|
|
|
|
public bool IsBound(string name)
|
|
{
|
|
return objects.Values.Any(x => x.Name == name);
|
|
}
|
|
|
|
public List<JavascriptObject> GetObjects(List<string> names = null)
|
|
{
|
|
if (names == null || names.Count == 0)
|
|
{
|
|
//TODO: JSB Declare Constant for All
|
|
RaiseResolveObjectEvent("All");
|
|
|
|
return objects.Values.ToList();
|
|
}
|
|
|
|
foreach (var name in names)
|
|
{
|
|
if (!IsBound(name))
|
|
{
|
|
RaiseResolveObjectEvent(name);
|
|
}
|
|
}
|
|
|
|
return objects.Values.Where(x => names.Contains(x.JavascriptName)).ToList();
|
|
}
|
|
|
|
private JavascriptObject CreateJavascriptObject(bool camelCaseJavascriptNames)
|
|
{
|
|
var id = Interlocked.Increment(ref lastId);
|
|
|
|
var result = new JavascriptObject { Id = id, CamelCaseJavascriptNames = camelCaseJavascriptNames };
|
|
objects[id] = result;
|
|
|
|
return result;
|
|
}
|
|
|
|
public void Register(string name, object value, bool isAsync, BindingOptions options)
|
|
{
|
|
if (!CefSharpSettings.WcfEnabled && !isAsync)
|
|
{
|
|
throw new InvalidOperationException("To enable synchronous JS bindings set WcfEnabled true in CefSettings during initialization.");
|
|
}
|
|
|
|
//Validation name is unique
|
|
if(objects.Values.Count(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)) > 0)
|
|
{
|
|
throw new ArgumentException("Object already bound with name:" + name , name);
|
|
}
|
|
|
|
var camelCaseJavascriptNames = options == null ? true : options.CamelCaseJavascriptNames;
|
|
var jsObject = CreateJavascriptObject(camelCaseJavascriptNames);
|
|
jsObject.Value = value;
|
|
jsObject.Name = name;
|
|
jsObject.JavascriptName = name;
|
|
jsObject.IsAsync = isAsync;
|
|
jsObject.Binder = options == null ? null : options.Binder;
|
|
jsObject.MethodInterceptor = options == null ? null : options.MethodInterceptor;
|
|
|
|
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: isAsync, readPropertyValue: false, camelCaseJavascriptNames: camelCaseJavascriptNames);
|
|
}
|
|
|
|
public bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
|
|
{
|
|
exception = "";
|
|
result = null;
|
|
JavascriptObject obj;
|
|
if (!objects.TryGetValue(objectId, out obj))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var method = obj.Methods.FirstOrDefault(p => p.JavascriptName == name);
|
|
if (method == null)
|
|
{
|
|
throw new InvalidOperationException(string.Format("Method {0} not found on Object of Type {1}", name, obj.Value.GetType()));
|
|
}
|
|
|
|
try
|
|
{
|
|
//Check if the bound object method contains a ParamArray as the last parameter on the method signature.
|
|
//NOTE: No additional parameters are permitted after the params keyword in a method declaration,
|
|
//and only one params keyword is permitted in a method declaration.
|
|
//https://msdn.microsoft.com/en-AU/library/w5zay9db.aspx
|
|
if (method.HasParamArray)
|
|
{
|
|
var paramList = new List<object>(method.Parameters.Count);
|
|
|
|
//Loop through all of the method parameters on the bound object.
|
|
for (var i = 0; i < method.Parameters.Count; i++)
|
|
{
|
|
//If the method parameter is a paramArray IE: (params string[] args)
|
|
//grab the parameters from the javascript function starting at the current bound object parameter index
|
|
//and add create an array that will be passed in as the last bound object method parameter.
|
|
if (method.Parameters[i].IsParamArray)
|
|
{
|
|
var convertedParams = new List<object>();
|
|
for (var s = i; s < parameters.Length; s++)
|
|
{
|
|
convertedParams.Add(parameters[s]);
|
|
}
|
|
paramList.Add(convertedParams.ToArray());
|
|
}
|
|
else
|
|
{
|
|
var jsParam = parameters.ElementAtOrDefault(i);
|
|
paramList.Add(jsParam);
|
|
}
|
|
}
|
|
|
|
parameters = paramList.ToArray();
|
|
}
|
|
|
|
//Check for parameter count missmatch between the parameters on the javascript function and the
|
|
//number of parameters on the bound object method. (This is relevant for methods that have default values)
|
|
//NOTE it's possible to have default params and a paramArray, so check missing params last
|
|
var missingParams = method.ParameterCount - parameters.Length;
|
|
|
|
if(missingParams > 0)
|
|
{
|
|
var paramList = new List<object>(parameters);
|
|
|
|
for (var i = 0; i < missingParams; i++)
|
|
{
|
|
paramList.Add(Type.Missing);
|
|
}
|
|
|
|
parameters = paramList.ToArray();
|
|
}
|
|
|
|
try
|
|
{
|
|
if(obj.Binder != null)
|
|
{
|
|
for (var i = 0; i < parameters.Length; i++)
|
|
{
|
|
if(parameters[i] != null)
|
|
{
|
|
var paramExpectedType = method.Parameters[i].Type;
|
|
var paramType = parameters[i].GetType();
|
|
if (typeof(IDictionary<string, object>).IsAssignableFrom(paramType))
|
|
{
|
|
var dictionary = (IDictionary<string, object>)parameters[i];
|
|
parameters[i] = obj.Binder.Bind(dictionary, paramExpectedType);
|
|
}
|
|
else if (typeof(IList<object>).IsAssignableFrom(paramType))
|
|
{
|
|
var list = (IList<object>)parameters[i];
|
|
parameters[i] = obj.Binder.Bind(list, paramExpectedType);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (obj.MethodInterceptor == null)
|
|
{
|
|
result = method.Function(obj.Value, parameters);
|
|
}
|
|
else
|
|
{
|
|
result = obj.MethodInterceptor.Intercept(() => method.Function(obj.Value, parameters), method.ManagedName);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new InvalidOperationException("Could not execute method: " + name + "(" + String.Join(", ", parameters) + ") " + (missingParams > 0 ? "- Missing Parameters: " + missingParams : ""), e);
|
|
}
|
|
|
|
if(result != null && IsComplexType(result.GetType()))
|
|
{
|
|
var jsObject = CreateJavascriptObject(obj.CamelCaseJavascriptNames);
|
|
jsObject.Value = result;
|
|
jsObject.Name = "FunctionResult(" + name + ")";
|
|
jsObject.JavascriptName = jsObject.Name;
|
|
|
|
AnalyseObjectForBinding(jsObject, analyseMethods: false, analyseProperties:true, readPropertyValue: true, camelCaseJavascriptNames: obj.CamelCaseJavascriptNames);
|
|
|
|
result = jsObject;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch(TargetInvocationException e)
|
|
{
|
|
var baseException = e.GetBaseException();
|
|
exception = baseException.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex.ToString();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool TryGetProperty(long objectId, string name, out object result, out string exception)
|
|
{
|
|
exception = "";
|
|
result = null;
|
|
JavascriptObject obj;
|
|
if (!objects.TryGetValue(objectId, out obj))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var property = obj.Properties.FirstOrDefault(p => p.JavascriptName == name);
|
|
if (property == null)
|
|
{
|
|
throw new InvalidOperationException(string.Format("Property {0} not found on Object of Type {1}", name, obj.Value.GetType()));
|
|
}
|
|
|
|
try
|
|
{
|
|
result = property.GetValue(obj.Value);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex.ToString();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool TrySetProperty(long objectId, string name, object value, out string exception)
|
|
{
|
|
exception = "";
|
|
JavascriptObject obj;
|
|
if (!objects.TryGetValue(objectId, out obj))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var property = obj.Properties.FirstOrDefault(p => p.JavascriptName == name);
|
|
if (property == null)
|
|
{
|
|
throw new InvalidOperationException(string.Format("Property {0} not found on Object of Type {1}", name, obj.Value.GetType()));
|
|
}
|
|
try
|
|
{
|
|
property.SetValue(obj.Value, value);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex.ToString();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Analyse the object and generate metadata which will
|
|
/// be used by the browser subprocess to interact with Cef.
|
|
/// Method is called recursively
|
|
/// </summary>
|
|
/// <param name="obj">Javascript object</param>
|
|
/// <param name="analyseMethods">Analyse methods for inclusion in metadata model</param>
|
|
/// <param name="readPropertyValue">When analysis is done on a property, if true then get it's value for transmission over WCF</param>
|
|
/// <param name="camelCaseJavascriptNames">camel case the javascript names of properties/methods</param>
|
|
/// <param name="analyseProperties">Analyse properties for binding</param>
|
|
private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods, bool readPropertyValue, bool camelCaseJavascriptNames, bool analyseProperties)
|
|
{
|
|
if (obj.Value == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var type = obj.Value.GetType();
|
|
if (type.IsPrimitive || type == typeof(string))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (analyseMethods)
|
|
{
|
|
foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(p => !p.IsSpecialName))
|
|
{
|
|
// Type objects can not be serialized.
|
|
if (methodInfo.ReturnType == typeof (Type) || Attribute.IsDefined(methodInfo, typeof (JavascriptIgnoreAttribute)))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var jsMethod = CreateJavaScriptMethod(methodInfo, camelCaseJavascriptNames);
|
|
obj.Methods.Add(jsMethod);
|
|
}
|
|
}
|
|
|
|
if (analyseProperties)
|
|
{
|
|
foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => !p.IsSpecialName))
|
|
{
|
|
//https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getindexparameters(v=vs.110).aspx
|
|
//An array of type ParameterInfo containing the parameters for the indexes. If the property is not indexed, the array has 0 (zero) elements.
|
|
//According to MSDN array has zero elements when it's not an indexer, so in theory no null check is required
|
|
var isIndexer = propertyInfo.GetIndexParameters().Length > 0;
|
|
var hasIgnoreAttribute = Attribute.IsDefined(propertyInfo, typeof (JavascriptIgnoreAttribute));
|
|
if (propertyInfo.PropertyType == typeof (Type) || isIndexer || hasIgnoreAttribute)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var jsProperty = CreateJavaScriptProperty(propertyInfo, camelCaseJavascriptNames);
|
|
if (jsProperty.IsComplexType)
|
|
{
|
|
var jsObject = CreateJavascriptObject(camelCaseJavascriptNames);
|
|
jsObject.Name = propertyInfo.Name;
|
|
jsObject.JavascriptName = GetJavascriptName(propertyInfo.Name, camelCaseJavascriptNames);
|
|
jsObject.Value = jsProperty.GetValue(obj.Value);
|
|
jsProperty.JsObject = jsObject;
|
|
|
|
AnalyseObjectForBinding(jsProperty.JsObject, analyseMethods, readPropertyValue, camelCaseJavascriptNames, true);
|
|
}
|
|
else if (readPropertyValue)
|
|
{
|
|
jsProperty.PropertyValue = jsProperty.GetValue(obj.Value);
|
|
}
|
|
obj.Properties.Add(jsProperty);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RaiseResolveObjectEvent(string name)
|
|
{
|
|
var handler = ResolveObject;
|
|
|
|
if(handler != null)
|
|
{
|
|
handler(this, new JavascriptBindingEventArgs(this, name));
|
|
}
|
|
}
|
|
|
|
private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, bool camelCaseJavascriptNames)
|
|
{
|
|
var jsMethod = new JavascriptMethod();
|
|
|
|
jsMethod.ManagedName = methodInfo.Name;
|
|
jsMethod.JavascriptName = GetJavascriptName(methodInfo.Name, camelCaseJavascriptNames);
|
|
jsMethod.Function = methodInfo.Invoke;
|
|
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
|
|
jsMethod.Parameters = methodInfo.GetParameters()
|
|
.Select(t => new MethodParameter()
|
|
{
|
|
IsParamArray = t.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0,
|
|
Type = t.ParameterType
|
|
}).ToList();
|
|
//Pre compute HasParamArray for a very minor performance gain
|
|
jsMethod.HasParamArray = jsMethod.Parameters.LastOrDefault(t => t.IsParamArray) != null;
|
|
|
|
return jsMethod;
|
|
}
|
|
|
|
private static JavascriptProperty CreateJavaScriptProperty(PropertyInfo propertyInfo, bool camelCaseJavascriptNames)
|
|
{
|
|
var jsProperty = new JavascriptProperty();
|
|
|
|
jsProperty.ManagedName = propertyInfo.Name;
|
|
jsProperty.JavascriptName = GetJavascriptName(propertyInfo.Name, camelCaseJavascriptNames);
|
|
jsProperty.SetValue = (o, v) => propertyInfo.SetValue(o, v, null);
|
|
jsProperty.GetValue = (o) => propertyInfo.GetValue(o, null);
|
|
|
|
jsProperty.IsComplexType = IsComplexType(propertyInfo.PropertyType);
|
|
jsProperty.IsReadOnly = !propertyInfo.CanWrite;
|
|
|
|
return jsProperty;
|
|
}
|
|
|
|
private static bool IsComplexType(Type type)
|
|
{
|
|
if (type == typeof(void))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var baseType = type;
|
|
|
|
var nullable = type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>);
|
|
|
|
if (nullable)
|
|
{
|
|
baseType = Nullable.GetUnderlyingType(type);
|
|
}
|
|
|
|
if (baseType == null || baseType.IsArray || baseType.Namespace.StartsWith("System"))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (baseType.IsValueType && !baseType.IsPrimitive && !baseType.IsEnum)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return !baseType.IsPrimitive && baseType != typeof(string);
|
|
}
|
|
|
|
private static string GetJavascriptName(string str, bool camelCaseJavascriptNames)
|
|
{
|
|
if (!camelCaseJavascriptNames)
|
|
{
|
|
return str;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return char.ToLowerInvariant(str[0]) + str.Substring(1);
|
|
}
|
|
}
|
|
}
|