// 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;
using System.Threading.Tasks;
namespace CefSharp.Internals
{
///
/// 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.
///
public class JavascriptObjectRepository : IJavascriptObjectRepository
{
public const string AllObjects = "All";
private static long lastId;
public event EventHandler ResolveObject;
public event EventHandler ObjectBoundInJavascript;
///
/// 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.
///
private readonly Dictionary objects = new Dictionary();
///
/// Has the browser this repository is associated with been initilized (set in OnAfterCreated)
///
public bool IsBrowserInitialized { get; set; }
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 GetObjects(List names = null)
{
//If there are no objects names or the count is 0 then we will raise
//the resolve event then return all objects that are registered,
//we'll only perform checking if object(s) of specific name is requested.
var getAllObjects = names == null || names.Count == 0;
if (getAllObjects)
{
RaiseResolveObjectEvent(AllObjects);
return objects.Values.ToList();
}
foreach (var name in names)
{
if (!IsBound(name))
{
RaiseResolveObjectEvent(name);
}
}
var objectsByName = objects.Values.Where(x => names.Contains(x.JavascriptName)).ToList();
//TODO: JSB Add another event that signals when no object matching a name
//in the list was provided.
return objectsByName;
}
public void ObjectsBound(List names)
{
//TODO: JSB Should this be a single event invocation or
// one per object that was bound??? (Currently one per object)
//Execute on Threadpool so we don't unnessicarily block the CEF IO thread
var handler = ObjectBoundInJavascript;
if(handler != null)
{
Task.Run(() =>
{
foreach (var name in names)
{
if (handler != null)
{
handler(this, new JavascriptBindingEventArgs(this, name));
}
}
});
}
}
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)
{
//Enable WCF if not already enabled - can only be done before the browser has been initliazed
//if done after the subprocess won't be WCF enabled it we'll have to throw an exception
if (!IsBrowserInitialized && !isAsync)
{
CefSharpSettings.WcfEnabled = true;
}
if (!CefSharpSettings.WcfEnabled && !isAsync)
{
throw new InvalidOperationException(@"To enable synchronous JS bindings set WcfEnabled true in CefSharpSettings before you create
your ChromiumWebBrowser instances.");
}
//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