Пример #1
0
        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);
        }
Пример #2
0
        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();
                    }
                }
            }
        }
Пример #3
0
 public static ScriptableObjectBinder Get(Type type, JSBindingOptions options)
 {
     lock (binders)
     {
         ScriptableObjectBinder value;
         if (binders.TryGetValue(type, out value))
         {
             return(value);
         }
         else
         {
             var binder = new ScriptableObjectBinder(type, options);
             binders[type] = binder;
             return(binder);
         }
     }
 }
 public static ScriptableObjectBinder Get(Type type, JSBindingOptions options)
 {
     lock (binders)
     {
         ScriptableObjectBinder value;
         if (binders.TryGetValue(type, out value))
         {
             return value;
         }
         else
         {
             var binder = new ScriptableObjectBinder(type, options);
             binders[type] = binder;
             return binder;
         }
     }
 }
 /// <summary>
 /// If instance == null -> then object reference will be got from object's userdata.
 /// Otherwise userdata ignored.
 /// </summary>
 public ScriptableObjectV8Handler(ScriptableObjectBinder binder, object instance, bool v8Extension)
 {
     this.instance      = instance;
     this.dispatchTable = binder.DispatchTable;
     this.v8Extension   = v8Extension;
 }
 public ScriptableObjectV8Handler(ScriptableObjectBinder binder, object instance)
     : this(binder, instance, false)
 {
 }
 public ScriptableObjectV8Handler(ScriptableObjectBinder binder, bool v8Extension)
     : this(binder, null, v8Extension)
 {
 }
 public ScriptableObjectV8Handler(ScriptableObjectBinder binder)
     : this(binder, false)
 {
 }
 /// <summary>
 /// If instance == null -> then object reference will be got from object's userdata.
 /// Otherwise userdata ignored.
 /// </summary>
 public ScriptableObjectV8Accessor(ScriptableObjectBinder binder, object instance)
 {
     this.instance      = instance;
     this.dispatchTable = binder.PropertyDispatchTable;
 }
 public ScriptableObjectV8Accessor(ScriptableObjectBinder binder)
     : this(binder, null)
 {
 }
 /// <summary>
 /// If instance == null -> then object reference will be got from object's userdata.
 /// Otherwise userdata ignored.
 /// </summary>
 public ScriptableObjectV8Accessor(ScriptableObjectBinder binder, object instance)
 {
     this.instance = instance;
     this.dispatchTable = binder.PropertyDispatchTable;
 }
 public ScriptableObjectV8Accessor(ScriptableObjectBinder binder)
     : this(binder, null)
 {
 }