// Copyright © 2010-2015 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;
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 : DisposableResource
{
private static long lastId;
// 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();
// This is the root of the objects that get serialized to the child
// process.
public JavascriptRootObject RootObject { get; private set; }
// This is the root of the objects that get serialized to the child
// process with cef ipc serialization (wcf not required).
public JavascriptRootObject AsyncRootObject { get; private set; }
public JavascriptObjectRepository()
{
RootObject = new JavascriptRootObject();
AsyncRootObject = new JavascriptRootObject();
}
public bool HasBoundObjects
{
get { return RootObject.MemberObjects.Count > 0 || AsyncRootObject.MemberObjects.Count > 0; }
}
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 RegisterAsync(string name, object value, bool camelCaseJavascriptNames)
{
AsyncRootObject.MemberObjects.Add(CreateInternal(name, value, camelCaseJavascriptNames, analyseProperties: false));
}
public void Register(string name, object value, bool camelCaseJavascriptNames)
{
RootObject.MemberObjects.Add(CreateInternal(name, value, camelCaseJavascriptNames, analyseProperties: true));
}
private JavascriptObject CreateInternal(string name, object value, bool camelCaseJavascriptNames, bool analyseProperties)
{
var jsObject = CreateJavascriptObject(camelCaseJavascriptNames);
jsObject.Value = value;
jsObject.Name = name;
jsObject.JavascriptName = name;
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: analyseProperties, readPropertyValue: false, camelCaseJavascriptNames: camelCaseJavascriptNames);
return jsObject;
}
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
{
// Do we have enough arguments? Add Type.Missing for any that we don't have incase of optional params
var missingParams = method.ParameterCount - parameters.Length;
if (missingParams > 0)
{
var paramList = new List