Skip to content

CefSharedMemoryRegion

Header: cef_shared_memory_region.h
Category: V8 & Process Messaging

Overview

JavaScript bridge, value tree, inter-process messages, shared memory regions.

CefSharedMemoryRegion

Source File: include/cef_shared_memory_region.h

Process / Thread Context: Not specified in header (the platform-dependent shared memory mapping is generally usable from any thread in either process once the region is mapped).

Purpose: Class that wraps platform-dependent shared memory region mapping. Returned from CefProcessMessage::GetSharedMemoryRegion() when the message was constructed via CefSharedProcessMessageBuilder.

Methods

virtual bool IsValid()

Parameters:

  • None.

Return Value: Returns true if the mapping is valid.

Usage Instruction: Always check before calling Size() or Memory().

Threading Constraint: Not specified in header.

virtual size_t Size()

Parameters:

  • None.

Return Value: Returns the size of the mapping in bytes. Returns 0 for invalid instances.

Usage Instruction: Use to size downstream reads/writes.

Threading Constraint: Not specified in header.

virtual void* Memory()

Parameters:

  • None.

Return Value: Returns the pointer to the mapped memory. Returns nullptr for invalid instances. The returned pointer is only valid for the life span of this CefSharedMemoryRegion object.

Usage Instruction: Read the shared memory contents via this pointer. Do not retain the pointer past the lifetime of the CefSharedMemoryRegion (retain the CefRefPtr<CefSharedMemoryRegion> instead).

Threading Constraint: Not specified in header.

Usage Example

cpp
// === Receive a shared-memory process message and read its bytes ===
bool OnProcessMessageReceived(CefRefPtr<CefBrowser>,
                              CefRefPtr<CefFrame>,
                              CefProcessId,
                              CefRefPtr<CefProcessMessage> msg) override {
  if (msg->GetName() == "shared-blob") {
    CefRefPtr<CefSharedMemoryRegion> region = msg->GetSharedMemoryRegion();
    if (region && region->IsValid()) {
      const void* p = region->Memory();
      size_t n = region->Size();
      // Use the buffer; e.g. feed it into an image decoder.
      ConsumeBlob(p, n);
    }
    return true;
  }
  return false;
}

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