salvadordf / CEF4Delphi

CEF4Delphi is an open source project to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows, Linux and MacOS.
https://www.briskbard.com/forum/
Other
1.21k stars 366 forks source link

ICefv8Value.SetUserData() (and GetUserData()) #394

Closed uniho closed 2 years ago

uniho commented 2 years ago

Can you change argument of ICefv8Value.SetUserData() and SetUserData() from ICefv8Value to ICefBaseRefCounted as CEF C++ API?

salvadordf commented 2 years ago

Hi,

You're right. The right argument data type for ICefv8Value.SetUserData() and ICefv8Value.GetUserData() should be ICefBaseRefCounted instead of ICefv8Value.

However, CEF has a very unique way to handle interfaces that breaks the Delphi definition of interface inheritance.

If we use ICefBaseRefCounted for SetUserData and GetUserData then we can't use "SysUtils.supports", "IInterface.QueryInterface" or "as" to get the right interface reference that we originally used with SetUserData.

Consider converting your user data to any of the supported data types that ICefv8Value can handle or try using an alternative method to pass that information.

uniho commented 2 years ago

Thanks for your quick reply! You mean we cannot know GC timing from native code like this thread says?

salvadordf commented 2 years ago

If you really need to know that then you can modify the ICefv8Value interface and the TCefv8ValueRef class declarations.

Set the data type for ICefv8Value.SetUserData() and ICefv8Value.GetUserData() with your own custom ICefBaseRefCounted descendant and send a message when the instance of that custom class is destroyed.

You will also have to remove the TCefRTTIExtension class because it uses ICefv8Value.SetUserData() and ICefv8Value.GetUserData() with the current data type.

uniho commented 2 years ago

Yeah, I want to create TProcess Object (I'm using Lazarus) on CefTask thread using JS Promise, to read stdin and stderr from pipe, and kill it on GC timing. I will try. It is kind of you!

salvadordf commented 2 years ago

I just remembered that you could also use the current ICefv8Value.SetUserData() and ICefv8Value.GetUserData() implementation and use TCefv8ValueRef.NewArrayBuffer to create a fake array with a user defined destruction callback function.

That callback will be called when the GC needs to destroy that array and you could also add extra code to send a message to the main browser process with your information.

uniho commented 2 years ago

Hi, new project launched! Thanks for your advice!