CefTask
Header: cef_task.h
Interfaces: CefTask, CefTaskRunner
Category: System Utilities
Overview
Tasks, threads, parsers, streams, cookies, URL requests, scheme registration, and constant tables.
CefTask (USER-IMPLEMENTED)
Source File: include/cef_task.h
Process / Thread Context: Defined by the target thread of the CefTaskRunner / CefPostTask call; the destructor may run on the source thread if posting failed, so destructors must be conservative.
Purpose: Implement this single-method interface to encapsulate a unit of asynchronous work that runs on a chosen CEF thread.
Methods
virtual void Execute() = 0
Parameters:
- (none)
Return Value: (none)
Usage Instruction: The body runs on the target thread. Perform work; do not block.
Threading Constraint: Target thread of the post.
Usage Example
class LogOnUiTask : public CefTask {
public:
explicit LogOnUiTask(std::string msg) : msg_(std::move(msg)) {}
void Execute() override { LOG(INFO) << "on UI thread: " << msg_; }
private:
std::string msg_;
IMPLEMENT_REFCOUNTING(LogOnUiTask);
};
// From any thread:
CefPostTask(TID_UI, new LogOnUiTask("hello"));CefTaskRunner
Source File: include/cef_task.h
Process / Thread Context: Methods are safe to call on any thread. Tasks themselves execute on the runner's associated thread.
Purpose: Handle to a thread's message loop. Use GetForCurrentThread() or GetForThread(CefThreadId) to obtain a runner, then PostTask / PostDelayedTask to schedule work.
Methods
static CefRefPtr<CefTaskRunner> GetForCurrentThread()
Parameters:
- (none)
Return Value: Runner for the current thread, or empty ref if not a CEF thread.
Threading Constraint: Any thread.
static CefRefPtr<CefTaskRunner> GetForThread(CefThreadId threadId)
Parameters:
threadId: any value fromcef_thread_id_t.
Return Value: Runner for the specified CEF thread.
Threading Constraint: Any thread.
virtual bool IsSame(CefRefPtr<CefTaskRunner> that)
Return Value: true if same underlying runner. Any thread.
Threading Constraint: Threading constraint not specified in source.
virtual bool BelongsToCurrentThread()
Return Value: true if this runner is for the current thread. Any thread.
Threading Constraint: Threading constraint not specified in source.
virtual bool BelongsToThread(CefThreadId threadId)
Return Value: true if this runner belongs to threadId. Any thread.
Threading Constraint: Threading constraint not specified in source.
virtual bool PostTask(CefRefPtr<CefTask> task)
Parameters:
task: task to run asynchronously.
Return Value: true if successfully posted.
Threading Constraint: Any thread.
virtual bool PostDelayedTask(CefRefPtr<CefTask> task, int64_t delay_ms)
Parameters:
task: task to run;delay_ms: millisecond delay.
Return Value: true on success. Note: Delayed tasks are not supported on V8 WebWorker threads — they execute without delay.
Threading Constraint: Any thread.
Usage Example
// Schedule UI work with a delay from any thread.
auto runner = CefTaskRunner::GetForThread(TID_UI);
runner->PostDelayedTask(new LogOnUiTask("delayed"), 2000);
// Or use the convenience free functions (next section):
CefPostDelayedTask(TID_UI, new LogOnUiTask("delayed"), 2000);