コード例 #1
0
        // TODO: CreateV8Handler self/universal
        // TODO: CreateV8Accessor self/universal

        public CefV8Value CreateScriptableObject(object instance)
        {
            // TODO: if object doesn't have properties -> do not create accessor

            var v8Handler  = new ScriptableObjectV8Handler(this, instance);
            var v8Accessor = new ScriptableObjectV8Accessor(this, instance);

            var obj = CefV8Value.CreateObject(v8Accessor);

            foreach (var property in this.PropertyDispatchTable.GetValues())
            {
                obj.SetValue(property.Name,
                             (property.CanRead ? CefV8AccessControl.AllCanRead : 0)
                             | (property.CanWrite ? CefV8AccessControl.AllCanWrite : 0),
                             CefV8PropertyAttribute.DontDelete);
            }

            foreach (var method in this.DispatchTable.GetValues())
            {
                if (method.Hidden)
                {
                    continue;
                }

                obj.SetValue(method.Name,
                             CefV8Value.CreateFunction(method.Name, v8Handler)
                             );
            }

            return(obj);
        }
コード例 #2
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);
        }