Skip to content

CefCommandHandler

Header: cef_command_handler.h
Category: Browser Handlers

Overview

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

CefCommandHandler

Source File: include/cef_command_handler.h

Process / Thread Context: Browser process / UI thread. USER-IMPLEMENTED (source=client); returned from CefClient::GetCommandHandler(). "Only used with Chrome style."

Purpose: Handle Chrome-style command invocations (from menus / keyboard shortcuts) and visibility/enable queries for Chrome app-menu items, page-action icons, and toolbar buttons.

Methods

bool OnChromeCommand(CefRefPtr<CefBrowser> browser, int command_id, cef_window_open_disposition_t disposition)

Parameters:

  • browser: The browser.
  • command_id: Numerical ID; use cef_id_for_command_id_name() for version-safe mapping from cef_command_ids.h IDC names.
  • disposition: Intended target.

Return Value: true if handled; false for default. Called after CefContextMenuHandler::OnContextMenuCommand for context-menu commands.

Usage Instruction: Override Chrome menu / shortcut commands.

Threading Constraint: UI thread per class comment.

bool IsChromeAppMenuItemVisible(CefRefPtr<CefBrowser> browser, int command_id)

Parameters:

  • browser: The browser.
  • command_id: Menu item ID (use cef_id_for_command_id_name()).

Return Value: true = visible; false = hidden. Only called for items that would be visible by default.

Usage Instruction: Hide specific Chrome app-menu items.

Threading Constraint: UI thread per class comment.

bool IsChromeAppMenuItemEnabled(CefRefPtr<CefBrowser> browser, int command_id)

Parameters:

  • browser: The browser.
  • command_id: Menu item ID.

Return Value: true = enabled; false = disabled. Only called for items that would be enabled by default.

Usage Instruction: Disable specific Chrome app-menu items.

Threading Constraint: UI thread per class comment.

bool IsChromePageActionIconVisible(cef_chrome_page_action_icon_type_t icon_type)

Parameters:

  • icon_type: Type of page-action icon. browser parameter is optional (declared via optional_param=browser).

Return Value: true = visible; false = hidden. Only called for icons that would be visible by default.

Usage Instruction: Hide specific Chrome page-action icons. Called during browser creation.

Threading Constraint: UI thread per class comment; "Called during browser creation".

bool IsChromeToolbarButtonVisible(cef_chrome_toolbar_button_type_t button_type)

Parameters:

  • button_type: Type of toolbar button. browser parameter is optional.

Return Value: true = visible; false = hidden. Only called for buttons that would be visible by default.

Usage Instruction: Hide specific Chrome toolbar buttons. Called during browser creation.

Threading Constraint: UI thread per class comment; "Called during browser creation".

Usage Example

cpp
class MyCommandHandler : public CefCommandHandler {
 public:
  bool OnChromeCommand(CefRefPtr<CefBrowser> b, int id,
                       cef_window_open_disposition_t disp) override {
    if (id == cef_id_for_command_id_name("IDC_PRINT")) {
      b->GetHost()->Print();
      return true;
    }
    return false;
  }
  bool IsChromeAppMenuItemVisible(CefRefPtr<CefBrowser>, int id) override {
    return id != cef_id_for_command_id_name("IDC_ABOUT");  // hide About
  }
  bool IsChromeAppMenuItemEnabled(CefRefPtr<CefBrowser>, int) override { return true; }
  bool IsChromePageActionIconVisible(cef_chrome_page_action_icon_type_t) override { return true; }
  bool IsChromeToolbarButtonVisible(cef_chrome_toolbar_button_type_t t) override {
    return t != CEF_CTTB_BACK;  // hide Back button
  }
  IMPLEMENT_REFCOUNTING(MyCommandHandler);
};

// MyClient::GetCommandHandler() returns this.

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