CefResourceBundle
Header: cef_resource_bundle.h
Interfaces: CefResourceBundle, CefResourceBundleHandler
Category: Core APIs
Overview
Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.
CefResourceBundle
Source File: include/cef_resource_bundle.h
Process / Thread Context: Any thread (per class comment: "The methods of this class may be called on any thread unless otherwise indicated.")
Purpose: USER-CALLED library interface for retrieving resources from the resource bundle (*.pak) files loaded at startup or supplied by CefApp::GetResourceBundleHandler(). Provides localized strings and binary data resources.
Methods
static CefRefPtr<CefResourceBundle> GetGlobal()
Parameters:
- None.
Return Value: The global resource bundle instance.
Usage Instruction: Use to fetch any localized string or binary resource.
Threading Constraint: Any thread.
CefString GetLocalizedString(int string_id)
Parameters:
string_id: numerical ID. Usecef_id_for_pack_string_name()(seecef_pack_strings.h) to map string IDR names to version-specific numerical IDs.
Return Value: Localized string, or empty if not found.
Usage Instruction: Always use cef_id_for_pack_string_name() for version-safe mapping.
Threading Constraint: Any thread.
CefRefPtr<CefBinaryValue> GetDataResource(int resource_id)
Parameters:
resource_id: numerical ID. Usecef_id_for_pack_resource_name()for version-safe mapping (seecef_pack_resources.h).
Return Value: Decompressed scale-independent binary resource, or NULL if not found.
Usage Instruction: Use for non-scaled resources (icons, scripts, fonts, etc.).
Threading Constraint: Any thread.
CefRefPtr<CefBinaryValue> GetDataResourceForScale(int resource_id, ScaleFactor scale_factor)
Parameters:
resource_id: numerical ID;scale_factor: desired scale (useSCALE_FACTOR_NONEfor scale-independent resources, or callGetDataResourceinstead).
Return Value: Decompressed resource nearest to scale_factor, or NULL if not found.
Usage Instruction: Use for scaled images (e.g., 2x assets for HiDPI displays).
Threading Constraint: Any thread.
Usage Example
CefRefPtr<CefResourceBundle> bundle = CefResourceBundle::GetGlobal();
if (bundle) {
// Version-safe string lookup:
int id = cef_id_for_pack_string_name("IDS_APP_TITLE");
CefString title = bundle->GetLocalizedString(id);
// Scale-independent binary:
int rid = cef_id_for_pack_resource_name("IDR_LOGO_PNG");
CefRefPtr<CefBinaryValue> logo = bundle->GetDataResource(rid);
// Scale-aware image:
CefRefPtr<CefBinaryValue> logo2x =
bundle->GetDataResourceForScale(rid, SCALE_FACTOR_200P);
}CefResourceBundleHandler
Source File: include/cef_resource_bundle_handler.h
Process / Thread Context: Multiple threads (per class comment: "The methods of this class may be called on multiple threads.")
Purpose: USER-IMPLEMENTED interface enabling custom resource bundle overrides. Returned from CefApp::GetResourceBundleHandler(). The methods are called whenever CEF/Chromium requests a localized string or a resource; the implementation can supply the data or defer to the default by returning false.
Usage Example
class MyResourceBundleHandler : public CefResourceBundleHandler {
public:
bool GetLocalizedString(int string_id, CefString& string) override {
auto it = my_translations_.find(string_id);
if (it != my_translations_.end()) {
string = it->second;
return true;
}
return false; // fall back to default pak translation
}
bool GetDataResource(int resource_id, void*& data,
size_t& data_size) override {
auto it = my_resources_.find(resource_id);
if (it != my_resources_.end()) {
data = it->second.ptr; // must remain resident in memory
data_size = it->second.size;
return true;
}
return false;
}
bool GetDataResourceForScale(int resource_id, ScaleFactor sf,
void*& data, size_t& data_size) override {
// For simplicity, fall through to the default for all scaled resources.
return false;
}
std::map<int, CefString> my_translations_;
std::map<int, Blob> my_resources_;
};
// Returned from CefApp::GetResourceBundleHandler().