Page 1 of 1

Re: How to work with ICefFrame, ICefBrowser correctly.

Posted: Thu Feb 15, 2018 1:59 pm
by salvadordf
Those interfaces are created inside the CEF libraries and have a very different lifespan.

ICefBrowser is available in TChromium.Browser and many TChromium events have a "browser" parameter. TChromium.Browser is valid if TChromium.Initialized is True which means that you can use that property until you destroy the TChromium component.

ICefFrame has a much shorter lifespan because it depends on the loaded document. Many TChromium events have a "frame" parameter that can be used in the scope of that event but if you assign it to a variable and the document changes or you try to close your app, it may be the cause of problems because that frame is not destroyed until you set that variable to nil.

Many CEF3 classes are reference counted and Henri Gourvest adapted those classes to Delphi interfaces in DCEF3. CEF4Delphi inherited that code and you should use those interfaces the same way you use regular Delphi interfaces.

The most important tip I would give is : Don't increment the reference count unless you really have to.

That means :
  • Use the event parameters.
  • Always pass them as "const" or "var" to other procedures.
  • If possible, use local variables to store those interfaces.
  • If you use class fields to store those interfaces, always set them to NIL when you no longer use them or before you destroy that object.
You can use some CEF functions in other threads but you should check the CEF code comments because some of them can only be used in certain processes and threads.

Read the comments about the Host interface (cef_browser_host_t) here :
https://bitbucket.org/chromiumembedded/ ... ew-default

Code: Select all

///
// Structure used to represent the browser process aspects of a browser window.
// The functions of this structure can only be called in the browser process.
// They may be called on any thread in that process unless otherwise indicated
// in the comments.
///
The SendKeyEvent function (send_key_event) has these comments :

Code: Select all

  ///
  // Send a key event to the browser.
  ///
In this case, you can safely use SendKeyEvent in any thread in the browser process.

Re: How to work with ICefFrame, ICefBrowser correctly.

Posted: Thu Feb 15, 2018 5:46 pm
by salvadordf
The TChromium component has some problems with popup windows created automatically by CEF.

You may have noticed that if you close a popup window with the MiniBrowser the browser loads the home address.

The right way would be to create a new form that includes another TChromium component in OSR mode. The problem is that VCL components must be created and destroyed in the main thread but OnBeforePopup is executed in a different thread.

For that reason you have to create a hidden form just in case the user tries to open a popup window. Let's call that hidden form "PopupForm".

If the user clicks on a link that opens a popup window then the OnBeforePopup event is triggered and you must call WindowInfoAsWindowless(windowInfo, 0, targetFrameName) and PopupForm.Chromium.CreateClientHandler(client) to set the "windowInfo" and "client" parameters.

After that you send a message to the main form to call PopupForm.Show and create the next hidden popup form in the main thread.

Using these custom forms for popup windows gives you access the all the interfaces you need because you have two TChromium components : one in the main form and one in the popup form.

Re: How to work with ICefFrame, ICefBrowser correctly.

Posted: Fri Feb 16, 2018 2:37 pm
by salvadordf
You were right about the OnAfterCreated event.

I'll update CEF4Delphi and the demos as soon as I can.

Re: How to work with ICefFrame, ICefBrowser correctly.

Posted: Mon Feb 19, 2018 8:53 am
by salvadordf
I've never used Synchronize outside a Delphi thread but I guess you can.