Skip to content

CefResourceHandler

Header: cef_resource_handler.h
Interfaces: CefResourceSkipCallback, CefResourceReadCallback, CefResourceHandler
Category: Browser Handlers

Overview

User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.

CefResourceSkipCallback

Source File: include/cef_resource_handler.h

Process / Thread Context: Browser process / IO thread (the originating Skip() is called on the IO thread). USER-CALLED (source=library).

Purpose: Asynchronous continuation callback for CefResourceHandler::Skip().

Methods

void Continue(int64_t bytes_skipped)

Parameters:

  • bytes_skipped: Bytes actually skipped. > 0 → Skip() will be called again until satisfied or the request proceeds. <= 0 → request fails with ERR_REQUEST_RANGE_NOT_SATISFIABLE.

Return Value: None.

Usage Instruction: Call from Skip() when the data has been skipped asynchronously.

Threading Constraint: Threading constraint not specified in header beyond "Skip will be called in sequence but not from a dedicated thread."

Usage Example

cpp
// Inside CefResourceHandler::Skip():
//   store callback, perform async seek, then later:
//   callback->Continue(bytes_actually_skipped);

CefResourceReadCallback

Source File: include/cef_resource_handler.h

Process / Thread Context: Browser process / IO thread (originating Read() is on IO thread). USER-CALLED (source=library).

Purpose: Asynchronous continuation callback for CefResourceHandler::Read().

Methods

void Continue(int bytes_read)

Parameters:

  • bytes_read: 0 → response considered complete. >0Read() is called again until complete. <0 → request fails with bytes_read as the error code.

Return Value: None.

Usage Instruction: Call when async read data becomes available.

Threading Constraint: Threading constraint not specified in header beyond "Read will be called in sequence but not from a dedicated thread."

Usage Example

cpp
// Inside CefResourceHandler::Read(): set bytes_read=0, return true,
// store callback. Later, when bytes ready, call callback->Continue(n).

CefResourceHandler

Source File: include/cef_resource_handler.h

Process / Thread Context: Browser process / IO thread (the header says "called on the IO thread unless otherwise indicated"; Open, Skip, Read are noted as "called in sequence but not from a dedicated thread"). USER-IMPLEMENTED (source=client); returned from CefResourceRequestHandler::GetResourceHandler.

Purpose: Custom request handler / stream reader. Distinguished from CefResourceRequestHandler: this interface is the response — once GetResourceHandler returns one of these, the application is responsible for producing all response bytes via Open/Read/Skip and the headers via GetResponseHeaders. CefResourceRequestHandler only decides whether to hand the request to a CefResourceHandler or to the default network loader.

Methods

bool Open(CefRefPtr<CefRequest> request, bool& handle_request, CefRefPtr<CefCallback> callback)

Parameters:

  • response: Set mime type, status code, headers, error here.

Return Value: None.

Usage Instruction: Called after Open/ProcessRequest completes to retrieve headers.

Threading Constraint: Threading constraint not specified in header.

bool Skip(int64_t bytes_to_skip, int64_t& bytes_skipped, CefRefPtr<CefResourceSkipCallback> callback)

Parameters:

  • bytes_to_skip: Number of bytes to skip (Range request).
  • callback: Used when deferring.

Return Value: true with positive bytes_skipped → done immediately; true with 0 → async; false with negative bytes_skipped → failure.

Usage Instruction: Default implementation sets bytes_skipped=-2; return false (i.e. fail with ERR_FAILED).

Threading Constraint: "Called in sequence but not from a dedicated thread."

bool Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback)

Parameters:

  • None.

Return Value: None.

Usage Instruction: Called by CEF when request processing has been canceled; release resources.

Threading Constraint: Threading constraint not specified in header.

Usage Example

cpp
class MyCustomResourceHandler : public CefResourceHandler {
 public:
  bool Open(CefRefPtr<CefRequest> request, bool& handle_request,
            CefRefPtr<CefCallback> callback) override {
    handle_request = true;
    // Prepare response synchronously
    body_ = "<html><body>Hello from CEF!</body></html>";
    return true;
  }

  void GetResponseHeaders(CefRefPtr<CefResponse> response,
                          int64_t& response_length,
                          CefString& redirectUrl) override {
    response->SetMimeType("text/html");
    response->SetStatus(200);
    response_length = body_.size();
  }

  bool Read(void* data_out, int bytes_to_read, int& bytes_read,
            CefRefPtr<CefResourceReadCallback> callback) override {
    int n = std::min<int>(bytes_to_read, body_.size() - offset_);
    if (n <= 0) { bytes_read = 0; return false; }   // EOF
    memcpy(data_out, body_.data() + offset_, n);
    offset_ += n;
    bytes_read = n;
    return true;
  }

  void Cancel() override {}

 private:
  std::string body_;
  size_t offset_ = 0;
  IMPLEMENT_REFCOUNTING(MyCustomResourceHandler);
};

// Plugged in via MyResourceRequestHandler::GetResourceHandler(...)

See Also

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