CefCookieManager
Header: cef_cookie.h
Interfaces: CefCookieManager, CefCookieVisitor, CefSetCookieCallback, CefDeleteCookiesCallback
Category: System Utilities
Overview
Tasks, threads, parsers, streams, cookies, URL requests, scheme registration, and constant tables.
CefCookieManager
Source File: include/cef_cookie.h
Process / Thread Context: Browser Process. The header states: "The methods of this class may be called on any thread unless otherwise indicated." Visitor/callback methods are explicitly documented as called on the UI thread.
Purpose: Class used for managing cookies. It exposes enumeration (visit), insertion (set), deletion, and persistence (flush) of cookies. The global manager is reachable via GetGlobalManager() and per-request-context managers are reachable via CefRequestContext::GetDefaultCookieManager().
Methods
static CefRefPtr<CefCookieManager> GetGlobalManager(CefRefPtr<CefCompletionCallback> callback)
Parameters:
callback: OptionalCefCompletionCallback. If non-NULL it is executed asynchronously on the UI thread after the manager's storage has been initialized.
Return Value: Returns the global cookie manager instance. Equivalent to CefRequestContext::GetGlobalContext()->GetDefaultCookieManager().
Usage Instruction: Use to obtain the cookie manager backed by cef_settings_t.cache_path (or in-memory if no cache path is configured). Pass a completion callback if you need to know when storage initialization is complete before issuing cookie commands.
Threading Constraint: Threading constraint not specified for the call itself; the optional callback runs on the UI thread.
bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor)
Parameters:
visitor: ACefCookieVisitorimplementation whoseVisit()method will be invoked once per cookie on the UI thread.
Return Value: Returns false if cookies cannot be accessed (i.e. the cookie store is unavailable). Returns true if the visit was successfully scheduled; visitor->Visit() is then called on the UI thread for each cookie, ordered by longest path then earliest creation date.
Usage Instruction: Use to enumerate every cookie in the store, e.g. for diagnostics or for selective deletion via deleteCookie out-param. The visitor may return false from Visit() to stop enumeration early.
Threading Constraint: Visit calls occur on the UI thread (per header comment).
bool VisitUrlCookies(const CefString& url, bool includeHttpOnly, CefRefPtr<CefCookieVisitor> visitor)
Parameters:
url: The URL whose cookies to enumerate. The result set is filtered by scheme, host, domain and path of this URL.includeHttpOnly: Iftrue, HTTP-only cookies are also included in the result set.visitor: ACefCookieVisitorimplementation invoked on the UI thread.
Return Value: Returns false if cookies cannot be accessed; otherwise true and the visitor is invoked once per matching cookie (ordered by longest path, then earliest creation date).
Usage Instruction: Use to inspect only cookies applicable to a particular URL — typically when you want to log out, sync, or selectively delete cookies for a specific site.
Threading Constraint: Visitor callbacks occur on the UI thread.
bool SetCookie(const CefString& url, const CefCookie& cookie, CefRefPtr<CefSetCookieCallback> callback)
Parameters:
url: The URL the cookie applies to; must be valid.cookie: ACefCookiestruct with explicit user-provided attributes. Each attribute must be well-formed; disallowed characters (e.g.;inside the value) cause the call to fail without setting the cookie.callback: OptionalCefSetCookieCallbackinvoked asynchronously on the UI thread after the cookie has been set;OnComplete(success)reports success.
Return Value: Returns false if url is invalid or cookies cannot be accessed; otherwise true and the set-cookie operation is queued.
Usage Instruction: Use to programmatically inject cookies (e.g. session bootstrap from a saved profile). Always pass a valid URL even if the cookie itself specifies domain/path independently.
Threading Constraint: The optional callback runs on the UI thread.
bool DeleteCookies(const CefString& url, const CefString& cookie_name, CefRefPtr<CefDeleteCookiesCallback> callback)
Parameters:
url: Combined withcookie_nameto choose the deletion scope. If both are specified, all host and domain cookies matching both are deleted. If onlyurlis specified, all host cookies (but not domain cookies) irrespective of path are deleted. Ifurlis empty, all cookies for all hosts and domains are deleted.cookie_name: Optional cookie name; when non-empty, only cookies with that name (and matchingurl) are deleted.callback: OptionalCefDeleteCookiesCallbackinvoked asynchronously on the UI thread;OnComplete(num_deleted)reports the number of cookies deleted.
Return Value: Returns false if a non-empty invalid URL is specified or if cookies cannot be accessed.
Usage Instruction: Use to programmatically clear cookies. Cookies can alternately be deleted via the Visit*Cookies() methods by setting the deleteCookie out-param.
Threading Constraint: The optional callback runs on the UI thread.
bool FlushStore(CefRefPtr<CefCompletionCallback> callback)
Parameters:
callback: OptionalCefCompletionCallbackinvoked asynchronously on the UI thread after the flush is complete.
Return Value: Returns false if cookies cannot be accessed; otherwise true and the flush operation is queued.
Usage Instruction: Use to ensure in-memory cookies are persisted to the backing store (e.g. before application shutdown).
Threading Constraint: The optional callback runs on the UI thread.
Usage Example
// === Enumerate, set, delete and flush cookies using the global manager ===
class CollectVisitor : public CefCookieVisitor {
public:
std::vector<CefCookie> cookies;
bool Visit(const CefCookie& c, int /*count*/, int /*total*/,
bool& deleteCookie) override {
cookies.push_back(c);
return true; // continue visiting
}
IMPLEMENT_REFCOUNTING(CollectVisitor);
};
class SetCb : public CefSetCookieCallback {
public:
void OnComplete(bool success) override { LOG(INFO) << "set: " << success; }
IMPLEMENT_REFCOUNTING(SetCb);
};
class DelCb : public CefDeleteCookiesCallback {
public:
void OnComplete(int n) override { LOG(INFO) << "deleted: " << n; }
IMPLEMENT_REFCOUNTING(DelCb);
};
class FlushCb : public CefCompletionCallback {
public:
void OnComplete() override { LOG(INFO) << "flushed"; }
IMPLEMENT_REFCOUNTING(FlushCb);
};
void DemoCookies() {
CefRefPtr<CefCookieManager> mgr =
CefCookieManager::GetGlobalManager(nullptr);
// Enumerate every cookie.
CefRefPtr<CollectVisitor> v = new CollectVisitor();
if (mgr->VisitAllCookies(v)) {
// Visitor invoked on UI thread; use v->cookies after the UI thread tour.
}
// Set a cookie.
CefCookie cookie;
CefString(&cookie.name) = "session";
CefString(&cookie.value) = "abc123";
CefString(&cookie.domain) = "example.com";
CefString(&cookie.path) = "/";
cookie.has_expires = true;
cookie.expires = CefTimeNow(); // hypothetical helper
mgr->SetCookie("https://example.com/", cookie, new SetCb());
// Delete cookies for a host.
mgr->DeleteCookies("https://example.com/", "session", new DelCb());
// Flush backing store.
mgr->FlushStore(new FlushCb());
}CefCookieVisitor
Source File: include/cef_cookie.h
Process / Thread Context: Browser Process / UI Thread (header: "The methods of this class will always be called on the UI thread").
Purpose: Interface to implement for visiting cookie values. Supplied by the embedder to CefCookieManager::VisitAllCookies / VisitUrlCookies to receive a callback per cookie.
Methods
bool Visit(const CefCookie& cookie, int count, int total, bool& deleteCookie)
Parameters:
cookie: The current cookie being visited (aCefCookiestruct).count: 0-based index of the current cookie within the iteration.total: Total number of cookies that will be visited.deleteCookie: Out-param — set totrueto delete the cookie currently being visited.
Return Value: Return true to continue visiting cookies; return false to stop visiting cookies early. The method may never be called if no cookies are found.
Usage Instruction: Implement to enumerate and optionally delete cookies. Mutating deleteCookie only deletes the current cookie, not the rest of the iteration.
Threading Constraint: Always called on the UI thread (per header).
Usage Example
class DeleteSessionCookies : public CefCookieVisitor {
public:
bool Visit(const CefCookie& cookie, int count, int total,
bool& deleteCookie) override {
CefString name = cookie.name;
if (name == "session") deleteCookie = true;
return count + 1 < total; // stop after the last cookie
}
IMPLEMENT_REFCOUNTING(DeleteSessionCookies);
};
// CefRefPtr<CefCookieManager> mgr = ...
mgr->VisitAllCookies(new DeleteSessionCookies());CefSetCookieCallback
Source File: include/cef_cookie.h
Process / Thread Context: Browser Process / UI Thread (per CefCookieManager::SetCookie documentation: callback "will be executed asnychronously on the UI thread after the cookie has been set").
Purpose: Interface to implement to be notified of asynchronous completion of CefCookieManager::SetCookie().
Methods
void OnComplete(bool success)
Parameters:
success:trueif the cookie was set successfully,falseotherwise (e.g. invalid attribute, disallowed character).
Return Value: Returns void.
Usage Instruction: Implement to react to the result of a programmatic SetCookie. Often used to log, retry, or signal a waitable event.
Threading Constraint: Called on the UI thread.
Usage Example
class LogSetCookie : public CefSetCookieCallback {
public:
void OnComplete(bool success) override {
LOG(INFO) << "Cookie set result: " << (success ? "ok" : "fail");
}
IMPLEMENT_REFCOUNTING(LogSetCookie);
};
// mgr->SetCookie("https://example.com/", cookie, new LogSetCookie());CefDeleteCookiesCallback
Source File: include/cef_cookie.h
Process / Thread Context: Browser Process / UI Thread (per CefCookieManager::DeleteCookies documentation: callback "will be executed asnychronously on the UI thread after the cookies have been deleted").
Purpose: Interface to implement to be notified of asynchronous completion of CefCookieManager::DeleteCookies().
Methods
void OnComplete(int num_deleted)
Parameters:
num_deleted: The number of cookies that were actually deleted.
Return Value: Returns void.
Usage Instruction: Implement to know how many cookies were removed (useful for telemetry, UI feedback, or to detect a no-op when the user expected to log out).
Threading Constraint: Called on the UI thread.
Usage Example
class CountDeleted : public CefDeleteCookiesCallback {
public:
void OnComplete(int n) override { LOG(INFO) << "Removed " << n << " cookies"; }
IMPLEMENT_REFCOUNTING(CountDeleted);
};
// mgr->DeleteCookies("https://example.com/", "", new CountDeleted());