CefCallback
Header: cef_callback.h
Interfaces: CefCallback, CefCompletionCallback
Category: System Utilities
Overview
Tasks, threads, parsers, streams, cookies, URL requests, scheme registration, and constant tables.
CefCallback
Source File: include/cef_callback.h
Process / Thread Context: Browser Process / IO Thread (typically — CefCallback is most often returned from CefResourceHandler and CefResourceRequestHandler methods, which the IO thread calls into).
Purpose: Generic callback interface used for asynchronous continuation. CEF passes a CefCallback to the embedder when an operation needs to be paused pending some asynchronous work; the embedder later calls Continue() to resume or Cancel() to abort.
Methods
void Continue()
Parameters:
- None.
Return Value: Returns void. Resumes the paused operation.
Usage Instruction: Call once when the asynchronous work that paused the original operation is complete. The operation that was waiting will resume.
Threading Constraint: Threading constraint not specified in header (in practice must be called on the same thread that received the callback unless documented otherwise by the API that produced it).
void Cancel()
Parameters:
- None.
Return Value: Returns void. Cancels the paused operation.
Usage Instruction: Call once when the embedder cannot or will not continue the operation. The original operation is aborted.
Threading Constraint: Threading constraint not specified in header.
Usage Example
// Inside a CefResourceHandler — pause a read while we fetch data async,
// then resume when it is ready.
class MyHandler : public CefResourceHandler {
void GetResponseHeaders(CefRefPtr<CefResponse> response,
int64_t& response_length,
CefString& redirectPath) override {
response->SetMimeType("text/plain");
response_length = -1; // unknown length
}
bool ReadResponse(void* data_out, int bytes_to_read,
int& bytes_read,
CefRefPtr<CefCallback> callback) override {
if (buffer_.empty()) {
// Defer: fetch the next chunk asynchronously; resume CEF later.
FetchNextChunk([this, callback]() {
if (buffer_.empty())
callback->Cancel(); // no more data — end stream
else
callback->Continue(); // data ready, ReadResponse will be called again
});
bytes_read = 0;
return true; // We will call `callback` later.
}
// ... copy buffer_ to data_out, set bytes_read ...
return true;
}
};CefCompletionCallback
Source File: include/cef_callback.h
Process / Thread Context: Browser Process / UI Thread (the most common documented usage is CefCookieManager::FlushStore/SetCookie/DeleteCookies, which all post the callback to the UI thread; the CefRequestContext::GetDefaultCookieManager(callback) is also UI thread).
Purpose: Generic callback interface used for asynchronous completion. Implement this and pass an instance to any CEF method that takes a CefCompletionCallback to be notified when the requested task finishes.
Methods
void OnComplete()
Parameters:
- None.
Return Value: Returns void. The single notification method.
Usage Instruction: Implement this to perform any post-completion logic (e.g. shutdown sequencing, signaling a waitable event, logging). It will be called exactly once when the asynchronous task completes, on the thread documented by the API that took the callback (typically the UI thread).
Threading Constraint: Threading constraint not specified in the CefCompletionCallback header itself; the calling API documents which thread OnComplete() is invoked on (e.g. UI thread for CefCookieManager methods).
Usage Example
class FlushCallback : public CefCompletionCallback {
public:
explicit FlushCallback(CefRefPtr<CefWaitableEvent> ev) : ev_(ev) {}
void OnComplete() override { ev_->Signal(); }
private:
CefRefPtr<CefWaitableEvent> ev_;
IMPLEMENT_REFCOUNTING(FlushCallback);
};
// Synchronously wait for the cookie store to flush (UI thread required by API).
CefRefPtr<CefWaitableEvent> ev = CefWaitableEvent::CreateWaitableEvent(false, false);
CefRefPtr<CefCookieManager> mgr = CefCookieManager::GetGlobalManager(
new FlushCallback(ev)); // optional init callback
mgr->FlushStore(new FlushCallback(ev));
ev->Wait();