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.19k stars 363 forks source link

Multiple instances with the same binaries and incognito mode #517

Closed EricGrange closed 1 month ago

EricGrange commented 1 month ago

Since version 120, starting multiple instances with the same cache path fails (https://briskbard.com/forum/viewtopic.php?t=2208), which was done to protect against the case of multiple instances sharing the same cache, which is not supported by Chromium.

However this mechanism also seems to prevent execution of multiple instances with an empty Cache & RootCache setting, which is incognito mode.

Question: Would it be safe (and can it be done in DCEF) to re-allow running multiple incognito instances ?

Use case: sharing CEF libs between multiple executables, where the CEF is used as a rendering engines with custom protocols (no web access)

Workaround: specifying a RootCache temporary directory, but even with Cache set to empty, the RootCache gets some content (Dictionaries, WideveinCdm & LocalPrefs.json), and since CEF processes can die after the main process, that temporary directory can't just be cleaned up outright when shutting down the main app.

salvadordf commented 1 month ago

Hi,

If GlobalCEFApp.Cache and GlobalCEFApp.RootCache are empty then the default platform-specific directory will be used :

  1. "_~/.config/cef_userdata" directory on Linux.
  2. "~/Library/Application Support/CEF/User Data" directory on MacOS.
  3. "AppData\Local\CEF\User Data" directory under the user profile directory on Windows.

Use of the default directory is not recommended in production applications. Multiple application instances writing to the same GlobalCEFApp.RootCache directory could result in data corruption.

If several instances of the same application run in incognito mode then they are sharing the default directory and that may cause data corruption.

Old CEF versions didn't have any lock to prevent data corruption but that is fixed since CEF 120. DCEF used a very old CEF version and it was affected by this issue.

Unless you modify CEF sources there's no way to disable the lock and it's recommended to adapt the application code to use different GlobalCEFApp.RootCache values in order to avoid data corruption.

The linked forum thread has several solutions for this. In my opinion, keeping one app instance running all the time and opening new tabs or child forms with independent web browsers would be the best solution (see the MDIBrowser demo). Alternatively you can use custom RootCache directories for each instance. For example: "c:\root-PID" and delete unused RootCache directories periodically.

c:\root-PID can be calculated with "c:\root-" + the process ID in Windows.

EricGrange commented 1 month ago

Thanks, I was under the impression that incognito mode kept everything in memory (and wrote nothing to disk), but if it's just falling back to a default directory, then segregating RootCache is probably best (different apps have different CEF settings, so reusing the same might be problematic, some are using off-screen non-GPU rendering, while others are interactive for instance)