Add wxEVT_WEBVIEW_CHROMIUM_MESSAGE_RECEIVED

This allows to handle IPC messages from custom JS functions in the code
using wxWebViewChromium.
This commit is contained in:
Vadim Zeitlin 2024-01-05 01:43:32 +01:00
parent faec76990e
commit 8655d7bb95
3 changed files with 164 additions and 0 deletions

View file

@ -254,6 +254,17 @@
- SetRoot() allows to use the given directory for all application files.
@section events Events
wxWebViewChromiumEvent is a specific event generated when a message from
the renderer process (in which any user-defined JavaScript functions are
executing) is received in the browser process (containing UI). Such
messages are sent using `CefFrame::SendProcessMessage(PID_BROWSER)` from
the renderer process, but describing Chromium IPC in details is out of
scope of this document, please see Chromium documentation for more
information.
@since 3.3.0
@library{wxwebview}
@category{webview}
@ -292,3 +303,63 @@ public:
*/
void SetRoot(const wxFileName& rootDir);
};
/**
Event class for events generated exclusively by wxWebViewChromium.
See wxWebViewEvent for other events also generated by wxWebViewEvent.
Currently there is a single event type corresponding to this event:
`wxEVT_WEBVIEW_CHROMIUM_MESSAGE_RECEIVED`. To use it (this example assumes
that the required CEF headers are included):
@code
webview->Bind(
wxEVT_WEBVIEW_CHROMIUM_MESSAGE_RECEIVED,
[this](wxWebViewChromiumEvent& event) {
if ( event.GetMessageName() == "MyCustomRequest" ) {
CefRefPtr<CefListValue> arguments = event.GetMessage().GetArgumentList();
CefRefPtr<CefProcessMessage> response = CefProcessMessage::Create("MyCustomResponse");
// ... do whatever is needed to construct the response ...
event.GetFrame().SendProcessMessage(PID_RENDERER, response);
} else {
// Not our message.
event.Skip();
}
}
);
@endcode
@since 3.3.0
@library{wxwebview}
@category{webview}
**/
class wxWebViewChromiumEvent : public wxCommandEvent
{
public:
/**
Get the associated browser frame.
This object can be used to call `SendProcessMessage(PID_RENDERER)` on
it to send the reply back to the renderer process.
*/
CefFrame& GetFrame() const;
/**
Get the actual message.
Message arguments can be used in the event handler to get information
associated with this message.
*/
CefProcessMessage& GetMessage() const;
/**
Get the message name.
This is just a convenient wrapper for `GetMessage().GetName()`.
*/
wxString GetMessageName() const;
};