CefResourceRequestHandler
Header: cef_resource_request_handler.h
Interfaces: CefResourceRequestHandler, CefCookieAccessFilter
Category: Browser Handlers
Overview
User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.
CefResourceRequestHandler
Source File: include/cef_resource_request_handler.h
Process / Thread Context: Browser process / IO thread (default for all methods). USER-IMPLEMENTED (source=client); returned from CefRequestHandler::GetResourceRequestHandler (and from CefRequestContextHandler::GetResourceRequestHandler).
Purpose: Per-request handler for resource interception. Distinguished from CefResourceHandler (file 3) as follows: CefResourceRequestHandler is invoked by CEF on every request to perform pre-load interception (cookie filtering, redirect control, response filtering, completion notifications, OS protocol execution); CefResourceHandler is the custom stream reader returned by CefResourceRequestHandler::GetResourceHandler when the application wants to fully synthesize the response itself.
Methods
CefRefPtr<CefCookieAccessFilter> GetCookieAccessFilter(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request)
Parameters:
request: Request contents; cannot be modified.
Return Value: Return a CefCookieAccessFilter to filter cookies; NULL to use default cookie handling.
Usage Instruction: Provides per-request cookie gating.
Threading Constraint: "Called on the IO thread before a resource request is loaded."
ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback)
Parameters:
request: The request; may be modified (URL change = redirect).callback: Used when returningRV_CONTINUE_ASYNC.
Return Value: RV_CONTINUE = proceed immediately; RV_CONTINUE_ASYNC = call callback->Continue()/Cancel() later; RV_CANCEL = cancel immediately.
Usage Instruction: Mutate headers/URL before the request goes on the wire, or block it.
Threading Constraint: "Called on the IO thread before a resource request is loaded."
CefRefPtr<CefResourceHandler> GetResourceHandler(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request)
Parameters:
request: Cannot be modified.
Return Value: NULL = use default network loader; otherwise return a CefResourceHandler to fully synthesize the response.
Usage Instruction: Use to implement custom schemes or in-app data: URLs.
Threading Constraint: "Called on the IO thread before a resource is loaded."
void OnResourceRedirect(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, CefString& new_url)
Parameters:
request: Contains the old URL and request info; read-only.response: The response that caused the redirect; read-only.
Return Value: None.
Usage Instruction: Rewrite or block redirects by changing new_url.
Threading Constraint: "Called on the IO thread when a resource load is redirected."
bool OnResourceResponse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response)
Parameters:
browser,frame,request,response(read-only).
Return Value: false = proceed without modification. true = redirect/retry by modifying request (URL change = redirect).
Usage Instruction: Deprecated for redirects — header explicitly warns: "Use OnBeforeResourceLoad or GetResourceHandler to perform redirects." Requests handled with the default network loader cannot be redirected here.
Threading Constraint: "Called on the IO thread when a resource response is received."
CefRefPtr<CefResponseFilter> GetResourceResponseFilter(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response)
Parameters:
browser,frame,request,response(all read-only).
Return Value: NULL = no filtering; otherwise return a CefResponseFilter to transform response body bytes.
Usage Instruction: Use to rewrite response bytes on the fly (e.g. compress, sanitize HTML).
Threading Constraint: "Called on the IO thread to optionally filter resource response content."
void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, URLRequestStatus status, int64_t received_content_length)
Parameters:
status:cef_urlrequest_status_tcompletion status.received_content_length: Number of bytes actually read.
Return Value: None.
Usage Instruction: Called for all requests (including aborted ones at shutdown). The header explicitly warns: may arrive after CefLifeSpanHandler::OnBeforeClose; use CefFrame::IsValid and do not call state-modifying methods if the frame is invalid.
Threading Constraint: "Called on the IO thread when a resource load has completed."
void OnProtocolExecution(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool& allow_os_execution)
Parameters:
request: Read-only.
Return Value: None.
Usage Instruction: Header explicitly warns of SECURITY risk: enforce scheme/host/URL restrictions before enabling OS execution.
Threading Constraint: "Called on the IO thread to handle requests for URLs with an unknown protocol component."
Usage Example
class MyResourceRequestHandler : public CefResourceRequestHandler {
public:
ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> b,
CefRefPtr<CefFrame> f,
CefRefPtr<CefRequest> req,
CefRefPtr<CefCallback> cb) override {
CefRequest::HeaderMap hdrs;
req->GetHeaderMap(hdrs);
hdrs.insert({"X-Custom-Header", "value"});
req->SetHeaderMap(hdrs);
return RV_CONTINUE;
}
CefRefPtr<CefResourceHandler> GetResourceHandler(
CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest> req) override {
if (req->GetURL().ToString() == "app://home")
return new MyCustomResourceHandler(); // from file 3
return nullptr;
}
CefRefPtr<CefResponseFilter> GetResourceResponseFilter(
CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefRequest>, CefRefPtr<CefResponse>) override {
return new MyResponseFilter(); // from file 4
}
// ... GetCookieAccessFilter, OnResourceRedirect, OnResourceResponse,
// OnResourceLoadComplete, OnProtocolExecution
IMPLEMENT_REFCOUNTING(MyResourceRequestHandler);
};
// Plugged in via:
// CefRefPtr<CefResourceRequestHandler>
// MyRequestHandler::GetResourceRequestHandler(...) { return new MyResourceRequestHandler; }CefCookieAccessFilter
Source File: include/cef_resource_request_handler.h
Process / Thread Context: Browser process / IO thread. USER-IMPLEMENTED (source=client); returned from CefResourceRequestHandler::GetCookieAccessFilter.
Purpose: Filter individual cookies that may be sent with a request or saved from a response.
Methods
bool CanSendCookie(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, const CefCookie& cookie)
Parameters:
request: Read-only.cookie: The cookie under consideration.
Return Value: true to send the cookie with the request; false to omit it.
Usage Instruction: Implement per-cookie gating on the send side.
Threading Constraint: "Called on the IO thread before a resource request is sent."
bool CanSaveCookie(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, const CefCookie& cookie)
Parameters:
cookie: The cookie the response wants to store.
Return Value: true to save; false to drop.
Usage Instruction: Implement per-cookie gating on the receive side.
Threading Constraint: "Called on the IO thread after a resource response is received."
Usage Example
class MyCookieFilter : public CefCookieAccessFilter {
public:
bool CanSendCookie(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefRequest>, const CefCookie& c) override {
// Block third-party tracking cookies
return std::string(c.domain.str).find("tracker.example") == std::string::npos;
}
bool CanSaveCookie(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefRequest>, CefRefPtr<CefResponse>,
const CefCookie& c) override { return true; }
IMPLEMENT_REFCOUNTING(MyCookieFilter);
};
// Returned from MyResourceRequestHandler::GetCookieAccessFilter(...)