Skip to content

CefRequestContext

Header: cef_request_context.h
Interfaces: CefResolveCallback, CefSettingObserver, CefRequestContext, CefRequestContextHandler
Category: Core APIs

Overview

Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.

CefResolveCallback

Source File: include/cef_request_context.h

Process / Thread Context: Browser Process / UI Thread (per method comment: "Called on the UI thread after the ResolveHost request has completed.")

Purpose: USER-IMPLEMENTED callback interface passed to CefRequestContext::ResolveHost() to receive the asynchronous host resolution result.

Usage Example

cpp
class MyResolver : public CefResolveCallback {
 public:
  void OnResolveCompleted(cef_errorcode_t result,
                          const std::vector<CefString>& ips) override {
    if (result == ERR_NONE) {
      for (const auto& ip : ips) { /* ... */ }
    }
  }
};

CefRefPtr<CefRequestContext> ctx = browser->GetHost()->GetRequestContext();
ctx->ResolveHost("https://example.com", new MyResolver());

CefSettingObserver

Source File: include/cef_request_context.h

Process / Thread Context: Browser Process / UI Thread (per class comment: "The methods of this class will be called on the browser process UI thread.")

Purpose: USER-IMPLEMENTED observer interface registered via CefRequestContext::AddSettingObserver (added in API 13401) to receive notifications when content or website settings change.

Usage Example

cpp
class MySettingObserver : public CefSettingObserver {
 public:
  void OnSettingChanged(const CefString& requesting_url,
                        const CefString& top_level_url,
                        cef_content_setting_types_t type) override {
    if (type == CEF_CONTENT_SETTING_TYPE_POPUPS) {
      // refresh UI to reflect new popups permission
    }
  }
};

CefRefPtr<CefRegistration> reg =
    ctx->AddSettingObserver(new MySettingObserver());
// reg released when no longer needed -> observer unregisters.

CefRequestContext

Source File: include/cef_request_context.h

Process / Thread Context: Browser Process / UI Thread (most methods) — some explicitly state "may be called on any thread in the browser process" (RegisterSchemeHandlerFactory, ClearSchemeHandlerFactories). The class itself is a CefPreferenceManager subclass; preference methods must be called on the browser process UI thread.

Purpose: USER-CALLED library interface representing a request context that governs a set of related browser and URL request objects. Provides cookie manager access, scheme handler factory registration, certificate exception/cache/auth clearing, host resolution, media router access, content/website settings, color scheme controls, and inherited preference-management methods.

Methods

static CefRefPtr<CefRequestContext> GetGlobalContext()

Return Value: Returns the global context.

Threading Constraint: Threading constraint not specified in source.

static CefRefPtr<CefRequestContext> CreateContext(const CefRequestContextSettings& settings, CefRefPtr<CefRequestContextHandler> handler)

Return Value: New context with settings and optional handler.

Threading Constraint: Threading constraint not specified in source.

static CefRefPtr<CefRequestContext> CreateContext(CefRefPtr<CefRequestContext> other, CefRefPtr<CefRequestContextHandler> handler)

Return Value: New context that shares storage with other; optional handler. (CAPI name cef_create_context_shared.)

Threading Constraint: Threading constraint not specified in source.

bool IsSame(CefRefPtr<CefRequestContext> other)

Return Value: True if pointing to the same context.

Threading Constraint: Threading constraint not specified in source.

bool IsSharingWith(CefRefPtr<CefRequestContext> other)

Return Value: True if sharing storage with other.

Threading Constraint: Threading constraint not specified in source.

bool IsGlobal()

Return Value: True if this is the global context (used when a NULL context is passed to CefBrowserHost factories).

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefRequestContextHandler> GetHandler()

Return Value: Returns the handler, if any.

Threading Constraint: Threading constraint not specified in source.

CefString GetCachePath()

Return Value: Returns the on-disk cache path. Empty means "incognito mode" (in-memory cache).

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefCookieManager> GetCookieManager(CefRefPtr<CefCompletionCallback> callback)

Parameters:

  • callback: optional; executed asynchronously on the UI thread after the manager's storage is initialized.

Return Value: The cookie manager (or NULL if storage not yet ready).

Usage Instruction: Obtain to add/remove cookies, flush store, etc.

Threading Constraint: Threading constraint not explicitly specified; inferred UI thread.

bool RegisterSchemeHandlerFactory(const CefString& scheme_name, const CefString& domain_name, CefRefPtr<CefSchemeHandlerFactory> factory)

Parameters:

  • scheme_name, domain_name (empty for all domains; ignored for non-standard schemes), factory (NULL to remove the factory).

Return Value: False on error.

Usage Instruction: Register custom scheme handlers. For custom schemes you must also implement CefApp::OnRegisterCustomSchemes().

Threading Constraint: Any thread in the browser process.

bool ClearSchemeHandlerFactories()

Return Value: Clears all factories. False on error. Any thread in the browser process.

Threading Constraint: Threading constraint not specified in source.

void ClearCertificateExceptions(CefRefPtr<CefCompletionCallback> callback)

Return Value: Clears cert exceptions added via CefRequestHandler::OnCertificateError(). Recommends calling CloseAllConnections() afterwards. callback runs on UI thread after completion.

Threading Constraint: Threading constraint not specified in source.

void ClearHttpAuthCredentials(CefRefPtr<CefCompletionCallback> callback)

Return Value: Clears HTTP auth credentials added via GetAuthCredentials.

Threading Constraint: Threading constraint not specified in source.

void CloseAllConnections(CefRefPtr<CefCompletionCallback> callback)

Return Value: Closes all active/idle connections; recommended before CefShutdown() if all other CEF objects are released.

Threading Constraint: Threading constraint not specified in source.

void ResolveHost(const CefString& origin, CefRefPtr<CefResolveCallback> callback)

Return Value: Resolves origin to IPs. callback runs on the UI thread after completion.

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefMediaRouter> GetMediaRouter(CefRefPtr<CefCompletionCallback> callback)

Return Value: Returns the CefMediaRouter for this context. callback runs asynchronously on the UI thread after initialization.

Threading Constraint: Threading constraint not specified in source.

CefRefPtr<CefValue> GetWebsiteSetting(const CefString& requesting_url, const CefString& top_level_url, cef_content_setting_types_t content_type)

Parameters:

  • requesting_url, top_level_url (both empty returns default), content_type.

Return Value: Current value or nullptr if not configured.

Threading Constraint: Browser process UI thread.

void SetWebsiteSetting(const CefString& requesting_url, const CefString& top_level_url, cef_content_setting_types_t content_type, CefRefPtr<CefValue> value)

Threading Constraint: Browser process UI thread.

cef_content_setting_values_t GetContentSetting(const CefString& requesting_url, const CefString& top_level_url, cef_content_setting_types_t content_type)

Return Value: Current value; defaults to CEF_CONTENT_SETTING_VALUE_DEFAULT.

Threading Constraint: Browser process UI thread.

void SetContentSetting(const CefString& requesting_url, const CefString& top_level_url, cef_content_setting_types_t content_type, cef_content_setting_values_t value)

Threading Constraint: Browser process UI thread.

CefRefPtr<CefRegistration> AddSettingObserver(CefRefPtr<CefSettingObserver> observer)

Return Value: CefRegistration that keeps the observer alive — destroyed registration unregisters the observer.

Threading Constraint: Browser process UI thread.

void SetChromeColorScheme(cef_color_variant_t variant, cef_color_t user_color)

Return Value: Sets the color scheme for all browsers sharing this context. variant of SYSTEM/LIGHT/DARK changes color mode; other variants determine how user_color is applied. user_color of 0 (transparent) uses default.

Threading Constraint: Threading constraint not specified in source.

cef_color_variant_t GetChromeColorSchemeMode()

Return Value: Returns current color mode (SYSTEM/LIGHT/DARK). Default CEF_COLOR_VARIANT_SYSTEM. Browser process UI thread.

Threading Constraint: Threading constraint not specified in source.

cef_color_t GetChromeColorSchemeColor()

Return Value: Returns current user color or 0 (transparent) for default. Default 0. Browser process UI thread.

Threading Constraint: Threading constraint not specified in source.

cef_color_variant_t GetChromeColorSchemeVariant()

Return Value: Returns current variant. Default CEF_COLOR_VARIANT_SYSTEM. Browser process UI thread.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
// Create an isolated (incognito) request context:
CefRequestContextSettings settings;
settings.cache_path = "";  // incognito
CefRefPtr<CefRequestContext> isolatedCtx =
    CefRequestContext::CreateContext(settings, new MyRequestContextHandler());

// Use it when creating a browser:
CefWindowInfo info;
CefBrowserHost::CreateBrowserSync(info, client, "https://example.com",
                                  CefBrowserSettings(), isolatedCtx, nullptr);

// Register a custom scheme handler on the context (any browser thread):
isolatedCtx->RegisterSchemeHandlerFactory("myapp", "",
                                          new MyAppSchemeHandlerFactory());

// Resolve a host:
isolatedCtx->ResolveHost("https://example.com", new MyResolver());

// Read a content setting on the UI thread:
cef_content_setting_values_t v =
    isolatedCtx->GetContentSetting("https://example.com",
                                   "https://example.com",
                                   CEF_CONTENT_SETTING_TYPE_POPUPS);

CefRequestContextHandler

Source File: include/cef_request_context_handler.h

Process / Thread Context: Browser Process — OnRequestContextInitialized on UI thread; GetResourceRequestHandler on IO thread (per method comments).

Purpose: USER-IMPLEMENTED handler interface attached to a CefRequestContext. Receives the post-initialization callback and supplies per-request CefResourceRequestHandler objects. The handler instance is not released until all objects related to the context are destroyed.

Usage Example

cpp
class MyRCHandler : public CefRequestContextHandler {
 public:
  void OnRequestContextInitialized(
      CefRefPtr<CefRequestContext> request_context) override {
    // Register application-specific scheme handlers once the context is ready.
    request_context->RegisterSchemeHandlerFactory(
        "myapp", "", new MyAppSchemeHandlerFactory());
  }

  CefRefPtr<CefResourceRequestHandler> GetResourceRequestHandler(
      CefRefPtr<CefBrowser> browser,
      CefRefPtr<CefFrame> frame,
      CefRefPtr<CefRequest> request,
      bool is_navigation,
      bool is_download,
      const CefString& request_initiator,
      bool& disable_default_handling) override {
    if (request->GetURL().ToString().find("private=") != std::string::npos) {
      disable_default_handling = true;
      return new MyResourceRequestHandler();  // intercept
    }
    return nullptr;  // default handling
  }
};

CefRefPtr<CefRequestContext> ctx =
    CefRequestContext::CreateContext(settings, new MyRCHandler());

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.