CefSharedProcessMessageBuilder
Header: cef_shared_process_message_builder.h
Category: V8 & Process Messaging
Overview
JavaScript bridge, value tree, inter-process messages, shared memory regions.
CefSharedProcessMessageBuilder
Source File: include/cef_shared_process_message_builder.h
Process / Thread Context: Not specified in header. The class is documented as "not thread-safe but may be used exclusively on a different thread from the one which constructed it" — i.e. once built on one thread, it may be used (Memory/Size/Build) on another thread provided the embedder serializes access.
Purpose: Class that builds a CefProcessMessage containing a shared memory region. Use it when you need to send a large payload (e.g. a frame buffer) between processes without copying the bytes through IPC marshalling.
Methods
static CefRefPtr<CefSharedProcessMessageBuilder> Create(const CefString& name, size_t byte_size)
Parameters:
name: The message name (same asCefProcessMessage::Create(name)).byte_size: The size in bytes of the shared memory region to allocate.
Return Value: Returns a new builder instance. Check IsValid() before using.
Usage Instruction: Create the builder, fill the writable memory via Memory(), then call Build() to obtain the final CefProcessMessage.
Threading Constraint: Not specified; the resulting builder is not thread-safe.
virtual bool IsValid()
Parameters:
- None.
Return Value: Returns true if the builder is valid.
Usage Instruction: Always check before Memory(), Size(), or Build().
Threading Constraint: Not specified in header (class is not thread-safe).
virtual size_t Size()
Parameters:
- None.
Return Value: Returns the size of the shared memory region in bytes. Returns 0 for invalid instances.
Usage Instruction: Use to know how many bytes you can write to Memory().
Threading Constraint: Not specified in header.
virtual void* Memory()
Parameters:
- None.
Return Value: Returns the pointer to the writable memory. Returns nullptr for invalid instances. The returned pointer is only valid for the life span of this object.
Usage Instruction: Write your payload into this buffer before calling Build(). Do not retain the pointer past the builder's lifetime.
Threading Constraint: Not specified in header.
virtual CefRefPtr<CefProcessMessage> Build()
Parameters:
- None.
Return Value: Returns the assembled CefProcessMessage. Returns nullptr for invalid instances. Invalidates the builder instance — the returned message takes ownership of the shared memory region.
Usage Instruction: Call once after writing the payload. After Build() returns, do not call any further methods on this builder; send the returned CefProcessMessage via CefBrowser::SendProcessMessage (or CefFrame::SendProcessMessage).
Threading Constraint: Not specified in header.
Usage Example
// === Send a large blob from the render process to the browser ===
void SendSharedBlob(CefRefPtr<CefBrowser> browser,
const std::vector<uint8_t>& bytes) {
CefRefPtr<CefSharedProcessMessageBuilder> builder =
CefSharedProcessMessageBuilder::Create("shared-blob", bytes.size());
if (!builder || !builder->IsValid()) return;
memcpy(builder->Memory(), bytes.data(), bytes.size());
CefRefPtr<CefProcessMessage> msg = builder->Build();
if (msg && msg->IsValid())
browser->SendProcessMessage(PID_BROWSER, msg);
}
// On the browser side, use CefProcessMessage::GetSharedMemoryRegion() to
// access the bytes (see CefSharedMemoryRegion usage example above).