cef-rs

repository·dev·Indexed 19 days ago

https://github.com/tauri-apps/cef-rs

Rust bindings for the Chromium Embedded Framework (CEF), enabling the embedding of a Chromium-based browser engine into Rust applications. The project includes the `cef` crate for high-level safe abstractions, `cef-dll-sys` for raw C API bindings, and utility tools such as `download-cef` for acquiring binaries, `export-cef-dir` for managing directory structures, and `bundle-cef-app` for packaging applications.

Tokens
10.6K
Snippets
26
Records
52
Agent score
64%

What's inside cef-rs

  1. Use download-cef to obtain prebuilt CEF archives

    dev
    The download-cef package provides utility functions designed to download and extract prebuilt Chromium Embedded Framework (CEF) archives. This utility supports multiple platforms and automates the process of acquiring the necessary binaries for use with cef-rs.
  2. Export CEF binaries using export-cef-dir

    dev
    The export-cef-dir tool allows you to export files from a prebuilt Chromium Embedded Framework (CEF) archive on any supported platform. The resulting directory structure is specifically designed to be compatible with the requirements of the cef-dll-sys crate.
  3. Configure CEF_PATH for building with cef-dll-sys

    dev

    When building a project that depends on cef-dll-sys, you must point the build process to the directory containing the exported CEF binaries. This is done by setting the CEF_PATH environment variable to the absolute path of your exported directory.

    # Example: setting CEF_PATH to a local directory
    export CEF_PATH="~/.local/share/cef"
  4. Set Environment Variables for CEF

    dev

    After installing the shared binaries, you must set environment variables so the system can locate the CEF libraries.

    Linux

    export CEF_PATH="$HOME/.local/share/cef"
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$CEF_PATH"

    macOS

    export CEF_PATH="$HOME/.local/share/cef"
    export DYLD_FALLBACK_LIBRARY_PATH="$DYLD_FALLBACK_LIBRARY_PATH:$CEF_PATH:$CEF_PATH/Chromium Embedded Framework.framework/Libraries"

    Windows (PowerShell)

    $env:CEF_PATH="$env:USERPROFILE/.local/share/cef"
    $env:PATH="$env:PATH;$env:CEF_PATH"
  5. Install Shared CEF Binaries

    dev

    To speed up builds of the cef crate, you can pre-install shared CEF binaries to a local directory. If you skip this, the cef-dll-sys crate will download and extract these files into its OUT_DIR for every build. You should repeat this step whenever you upgrade to a new version of the cef crate.

    Linux or macOS:

    cargo run -p export-cef-dir -- --force $HOME/.local/share/cef

    Windows (PowerShell):

    cargo run -p export-cef-dir -- --force $env:USERPROFILE/.local/share/cef
    cargo run -p export-cef-dir -- --force $HOME/.local/share/cef
  6. Regenerate CEF bindings using update-bindings

    dev

    To update the Rust bindings for the Chromium Embedded Framework (CEF), you must download a prebuilt CEF archive for your supported platform and then run the bindgen process. This process updates the cef-dll-sys crate (the raw C API bindings) and subsequently regenerates the safe Rust bindings in the cef crate.

    1. Download the latest prebuilt CEF archive from the Chromium Embedded Framework (CEF) Automated Builds.
    2. Run the bindgen tool against the C API for the cef-dll-sys crate.
    3. Regenerate the safe bindings in the cef crate.
  7. Configure library loader paths for CEF runtime

    dev

    To ensure the application can find and load the CEF DLLs/libraries at runtime, you must update the platform-specific library loader path using the CEF_PATH environment variable.

    ### Linux
    ```sh
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$CEF_PATH"

    macOS

    export DYLD_FALLBACK_LIBRARY_PATH="$DYLD_FALLBACK_LIBRARY_PATH:$CEF_PATH"

    Windows (PowerShell)

    $env:PATH = "$env:PATH;$env:CEF_PATH"
  8. Configure `bundle-cef-app` in Cargo.toml

    dev

    You can customize the application bundle by adding metadata to your Cargo.toml. This allows you to define the name of the macOS helper and specify a resource directory to be included in the bundle.

    [package.metadata.cef.bundle]
    helper_name = "cefsimple_helper"
    resources_path = "resources"
    [package.metadata.cef.bundle]
    helper_name = "cefsimple_helper"
    resources_path = "resources"
  9. How the MessageRouter handles renderer-to-browser communication

    dev

    The MessageRouter facilitates asynchronous communication between the Renderer process (JavaScript) and the Browser process (Rust) using a request/response pattern.

    1. Sending a Query (Renderer Side)

    In JavaScript, the router exposes a query function (configured via js_query_function). This function expects a single object argument with the following members:

    • request: A string or ArrayBuffer containing the query data.
    • on_success: A callback function executed when the browser process returns a successful response.
    • on_failure: A callback function executed if the query fails.
    • persistent: (Optional) A boolean indicating if the request should be kept in the router's tracking map.

    Calling this function returns a unique request_id immediately.

    2. Processing the Query (Browser Side)

    The RendererSideRouter captures the request and sends it to the browser process. The browser process uses a BrowserSideRouter to find a registered BrowserSideHandler. The handler processes the request and uses a BrowserSideRouterCallback to send the result back.

    3. Canceling Requests

    Users can cancel a specific request using the function configured via js_cancel_function, passing the request_id as an integer. If a special RESERVED_ID is used, all requests associated with the current context_id are canceled.