Managing User Data Directories in Embedded Chromium

This article provides an overview of how embedded instances of the Chromium browser—such as those powered by Electron, the Chromium Embedded Framework (CEF), and Microsoft Edge WebView2—manage separate user data directories to prevent conflicts with a user's primary browser profile. By utilizing application-specific operating system paths, explicit API configurations, command-line flags, and file-locking mechanisms, these frameworks ensure that cookies, caches, and local storage remain strictly isolated from standalone installations of Google Chrome or Microsoft Edge.

The Necessity of Directory Isolation

Chromium relies heavily on local databases (primarily SQLite) to store user data, including cookies, browsing history, LocalStorage, and cached network resources. To prevent data corruption, Chromium enforces a strict single-process lock on a user data directory using a file often named SingletonLock.

If an embedded Chromium application attempted to read and write to the default Google Chrome user data directory while Chrome was already running, the embedded instance would either fail to launch, crash, or hijack the existing browser session. Therefore, strict directory isolation is required for both stability and security.

Operating System Path Conventions

Instead of pointing to the default browser directories (such as Google\Chrome\User Data), embedded frameworks automatically generate a unique path based on the host application's name. They rely on standard operating system environment variables to store this data safely:

  • Windows: %APPDATA%\<AppName> or %LOCALAPPDATA%\<AppName>
  • macOS: ~/Library/Application Support/<AppName>
  • Linux: ~/.config/<AppName>

By defaulting to the host application's identifier rather than the browser engine's identifier, the embedded instance guarantees its storage space will not overlap with the user's primary web browser.

Framework-Specific Management

Different embedding frameworks handle the assignment of the user data directory through their own initialization APIs, which ultimately pass the correct paths down to the core Chromium engine.

Electron

Electron automatically configures the user data directory during the application startup phase. It reads the name field from the application's package.json file and appends it to the operating system's standard application support directory. Developers can also programmatically override this location before the app is ready by calling app.setPath('userData', '/custom/path').

Chromium Embedded Framework (CEF)

CEF requires the host application to explicitly define storage paths during the initialization of the framework. Developers populate the CefSettings struct before calling CefInitialize. Specifically, the CefString(&settings.root_cache_path) and CefString(&settings.user_data_path) properties are set to a custom directory dictated by the developer. If left blank, CEF will often default to a temporary or in-memory profile, or log a warning depending on the version.

Microsoft Edge WebView2

WebView2 shares its underlying binaries with the Edge browser (via the Evergreen distribution model), making data isolation critical. When a developer initializes a WebView2 environment using CreateCoreWebView2EnvironmentWithOptions, they pass a userDataFolder parameter. If the developer does not specify a path, WebView2 defaults to creating a folder named <ExecutableFileName>.WebView2 in the same directory as the host application's executable. Because applications installed in Program Files lack write permissions in their own directories, developers typically configure WebView2 to point to %LOCALAPPDATA%.

The Underlying Command-Line Flag

Under the hood, almost all embedded Chromium frameworks rely on Chromium's native command-line switch to enforce directory separation:

--user-data-dir="/path/to/custom/directory"

When the host application initializes the embedded browser, the framework translates its API configuration into this command-line argument and passes it to the Chromium browser process upon spawning. Chromium reads this flag at launch and redirects all profile, cache, and database read/write operations to the specified location.

Ephemeral and In-Memory Profiles

In scenarios where an embedded browser only needs to display stateless content—such as a login screen or a help file—creating a persistent directory on the hard drive is unnecessary. Embedded frameworks allow developers to initialize Chromium in an "incognito" or off-the-record mode.

When configured this way, Chromium creates an in-memory user data directory. All cookies, cache files, and session data are stored in RAM and are immediately destroyed when the embedded browser instance is closed, entirely bypassing the local file system and any risk of profile collision.