Пример #1
0
        private unsafe static cef_base_scoped_t *Allocate(int size)
        {
            cef_base_scoped_t *instance = (cef_base_scoped_t *)CefStructure.Allocate(size);

            instance->del = (void *)Marshal.GetFunctionPointerForDelegate(fnDel);
            return(instance);
        }
Пример #2
0
 private unsafe static RefCountedWrapperStruct *GetWrapperStructPtr(void *instance)
 {
     if (CefStructure.IsAllocated(new IntPtr(instance)))
     {
         return(null);
     }
     return(RefCountedWrapperStruct.FromRefCounted(instance));
 }
Пример #3
0
        private unsafe static cef_base_ref_counted_t *Allocate(int size)
        {
            cef_base_ref_counted_t *instance = (cef_base_ref_counted_t *)CefStructure.Allocate(size);

            instance->add_ref              = (void *)Marshal.GetFunctionPointerForDelegate(fnAddRef);
            instance->release              = (void *)Marshal.GetFunctionPointerForDelegate(fnRelease);
            instance->has_one_ref          = (void *)Marshal.GetFunctionPointerForDelegate(fnHasOneRef);
            instance->has_at_least_one_ref = (void *)Marshal.GetFunctionPointerForDelegate(fnHasAtLeastOneRef);
            return(instance);
        }
Пример #4
0
#pragma warning disable CS1591
        protected unsafe override void Dispose(bool disposing)
        {
            IntPtr mem = (IntPtr)_instance;
            bool   alive;

            lock (Scope)
            {
                alive = Scope.Remove(mem);
            }
            if (alive)
            {
                CefStructure.Free(mem);
                _instance = null;
            }
        }
Пример #5
0
#pragma warning disable CS1591
        protected unsafe override void Dispose(bool disposing)
        {
            IntPtr key = Volatile.Read(ref _instance);

            if (key != IntPtr.Zero)
            {
                GlobalSyncRoot.EnterWriteLock();
                try
                {
                    if (CefApi.UseUnsafeImplementation)
                    {
                        RefCountedWrapperStruct *ws = GetWrapperStructPtr((void *)key);
                        if (ws != null)
                        {
                            UnsafeRefCounted.Remove(ws->cppObject);
                        }
                    }
                    RefCounted.Remove(key);
                }
                finally
                {
                    GlobalSyncRoot.ExitWriteLock();
                }
#if NETFRAMEWORK
                if (Environment.HasShutdownStarted)
                {
                    if (CefStructure.IsAllocated(key))                     // allow leaks to fix potential UAF
                    {
                        return;
                    }
                }
                else
#endif
                if (CefStructure.Free(key))
                {
                    return;
                }

                base.Dispose(disposing);
            }
        }
Пример #6
0
        /// <summary>
        /// Returns a wrapper for the specified pointer.
        /// </summary>
        /// <typeparam name="TClass">The type of wrapper.</typeparam>
        /// <param name="create">Represents a method that create a new wrapper.</param>
        /// <param name="instance">The pointer to ref-counted CEF struct.</param>
        /// <returns>Returns an existing or new wrapper for the specified pointer.</returns>
        public unsafe static TClass Wrap <TClass>(Func <IntPtr, TClass> create, T *instance)
            where TClass : CefBaseRefCounted <T>
        {
            if (instance == null)
            {
                return(null);
            }

            RefCountedWrapperStruct *ws = null;
            CefBaseRefCounted        wrapper;
            IntPtr key = new IntPtr(instance);

            Internal.CefBaseRefCountedImpl.GlobalSyncRoot.EnterUpgradeableReadLock();
            try
            {
                if (CefApi.UseUnsafeImplementation)
                {
                    ws = GetWrapperStructPtr(instance);
                    if (ws != null && UnsafeRefCounted.TryGetValue(ws->cppObject, out WeakReference <CefBaseRefCounted> weakRef) &&
                        weakRef.TryGetTarget(out wrapper))
                    {
                        ((cef_base_ref_counted_t *)instance)->Release();
                        return((TClass)wrapper);
                    }
                }

                if (RefCounted.TryGetValue(key, out RefCountedReference reference) &&
                    reference.Instance.TryGetTarget(out wrapper))
                {
                    ((cef_base_ref_counted_t *)instance)->Release();
                    return((TClass)wrapper);
                }
#if DEBUG
                else if (CefStructure.IsAllocated(key))
                {
                    throw new InvalidCefObjectException(string.Format("Unexpected access to {0}.", typeof(TClass).Name));
                }
#endif
                else
                {
                    Internal.CefBaseRefCountedImpl.GlobalSyncRoot.EnterWriteLock();
                    try
                    {
                        TClass typedWrapper = create(key);
                        var    weakRef      = new WeakReference <CefBaseRefCounted>(typedWrapper);
                        RefCounted[key] = new RefCountedReference(weakRef);
                        if (ws != null)
                        {
                            UnsafeRefCounted[ws->cppObject] = weakRef;
                        }
                        return(typedWrapper);
                    }
                    finally
                    {
                        Internal.CefBaseRefCountedImpl.GlobalSyncRoot.ExitWriteLock();
                    }
                }
            }
            finally
            {
                Internal.CefBaseRefCountedImpl.GlobalSyncRoot.ExitUpgradeableReadLock();
            }
        }
Пример #7
0
 public CefBrowserSettings()
 {
     _disposable     = true;
     _instance       = (cef_browser_settings_t *)CefStructure.Allocate(sizeof(cef_browser_settings_t));
     _instance->size = new UIntPtr((uint)sizeof(cef_browser_settings_t));
 }
Пример #8
0
 public CefRequestContextSettings()
 {
     _disposable     = true;
     _instance       = (cef_request_context_settings_t *)CefStructure.Allocate(sizeof(cef_request_context_settings_t));
     _instance->size = new UIntPtr((uint)sizeof(cef_request_context_settings_t));
 }