CefContextMenuHandler
Header: cef_context_menu_handler.h
Interfaces: CefRunContextMenuCallback, CefRunQuickMenuCallback, CefContextMenuHandler, CefContextMenuParams
Category: Browser Handlers
Overview
User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.
CefRunContextMenuCallback
Source File: include/cef_context_menu_handler.h
Process / Thread Context: Browser process / UI thread. USER-CALLED (source=library).
Purpose: Async continuation for custom context-menu display.
Methods
void Continue(int command_id, cef_event_flags_t event_flags)
Parameters:
command_id: Selected command ID.event_flags: Modifier flags at the time of selection.
Return Value: None.
Usage Instruction: Complete the context-menu display by reporting a selection.
Threading Constraint: Threading constraint not specified explicitly per-method; class comment for CefContextMenuHandler says UI thread.
void Cancel()
Parameters:
- None.
Return Value: None.
Usage Instruction: Cancel the context menu.
Threading Constraint: Threading constraint not specified explicitly per-method.
Usage Example
// After custom menu dismissed:
callback->Continue(MENU_ID_USER_FIRST + 1, EVENTFLAG_NONE);
// or:
callback->Cancel();CefRunQuickMenuCallback
Source File: include/cef_context_menu_handler.h
Process / Thread Context: Browser process / UI thread. USER-CALLED (source=library).
Purpose: Async continuation for custom quick-menu display (windowless browsers).
Methods
void Continue(int command_id, cef_event_flags_t event_flags)
Parameters:
command_id: Selected command ID.event_flags: Modifier flags.
Return Value: None.
Usage Instruction: Complete quick-menu display.
Threading Constraint: Threading constraint not specified explicitly per-method; class comment says UI thread.
void Cancel()
Parameters:
- None.
Return Value: None.
Usage Instruction: Cancel the quick menu.
Threading Constraint: Threading constraint not specified explicitly per-method.
CefContextMenuHandler
Source File: include/cef_context_menu_handler.h
Process / Thread Context: Browser process / UI thread. USER-IMPLEMENTED (source=client); returned from CefClient::GetContextMenuHandler().
Purpose: Handle context-menu events — modify the default menu, run a custom menu UI, dispatch command selections, and (for windowless browsers) handle the quick-menu for selected text regions.
Methods
void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model)
Parameters:
params: State info (seeCefContextMenuParams).model: Initially the default menu; clear or modify it. Do not retain references toparamsormodelpast this callback.
Return Value: None.
Usage Instruction: Clear model to show no menu, or modify it.
Threading Constraint: UI thread per class comment.
bool RunContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model, CefRefPtr<CefRunContextMenuCallback> callback)
Parameters:
model: The model resulting fromOnBeforeContextMenu.callback: CallContinue(command_id, flags)orCancel().
Return Value: true = custom display; execute callback sync or async. false = default display.
Usage Instruction: Implement for fully custom menu UI.
Threading Constraint: UI thread per class comment.
bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, int command_id, EventFlags event_flags)
Parameters:
command_id: Selected command; user-defined IDs should be betweenMENU_ID_USER_FIRSTandMENU_ID_USER_LAST.event_flags: Modifiers.
Return Value: true if handled; false for default implementation (see cef_menu_id_t for default-handled IDs).
Usage Instruction: Dispatch custom menu commands here.
Threading Constraint: UI thread per class comment.
void OnContextMenuDismissed(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
Parameters:
browser,frame.
Return Value: None.
Usage Instruction: Called regardless of whether menu was canceled or a command selected.
Threading Constraint: UI thread per class comment.
bool RunQuickMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefPoint& location, const CefSize& size, QuickMenuEditStateFlags edit_state_flags, CefRefPtr<CefRunQuickMenuCallback> callback)
Parameters:
location: Top-left of the selected region.size: Size of the selected region.edit_state_flags: Quick-menu state flags.callback: CallContinue(command_id, flags)orCancel().
Return Value: true = will handle; false = cancel the menu.
Usage Instruction: Windowless-browser-only quick menu.
Threading Constraint: UI thread per class comment.
bool OnQuickMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int command_id, EventFlags event_flags)
Parameters:
command_id: Selected command.event_flags: Modifiers.
Return Value: true if handled; false for default.
Usage Instruction: Dispatch quick-menu commands (windowless only).
Threading Constraint: UI thread per class comment.
void OnQuickMenuDismissed(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
Parameters:
browser,frame.
Return Value: None.
Usage Instruction: Called regardless of whether the quick menu was canceled or a command selected.
Threading Constraint: UI thread per class comment.
Usage Example
class MyContextMenuHandler : public CefContextMenuHandler {
public:
void OnBeforeContextMenu(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) override {
if (params->GetTypeFlags() & CM_TYPEFLAG_LINK)
model->AddItem(MENU_ID_USER_FIRST + 1, "Open in new tab");
}
bool OnContextMenuCommand(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefContextMenuParams>, int id,
EventFlags) override {
if (id == MENU_ID_USER_FIRST + 1) {
// Handle "open in new tab"
return true;
}
return false;
}
void OnContextMenuDismissed(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>) override {}
// RunContextMenu / RunQuickMenu / OnQuickMenuCommand / OnQuickMenuDismissed: defaults
IMPLEMENT_REFCOUNTING(MyContextMenuHandler);
};
// MyClient::GetContextMenuHandler() returns this.CefContextMenuParams
Source File: include/cef_context_menu_handler.h
Process / Thread Context: Browser process / UI thread (header: "methods of this class can only be accessed on browser process the UI thread"). USER-CALLED (source=library); CEF creates the object and passes it to CefContextMenuHandler callbacks.
Purpose: Provides information about the context-menu state (mouse coords, element type, link/image URLs, media state, selection, spell-check info, edit state, etc.).
Methods
int GetXCoord()
Return Value: X coordinate of the mouse when the menu was invoked; relative to the RenderView origin.
Threading Constraint: Threading constraint not specified in source.
int GetYCoord()
Return Value: Y coordinate of the mouse; relative to the RenderView origin.
Threading Constraint: Threading constraint not specified in source.
TypeFlags GetTypeFlags()
Return Value: Returns cef_context_menu_type_flags_t flags describing the node type. Default CM_TYPEFLAG_NONE.
Threading Constraint: Threading constraint not specified in source.
CefString GetLinkUrl()
Return Value: URL of the enclosing link, if any.
Threading Constraint: Threading constraint not specified in source.
CefString GetUnfilteredLinkUrl()
Return Value: Link URL used ONLY for "copy link address"; not validated in the frontend process.
Threading Constraint: Threading constraint not specified in source.
CefString GetSourceUrl()
Return Value: Source URL of the element (img/audio/video), if any.
Threading Constraint: Threading constraint not specified in source.
bool HasImageContents()
Return Value: true if invoked on an image with non-empty contents.
Threading Constraint: Threading constraint not specified in source.
CefString GetTitleText()
Return Value: Title or alt text if invoked on an image.
Threading Constraint: Threading constraint not specified in source.
CefString GetPageUrl()
Return Value: URL of the top-level page.
Threading Constraint: Threading constraint not specified in source.
CefString GetFrameUrl()
Return Value: URL of the subframe.
Threading Constraint: Threading constraint not specified in source.
CefString GetFrameCharset()
Return Value: Character encoding of the subframe.
Threading Constraint: Threading constraint not specified in source.
MediaType GetMediaType()
Return Value: cef_context_menu_media_type_t of the context node. Default CM_MEDIATYPE_NONE.
Threading Constraint: Threading constraint not specified in source.
MediaStateFlags GetMediaStateFlags()
Return Value: Flags describing actions supported by the media element. Default CM_MEDIAFLAG_NONE.
Threading Constraint: Threading constraint not specified in source.
CefString GetSelectionText()
Return Value: Text of the selection, if any.
Threading Constraint: Threading constraint not specified in source.
CefString GetMisspelledWord()
Return Value: Misspelled word under the cursor, if any.
Threading Constraint: Threading constraint not specified in source.
bool GetDictionarySuggestions(std::vector<CefString>& suggestions)
Return Value: true if suggestions exist; fills suggestions from the spell-check service.
Threading Constraint: Threading constraint not specified in source.
bool IsEditable()
Return Value: true if invoked on an editable node.
Threading Constraint: Threading constraint not specified in source.
bool IsSpellCheckEnabled()
Return Value: true if invoked on an editable node where spell-check is enabled.
Threading Constraint: Threading constraint not specified in source.
EditStateFlags GetEditStateFlags()
Return Value: cef_context_menu_edit_state_flags_t flags for supported actions. Default CM_EDITFLAG_NONE.
Threading Constraint: Threading constraint not specified in source.
bool IsCustomMenu()
Return Value: true if the menu contains items specified by the renderer process.
Threading Constraint: Threading constraint not specified in source.
Usage Example
// Inside OnBeforeContextMenu:
if (params->GetMediaType() == CM_MEDIATYPE_VIDEO) {
model->AddItem(MENU_ID_USER_FIRST + 10, "Download video");
video_url_ = params->GetSourceUrl();
}