public void BindJSObject(string name, object obj, Type type, JSBindingOptions options) { if (this.owner != null && (options & JSBindingOptions.Extension) != 0) { throw new NotSupportedException("Extension can be bound only on global context. Use Cef.JSBinding instead."); } var def = new JSObjectDef(obj, type, name, options); if (this.objects.ContainsKey(def.MemberName)) { throw new JSBindingException(string.Format( "JSObject with name '{0}' already registered.", name )); } // force binder creation var binder = ScriptableObjectBinder.Get(def.TargetType, options); if (def.Extension) { // Register V8 extension var javaScriptCode = binder.CreateJavaScriptExtension(def.MemberName); var handler = new ScriptableObjectV8Handler(binder, def.Target, true); if (!Cef.RegisterExtension(def.ExtensionName, javaScriptCode, (CefV8Handler)handler)) { throw new InvalidOperationException("Cef.RegisterExtension failed."); } } this.objects.Add(def.MemberName, def); }
public void BindObjects(CefV8Value target) { foreach (var def in this.objects.Values.Where(_ => !_.Extension)) { var binder = ScriptableObjectBinder.Get(def.TargetType, def.Options); List <CefV8Value> proxies = null; var name = def.MemberName; var obj = target; if (name.Contains('.')) { proxies = new List <CefV8Value>(); var parts = name.Split('.'); for (var i = 0; i < parts.Length - 1; i++) { name = parts[i]; var nsObj = obj.GetValue(name); proxies.Add(nsObj); if (nsObj.IsUndefined) { var newObj = CefV8Value.CreateObject(); proxies.Add(newObj); obj.SetValue(name, newObj); obj = newObj; continue; } else if (nsObj.IsObject) { obj = nsObj; continue; } else { throw new JSBindingException("Invalid member access expression. Invalid object in path."); } } name = parts[parts.Length - 1]; } var boundObject = binder.CreateScriptableObject(def.Target); obj.SetValue(name, boundObject); boundObject.Dispose(); if (proxies != null) { foreach (var proxy in proxies) { proxy.Dispose(); } } } }