Beispiel #1
0
        public long AddNativeObject(object obj, string genericInfo)
        {
            if (obj == null)
            {
                throw new InvalidOperationException("不能把空对象添加到浏览器的本机对象列表中");
            }
            if (NativeObjectInfoDic.Values.SingleOrDefault(item => item.RealObject == obj) != null)
            {
                throw new InvalidOperationException("不能重复添加对象到相同浏览器的本机对象列表中");
            }
            var id   = CreateId();
            var info = new NativeObjectInfo();

            info.Id          = id;
            info.RealObject  = obj;
            info.GenericInfo = genericInfo;
            info.GcInfo      = 1;
            var jsGc = new Tnelab.MiniBlink.NativeMethods.jsData();

            jsGc.typeName    = "NativeObjectGC";
            jsGc.propertyGet = (es, jobj, name) =>
            {
                return(Tnelab.MiniBlink.NativeMethods.jsUndefined());
            };
            jsGc.propertySet = (es, jobj, name, jv) =>
            {
                return(true);
            };
            jsGc.callAsFunction = (es, jobj, args, argsCount) =>
            {
                return(Tnelab.MiniBlink.NativeMethods.jsUndefined());
            };
            var gcPtr = Marshal.AllocHGlobal(Marshal.SizeOf <MiniBlink.NativeMethods.jsData>());

            Marshal.StructureToPtr(jsGc, gcPtr, true);

            jsGc.finalize = (ref Tnelab.MiniBlink.NativeMethods.jsData data) =>
            {
                this.DestroyNativeObject(id, true);
                Marshal.FreeHGlobal(gcPtr);
            };
            this.WebBrowser.JsExecStateInvoke((es) => {
                var tes          = es;
                var gcValue      = MiniBlink.NativeMethods.jsObject(tes, gcPtr);
                var idValue      = MiniBlink.NativeMethods.jsInt((int)id);
                var tnelabValue  = MiniBlink.NativeMethods.jsGetGlobal(tes, "Tnelab");
                var onSetGCValue = MiniBlink.NativeMethods.jsGet(tes, tnelabValue, "OnSetGC");
                MiniBlink.NativeMethods.jsCall(tes, onSetGCValue, tnelabValue, new long[] { idValue, gcValue }, 2);
            });
            info.JsGC = jsGc;
            NativeObjectInfoDic.Add(id, info);
            return(id);
        }
Beispiel #2
0
        public long GetNativeObjectId(object obj)
        {
            if (this.GetParentControl() == obj)
            {
                return(this.ParentControlId);
            }
            var iobj = NativeObjectInfoDic.SingleOrDefault(it => it.Value.RealObject == obj);

            if (iobj.Value != null)
            {
                return(iobj.Key);
            }
            return(-1);
        }
Beispiel #3
0
        public object GetNativeObject(long id, bool addGC)
        {
            if (this.ParentControlId == id)
            {
                return(this.GetParentControl());
            }

            if (NativeObjectInfoDic.ContainsKey(id))
            {
                if (addGC)
                {
                    NativeObjectInfoDic[id].GcInfo++;
                }
                return(NativeObjectInfoDic[id].RealObject);
            }
            return(null);
        }
Beispiel #4
0
 public void DestroyNativeObject(long id, bool isGC)
 {
     if (NativeObjectInfoDic.ContainsKey(id))
     {
         var obj = NativeObjectInfoDic[id];
         if (isGC)
         {
             obj.GcInfo--;
         }
         else
         {
             obj.GcInfo = 0;
         }
         if (obj.GcInfo == 0)
         {
             this.NativeObjectInfoDic.Remove(id);
             if (obj.RealObject is IDisposable)
             {
                 var disposableObj = obj.RealObject as IDisposable;
                 disposableObj.Dispose();
             }
         }
     }
 }